tmm_mf_hdf.F90 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728
  1. !###############################################################################
  2. !
  3. ! Input/output of meteofiles : hdf version.
  4. !
  5. !### macro's ###################################################################
  6. !
  7. #define TRACEBACK write (gol,'("in ",a," (",a,", line",i5,")")') rname, __FILE__, __LINE__; call goErr
  8. #define IF_NOTOK_RETURN(action) if (status/=0) then; TRACEBACK; action; return; end if
  9. #define IF_ERROR_RETURN(action) if (status> 0) then; TRACEBACK; action; return; end if
  10. !
  11. #include "tmm.inc"
  12. !
  13. !###############################################################################
  14. module tmm_mf_hdf
  15. use GO , only : gol, goErr, goPr, goBug
  16. use GO , only : TDate
  17. use file_hdf, only : THdfFile, TSds
  18. implicit none
  19. ! --- in/out ----------------------------
  20. private
  21. public :: TMeteoFile_hdf
  22. public :: Init, Done
  23. public :: Get
  24. public :: ReadRecord
  25. ! public :: ReadEqvLatStuff
  26. public :: WriteRecord
  27. ! --- const ------------------------------
  28. character(len=*), parameter :: mname = 'tmm_mf_hdf'
  29. ! ~~~ output keys and defaults
  30. ! current format version
  31. character(len=*), parameter :: output_format = 'tmm-1.0'
  32. ! integer/real kinds
  33. integer, parameter :: iknd = 4 ! integer
  34. integer, parameter :: rknd = 8 ! real
  35. integer, parameter :: iknd_ds = 2 ! integer for big ds
  36. integer, parameter :: rknd_ds = 4 ! real for big ds
  37. ! z compression
  38. character(len=*), parameter :: compression = 'deflate'
  39. integer, parameter :: deflate_level = 6
  40. !--- type ---------------------------------
  41. type TMeteoFile_hdf
  42. ! input/output ?
  43. character(len=1) :: io
  44. !
  45. ! field collection
  46. !
  47. character(len=64) :: form
  48. character(len=256) :: fname
  49. type(THdfFile) :: hdf
  50. character(len=256) :: paramkeys
  51. type(TDate) :: trange(2)
  52. !
  53. ! file
  54. !
  55. type(TSds) :: sds
  56. logical :: selected
  57. character(len=16) :: paramkey
  58. !
  59. ! pw specials
  60. !
  61. logical :: mfw_redir
  62. !
  63. ! surface stress specials
  64. !
  65. logical :: sstr_to_ewss_nsss
  66. !
  67. ! surface pressure field
  68. !
  69. logical :: spm_load ! load spm ?
  70. logical :: spm_selected ! data set filled ?
  71. type(TSds) :: spm_sds ! data set for sp
  72. logical :: spm_incl ! record included in main file ?
  73. logical :: spm_extr ! record in external spm file ?
  74. character(len=256) :: spm_fname
  75. type(THdfFile) :: spm_hdf
  76. logical :: spm_n_to_uv
  77. !
  78. ! output
  79. !
  80. logical :: output_initialised
  81. integer :: output_nrec
  82. integer :: output_ntrec
  83. character(len=20) :: output_names(20)
  84. integer :: output_nname
  85. !
  86. ! adhoc ...
  87. integer :: fixyear
  88. logical :: qad
  89. end type TMeteoFile_hdf
  90. ! --- interfaces -------------------------
  91. interface Init
  92. module procedure mf_Init
  93. end interface
  94. interface Done
  95. module procedure mf_Done
  96. end interface
  97. interface Get
  98. module procedure mf_Get
  99. end interface
  100. interface ReadRecord
  101. module procedure mf_ReadRecord
  102. end interface
  103. ! interface ReadEqvLatStuff
  104. ! module procedure mf_ReadEqvLatStuff
  105. ! end interface
  106. interface WriteRecord
  107. module procedure mf_WriteRecord_2d
  108. module procedure mf_WriteRecord_3d
  109. end interface
  110. contains
  111. ! ==============================================================
  112. subroutine mf_Init( mf, io, dir, archivekeys, paramkey, &
  113. tref, t1, t2, status )
  114. use GO, only : TDate, Set, Get, NewDate, AnyDate, IsAnyDate
  115. use GO, only : rTotal, operator(-), operator(>=)
  116. use GO, only : goVarValue, goWriteKeyNum
  117. ! --- in/out ----------------------------
  118. type(TMeteoFile_hdf), intent(out) :: mf
  119. character(len=1), intent(in) :: io
  120. character(len=*), intent(in) :: dir
  121. character(len=*), intent(in) :: archivekeys
  122. character(len=*), intent(in) :: paramkey
  123. type(TDate), intent(in) :: tref, t1, t2
  124. integer, intent(out) :: status
  125. ! --- const --------------------------------------
  126. character(len=*), parameter :: rname = mname//'/mf_Init'
  127. ! --- local --------------------------------
  128. character(len=64) :: mf_mdir, mf_tres
  129. character(len=64) :: mf_class, mf_type, mf_grid, mf_levs
  130. character(len=1) :: mf_psep, mf_nsep
  131. character(len=64) :: mf_filekey
  132. character(len=4) :: mf_fckey
  133. type(TDate) :: tfile
  134. integer :: ccyy, mm, dd, dh
  135. type(TDate) :: tc
  136. logical :: exist
  137. ! --- begin --------------------------------
  138. ! store i/o :
  139. mf%io = io
  140. ! default flags:
  141. mf%sstr_to_ewss_nsss = .false.
  142. mf%mfw_redir = .false.
  143. !
  144. ! extract fields from archivekey :
  145. ! mdir=ec-fg_3h-ml60-glb3x2;tres=_21p06
  146. !
  147. mf%form = 'tmpp'
  148. call goVarValue( archivekeys, ';', 'form', '=', mf%form, status )
  149. if (status>0) then; TRACEBACK; status=1; return; end if
  150. !
  151. mf%fixyear = -1
  152. call goVarValue( archivekeys, ';', 'fixyear', '=', mf%fixyear, status )
  153. if (status>0) then; TRACEBACK; status=1; return; end if
  154. ! tmpp or tm5 hdf files ?
  155. select case ( mf%form )
  156. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  157. case ( 'tmpp' )
  158. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  159. mf_tres = 'no_tres'
  160. call goVarValue( archivekeys, ';', 'tres', '=', mf_tres, status )
  161. if (status>0) then; TRACEBACK; status=1; return; end if
  162. !
  163. mf_class = 'no_class'
  164. call goVarValue( archivekeys, ';', 'class', '=', mf_class, status )
  165. if (status>0) then; TRACEBACK; status=1; return; end if
  166. !
  167. mf_type = 'no_type'
  168. call goVarValue( archivekeys, ';', 'type', '=', mf_type, status )
  169. if (status>0) then; TRACEBACK; status=1; return; end if
  170. !
  171. mf_grid = 'no_grid'
  172. call goVarValue( archivekeys, ';', 'grid', '=', mf_grid, status )
  173. if (status>0) then; TRACEBACK; status=1; return; end if
  174. !
  175. mf_levs = 'no_mlevs'
  176. call goVarValue( archivekeys, ';', 'levs', '=', mf_levs, status )
  177. if (status>0) then; TRACEBACK; status=1; return; end if
  178. !
  179. ! path seperation character:
  180. mf_psep = '/'
  181. call goVarValue( archivekeys, ';', 'pathsep', '=', mf_psep, status )
  182. if (status>0) then; TRACEBACK; status=1; return; end if
  183. !
  184. ! name seperation character:
  185. mf_nsep = '-'
  186. call goVarValue( archivekeys, ';', 'namesep', '=', mf_nsep, status )
  187. if (status>0) then; TRACEBACK; status=1; return; end if
  188. !
  189. ! quick and dirty time checks ?
  190. mf%qad = .false.
  191. !call goVarValue( archivekeys, ';', 'qad', '=', mf%qad, status )
  192. !if (status>0) then; TRACEBACK; status=1; return; end if
  193. !
  194. ! adhoc flag
  195. mf%sstr_to_ewss_nsss = .false.
  196. call goVarValue( archivekeys, ';', 'sstr', '=', mf%sstr_to_ewss_nsss, status )
  197. if (status>0) then; TRACEBACK; status=1; return; end if
  198. !
  199. ! main file
  200. !
  201. ! by default, no surface pressure stuff ...
  202. mf%spm_load = .false.
  203. mf%spm_incl = .false. ! no spm fields included in hdf file
  204. mf%spm_extr = .false. ! no extra spm file
  205. mf%spm_n_to_uv = .false. ! no interpolation from 'n' to 'u' or 'v'
  206. ! default time resolution for tmpp produced files:
  207. mf_tres = '_21p06'
  208. ! * set mf_filekey (uvsp,t,etc) and parmeters:
  209. select case ( paramkey )
  210. case ( 'sp', 'pu', 'pv', 'mfu', 'mfv' )
  211. mf_filekey = 'uvsp'
  212. mf%paramkeys = '-sp-pu-pv-mfu-mfv-'
  213. mf%spm_load = .true.
  214. case ( 'pw', 'mfw' )
  215. mf_filekey = 'w'
  216. mf%paramkeys = '-pw-mfw-'
  217. mf%spm_load = .true.
  218. case ( 'T' )
  219. mf_filekey = 't'
  220. mf%paramkeys = '-T-'
  221. mf%spm_load = .true.
  222. case ( 'Q' )
  223. mf_filekey = 'q'
  224. mf%paramkeys = '-Q-'
  225. mf%spm_load = .true.
  226. case ( 'CLWC', 'CIWC', 'CC', 'CCO', 'CCU', &
  227. 'clwc', 'ciwc', 'cc', 'cco', 'ccu' )
  228. mf_filekey = 'cld'
  229. mf%paramkeys = '-CLWC-CIWC-CC-CCO-CCU-'
  230. mf%spm_load = .true.
  231. case ( 'eu', 'ed', 'du', 'dd', 'cloud_base', 'cloud_top', 'cloud_lfs' )
  232. mf_filekey = 'sub'
  233. mf%paramkeys = '-eu-ed-du-dd-cloud_base-cloud_top-cloud_lfs-'
  234. mf%spm_load = .true.
  235. case ( 'oro', 'lsm', 'sr', 'srols', 'srmer', &
  236. 'cvl', 'cvh', &
  237. 'tv01', 'tv02', 'tv03', 'tv04', 'tv05', &
  238. 'tv06', 'tv07', 'tv08', 'tv09', 'tv10', &
  239. 'tv11', 'tv12', 'tv13', 'tv14', 'tv15', &
  240. 'tv16', 'tv17', 'tv18', 'tv19', 'tv20', &
  241. 'swvl1', &
  242. 'albedo', 'lsrh', 'ci', 'g10m', 'u10m', 'v10m', 'sd', &
  243. 'lsp', 'cp', 'sf', 'sshf', 'slhf', 'blh', &
  244. 't2m', 'd2m', 'ssr', 'sstr', 'src', 'raero', 'ustar', &
  245. 'sst', 'sps', &
  246. 'ewss', 'nsss' )
  247. mf_levs = 'sfc'
  248. mf_grid = 'glb1x1'
  249. mf_filekey = 'surf'
  250. mf_tres = '_21p03'
  251. mf%paramkeys= '-'
  252. mf%paramkeys= trim(mf%paramkeys)//'oro-lsm-sr-srols-srmer-'
  253. mf%paramkeys= trim(mf%paramkeys)//'cvl-cvh-'
  254. mf%paramkeys= trim(mf%paramkeys)//'tv01-tv02-tv03-tv04-tv05-'
  255. mf%paramkeys= trim(mf%paramkeys)//'tv06-tv07-tv08-tv09-tv10-'
  256. mf%paramkeys= trim(mf%paramkeys)//'tv11-tv12-tv13-tv14-tv15-'
  257. mf%paramkeys= trim(mf%paramkeys)//'tv16-tv17-tv18-tv19-tv20-'
  258. mf%paramkeys= trim(mf%paramkeys)//'swvl1-'
  259. mf%paramkeys= trim(mf%paramkeys)//'albedo-lsrh-ci-10fg-u10m-v10m-sd-'
  260. mf%paramkeys= trim(mf%paramkeys)//'lsp-cp-sf-sshf-slhf-blh-'
  261. mf%paramkeys= trim(mf%paramkeys)//'t2m-d2m-ssr-sstr-src-raero-ustar-'
  262. mf%paramkeys= trim(mf%paramkeys)//'sst-sps-'
  263. mf%paramkeys= trim(mf%paramkeys)//'ewss-nsss-'
  264. case ( 'spm' )
  265. mf_levs = 'ml1'
  266. mf_filekey = 'spm'
  267. mf_tres = '_00p06'
  268. mf%paramkeys = '-spm-'
  269. case default
  270. write (gol,'("unsupported paramkey `",a,"`")') paramkey; call goErr
  271. TRACEBACK; status=1; return
  272. end select
  273. ! convert input times to file name times:
  274. call GetTime( mf_filekey, mf_tres, tref, t1, t2, status, &
  275. tfile=tfile, trange=mf%trange )
  276. IF_NOTOK_RETURN(status=1)
  277. ! adhoc: fixed year ?
  278. if ( mf%fixyear > 0 ) call Set( tfile, year=mf%fixyear )
  279. ! extract time values:
  280. call Get( tfile, year=ccyy, month=mm, day=dd )
  281. ! main file:
  282. write (mf%fname,'(a,a,a,a,a,a,i4.4,a,i2.2,a,a,a,a,a,a,"_",i4.4,2i2.2,a,a)') &
  283. trim(dir), mf_psep, &
  284. trim(mf_class), mf_nsep, trim(mf_type), mf_nsep, &
  285. ccyy, mf_nsep, mm, mf_nsep, trim(mf_levs), mf_nsep, trim(mf_grid), mf_nsep, &
  286. trim(mf_filekey), ccyy, mm, dd, trim(mf_tres), '.hdf'
  287. ! pw specials
  288. mf%mfw_redir = .true.
  289. ! load surface pressure with 3d field ?
  290. if ( mf%spm_load ) then
  291. ! external surface pressure file:
  292. mf%spm_extr = .true.
  293. write (mf%spm_fname,'(a,a,a,a,a,a,i4.4,a,i2.2,a,a,a,a,a,a,"_",i4.4,2i2.2,a,a)') &
  294. trim(dir), mf_psep, &
  295. trim(mf_class), mf_nsep, trim(mf_type), mf_nsep, &
  296. ccyy, mf_nsep, mm, mf_nsep, 'ml1', mf_nsep, trim(mf_grid), mf_nsep, &
  297. 'spm', ccyy, mm, dd, '_00p06', '.hdf'
  298. ! only cell surface pressure in tmpp output ...
  299. mf%spm_n_to_uv = .true.
  300. end if
  301. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  302. case ( 'tm5' )
  303. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  304. mf_mdir = 'no_mdir'
  305. call goVarValue( archivekeys, ';', 'mdir', '=', mf_mdir, status )
  306. if (status>0) then; TRACEBACK; status=1; return; end if
  307. !
  308. mf_tres = 'no_tres'
  309. call goVarValue( archivekeys, ';', 'tres', '=', mf_tres, status )
  310. if (status>0) then; TRACEBACK; status=1; return; end if
  311. !
  312. ! path seperation character:
  313. mf_psep = '/'
  314. call goVarValue( archivekeys, ';', 'pathsep', '=', mf_psep, status )
  315. if (status>0) then; TRACEBACK; status=1; return; end if
  316. !
  317. ! name seperation character:
  318. mf_nsep = '-'
  319. call goVarValue( archivekeys, ';', 'namesep', '=', mf_nsep, status )
  320. if (status>0) then; TRACEBACK; status=1; return; end if
  321. !
  322. ! adhoc flag
  323. mf%sstr_to_ewss_nsss = .false.
  324. call goVarValue( archivekeys, ';', 'sstr', '=', mf%sstr_to_ewss_nsss, status )
  325. if (status>0) then; TRACEBACK; status=1; return; end if
  326. !
  327. ! main file
  328. !
  329. ! by default, no surface pressure stuff ...
  330. mf%spm_load = .false.
  331. mf%spm_incl = .false. ! no spm fields included in hdf file
  332. mf%spm_extr = .false. ! no extra spm file
  333. mf%spm_n_to_uv = .false. ! no interpolation from 'n' to 'u' or 'v'
  334. ! * set mf_filekey (uvsp,t,etc) and parmeters:
  335. select case ( paramkey )
  336. case ( 'sp', 'tsp' )
  337. mf_filekey = paramkey
  338. mf%paramkeys = '-'//trim(paramkey)//'-'
  339. mf%spm_load = .false.
  340. case ( 'spm' )
  341. mf_levs = 'ml1'
  342. mf_filekey = 'spm'
  343. mf_tres = '_00p06'
  344. mf%paramkeys = '-spm-'
  345. case ( 'mfu', 'mfv' )
  346. mf_filekey = 'mfuv'
  347. mf%paramkeys = '-mfu-mfv-'
  348. mf%spm_load = .true.
  349. case ( 'mfw' )
  350. mf_filekey = 'mfw'
  351. mf%paramkeys = '-mfw-'
  352. mf%spm_load = .true.
  353. case ( 'T' )
  354. mf_filekey = 't'
  355. mf%paramkeys = '-T-'
  356. mf%spm_load = .true.
  357. case ( 'Q' )
  358. mf_filekey = 'q'
  359. mf%paramkeys = '-Q-'
  360. mf%spm_load = .true.
  361. case ( 'CLWC', 'CIWC', 'CC', 'CCO', 'CCU', &
  362. 'clwc', 'ciwc', 'cc', 'cco', 'ccu' )
  363. mf_filekey = 'cld'
  364. mf%paramkeys = '-CLWC-CIWC-CC-CCO-CCU-'
  365. mf%spm_load = .true.
  366. case ( 'eu', 'ed', 'du', 'dd', 'cloud_base', 'cloud_top', 'cloud_lfs' )
  367. mf_filekey = 'sub'
  368. mf%paramkeys = '-eu-ed-du-dd-cloud_base-cloud_top-cloud_lfs-'
  369. mf%spm_load = .true.
  370. ! o constant fields
  371. case ( 'oro', 'lsm' )
  372. mf_tres = 'constant'
  373. mf_filekey = trim(paramkey)
  374. mf%paramkeys = '-'//trim(paramkey)//'-'
  375. mf%spm_load = .false.
  376. ! o monthly fields:
  377. case ( 'srols' )
  378. mf_tres = 'month'
  379. mf_filekey = trim(paramkey)
  380. mf%paramkeys = '-'//trim(paramkey)//'-'
  381. mf%spm_load = .false.
  382. ! o vegetation fields
  383. case ( 'cvl', 'cvh', &
  384. 'tv01', 'tv02', 'tv03', 'tv04', 'tv05', &
  385. 'tv06', 'tv07', 'tv08', 'tv09', 'tv10', &
  386. 'tv11', 'tv12', 'tv13', 'tv14', 'tv15', &
  387. 'tv16', 'tv17', 'tv18', 'tv19', 'tv20' )
  388. mf_filekey = 'veg'
  389. mf%paramkeys= '-'
  390. mf%paramkeys= trim(mf%paramkeys)//'cvl-cvh-'
  391. mf%paramkeys= trim(mf%paramkeys)//'tv01-tv02-tv03-tv04-tv05-'
  392. mf%paramkeys= trim(mf%paramkeys)//'tv06-tv07-tv08-tv09-tv10-'
  393. mf%paramkeys= trim(mf%paramkeys)//'tv11-tv12-tv13-tv14-tv15-'
  394. mf%paramkeys= trim(mf%paramkeys)//'tv16-tv17-tv18-tv19-tv20-'
  395. mf%spm_load = .false.
  396. ! o each surface file in a seperate file;
  397. ! might be collected in surf_*.tar files afterwards
  398. case ( 'sr', 'srmer', &
  399. 'swvl1', &
  400. 'albedo', 'lsrh', 'ci', 'g10m', 'u10m', 'v10m', 'sd', &
  401. 'lsp', 'cp', 'sf', 'sshf', 'slhf', 'blh', &
  402. 't2m', 'd2m', 'ssr', 'ssrd', 'str', 'strd', &
  403. 'sstr', 'src', 'raero', 'ustar', &
  404. 'sst', 'sps', 'skt' )
  405. mf_filekey = trim(paramkey)
  406. mf%paramkeys = '-'//trim(paramkey)//'-'
  407. mf%spm_load = .false.
  408. case ( 'ewss', 'nsss' )
  409. if ( mf%sstr_to_ewss_nsss ) then
  410. mf_filekey = 'sstr'
  411. mf%paramkeys = '-sstr-'
  412. else
  413. mf_filekey = trim(paramkey)
  414. mf%paramkeys = '-'//trim(paramkey)//'-'
  415. end if
  416. mf%spm_load = .false.
  417. case default
  418. write (gol,'("unsupported paramkey `",a,"`")') paramkey; call goErr
  419. TRACEBACK; status=1; return
  420. end select
  421. ! convert input times to file name times:
  422. call GetTime( mf_filekey, mf_tres, tref, t1, t2, status, &
  423. tfile=tfile, trange=mf%trange )
  424. IF_NOTOK_RETURN(status=1)
  425. ! adhoc: fixed year ?
  426. if ( mf%fixyear > 0 ) call Set( tfile, year=mf%fixyear )
  427. ! extract time values:
  428. call Get( tfile, year=ccyy, month=mm, day=dd )
  429. ! special data set: trap change from fg to fc data:
  430. if ( mf_tres == '_fg006up4tr3' ) then
  431. tc = NewDate( 2000, 09, 12 )
  432. if ( tfile >= tc ) mf_tres = '_fc012up2tr3'
  433. end if
  434. ! forecast key: '', 'f1', .., 'f10' ;
  435. ! no key for constant files (t1 and t2 are anydate)
  436. ! or month files (t1 is begin of month thus probably < tref)
  437. mf_fckey = ''
  438. if ( (.not. IsAnyDate(t1)) .and. (t1 >= tref) ) then
  439. dh = floor( rTotal( t1 - tref, 'day' ) )
  440. if ( dh > 0 ) call goWriteKeyNum( mf_fckey, 'f', dh )
  441. end if
  442. ! create file name:
  443. ! dir / ec_od-ml60-T159 - oro.hdf
  444. ! dir / ec_od-ml60-T159 - T_20000101_fg006up4tr3.hdf
  445. select case ( mf_tres )
  446. case ( 'constant' )
  447. ! filename without date:
  448. write (mf%fname,'(a,a,a,a,a,".hdf")') trim(dir), mf_psep, trim(mf_mdir), mf_nsep, trim(mf_filekey)
  449. case ( 'month' )
  450. ! filename without day and forecast key:
  451. write (mf%fname,'(a,a,a,a,a,"_",i4.4,i2.2,a)') &
  452. trim(dir), mf_psep, trim(mf_mdir), mf_nsep, trim(mf_filekey), ccyy, mm, '.hdf'
  453. case default
  454. ! filename including date:
  455. write (mf%fname,'(a,a,a,a,a,"_",i4.4,2i2.2,a,a,a)') &
  456. trim(dir), mf_psep, trim(mf_mdir), mf_nsep, &
  457. trim(mf_filekey), ccyy, mm, dd, trim(mf_fckey), trim(mf_tres), '.hdf'
  458. ! trap request for sps ; might be in sp file ...
  459. if ( trim(mf_filekey) == 'sps' ) then
  460. inquire( file=trim(mf%fname), exist=exist )
  461. if ( .not. exist ) then
  462. write (mf%fname,'(a,a,a,a,a,"_",i4.4,2i2.2,a,a,a)') &
  463. trim(dir), mf_psep, trim(mf_mdir), mf_nsep, &
  464. 'sp', ccyy, mm, dd, trim(mf_fckey), trim(mf_tres), '.hdf'
  465. end if
  466. end if
  467. end select
  468. ! spm is included in files ...
  469. mf%spm_incl = mf%spm_load ! used to be external, now included
  470. mf%spm_fname = mf%fname ! same file
  471. mf%spm_extr = .false. ! not external for sure ...
  472. ! in case of output, not initialised yet ...
  473. mf%output_initialised = .false.
  474. mf%output_nname = 0
  475. ! number of expected time records in a file:
  476. call GetTime( mf_filekey, mf_tres, tref, t1, t2, status, nrec=mf%output_ntrec )
  477. IF_NOTOK_RETURN(status=1)
  478. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  479. case default
  480. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  481. write (gol,'("unsupported form `",a,"`")') trim(mf%form); call goErr
  482. TRACEBACK; status=1; return
  483. end select
  484. ! ok
  485. status = 0
  486. end subroutine mf_Init
  487. ! ***
  488. subroutine mf_Done( mf, status )
  489. !use file_hdf, only : Done
  490. ! --- in/out ------------------------------------
  491. type(TMeteoFile_hdf), intent(inout) :: mf
  492. integer, intent(out) :: status
  493. ! --- const --------------------------------------
  494. character(len=*), parameter :: rname = mname//'/mf_Done'
  495. ! --- begin -------------------------------------
  496. ! files have been closed in ReadRecord/WriteRecord
  497. ! ok
  498. status = 0
  499. end subroutine mf_Done
  500. ! ***
  501. subroutine mf_Get( mf, status, trange1, trange2, paramkeys )
  502. use GO, only : TDate
  503. ! --- in/out ----------------------------
  504. type(TMeteoFile_hdf), intent(in) :: mf
  505. integer, intent(out) :: status
  506. type(TDate), intent(out), optional :: trange1, trange2
  507. character(len=*), intent(out), optional :: paramkeys
  508. ! --- const --------------------------------------
  509. character(len=*), parameter :: rname = mname//'/mf_Get'
  510. ! --- local --------------------------------
  511. ! --- begin --------------------------------
  512. ! time range:
  513. if ( present(trange1) ) trange1 = mf%trange(1)
  514. if ( present(trange2) ) trange2 = mf%trange(2)
  515. ! parameter names:
  516. if ( present(paramkeys) ) paramkeys = mf%paramkeys
  517. ! ok
  518. status = 0
  519. end subroutine mf_Get
  520. ! ******************************************************************
  521. ! ***
  522. ! *** time range, parameters, file names
  523. ! ***
  524. ! ******************************************************************
  525. ! !
  526. ! ! Archive type:
  527. ! ! 'tmpp' : year and month are inserted in directory names,
  528. ! ! thus archive name 'od-fc-ml60-glb3x2'
  529. ! ! becomes 'od-fc-2000-01ml60-glb3x2' .
  530. ! ! 'tm5' : archivename contains var=value pairs:
  531. ! ! mdir=ec-fg_3h-ml60-glb3x2;tres=_00p06
  532. ! !
  533. !
  534. ! subroutine mf_hdf_GetKeys( archivetype, archivename, &
  535. ! paramkey, t1, t2, dirname, &
  536. ! mf_t1, mf_t2, mf_paramkeys, &
  537. ! mf_filename, mf_spm_filename, &
  538. ! status )
  539. !
  540. ! use GO, only : goSplitLine, goReadFromLine
  541. ! use GO, only : TDate, IncrDate, Get, NewDate, wrtgol, &
  542. ! Operator(+), Operator(-), Operator(/)
  543. !
  544. ! ! --- in/out -------------------------------------
  545. !
  546. ! character(len=*), intent(in) :: archivetype
  547. ! character(len=*), intent(in) :: archivename
  548. ! character(len=*), intent(in) :: paramkey
  549. ! type(TDate), intent(in) :: t1, t2
  550. ! character(len=*), intent(in) :: dirname
  551. !
  552. ! type(TDate), intent(out) :: mf_t1, mf_t2
  553. ! character(len=*), intent(out) :: mf_paramkeys
  554. ! character(len=*), intent(out) :: mf_filename
  555. ! character(len=*), intent(out) :: mf_spm_filename
  556. ! integer, intent(inout) :: status
  557. !
  558. ! ! --- const -------------------------------------
  559. !
  560. ! character(len=*), parameter :: rname = mname//'/mf_hdf_GetKeys'
  561. !
  562. ! ! --- local -------------------------------------
  563. !
  564. ! integer :: year1, month1, day1, hour1
  565. ! integer :: year2, month2, day2
  566. !
  567. ! character(len=100) :: archivenameX
  568. ! character(len=100) :: archivenameX_spm
  569. ! character(len=10) :: mclass
  570. ! character(len=10) :: mtype
  571. ! character(len=10) :: mlevs
  572. ! character(len=10) :: mgrid
  573. ! character(len=10) :: filekey
  574. ! character(len=10) :: treskey
  575. ! logical :: with_spm
  576. !
  577. ! character(len=256) :: archivekeys2
  578. ! character(len=64) :: varval
  579. ! character(len=64) :: var, val
  580. ! character(len=16) :: tm5_mdir, tm5_treskey
  581. !
  582. ! ! --- begin -------------------------------------
  583. !
  584. ! ! extract time values:
  585. ! call Get( t1, year=year1, month=month1, day=day1, hour=hour1 )
  586. !
  587. ! ! time range: [21,21]
  588. ! ! * set t1 to 21:00 previous day = 00:00 today minus 3 hour
  589. ! if ( hour1 < 21 ) then
  590. ! mf_t1 = NewDate( year=year1, month=month1, day=day1, hour=0 )
  591. ! mf_t1 = mf_t1 - IncrDate(hour=3)
  592. ! else
  593. ! mf_t1 = NewDate( year=year1, month=month1, day=day1, hour=21 )
  594. ! end if
  595. ! ! * set t2 to 21:00 today:
  596. ! mf_t2 = mf_t1 + IncrDate(hour=24)
  597. ! ! * date in file name is today:
  598. ! call Get( mf_t2, year=year2, month=month2, day=day2 )
  599. !
  600. ! !call wrtgol( ' fields valid from : ', mf%t1 )
  601. ! !call wrtgol( ' to : ', mf%t2 )
  602. !
  603. ! ! file type, name, params, ...
  604. ! ! * archivename (directory)
  605. ! select case ( archivetype )
  606. ! case ( 'tmpp' )
  607. ! archivenameX = archivename
  608. ! call goReadFromLine( archivenameX, mclass, sep='-' )
  609. ! call goReadFromLine( archivenameX, mtype , sep='-' )
  610. ! call goReadFromLine( archivenameX, mlevs , sep='-' )
  611. ! call goReadFromLine( archivenameX, mgrid , sep='-' )
  612. ! case ( 'tm5' )
  613. ! ! default values:
  614. ! tm5_mdir = 'xx'
  615. ! tm5_treskey = '_tres'
  616. ! ! overwrite with values in archivekey:
  617. ! archivekeys2 = archivename
  618. ! do
  619. ! ! no keys left ? then leave
  620. ! if ( len_trim(archivekeys2) == 0 ) exit
  621. ! ! extract var=value:
  622. ! call goReadFromLine( archivekeys2, varval, sep=';' )
  623. ! ! split in var and value:
  624. ! call goSplitLine( varval, var, '=', val )
  625. ! ! store:
  626. ! select case ( var )
  627. ! case ( 'mdir' ) ; tm5_mdir = trim(val)
  628. ! case ( 'treskey' ) ; tm5_treskey = trim(val)
  629. ! case default
  630. ! write (gol,'("unsupported archive key `",a,"`")') trim(varval); call goErr
  631. ! write (gol,'(" ",a)') trim(archivekeys); call goErr
  632. ! TRACEBACK; status=1; return
  633. ! end select
  634. ! end do
  635. ! case default
  636. ! write (gol,'("unsupported archivetype `",a,"`")') trim(archivetype); call goErr
  637. ! TRACEBACK; status=1; return
  638. ! end select
  639. ! ! * other
  640. ! treskey = '_21p06'
  641. ! with_spm = .true.
  642. ! ! * overwrite some fields:
  643. ! select case ( paramkey )
  644. ! case ( 'sp', 'pu', 'pv' )
  645. ! filekey = 'uvsp'
  646. ! mf_paramkeys = '-sp-pu-pv-'
  647. ! case ( 'T' )
  648. ! filekey = 't'
  649. ! mf_paramkeys = '-T-'
  650. ! case ( 'Q' )
  651. ! filekey = 'q'
  652. ! mf_paramkeys= '-Q-'
  653. ! case ( 'CLWC', 'CIWC', 'CC', 'CCO', 'CCU' )
  654. ! filekey = 'cld'
  655. ! mf_paramkeys= '-CLWC-CIWC-CC-CCO-CCU-'
  656. ! case ( 'eu', 'ed', 'du', 'dd', 'cloud_base', 'cloud_top', 'cloud_lfs' )
  657. ! filekey = 'sub'
  658. ! mf_paramkeys= '-eu-ed-du-dd-cloud_base-cloud_top-cloud_lfs-'
  659. ! case ( 'oro', 'lsm', 'sr_ecm', 'sr_ols', 'sr_mer', &
  660. ! 'cvl', 'cvh', &
  661. ! 'tv01', 'tv02', 'tv03', 'tv04', 'tv05', &
  662. ! 'tv06', 'tv07', 'tv08', 'tv09', 'tv10', &
  663. ! 'tv11', 'tv12', 'tv13', 'tv14', 'tv15', &
  664. ! 'tv16', 'tv17', 'tv18', 'tv19', 'tv20', &
  665. ! 'swvl1', &
  666. ! 'al', 'lsrh', 'ci', '10fg', 'u10m', 'v10m', 'sd', &
  667. ! 'lsp', 'cp', 'sf', 'sshf', 'slhf', 'blh', &
  668. ! 't2m', 'd2m', 'ssr', 'sstr', 'src', 'raero', 'ustar' )
  669. ! mlevs = 'sfc'
  670. ! mgrid = 'glb1x1'
  671. ! filekey = 'surf'
  672. ! treskey = '_21p03'
  673. ! mf_paramkeys= '-'
  674. ! mf_paramkeys= trim(mf_paramkeys)//'oro-lsm-sr_ecm-sr_ols-sr_mer-'
  675. ! mf_paramkeys= trim(mf_paramkeys)//'cvl-cvh-'
  676. ! mf_paramkeys= trim(mf_paramkeys)//'tv01-tv02-tv03-tv04-tv05-'
  677. ! mf_paramkeys= trim(mf_paramkeys)//'tv06-tv07-tv08-tv09-tv10-'
  678. ! mf_paramkeys= trim(mf_paramkeys)//'tv11-tv12-tv13-tv14-tv15-'
  679. ! mf_paramkeys= trim(mf_paramkeys)//'tv16-tv17-tv18-tv19-tv20-'
  680. ! mf_paramkeys= trim(mf_paramkeys)//'swvl1-'
  681. ! mf_paramkeys= trim(mf_paramkeys)//'al-lsrh-ci-10fg-u10m-v10m-sd-'
  682. ! mf_paramkeys= trim(mf_paramkeys)//'lsp-cp-sf-sshf-slhf-blh-'
  683. ! mf_paramkeys= trim(mf_paramkeys)//'t2m-d2m-ssr-sstr-src-raero-ustar-'
  684. ! with_spm = .false.
  685. ! case ( 'spm' )
  686. ! mlevs = 'ml1'
  687. ! filekey = 'spm'
  688. ! treskey = '_00p06'
  689. ! mf_paramkeys= '-spm-'
  690. ! case default
  691. ! write (gol,'("unsupported tmpp paramkey `",a,"`")') paramkey; call goErr
  692. ! TRACEBACK; status=1; return
  693. ! end select
  694. ! ! * create archive names:
  695. ! select case ( archivetype )
  696. ! case ( 'tmpp' )
  697. ! write (archivenameX,'(a,"-",a,"-",i4.4,"-",i2.2,"-",a,"-",a)') &
  698. ! trim(mclass), trim(mtype), year2, month2, trim(mlevs), trim(mgrid)
  699. ! write (archivenameX_spm,'(a,"-",a,"-",i4.4,"-",i2.2,"-",a,"-",a)') &
  700. ! trim(mclass), trim(mtype), year2, month2, 'ml1', trim(mgrid)
  701. ! case ( 'tm5' )
  702. ! archivenameX = tm5_mdir
  703. ! archivenameX_spm = tm5_mdir
  704. ! treskey = tm5_treskey
  705. ! case default
  706. ! write (gol,'("unsupported archivetype `",a,"`")') trim(archivetype); call goErr
  707. ! TRACEBACK; status=1; return
  708. ! end select
  709. ! ! * create filenames
  710. ! write (mf_filename,'(a,"/",a,"-",a,"_",i4.4,i2.2,i2.2,a,".hdf")') &
  711. ! trim(dirname), trim(archivenameX), &
  712. ! trim(filekey), year2, month2, day2, trim(treskey)
  713. ! if ( with_spm ) then
  714. ! write (mf_spm_filename,'(a,"/",a,"-",a,"_",i4.4,i2.2,i2.2,a,".hdf")') &
  715. ! trim(dirname), trim(archivenameX_spm), &
  716. ! 'spm', year2, month2, day2, '_00p06'
  717. ! else
  718. ! mf_spm_filename = 'none'
  719. ! endif
  720. !
  721. ! ! ok
  722. ! status = 0
  723. !
  724. ! end subroutine mf_hdf_GetKeys
  725. ! ***
  726. !
  727. ! Return time parameters:
  728. ! o tfile : date in filename
  729. ! o trange : time interval covered by fields in file
  730. !
  731. subroutine GetTime( filekey, tres, tref, t1, t2, status, tfile, trange, nrec )
  732. use GO, only : TDate, NewDate, AnyDate, Get, Set, wrtgol, IncrDate, IsAnyDate
  733. use GO, only : operator(<), operator(+), operator(-), rTotal
  734. ! --- in/out --------------------------------
  735. character(len=*), intent(in) :: filekey
  736. character(len=*), intent(in) :: tres
  737. type(TDate), intent(in) :: tref, t1, t2
  738. integer, intent(out) :: status
  739. type(TDate), intent(out), optional :: tfile
  740. type(TDate), intent(out), optional :: trange(2)
  741. integer, intent(out), optional :: nrec
  742. ! --- const --------------------------------------
  743. character(len=*), parameter :: rname = mname//'/GetTime'
  744. ! --- local --------------------------------
  745. integer :: year, month
  746. integer :: hour1, time6(6)
  747. integer :: dd, hh, step
  748. logical :: interval
  749. real :: dhr
  750. ! --- begin --------------------------------
  751. ! set day shift, start hour, and step
  752. select case ( tres )
  753. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  754. ! tmpp [21,21]
  755. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  756. case ( '_21p06', '_21p03', '_av21' )
  757. ! routine is called with tref,t1,t2:
  758. ! t1,t1,t2
  759. ! t1,any,any (oro and other constant fields)
  760. ! thus use tref to construct the file times
  761. if ( IsAnyDate(t1) ) then
  762. call Get( tref, hour=hour1 )
  763. interval = .false.
  764. else
  765. call Get( t1, hour=hour1 )
  766. interval = t1 < t2
  767. end if
  768. ! file ccyymmdd contains fields for (21,21];
  769. ! only uvsp is valid for [21,21] since it contains surface pressure for 21:00
  770. if ( present(tfile) ) then
  771. tfile = tref
  772. call Set( tfile, hour=0, min=0, sec=0 )
  773. if ( (hour1 > 21) .or. ((interval .or. filekey=='uvsp') .and. hour1==21) ) then
  774. tfile = tfile + IncrDate(day=1)
  775. end if
  776. end if
  777. ! fields by default valid for (21,21];
  778. ! only uvsp is valid for [21,21] since it contains surface pressure for 21:00
  779. if ( present(trange) ) then
  780. trange(1) = tref
  781. call Set( trange(1), hour=0, min=0, sec=0 ) ! 00:00 today
  782. if ( (hour1 > 21) .or. ((interval .or. filekey=='uvsp') .and. hour1==21) ) then
  783. trange(1) = trange(1) + IncrDate(day=1)
  784. end if
  785. trange(1) = trange(1) - IncrDate(hour=3) ! previous 21:00
  786. trange(2) = trange(1) + IncrDate(day=1) ! next 21:00
  787. ! boundary not included in most cases:
  788. if ( filekey /= 'uvsp' ) trange(1) = trange(1) + IncrDate(mili=1)
  789. end if
  790. ! number of records in file:
  791. if ( present(nrec) ) then
  792. select case ( tres )
  793. case ( '_21p06' ) ; nrec = 24/6
  794. case ( '_21p03' ) ; nrec = 24/3
  795. case ( '_av21' ) ; nrec = 24/24
  796. case default
  797. write (gol,'("unsupported tres for setting nrec : ",a)') tres; call goErr
  798. TRACEBACK; status=1; return
  799. end select
  800. end if
  801. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  802. ! tm5 constant
  803. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  804. case ( 'constant' )
  805. ! no date in filename ...
  806. if ( present(tfile) ) tfile = AnyDate()
  807. ! fields always valid ...
  808. if ( present(trange) ) then
  809. trange(1) = AnyDate()
  810. trange(2) = AnyDate()
  811. end if
  812. ! only one output record in constant file:
  813. if ( present(nrec) ) nrec = 1
  814. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  815. ! tm5 monthly file
  816. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  817. case ( 'month' )
  818. ! file ccyymmdd contains fields for this month:
  819. if ( present(tfile) ) then
  820. call Get( t1, year=year, month=month )
  821. tfile = NewDate( year=year, month=month, day=1 )
  822. end if
  823. ! field valid from begin to end of month:
  824. if ( present(trange) ) then
  825. call Get( t1, year=year, month=month )
  826. trange(1) = NewDate( year=year, month=month, day=1, hour=00 )
  827. month = month + 1
  828. if ( month > 12 ) then
  829. year = year + 1
  830. month = 1
  831. end if
  832. trange(2) = NewDate( year=year, month=month, day=1, hour=00 )
  833. end if
  834. ! only one output record in month file:
  835. if ( present(nrec) ) nrec = 1
  836. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  837. ! tm5 [00,24]
  838. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  839. case ( '_00p06', '_00p03', '_an0tr6', '_fg006up4tr3', '_fc012up2tr3', '_00p01' )
  840. ! file ccyymmdd contains fields for [00,24) :
  841. if ( present(tfile) ) then
  842. tfile = t1
  843. call Set( tfile, hour=0, min=0, sec=0 )
  844. end if
  845. ! fields valid for [00,24) :
  846. if ( present(trange) ) then
  847. trange(1) = t1
  848. call Set( trange(1), hour=0, min=0, sec=0 ) ! 00:00 today
  849. trange(2) = trange(1) + IncrDate(hour=24) - IncrDate(mili=1)
  850. end if
  851. ! number of records in file:
  852. if ( present(nrec) ) then
  853. select case ( tres )
  854. case ( '_00p06' ) ; nrec = 24/6
  855. case ( '_an0tr6' ) ; nrec = 24/6
  856. case ( '_00p03', '_fg006up4tr3', '_fc012up2tr3' )
  857. ! by default: 3 hourly files
  858. ! for forecasts after 12+72, only 6 hourly available:
  859. ! f0 [ 00, 24) : 00 03 06 09 12 15 18 21 : nrec=8
  860. ! f1 [ 24, 48) : 00 03 06 09 12 15 18 21 : nrec=8
  861. ! f2 [ 48, 72) : 00 03 06 09 12 15 18 21 : nrec=8
  862. ! f3 [ 72, 96) : 00 03 06 09 12 18 : nrec=6
  863. ! f4 [ 96,120) : 00 06 12 18 : nrec=4
  864. ! :
  865. ! f9 [192,216) : 00 06 12 18 : nrec=4
  866. ! f10 [216,240) : 00 06 12 : nrec=3
  867. dhr = rTotal( t1 - tref, 'hour' )
  868. if ( dhr < 72.0 ) then
  869. nrec = 8
  870. else if ( dhr < 96 ) then
  871. nrec = 4+2
  872. else if ( dhr < 216 ) then
  873. nrec = 4
  874. else
  875. nrec = 3
  876. end if
  877. case ( '_00p01' ) ; nrec = 24
  878. case default
  879. write (gol,'("unsupported tres for setting nrec : ",a)') tres; call goErr
  880. TRACEBACK; status=1; return
  881. end select
  882. end if
  883. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  884. ! ???
  885. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  886. case default
  887. write (gol,'("unsupported time resolution key:")'); call goErr
  888. write (gol,'(" ",a)') trim(tres); call goErr
  889. TRACEBACK; status=1; return
  890. end select
  891. ! ok
  892. status = 0
  893. end subroutine GetTime
  894. ! ******************************************************************
  895. ! ***
  896. ! *** input
  897. ! ***
  898. ! ******************************************************************
  899. subroutine mf_SelectRecord( mf, paramkey, t1, t2, status )
  900. use GO, only : TDate, NewDate, IncrDate, wrtgol, Set, Get, rTotal
  901. use GO, only : operator(-), operator(+), operator(/), operator(==), operator(/=)
  902. use GO, only : operator(>=), operator(<=), operator(>)
  903. use file_hdf, only : GetInfo
  904. use file_hdf, only : TSds, Select, CheckInfo, CheckAttribute, ReadAttribute
  905. ! --- in/out --------------------------------
  906. type(TMeteoFile_hdf), intent(inout) :: mf
  907. character(len=*), intent(in) :: paramkey
  908. type(TDate), intent(in) :: t1, t2
  909. integer, intent(out) :: status
  910. ! --- const --------------------------------------
  911. character(len=*), parameter :: rname = mname//'/mf_SelectRecord'
  912. ! --- local -------------------------------
  913. integer :: nsds, isds
  914. integer :: time1(6), time2(6)
  915. type(TDate) :: tmid, treq, thelp
  916. type(TDate) :: t_spm
  917. integer :: hour, dhour
  918. integer :: status1, status2
  919. integer :: time1s(6), time2s(6)
  920. type(TDate) :: tim1s, tim2s
  921. !! debug ...
  922. !character(len=64) :: xname
  923. !integer :: xtime1(6), xtime2(6)
  924. ! --- begin -------------------------------
  925. mf%selected = .false.
  926. ! initial no record is found ...
  927. status = 1
  928. ! number of data sets:
  929. call GetInfo( mf%hdf, status, num_datasets=nsds )
  930. if (status/=0) then; TRACEBACK; return; end if
  931. ! loop over all data sets
  932. do isds = 1, nsds
  933. !! debug ..
  934. !write (gol,'("DEBUG - isds = ",i6," / ",i6)') isds, nsds; call goPr
  935. call Select( mf%sds, mf%hdf, isds-1, status )
  936. if (status/=0) then; TRACEBACK; return; end if
  937. !! debug ..
  938. !call GetInfo( mf%sds, status, name=xname )
  939. !write (gol,'("DEBUG - sds name = ",a)') trim(xname); call goPr
  940. ! correct param ?
  941. status=-1; call CheckInfo( mf%sds, status, name=paramkey )
  942. if (status>0) then; TRACEBACK; status=1; return; end if
  943. ! adhoc: some data sets have other name ...
  944. if ( status < 0 ) then
  945. select case ( paramkey )
  946. case ( 'sp', 'spm' )
  947. status=-1; call CheckInfo( mf%sds, status, name='ps' )
  948. case ( 'sps' )
  949. status=-1; call CheckInfo( mf%sds, status, name='sp' )
  950. case ( 'mfu' )
  951. status=-1; call CheckInfo( mf%sds, status, name='pu' )
  952. case ( 'mfv' )
  953. status=-1; call CheckInfo( mf%sds, status, name='pv' )
  954. case ( 'mfw' )
  955. status=-1; call CheckInfo( mf%sds, status, name='pw' )
  956. case ( 'PVo' )
  957. status=-1; call CheckInfo( mf%sds, status, name='PV' )
  958. case ( 'sr' )
  959. status=-1; call CheckInfo( mf%sds, status, name='sr_ecm' )
  960. case ( 'srols' )
  961. status=-1; call CheckInfo( mf%sds, status, name='sr_ols' )
  962. case ( 'srmer' )
  963. status=-1; call CheckInfo( mf%sds, status, name='sr_mer' )
  964. case ( 'albedo' )
  965. status=-1; call CheckInfo( mf%sds, status, name='al' )
  966. case ( 'ewss', 'nsss' )
  967. if ( mf%sstr_to_ewss_nsss ) then
  968. status=-1; call CheckInfo( mf%sds, status, name='sstr' )
  969. end if
  970. end select
  971. if (status>0) then; TRACEBACK; status=1; return; end if
  972. ! try other data set ?
  973. if ( status < 0 ) cycle
  974. end if
  975. ! correct time ?
  976. select case ( paramkey )
  977. case ( 'oro' )
  978. ! constant field, skip time check
  979. case ( 'srols' )
  980. ! monthly field:
  981. ! in TMPP files: daily samples, valid for [21,21]
  982. ! in TM5 files: monthly value
  983. ! read time2 :
  984. call ReadAttribute( mf%sds, 'time2', time2s, status )
  985. if (status/=0) then; TRACEBACK; return; end if
  986. tim2s = NewDate( time6=time2s )
  987. ! [t1,t2] covers month; time2 should be in this month:
  988. if ( (t1 >= tim2s) .and. (tim2s <= t2) ) then
  989. ! ok
  990. status = 0
  991. end if
  992. case default
  993. ! extract time arrays:
  994. call Get( t1, time6=time1 )
  995. call Get( t2, time6=time2 )
  996. !
  997. ! replace year ?
  998. if ( mf%fixyear > 0 ) then
  999. select case ( mf%form )
  1000. ! year is valid for time interval:
  1001. ! ( fixyear-1/12/31 21:00 , fixyear/12/31 21:00 ]
  1002. case ( 'tmpp' )
  1003. if ( t1 == t2 ) then
  1004. ! instant time
  1005. thelp = NewDate( year=time1(1), month=12, day=31, hour=21 )
  1006. if ( t1 > thelp ) then
  1007. time1(1) = mf%fixyear - 1
  1008. else
  1009. time1(1) = mf%fixyear
  1010. end if
  1011. time2 = time1
  1012. else
  1013. ! time interval
  1014. thelp = NewDate( year=time1(1), month=12, day=31, hour=21 )
  1015. if ( t1 >= thelp ) then
  1016. time1(1) = mf%fixyear - 1
  1017. else
  1018. time1(1) = mf%fixyear
  1019. end if
  1020. thelp = NewDate( time6=time1 ) + (t2-t1)
  1021. call Get( thelp, time6=time2 )
  1022. endif
  1023. ! year is valid for time interval:
  1024. ! [ fixyear/01/01 00:00 , fixyear/12/31 24:00 )
  1025. case ( 'tm5' )
  1026. if ( t1 == t2 ) then
  1027. ! instant time
  1028. time1(1) = mf%fixyear
  1029. time2 = time1
  1030. else
  1031. ! time interval
  1032. time1(1) = mf%fixyear
  1033. thelp = NewDate( time6=time1 ) + (t2-t1)
  1034. call Get( thelp, time6=time2 )
  1035. endif
  1036. ! unknown ..
  1037. case default
  1038. write (gol,'("fixyear : unsupported mf form : ",a)') trim(mf%form); call goErr
  1039. TRACEBACK; status=1; return
  1040. end select
  1041. end if
  1042. !
  1043. !! debug
  1044. !call ReadAttribute( mf%sds, 'time1', xtime1, status )
  1045. !call ReadAttribute( mf%sds, 'time2', xtime2, status )
  1046. !write (gol,'("DEBUG - sds time1 = ",i4,5i3)') xtime1; call goPr
  1047. !write (gol,'("DEBUG - sds time2 = ",i4,5i3)') xtime2; call goPr
  1048. !write (gol,'("DEBUG - request time1 = ",i4,5i3)') time1; call goPr
  1049. !write (gol,'("DEBUG - request time2 = ",i4,5i3)') time2; call goPr
  1050. !
  1051. if ( mf%qad ) then
  1052. ! test if times match exactly:
  1053. status1=-1 ; call CheckAttribute( mf%sds, 'time1', time1, status1 )
  1054. if (status1>0) then; TRACEBACK; status=1; return; end if
  1055. status2=-1 ; call CheckAttribute( mf%sds, 'time2', time2, status2 )
  1056. if (status2>0) then; TRACEBACK; status=1; return; end if
  1057. status = status1 + status2
  1058. ! times do not match ?
  1059. if ( status < 0 ) then
  1060. ! read time1 and time2 :
  1061. call ReadAttribute( mf%sds, 'time1', time1s, status )
  1062. if (status/=0) then; TRACEBACK; return; end if
  1063. call ReadAttribute( mf%sds, 'time2', time2s, status )
  1064. if (status/=0) then; TRACEBACK; return; end if
  1065. ! hours should match ...
  1066. if ( (time1s(4) /= time1(4)) .or. (time2s(4) /= time2(4)) ) then
  1067. status = -1
  1068. cycle
  1069. end if
  1070. ! warning ...
  1071. write (gol,'("WARNING - weak time check passed:")'); call goPr
  1072. write (gol,'("WARNING - parameter : ",a)') trim(paramkey); call goPr
  1073. call wrtgol( 'WARNING - t1 : ', t1 ); call goPr
  1074. call wrtgol( 'WARNING - t2 : ', t2 ); call goPr
  1075. write (gol,'("WARNING - file : ",a)') trim(mf%fname); call goPr
  1076. ! ok
  1077. status = 0
  1078. end if
  1079. ! weak time check passed
  1080. else
  1081. !! tmpp fields valid for [21,21] should be read as valid for [00,24] ...
  1082. !if ( rTotal(t2-t1,'hour') == 24.0 ) then
  1083. ! tim1s = t1 + IncrDate(hour=3)
  1084. ! tim2s = t2 + IncrDate(hour=3)
  1085. ! call Get( tim1s, time6=time1 )
  1086. ! call Get( tim2s, time6=time2 )
  1087. !end if
  1088. ! first try wether times exactely match;
  1089. status1=-1 ; call CheckAttribute( mf%sds, 'time1', time1, status1 )
  1090. if (status1>0) then; TRACEBACK; status=1; return; end if
  1091. status2=-1 ; call CheckAttribute( mf%sds, 'time2', time2, status2 )
  1092. if (status2>0) then; TRACEBACK; status=1; return; end if
  1093. status = status1 + status2
  1094. ! try mid time ?
  1095. if ( status < 0 ) then
  1096. if ( t1 == t2 ) then
  1097. ! instant time requested; check wether this is mid of times in file:
  1098. call ReadAttribute( mf%sds, 'time1', time1s, status )
  1099. IF_NOTOK_RETURN(status=1)
  1100. tim1s = NewDate( time6=time1s )
  1101. call ReadAttribute( mf%sds, 'time2', time2s, status )
  1102. IF_NOTOK_RETURN(status=1)
  1103. tim2s = NewDate( time6=time2s )
  1104. ! mid of times in file:
  1105. tmid = tim1s + (tim2s-tim1s)/2
  1106. ! requested time:
  1107. treq = t1
  1108. if ( mf%fixyear > 0 ) call Set( treq, year=mf%fixyear )
  1109. ! not ok ? then try again
  1110. if ( treq /= tmid ) then
  1111. status = -1
  1112. cycle
  1113. end if
  1114. else
  1115. ! interval requested; check wether time in file is mid time:
  1116. tmid = t1 + (t2-t1)/2
  1117. if ( mf%fixyear > 0 ) call Set( tmid, year=mf%fixyear )
  1118. call Get( tmid, time6=time1 )
  1119. status1=-1 ; call CheckAttribute( mf%sds, 'time1', time1, status1 )
  1120. if (status1>0) then; TRACEBACK; status=1; return; end if
  1121. status2=-1 ; call CheckAttribute( mf%sds, 'time2', time1, status2 )
  1122. if (status2>0) then; TRACEBACK; status=1; return; end if
  1123. status = status1 + status2
  1124. if ( status < 0 ) cycle
  1125. end if
  1126. end if
  1127. ! time check passed
  1128. end if
  1129. end select
  1130. ! found!
  1131. exit
  1132. end do
  1133. ! not found ?
  1134. if ( status /= 0 ) then
  1135. write (gol,'("Unable to locate field in hdf file:")'); call goErr
  1136. write (gol,'(" parameter : ",a)') trim(paramkey); call goErr
  1137. call wrtgol( ' t1 : ', t1 ); call goErr
  1138. call wrtgol( ' t2 : ', t2 ); call goErr
  1139. call wrtgol( ' mf%tr(1) : ', mf%trange(1) ); call goErr
  1140. call wrtgol( ' mf%tr(2) : ', mf%trange(2) ); call goErr
  1141. write (gol,'(" file : ",a)') trim(mf%fname); call goErr
  1142. TRACEBACK; status = 1; return
  1143. end if
  1144. ! store paramkey
  1145. mf%paramkey = paramkey
  1146. ! ok
  1147. mf%selected = .true.
  1148. ! ~~~ surface pressure ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1149. if ( mf%spm_load ) then
  1150. mf%spm_selected = .false.
  1151. if ( paramkey /= 'sp' ) then
  1152. ! only hours 00, 06, 12, 18
  1153. dhour = nint(rTotal(t2-t1,'hour'))
  1154. select case ( dhour )
  1155. case ( 0 )
  1156. t_spm = t1
  1157. case ( 3 )
  1158. call Get( t1, hour=hour )
  1159. if ( modulo(hour,6) == 0 ) then
  1160. t_spm = t1
  1161. else
  1162. t_spm = t2
  1163. end if
  1164. case ( 6 )
  1165. t_spm = t1 + IncrDate( hour=3 )
  1166. case default
  1167. write (gol,'("do not know how to form time for mid surface pressure:")'); call goErr
  1168. call wrtgol( ' t1 : ', t1 ); call goErr
  1169. call wrtgol( ' t2 : ', t2 ); call goErr
  1170. TRACEBACK; status = 1; return
  1171. end select
  1172. ! replace year ?
  1173. if ( mf%fixyear > 0 ) then
  1174. call Set( t_spm, year=mf%fixyear )
  1175. end if
  1176. ! initial no record is found ...
  1177. status = 1
  1178. ! number of data sets:
  1179. if ( mf%spm_extr ) then
  1180. call GetInfo( mf%spm_hdf, status, num_datasets=nsds )
  1181. else
  1182. call GetInfo( mf%hdf, status, num_datasets=nsds )
  1183. end if
  1184. if (status/=0) then; TRACEBACK; return; end if
  1185. ! loop over all data sets:
  1186. do isds = 1, nsds
  1187. ! select data set:
  1188. if ( mf%spm_extr ) then
  1189. call Select( mf%spm_sds, mf%spm_hdf, isds-1, status )
  1190. else
  1191. call Select( mf%spm_sds, mf%hdf, isds-1, status )
  1192. end if
  1193. if (status/=0) then; TRACEBACK; return; end if
  1194. ! correct param ?
  1195. status=-1; call CheckInfo( mf%spm_sds, status, name='ps' )
  1196. if (status>0) then; TRACEBACK; status=1; return; end if
  1197. ! not found ? also try other names:
  1198. if ( status < 0) then
  1199. select case ( paramkey )
  1200. case ( 'mfu', 'pu' )
  1201. status=-1; call CheckInfo( mf%spm_sds, status, name='spu' )
  1202. case ( 'mfv', 'pv' )
  1203. status=-1; call CheckInfo( mf%spm_sds, status, name='spv' )
  1204. case default
  1205. status=-1; call CheckInfo( mf%spm_sds, status, name='sp' )
  1206. end select
  1207. if (status>0) then; TRACEBACK; status=1; return; end if
  1208. ! try next ?
  1209. if ( status < 0 ) cycle
  1210. end if
  1211. ! correct time ?
  1212. call Get( t1, time6=time1 )
  1213. status=-1; call CheckAttribute( mf%spm_sds, 'time1', time1, status )
  1214. if (status>0) then; TRACEBACK; status=1; return; end if
  1215. if (status==0) then
  1216. call Get( t2, time6=time1 )
  1217. status=-1; call CheckAttribute( mf%spm_sds, 'time2', time2, status )
  1218. if (status>0) then; TRACEBACK; status=1; return; end if
  1219. else
  1220. ! try special spm times
  1221. call Get( t_spm, time6=time1 )
  1222. status=-1; call CheckAttribute( mf%spm_sds, 'time1', time1, status )
  1223. if (status<0) cycle
  1224. if (status>0) then; TRACEBACK; status=1; return; end if
  1225. ! try special spm times
  1226. call Get( t_spm, time6=time2 )
  1227. status=-1; call CheckAttribute( mf%spm_sds, 'time2', time2, status )
  1228. if (status<0) cycle
  1229. if (status>0) then; TRACEBACK; status=1; return; end if
  1230. end if
  1231. ! found!
  1232. exit
  1233. end do
  1234. ! not found ?
  1235. if ( status /= 0 ) then
  1236. write (gol,'("Unable to locate surface pressure field in hdf file:")'); call goErr
  1237. call wrtgol( ' t1 - t2 : ', t1, ' - ', t2 ); call goErr
  1238. call wrtgol( ' t_spm : ', t_spm ); call goErr
  1239. write (gol,'(" file : ",a)') trim(mf%spm_fname); call goErr
  1240. TRACEBACK; status = 1; return
  1241. end if
  1242. ! ok
  1243. mf%spm_selected = .true.
  1244. end if
  1245. end if
  1246. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1247. ! ok
  1248. status = 0
  1249. end subroutine mf_SelectRecord
  1250. ! ***
  1251. !
  1252. ! initialiase grid info from sds
  1253. !
  1254. subroutine lli_Init_mf( lli, nuv, mf, status )
  1255. use file_hdf, only : ReadAttribute, CheckAttribute
  1256. use Grid, only : TllGridInfo, Init
  1257. ! --- in/out ----------------------------------
  1258. type(TllGridInfo), intent(inout) :: lli
  1259. character(len=1), intent(in) :: nuv
  1260. type(TMeteoFile_hdf), intent(in) :: mf
  1261. integer, intent(out) :: status
  1262. ! --- const --------------------------------------
  1263. character(len=*), parameter :: rname = mname//'/lli_Init_mf'
  1264. ! --- local -----------------------------------
  1265. real :: lon_deg, dlon_deg
  1266. integer :: nlon
  1267. real :: lat_deg, dlat_deg
  1268. integer :: nlat
  1269. integer :: stat
  1270. ! --- begin ------------------------------------
  1271. ! record not selected ? then return
  1272. if ( .not. mf%selected ) then
  1273. write (gol,'("no record selected ...")'); call goErr
  1274. TRACEBACK; status = 1; return
  1275. end if
  1276. ! check ...
  1277. call CheckAttribute( mf%sds, 'gridtype' , 'll', status )
  1278. if ( status /= 0 ) then
  1279. write (gol,'("sds does not seem to contain ll grid")'); call goErr
  1280. TRACEBACK; status=1; return
  1281. end if
  1282. ! extract grid position parameters from sds:
  1283. call ReadAttribute( mf%sds, 'lon_first', lon_deg , status )
  1284. if (status/=0) then; TRACEBACK; return; end if
  1285. call ReadAttribute( mf%sds, 'lon_inc ', dlon_deg, status )
  1286. if (status/=0) then; TRACEBACK; return; end if
  1287. call ReadAttribute( mf%sds, 'lon_n ', nlon , status )
  1288. if (status/=0) then; TRACEBACK; return; end if
  1289. call ReadAttribute( mf%sds, 'lat_first', lat_deg , status )
  1290. if (status/=0) then; TRACEBACK; return; end if
  1291. call ReadAttribute( mf%sds, 'lat_inc ', dlat_deg, status )
  1292. if (status/=0) then; TRACEBACK; return; end if
  1293. call ReadAttribute( mf%sds, 'lat_n ', nlat , status )
  1294. if (status/=0) then; TRACEBACK; return; end if
  1295. ! the just read values might define points on the cell boundaries,
  1296. ! while lli should define the cell centers;
  1297. ! fill correct values using nuv :
  1298. select case ( nuv )
  1299. case ( 'n' )
  1300. ! sds attributes defined for lat/lon of center
  1301. call Init( lli, lon_deg, dlon_deg, nlon, &
  1302. lat_deg, dlat_deg, nlat, status )
  1303. if (status/=0) then; TRACEBACK; return; end if
  1304. case ( 'u' )
  1305. ! sds attributes defined for lat/lon of east/west bound
  1306. call Init( lli, lon_deg+0.5*dlon_deg, dlon_deg, nlon-1, &
  1307. lat_deg , dlat_deg, nlat , status )
  1308. if (status/=0) then; TRACEBACK; return; end if
  1309. case ( 'v' )
  1310. ! sds attributes defined for lat/lon of south/north bound
  1311. call Init( lli, lon_deg , dlon_deg, nlon , &
  1312. lat_deg+0.5*dlat_deg, dlat_deg, nlat-1, status )
  1313. if (status/=0) then; TRACEBACK; return; end if
  1314. case default
  1315. write (gol,'("unsupported nuv `",a,"`")') nuv; call goErr
  1316. TRACEBACK; status=1; return
  1317. end select
  1318. ! ok
  1319. status = 0
  1320. end subroutine lli_Init_mf
  1321. ! ***
  1322. !
  1323. ! initialiase level info from sds
  1324. !
  1325. subroutine levi_Init_mf( levi, mf, status )
  1326. use file_hdf, only : GetInfo, ReadAttribute
  1327. use Grid, only : TLevelInfo, Init
  1328. ! --- in/out ----------------------------------
  1329. type(TLevelInfo), intent(out) :: levi
  1330. type(TMeteoFile_hdf), intent(in) :: mf
  1331. integer, intent(out) :: status
  1332. ! --- const --------------------------------------
  1333. character(len=*), parameter :: rname = mname//'/levi_Init_mf'
  1334. ! --- local -----------------------------------
  1335. integer :: data_rank
  1336. integer :: lm
  1337. real, allocatable :: a(:), b(:)
  1338. ! --- begin ------------------------------------
  1339. ! record not selected ? then return
  1340. if ( .not. mf%selected ) then
  1341. write (gol,'("no record selected ...")'); call goErr
  1342. TRACEBACK; status = 1; return
  1343. end if
  1344. ! extract size of data array:
  1345. call GetInfo( mf%sds, status, data_rank=data_rank )
  1346. if ( status/= 0 ) then; TRACEBACK; status=1; return; end if
  1347. ! 2D or 3D
  1348. select case ( data_rank )
  1349. case ( 2 )
  1350. ! set dummy values ...
  1351. call Init( levi, 1, (/0.0,0.0/), (/0.0,0.0/), status )
  1352. if ( status/= 0 ) then; TRACEBACK; status=1; return; end if
  1353. case ( 3 )
  1354. ! read number of levels and hybride parameters:
  1355. call ReadAttribute( mf%sds, 'lm', lm, status )
  1356. ! extract hybride coeff
  1357. allocate( a(lm+1), b(lm+1) )
  1358. call ReadAttribute( mf%sds, 'at', a, status )
  1359. if (status/=0) then; TRACEBACK; return; end if
  1360. call ReadAttribute( mf%sds, 'bt', b, status )
  1361. if (status/=0) then; TRACEBACK; return; end if
  1362. ! fill ...
  1363. call Init( levi, lm, a, b, status )
  1364. if ( status/= 0 ) then; TRACEBACK; status=1; return; end if
  1365. ! done
  1366. deallocate( a, b )
  1367. case default
  1368. write (gol,'("unsupported data rank : ",i6)') data_rank; call goErr
  1369. TRACEBACK; status=1; return
  1370. end select
  1371. ! ok
  1372. status = 0
  1373. end subroutine levi_Init_mf
  1374. ! ***
  1375. subroutine mf_GetField( mf, status, gridtype, ll, spm )
  1376. use PArray, only : pa_SetShape
  1377. use file_hdf, only : GetInfo, ReadData, ReadAttribute
  1378. ! --- in/out --------------------------------
  1379. type(TMeteoFile_hdf), intent(inout) :: mf
  1380. integer, intent(out) :: status
  1381. character(len=*), intent(out), optional :: gridtype
  1382. real, pointer, optional :: ll(:,:,:)
  1383. real, pointer, optional :: spm(:,:)
  1384. ! --- const --------------------------------------
  1385. character(len=*), parameter :: rname = mname//'/mf_GetField'
  1386. ! --- local -------------------------------
  1387. integer :: data_rank
  1388. integer :: data_dims(3)
  1389. ! --- begin -------------------------------
  1390. ! record not selected ? then return
  1391. if ( .not. mf%selected ) then
  1392. write (gol,'("no record selected ...")'); call goErr
  1393. TRACEBACK; status = 1; return
  1394. end if
  1395. ! initial data is not extracted ...
  1396. status = 1
  1397. ! return grid type ?
  1398. if ( present(gridtype) ) then
  1399. call ReadAttribute( mf%sds, 'gridtype', gridtype, status )
  1400. if (status/=0) then; TRACEBACK; return; end if
  1401. end if
  1402. ! return 3d data array ?
  1403. if ( present(ll) ) then
  1404. ! extract data rank and shape:
  1405. call GetInfo( mf%sds, status, data_rank=data_rank, data_dims=data_dims )
  1406. if (status/=0) then; TRACEBACK; return; end if
  1407. ! extract data array:
  1408. select case ( data_rank )
  1409. case ( 2 )
  1410. data_dims(3) = 1
  1411. call pa_SetShape( ll, data_dims )
  1412. call ReadData( mf%sds, ll(:,:,1), status )
  1413. if (status/=0) then; TRACEBACK; return; end if
  1414. case ( 3 )
  1415. call pa_SetShape( ll, data_dims )
  1416. call ReadData( mf%sds, ll, status )
  1417. if (status/=0) then; TRACEBACK; return; end if
  1418. case default
  1419. write (gol,'("unsupported data rank:",i6)') data_rank; call goErr
  1420. TRACEBACK; status=1; return
  1421. end select
  1422. ! ReadData breaks on error ...
  1423. status = 0
  1424. end if
  1425. ! return surface pressure array ?
  1426. if ( present(spm) ) then
  1427. if ( .not. mf%spm_selected ) then
  1428. write (gol,'("no spm record selected ...")'); call goErr
  1429. TRACEBACK; status = 1; return
  1430. end if
  1431. ! extract data rank and shape:
  1432. call GetInfo( mf%spm_sds, status, data_rank=data_rank, data_dims=data_dims )
  1433. if (status/=0) then; TRACEBACK; return; end if
  1434. ! extract data array:
  1435. select case ( data_rank )
  1436. case ( 2 )
  1437. call pa_SetShape( spm, data_dims(1:2) )
  1438. call ReadData( mf%spm_sds, spm, status )
  1439. if (status/=0) then; TRACEBACK; return; end if
  1440. case default
  1441. write (gol,'("unsupported data rank for spm:",i6)') data_rank; call goErr
  1442. TRACEBACK; status=1; return
  1443. end select
  1444. ! ReadData breaks on error ...
  1445. status = 0
  1446. end if
  1447. ! no arguments processed ?
  1448. ! probably subroutine is called incorrectly ...
  1449. if ( status /= 0 ) then
  1450. write (gol,'("no arguments processed; wrong call ?")'); call goErr
  1451. TRACEBACK; status = 1; return
  1452. end if
  1453. ! ok
  1454. status = 0
  1455. end subroutine mf_GetField
  1456. ! ***
  1457. subroutine mf_ReadRecord( mf, paramkey, t1, t2, nuv, nw, &
  1458. gridtype, levi, &
  1459. lli, ll, sp_ll, &
  1460. status )
  1461. use PArray, only : pa_Done
  1462. use GO, only : TDate
  1463. use Grid, only : TllGridInfo, TLevelInfo
  1464. use file_hdf, only : Init, Done
  1465. ! --- in/out -------------------------------
  1466. type(TMeteoFile_hdf), intent(inout) :: mf
  1467. character(len=*), intent(in) :: paramkey
  1468. type(TDate), intent(in) :: t1, t2
  1469. character(len=1), intent(in) :: nuv
  1470. character(len=2), intent(out) :: gridtype
  1471. type(TLevelInfo), intent(out) :: levi
  1472. character(len=1), intent(in) :: nw
  1473. type(TllGridInfo), intent(inout) :: lli
  1474. real, pointer :: ll(:,:,:)
  1475. real, pointer :: sp_ll(:,:)
  1476. integer, intent(out) :: status
  1477. ! --- const --------------------------------------
  1478. character(len=*), parameter :: rname = mname//'/mf_ReadRecord'
  1479. ! --- local -------------------------------
  1480. logical :: exist
  1481. real, allocatable :: mfw_n(:,:,:)
  1482. real, allocatable :: sp_ll_n(:,:)
  1483. integer :: i, j
  1484. ! --- begin ---------------------------------
  1485. ! input ?
  1486. if ( mf%io /= 'i' ) then
  1487. write (gol,'("file should have been opened for input, but io=",a)') mf%io; call goErr
  1488. TRACEBACK; status=1; return
  1489. end if
  1490. ! open for reading:
  1491. ! if it is opened in Init, to many hdf files would be open:
  1492. !
  1493. inquire( file=trim(mf%fname), exist=exist )
  1494. if ( .not. exist ) then
  1495. write (gol,'("main file does not exist:")'); call goErr
  1496. write (gol,'(" ",a)') trim(mf%fname); call goErr
  1497. TRACEBACK; status=1; return
  1498. end if
  1499. !
  1500. call Init( mf%hdf, trim(mf%fname), 'read', status )
  1501. if (status/=0) then; TRACEBACK; return; end if
  1502. !
  1503. call Init( mf%sds, status )
  1504. if (status/=0) then; TRACEBACK; return; end if
  1505. !
  1506. mf%selected = .false.
  1507. ! open spm file if necessary;
  1508. ! if it is opened in Init, to many hdf files would be open:
  1509. if ( mf%spm_load ) then
  1510. !
  1511. if ( mf%spm_extr ) then
  1512. !
  1513. inquire( file=trim(mf%spm_fname), exist=exist )
  1514. if ( .not. exist ) then
  1515. write (gol,'("spm file does not exist:")'); call goErr
  1516. write (gol,'(" ",a)') trim(mf%spm_fname); call goErr
  1517. TRACEBACK; status=1; return
  1518. end if
  1519. !
  1520. call Init( mf%spm_hdf, trim(mf%spm_fname), 'read', status )
  1521. if (status/=0) then; TRACEBACK; return; end if
  1522. !
  1523. end if
  1524. !
  1525. call Init( mf%spm_sds, status )
  1526. if (status/=0) then; TRACEBACK; return; end if
  1527. !
  1528. mf%spm_selected = .false.
  1529. end if
  1530. ! select records in hdf files:
  1531. call mf_SelectRecord( mf, paramkey, t1, t2, status )
  1532. if ( status/= 0 ) then; TRACEBACK; status=1; return; end if
  1533. ! always regular lat/lon grid ..
  1534. gridtype = 'll'
  1535. ! setup grid definition:
  1536. call lli_Init_mf( lli, nuv, mf, status )
  1537. if ( status/= 0 ) then; TRACEBACK; status=1; return; end if
  1538. ! setup level definition:
  1539. call levi_Init_mf( levi, mf, status )
  1540. if ( status/= 0 ) then; TRACEBACK; status=1; return; end if
  1541. ! fill data array:
  1542. call mf_GetField( mf, status, ll=ll )
  1543. if ( status/= 0 ) then; TRACEBACK; status=1; return; end if
  1544. ! ***
  1545. ! special treatment of pw fields ?
  1546. if ( (paramkey == 'mfw') .or. (paramkey == 'pw') ) then
  1547. ! add extra top level ?
  1548. if ( size(ll,3) == levi%nlev ) then
  1549. ! copy current pw ('n' levels)
  1550. allocate( mfw_n(lli%im,lli%jm,levi%nlev) )
  1551. mfw_n = ll
  1552. ! renew target array
  1553. deallocate( ll )
  1554. allocate( ll(lli%nlon,lli%nlat,levi%nlev+1) )
  1555. ! store new pw:
  1556. ll(:,:,1:levi%nlev) = mfw_n
  1557. ll(:,:,levi%nlev+1) = 0.0 ! kg/s
  1558. ! clear
  1559. deallocate( mfw_n )
  1560. end if
  1561. ! change flux direction ?
  1562. if ( mf%mfw_redir ) ll = - ll ! upwards (increasing level)
  1563. end if
  1564. ! ***
  1565. ! special treatment of surface stress fields ?
  1566. if ( ((paramkey=='ewss') .or. (paramkey=='nsss')) .and. mf%sstr_to_ewss_nsss ) then
  1567. ! sstr**2 = (sstr**2)/2 + (sstr**2)/2 = nsss**2 + ewss**2
  1568. ! thus nsss = sqrt( (sstr**2)/2.0 )
  1569. ll = sqrt( (ll**2) / 2.0 )
  1570. end if
  1571. ! ***
  1572. ! fill surface pressure field ?
  1573. if ( mf%spm_load .and. mf%spm_selected ) then
  1574. ! read field:
  1575. call mf_GetField( mf, status, spm=sp_ll )
  1576. if ( status/= 0 ) then; TRACEBACK; status=1; return; end if
  1577. ! convert from 'n' to 'u'/'v' ?
  1578. if ( mf%spm_n_to_uv ) then
  1579. select case ( nuv )
  1580. case ( 'n' )
  1581. ! no interpolation
  1582. case ( 'u' )
  1583. ! copy current sp ('n')
  1584. allocate( sp_ll_n(lli%im,lli%jm) )
  1585. sp_ll_n = sp_ll
  1586. ! renew size of output array:
  1587. deallocate( sp_ll )
  1588. allocate( sp_ll(0:lli%im,lli%jm) )
  1589. ! interpol:
  1590. sp_ll(0,:) = 1.5*sp_ll_n(1,:) - 0.5*sp_ll_n(2,:)
  1591. do i = 1, lli%im-1
  1592. sp_ll(i,:) = 1.5*sp_ll_n(i,:) + 0.5*sp_ll_n(i+1,:)
  1593. end do
  1594. sp_ll(lli%im,:) = -0.5*sp_ll_n(lli%im-1,:) + 1.5*sp_ll_n(lli%im,:)
  1595. ! clear
  1596. deallocate( sp_ll_n )
  1597. case ( 'v' )
  1598. ! copy current sp ('n')
  1599. allocate( sp_ll_n(lli%im,lli%jm) )
  1600. sp_ll_n = sp_ll
  1601. ! renew size of output array:
  1602. deallocate( sp_ll )
  1603. allocate( sp_ll(lli%im,0:lli%jm) )
  1604. ! interpol:
  1605. sp_ll(:,0) = 1.5*sp_ll_n(:,1) - 0.5*sp_ll_n(:,2)
  1606. do j = 1, lli%jm-1
  1607. sp_ll(:,j) = 1.5*sp_ll_n(:,j) + 0.5*sp_ll_n(:,j+1)
  1608. end do
  1609. sp_ll(:,lli%jm) = -0.5*sp_ll_n(:,lli%jm-1) + 1.5*sp_ll_n(:,lli%jm)
  1610. ! clear
  1611. deallocate( sp_ll_n )
  1612. case default
  1613. write (gol,'("unsupported nuv `",a,"`")') nuv; call goErr
  1614. TRACEBACK; status=1; return
  1615. end select
  1616. end if
  1617. else
  1618. call pa_Done( sp_ll )
  1619. end if
  1620. ! close surface pressure file ?
  1621. if ( mf%spm_load ) then
  1622. call Done( mf%spm_sds, status )
  1623. if (status/=0) then; TRACEBACK; return; end if
  1624. !
  1625. if ( mf%spm_extr ) then
  1626. call Done( mf%spm_hdf, status )
  1627. if (status/=0) then; TRACEBACK; return; end if
  1628. end if
  1629. end if
  1630. ! close field file:
  1631. call Done( mf%sds, status )
  1632. if (status/=0) then; TRACEBACK; return; end if
  1633. call Done( mf%hdf, status )
  1634. if (status/=0) then; TRACEBACK; return; end if
  1635. ! ok
  1636. status = 0
  1637. end subroutine mf_ReadRecord
  1638. ! ***
  1639. ! !
  1640. ! ! Read equivalent latitude bounds and indices
  1641. ! !
  1642. !
  1643. ! subroutine mf_ReadEqvLatStuff( mf, t1, t2, eqvlatb, eqvinds, status )
  1644. !
  1645. ! use PArray, only : pa_Done
  1646. ! use GO, only : TDate, Get, wrtgol
  1647. ! use file_hdf, only : TSds, Select, CheckInfo, CheckAttribute
  1648. ! use file_hdf, only : GetInfo, ReadData
  1649. !
  1650. ! ! --- in/out -------------------------------
  1651. !
  1652. ! type(TMeteoFile_hdf), intent(inout) :: mf
  1653. ! type(TDate), intent(in) :: t1, t2
  1654. ! real, intent(out) :: eqvlatb(:,:)
  1655. ! integer, intent(out) :: eqvinds(:,:,:)
  1656. ! integer, intent(out) :: status
  1657. !
  1658. ! ! --- const --------------------------------------
  1659. !
  1660. ! character(len=*), parameter :: rname = mname//'/mf_ReadEqvLatStuff'
  1661. !
  1662. ! ! --- local -------------------------------
  1663. !
  1664. ! integer :: nsds, isds
  1665. ! integer :: time(6)
  1666. ! type(TDate) :: t_spm
  1667. ! integer :: hour, dhour
  1668. !
  1669. ! character(len=20) :: paramkey
  1670. ! integer :: data_dims(2)
  1671. !
  1672. ! ! --- begin ---------------------------------
  1673. !
  1674. ! ! input ?
  1675. ! if ( mf%io /= 'i' ) then
  1676. ! write (gol,'("file should have been opened for input, but io=",a)') mf%io; call goErr
  1677. ! TRACEBACK; status=1; return
  1678. ! end if
  1679. !
  1680. ! mf%selected = .false.
  1681. !
  1682. ! ! initial no record is found ...
  1683. ! status = 1
  1684. !
  1685. ! ! ***
  1686. !
  1687. ! paramkey = 'eqvlatb'
  1688. !
  1689. ! ! loop over all data sets
  1690. ! call GetInfo( mf%hdf, status, num_datasets=nsds )
  1691. ! if (status/=0) then; TRACEBACK; return; end if
  1692. ! do isds = 1, nsds
  1693. ! call Select( mf%sds, mf%hdf, isds-1, status )
  1694. ! if (status/=0) then; TRACEBACK; return; end if
  1695. !
  1696. ! ! correct param ?
  1697. ! call CheckInfo( mf%sds, status, name=paramkey )
  1698. ! if (status/=0) cycle
  1699. !
  1700. ! ! correct time ?
  1701. ! call Get( t1, time6=time )
  1702. ! call CheckAttribute( mf%sds, 'time1', time, status )
  1703. ! if (status/=0) cycle
  1704. ! call Get( t2, time6=time )
  1705. ! call CheckAttribute( mf%sds, 'time2', time, status )
  1706. ! if (status/=0) cycle
  1707. !
  1708. ! ! found!
  1709. ! exit
  1710. ! end do
  1711. !
  1712. ! ! not found ?
  1713. ! if ( status /= 0 ) then
  1714. ! write (gol,'("Unable to locate field in hdf file:")'); call goErr
  1715. ! write (gol,'(" parameter : ",a)') trim(paramkey); call goErr
  1716. ! call wrtgol( ' t1 : ', t1 ); call goErr
  1717. ! call wrtgol( ' t2 : ', t2 ); call goErr
  1718. ! write (gol,'(" file : ",a)') trim(mf%fname); call goErr
  1719. ! TRACEBACK; status = 1; return
  1720. ! end if
  1721. !
  1722. ! ! read field
  1723. ! call ReadData( mf%sds, eqvlatb, status )
  1724. ! if (status/=0) then; TRACEBACK; return; end if
  1725. !
  1726. !
  1727. ! ! ***
  1728. !
  1729. ! paramkey = 'eqvinds'
  1730. !
  1731. ! ! loop over all data sets
  1732. ! call GetInfo( mf%hdf, status, num_datasets=nsds )
  1733. ! if (status/=0) then; TRACEBACK; return; end if
  1734. ! do isds = 1, nsds
  1735. ! call Select( mf%sds, mf%hdf, isds-1, status )
  1736. ! if (status/=0) then; TRACEBACK; return; end if
  1737. !
  1738. ! ! correct param ?
  1739. ! call CheckInfo( mf%sds, status, name=paramkey )
  1740. ! if (status/=0) cycle
  1741. !
  1742. ! ! correct time ?
  1743. ! call Get( t1, time6=time )
  1744. ! call CheckAttribute( mf%sds, 'time1', time, status )
  1745. ! if (status/=0) cycle
  1746. ! call Get( t2, time6=time )
  1747. ! call CheckAttribute( mf%sds, 'time2', time, status )
  1748. ! if (status/=0) cycle
  1749. !
  1750. ! ! found!
  1751. ! exit
  1752. ! end do
  1753. !
  1754. ! ! not found ?
  1755. ! if ( status /= 0 ) then
  1756. ! write (gol,'("Unable to locate field in hdf file:")'); call goErr
  1757. ! write (gol,'(" parameter : ",a)') trim(paramkey); call goErr
  1758. ! call wrtgol( ' t1 : ', t1 ); call goErr
  1759. ! call wrtgol( ' t2 : ', t2 ); call goErr
  1760. ! write (gol,'(" file : ",a)') trim(mf%fname); call goErr
  1761. ! TRACEBACK; status = 1; return
  1762. ! end if
  1763. !
  1764. ! ! read field
  1765. ! call ReadData( mf%sds, eqvinds, status )
  1766. ! if (status/=0) then; TRACEBACK; return; end if
  1767. !
  1768. ! ! ***
  1769. !
  1770. ! ! ok
  1771. ! status = 0
  1772. !
  1773. ! end subroutine mf_ReadEqvLatStuff
  1774. ! ******************************************************************
  1775. ! ***
  1776. ! *** output
  1777. ! ***
  1778. ! ******************************************************************
  1779. subroutine WriteHeader( mf, lli, status, levi )
  1780. use Binas, only : grav, ae
  1781. use Grid, only : TllGridInfo, TLevelInfo
  1782. use file_hdf, only : WriteAttribute
  1783. ! --- in/out -------------------------------
  1784. type(TMeteoFile_hdf), intent(inout) :: mf
  1785. type(TllGridInfo), intent(in) :: lli
  1786. integer, intent(out) :: status
  1787. type(TLevelInfo), intent(in), optional :: levi
  1788. ! --- const --------------------------------------
  1789. character(len=*), parameter :: rname = mname//'/WriteHeader'
  1790. ! --- begin ---------------------------------
  1791. ! write header:
  1792. call WriteAttribute( mf%hdf, 'fname' , trim(mf%fname), status )
  1793. if (status/=0) then; TRACEBACK; return; end if
  1794. call WriteAttribute( mf%hdf, 'format' , output_format, status )
  1795. if (status/=0) then; TRACEBACK; return; end if
  1796. call WriteAttribute( mf%hdf, 'gridtype', 'll' , status )
  1797. if (status/=0) then; TRACEBACK; return; end if
  1798. ! save first and last lon/lat (center) for use with HIPHOP
  1799. call WriteAttribute( mf%hdf, 'lonmin', lli%lon_deg(1) , status, knd=rknd )
  1800. if (status/=0) then; TRACEBACK; return; end if
  1801. call WriteAttribute( mf%hdf, 'lonmax', lli%lon_deg(lli%nlon), status, knd=rknd )
  1802. if (status/=0) then; TRACEBACK; return; end if
  1803. call WriteAttribute( mf%hdf, 'latmin', lli%lat_deg(1) , status, knd=rknd )
  1804. if (status/=0) then; TRACEBACK; return; end if
  1805. call WriteAttribute( mf%hdf, 'latmax', lli%lat_deg(lli%nlat), status, knd=rknd )
  1806. if (status/=0) then; TRACEBACK; return; end if
  1807. ! other useful stuff ...
  1808. call WriteAttribute( mf%hdf, 'grav' , grav , status, knd=rknd )
  1809. if (status/=0) then; TRACEBACK; return; end if
  1810. call WriteAttribute( mf%hdf, 'ae' , ae , status, knd=rknd )
  1811. if (status/=0) then; TRACEBACK; return; end if
  1812. call WriteAttribute( mf%hdf, 'area_m2', lli%area_m2, status, knd=rknd )
  1813. if (status/=0) then; TRACEBACK; return; end if
  1814. ! level stuff
  1815. if ( present(levi) ) then
  1816. call WriteAttribute( mf%hdf, 'lm', levi%nlev, status, knd=iknd )
  1817. if (status/=0) then; TRACEBACK; return; end if
  1818. call WriteAttribute( mf%hdf, 'at', levi%a , status, knd=rknd )
  1819. if (status/=0) then; TRACEBACK; return; end if
  1820. call WriteAttribute( mf%hdf, 'bt', levi%b , status, knd=rknd )
  1821. if (status/=0) then; TRACEBACK; return; end if
  1822. end if
  1823. ! ok
  1824. status = 0
  1825. end subroutine WriteHeader
  1826. ! ***
  1827. subroutine WriteSdsHeader( sds, tmi, unit, tref, t1, t2, lli, nuv, status, &
  1828. levi, nw, nlev )
  1829. use Binas , only : p_global
  1830. use file_hdf, only : TSds
  1831. use file_hdf, only : SetDim, WriteAttribute, Compress
  1832. use GO , only : TDate, Get, rTotal, operator(-), IsAnyDate
  1833. use Grid , only : TllGridInfo, TLevelInfo
  1834. use tmm_info, only : TMeteoInfo
  1835. ! --- in/out -----------------------------
  1836. type(TSds), intent(inout) :: sds
  1837. type(TMeteoInfo), intent(in) :: tmi
  1838. character(len=*), intent(in) :: unit
  1839. type(TDate), intent(in) :: tref, t1, t2
  1840. type(TllGridInfo), intent(in) :: lli
  1841. character(len=1), intent(in) :: nuv
  1842. integer, intent(out) :: status
  1843. type(TLevelInfo), intent(in), optional :: levi
  1844. character(len=1), intent(in), optional :: nw
  1845. integer, intent(in), optional :: nlev
  1846. ! --- const --------------------------------------
  1847. character(len=*), parameter :: rname = mname//'/WriteSdsHeader'
  1848. ! --- local -----------------------------
  1849. integer :: time6(6)
  1850. integer :: dhour
  1851. ! --- begin -----------------------------
  1852. ! *** history
  1853. ! write history of meteo field:
  1854. if ( len_trim(tmi%history) < 1 ) then
  1855. call WriteAttribute( sds, 'history', '-', status )
  1856. IF_NOTOK_RETURN(status=1)
  1857. else
  1858. call WriteAttribute( sds, 'history', trim(tmi%history), status )
  1859. IF_NOTOK_RETURN(status=1)
  1860. end if
  1861. ! *** unit
  1862. ! write unit attribute
  1863. call WriteAttribute( sds, 'unit', unit, status )
  1864. IF_NOTOK_RETURN(status=1)
  1865. ! *** time
  1866. ! time interval in hours
  1867. if ( IsAnyDate(t1) .or. IsAnyDate(t2) ) then
  1868. dhour = 0
  1869. else
  1870. dhour = nint(rTotal( t2 - t2, 'hour' ))
  1871. end if
  1872. ! write old time attributes
  1873. call Get( t1, time6=time6 )
  1874. call WriteAttribute( sds, 'idate', time6, status, knd=iknd )
  1875. IF_NOTOK_RETURN(status=1)
  1876. call WriteAttribute( sds, 'dthrs', dhour, status, knd=iknd ) ! hours
  1877. IF_NOTOK_RETURN(status=1)
  1878. ! write new time attributes
  1879. call Get( tref, time6=time6 )
  1880. call WriteAttribute( sds, 'tref', time6, status, knd=iknd )
  1881. IF_NOTOK_RETURN(status=1)
  1882. call Get( t1, time6=time6 )
  1883. call WriteAttribute( sds, 'time1', time6, status, knd=iknd )
  1884. IF_NOTOK_RETURN(status=1)
  1885. call Get( t2, time6=time6 )
  1886. call WriteAttribute( sds, 'time2', time6, status, knd=iknd )
  1887. IF_NOTOK_RETURN(status=1)
  1888. time6 = 0
  1889. time6(4) = dhour
  1890. call WriteAttribute( sds, 'dtime', time6, status, knd=iknd )
  1891. IF_NOTOK_RETURN(status=1)
  1892. ! *** grid
  1893. select case ( nuv )
  1894. case ( 'n' )
  1895. ! sds dimensions:
  1896. call SetDim( sds, 1-1, 'LON' , 'deg', lli%lon_deg, status, knd=rknd )
  1897. IF_NOTOK_RETURN(status=1)
  1898. call SetDim( sds, 2-1, 'LAT' , 'deg', lli%lat_deg, status, knd=rknd )
  1899. IF_NOTOK_RETURN(status=1)
  1900. ! sds attributes:
  1901. call WriteAttribute( sds, 'gridtype' , 'll' , status )
  1902. IF_NOTOK_RETURN(status=1)
  1903. call WriteAttribute( sds, 'lon_first', lli%lon_deg(1), status, knd=rknd )
  1904. IF_NOTOK_RETURN(status=1)
  1905. call WriteAttribute( sds, 'lon_inc ', lli%dlon_deg , status, knd=rknd )
  1906. IF_NOTOK_RETURN(status=1)
  1907. call WriteAttribute( sds, 'lon_n ', lli%nlon , status, knd=iknd )
  1908. IF_NOTOK_RETURN(status=1)
  1909. call WriteAttribute( sds, 'lat_first', lli%lat_deg(1), status, knd=rknd )
  1910. IF_NOTOK_RETURN(status=1)
  1911. call WriteAttribute( sds, 'lat_inc ', lli%dlat_deg , status, knd=rknd )
  1912. IF_NOTOK_RETURN(status=1)
  1913. call WriteAttribute( sds, 'lat_n ', lli%nlat , status, knd=iknd )
  1914. IF_NOTOK_RETURN(status=1)
  1915. case ( 'u' )
  1916. ! sds dimensions:
  1917. call SetDim( sds, 1-1, 'LONP1' , 'deg', lli%blon_deg, status, knd=rknd )
  1918. IF_NOTOK_RETURN(status=1)
  1919. call SetDim( sds, 2-1, 'LAT' , 'deg', lli%lat_deg , status, knd=rknd )
  1920. IF_NOTOK_RETURN(status=1)
  1921. ! sds attributes:
  1922. call WriteAttribute( sds, 'gridtype' , 'll', status )
  1923. IF_NOTOK_RETURN(status=1)
  1924. call WriteAttribute( sds, 'lon_first', lli%blon_deg(0), status, knd=rknd )
  1925. IF_NOTOK_RETURN(status=1)
  1926. call WriteAttribute( sds, 'lon_inc ', lli%dlon_deg , status, knd=rknd )
  1927. IF_NOTOK_RETURN(status=1)
  1928. call WriteAttribute( sds, 'lon_n ', lli%im+1 , status, knd=iknd )
  1929. IF_NOTOK_RETURN(status=1)
  1930. call WriteAttribute( sds, 'lat_first', lli%lat_deg(1) , status, knd=rknd )
  1931. IF_NOTOK_RETURN(status=1)
  1932. call WriteAttribute( sds, 'lat_inc ', lli%dlat_deg , status, knd=rknd )
  1933. IF_NOTOK_RETURN(status=1)
  1934. call WriteAttribute( sds, 'lat_n ', lli%nlat , status, knd=iknd )
  1935. IF_NOTOK_RETURN(status=1)
  1936. case ( 'v' )
  1937. ! sds dimensions:
  1938. call SetDim( sds, 1-1, 'LON' , 'deg', lli%lon_deg , status, knd=rknd )
  1939. IF_NOTOK_RETURN(status=1)
  1940. call SetDim( sds, 2-1, 'LATP1' , 'deg', lli%blat_deg, status, knd=rknd )
  1941. IF_NOTOK_RETURN(status=1)
  1942. ! sds attributes:
  1943. call WriteAttribute( sds, 'gridtype' , 'll', status )
  1944. call WriteAttribute( sds, 'lon_first', lli%lon_deg(1) , status, knd=rknd )
  1945. IF_NOTOK_RETURN(status=1)
  1946. call WriteAttribute( sds, 'lon_inc ', lli%dlon_deg , status, knd=rknd )
  1947. IF_NOTOK_RETURN(status=1)
  1948. call WriteAttribute( sds, 'lon_n ', lli%nlon , status, knd=iknd )
  1949. IF_NOTOK_RETURN(status=1)
  1950. call WriteAttribute( sds, 'lat_first', lli%blat_deg(0), status, knd=rknd )
  1951. IF_NOTOK_RETURN(status=1)
  1952. call WriteAttribute( sds, 'lat_inc ', lli%dlat_deg , status, knd=rknd )
  1953. IF_NOTOK_RETURN(status=1)
  1954. call WriteAttribute( sds, 'lat_n ', lli%jm+1 , status, knd=iknd )
  1955. IF_NOTOK_RETURN(status=1)
  1956. case default
  1957. write (gol,'("unsupported nuv `",a,"`")') nuv; call goErr
  1958. TRACEBACK; status=1; return
  1959. end select
  1960. ! *** levels
  1961. if ( present(levi) ) then
  1962. if ( .not. present(nw) ) then
  1963. write (gol,'("optional levi requires nw")'); call goErr
  1964. TRACEBACK; status=1; return
  1965. end if
  1966. ! sds dimensions:
  1967. select case ( nw )
  1968. case ( '*' )
  1969. if ( present(nlev) ) then
  1970. call SetDim( sds, 3-1, 'HYBRID_SELECTED', 'Pa' , levi%fp0(1:nlev), status, knd=rknd )
  1971. IF_NOTOK_RETURN(status=1)
  1972. end if
  1973. case ( 'n' )
  1974. call SetDim( sds, 3-1, 'HYBRID', 'Pa' , levi%fp0, status, knd=rknd )
  1975. IF_NOTOK_RETURN(status=1)
  1976. case ( 'w' )
  1977. call SetDim( sds, 3-1, 'HYBRIDh', 'Pa', levi%p0, status, knd=rknd )
  1978. IF_NOTOK_RETURN(status=1)
  1979. case default
  1980. write (gol,'("unsupported nw `",a,"` xxx")') nw; call goErr
  1981. TRACEBACK; status=1; return
  1982. end select
  1983. ! sds attributes:
  1984. call WriteAttribute( sds, 'lm', levi%nlev, status, knd=iknd )
  1985. IF_NOTOK_RETURN(status=1)
  1986. call WriteAttribute( sds, 'at', levi%a , status, knd=rknd )
  1987. IF_NOTOK_RETURN(status=1)
  1988. call WriteAttribute( sds, 'bt', levi%b , status, knd=rknd )
  1989. IF_NOTOK_RETURN(status=1)
  1990. end if
  1991. ! *** data compression
  1992. call Compress( sds, compression, status, deflate_level=deflate_level )
  1993. IF_NOTOK_RETURN(status=1)
  1994. ! *** end
  1995. ! ok
  1996. status = 0
  1997. end subroutine WriteSdsHeader
  1998. ! ***
  1999. subroutine WriteStatus( mf, msg, status )
  2000. ! --- in/out -------------------------------
  2001. type(TMeteoFile_hdf), intent(inout) :: mf
  2002. character(len=*), intent(in) :: msg
  2003. integer, intent(out) :: status
  2004. ! --- const --------------------------------------
  2005. character(len=*), parameter :: rname = mname//'/WriteStatus'
  2006. ! --- local ------------------------------
  2007. integer :: fu
  2008. logical :: opened
  2009. ! --- begin ---------------------------------
  2010. ! select unused file unit:
  2011. fu = 1234
  2012. do
  2013. inquire( unit=fu, opened=opened )
  2014. if ( .not. opened ) exit
  2015. fu = fu + 1
  2016. end do
  2017. ! open:
  2018. open( fu, file=trim(mf%fname)//'.status', form='formatted', iostat=status )
  2019. if (status/=0) then
  2020. write (gol,'("opening status file:")'); call goErr
  2021. write (gol,'(" file : ",a)') trim(mf%fname); call goErr
  2022. TRACEBACK; status=1; return
  2023. end if
  2024. ! write message:
  2025. write (fu,'(a)',iostat=status) msg
  2026. if (status/=0) then
  2027. write (gol,'("writing status:")'); call goErr
  2028. write (gol,'(" file : ",a)') trim(mf%fname); call goErr
  2029. write (gol,'(" msg : ",a)') msg; call goErr
  2030. TRACEBACK; status=1; return
  2031. end if
  2032. ! done:
  2033. close( fu, iostat=status )
  2034. if (status/=0) then
  2035. write (gol,'("closing status file:")'); call goErr
  2036. write (gol,'(" file : ",a)') trim(mf%fname); call goErr
  2037. TRACEBACK; status=1; return
  2038. end if
  2039. ! ok
  2040. status = 0
  2041. end subroutine WriteStatus
  2042. ! ***
  2043. subroutine AddOutputName( mf, name, status )
  2044. ! --- in/out -------------------------------
  2045. type(TMeteoFile_hdf), intent(inout) :: mf
  2046. character(len=*), intent(in) :: name
  2047. integer, intent(out) :: status
  2048. ! --- const --------------------------------------
  2049. character(len=*), parameter :: rname = mname//'/AddOuputName'
  2050. ! --- local ------------------------------
  2051. integer :: iname
  2052. ! --- begin ---------------------------------
  2053. ! if not present yet, add name
  2054. iname = 1
  2055. do
  2056. ! add ?
  2057. if ( iname > mf%output_nname ) then
  2058. ! place to store ?
  2059. if ( iname > size(mf%output_names) ) then
  2060. write (gol,'("length of mf%output_names array too small:")'); call goErr
  2061. do iname = 1, size(mf%output_names)
  2062. write (gol,'(" ",i3," ",a)') iname, trim(mf%output_names(iname)); call goErr
  2063. end do
  2064. TRACEBACK; status=1; return
  2065. end if
  2066. ! long enough for name ?
  2067. if ( len(mf%output_names(iname)) < len(name) ) then
  2068. write (gol,'("length of mf%output_names too small:")'); call goErr
  2069. write (gol,'(" len(mf%output_names(i)) : ",i4)') len(mf%output_names(iname)); call goErr
  2070. write (gol,'(" len(mname) : ",i4)') len(name); call goErr
  2071. TRACEBACK; status=1; return
  2072. end if
  2073. ! store:
  2074. mf%output_names(iname) = name
  2075. ! increase counter:
  2076. mf%output_nname = iname
  2077. ! leave:
  2078. exit
  2079. end if
  2080. ! found ? then leave loop
  2081. if ( mf%output_names(iname) == name ) exit
  2082. ! next index
  2083. iname = iname + 1
  2084. end do
  2085. ! ok
  2086. status = 0
  2087. end subroutine AddOutputName
  2088. ! ***
  2089. subroutine mf_WriteRecord_2d( mf, tmi, paramkey, unit, tref, t1, t2, &
  2090. lli, nuv, ll, status )
  2091. use GO , only : TDate
  2092. use Grid , only : TllGridInfo
  2093. use file_hdf, only : TSds, WriteData
  2094. use file_hdf_base, only : Init, Done ! needed on aster
  2095. use tmm_info, only : TMeteoInfo
  2096. ! --- in/out -------------------------------
  2097. type(TMeteoFile_hdf), intent(inout) :: mf
  2098. type(TMeteoInfo), intent(in) :: tmi
  2099. character(len=*), intent(in) :: paramkey, unit
  2100. type(TDate), intent(in) :: tref, t1, t2
  2101. type(TllGridInfo), intent(in) :: lli
  2102. character(len=1), intent(in) :: nuv
  2103. real, intent(in) :: ll(:,:)
  2104. integer, intent(out) :: status
  2105. ! --- const --------------------------------------
  2106. character(len=*), parameter :: rname = mname//'/mf_WriteRecord_2d'
  2107. ! --- local ------------------------------
  2108. type(TSds) :: sds
  2109. ! --- begin ---------------------------------
  2110. ! output ?
  2111. if ( mf%io /= 'o' ) then
  2112. write (gol,'("file should have been opened for output, but io=",a)') mf%io; call goErr
  2113. TRACEBACK; status=1; return
  2114. end if
  2115. ! new or existing ?
  2116. if ( .not. mf%output_initialised ) then
  2117. ! open new file, destroy old:
  2118. call Init( mf%hdf, trim(mf%fname), 'create', status )
  2119. IF_NOTOK_RETURN(status=1)
  2120. ! write file header:
  2121. call WriteHeader( mf, lli, status )
  2122. IF_NOTOK_RETURN(status=1)
  2123. ! status new
  2124. call WriteStatus( mf, 'in-progress', status )
  2125. IF_NOTOK_RETURN(status=1)
  2126. ! no records written yet:
  2127. mf%output_nrec = 0
  2128. ! now the file is initialised
  2129. mf%output_initialised = .true.
  2130. else
  2131. ! re-open file:
  2132. call Init( mf%hdf, trim(mf%fname), 'write', status )
  2133. IF_NOTOK_RETURN(status=1)
  2134. endif
  2135. ! *** data set
  2136. ! add record name:
  2137. call AddOutputName( mf, paramkey, status )
  2138. IF_NOTOK_RETURN(status=1)
  2139. ! init data set:
  2140. call Init( sds, mf%hdf, paramkey, shape(ll), 'real', status, knd=rknd_ds )
  2141. IF_NOTOK_RETURN(status=1)
  2142. ! write unit, time, and grid info:
  2143. call WriteSdsHeader( sds, tmi, unit, tref, t1, t2, lli, nuv, status )
  2144. IF_NOTOK_RETURN(status=1)
  2145. ! write grid
  2146. call WriteData( sds, ll, status )
  2147. IF_NOTOK_RETURN(status=1)
  2148. ! done:
  2149. call Done( sds, status )
  2150. IF_NOTOK_RETURN(status=1)
  2151. ! *** completed ?
  2152. ! next record has been written:
  2153. mf%output_nrec = mf%output_nrec + 1
  2154. ! completed ?
  2155. if ( mf%output_nrec == mf%output_ntrec*mf%output_nname ) then
  2156. ! re-write status file:
  2157. call WriteStatus( mf, 'completed', status )
  2158. IF_NOTOK_RETURN(status=1)
  2159. ! ensure that new file is initialized ...
  2160. mf%output_initialised = .false.
  2161. end if
  2162. ! *** close
  2163. call Done( mf%hdf, status )
  2164. IF_NOTOK_RETURN(status=1)
  2165. ! ok
  2166. status = 0
  2167. end subroutine mf_WriteRecord_2d
  2168. ! ***
  2169. subroutine mf_WriteRecord_3d( mf, tmi, spname, paramkey, unit, tref, t1, t2, &
  2170. lli, nuv, levi, nw, ps, ll, status )
  2171. use GO , only : TDate
  2172. use Grid , only : TllGridInfo, TLevelInfo
  2173. use file_hdf, only : TSds, WriteData
  2174. use file_hdf_base, only : Init, Done !needed on aster
  2175. use tmm_info, only : TMeteoInfo
  2176. ! --- in/out -------------------------------
  2177. type(TMeteoFile_hdf), intent(inout) :: mf
  2178. type(TMeteoInfo), intent(in) :: tmi
  2179. character(len=*), intent(in) :: spname, paramkey, unit
  2180. type(TDate), intent(in) :: tref, t1, t2
  2181. type(TllGridInfo), intent(in) :: lli
  2182. character(len=1), intent(in) :: nuv
  2183. type(TLevelInfo), intent(in) :: levi
  2184. character(len=1), intent(in) :: nw
  2185. real, intent(in) :: ps(:,:)
  2186. real, intent(in) :: ll(:,:,:)
  2187. integer, intent(out) :: status
  2188. ! --- const --------------------------------------
  2189. character(len=*), parameter :: rname = mname//'/mf_WriteRecord_3d'
  2190. ! --- local ------------------------------
  2191. type(TSds) :: sds
  2192. integer :: iname
  2193. ! --- begin ---------------------------------
  2194. ! output ?
  2195. if ( mf%io /= 'o' ) then
  2196. write (gol,'("file should have been opened for output, but io=",a)') mf%io; call goErr
  2197. TRACEBACK; status=1; return
  2198. end if
  2199. ! new or existing ?
  2200. if ( .not. mf%output_initialised ) then
  2201. ! open new file, destroy old:
  2202. call Init( mf%hdf, trim(mf%fname), 'create', status )
  2203. IF_NOTOK_RETURN(status=1)
  2204. ! write file header:
  2205. call WriteHeader( mf, lli, status, levi )
  2206. IF_NOTOK_RETURN(status=1)
  2207. ! status new
  2208. call WriteStatus( mf, 'in-progress', status )
  2209. IF_NOTOK_RETURN(status=1)
  2210. ! no records written yet:
  2211. mf%output_nrec = 0
  2212. ! now the file is initialised
  2213. mf%output_initialised = .true.
  2214. else
  2215. ! re-open file:
  2216. call Init( mf%hdf, trim(mf%fname), 'write', status )
  2217. IF_NOTOK_RETURN(status=1)
  2218. endif
  2219. ! *** data set
  2220. ! add record name:
  2221. call AddOutputName( mf, paramkey, status )
  2222. IF_NOTOK_RETURN(status=1)
  2223. ! init data set:
  2224. call Init( sds, mf%hdf, paramkey, shape(ll), 'real', status, knd=rknd_ds )
  2225. IF_NOTOK_RETURN(status=1)
  2226. ! write unit, time, and grid info:
  2227. call WriteSdsHeader( sds, tmi, unit, tref, t1, t2, lli, nuv, status, levi, nw )
  2228. IF_NOTOK_RETURN(status=1)
  2229. ! write grid
  2230. call WriteData( sds, ll, status )
  2231. IF_NOTOK_RETURN(status=1)
  2232. ! done:
  2233. call Done( sds, status )
  2234. IF_NOTOK_RETURN(status=1)
  2235. ! *** surface pressure
  2236. ! init data set:
  2237. call Init( sds, mf%hdf, spname, shape(ps), 'real', status, knd=rknd_ds )
  2238. IF_NOTOK_RETURN(status=1)
  2239. ! write unit, time, and grid info:
  2240. call WriteSdsHeader( sds, tmi, 'Pa', tref, t1, t2, lli, nuv, status )
  2241. IF_NOTOK_RETURN(status=1)
  2242. ! write grid
  2243. call WriteData( sds, ps, status )
  2244. IF_NOTOK_RETURN(status=1)
  2245. ! done:
  2246. call Done( sds, status )
  2247. IF_NOTOK_RETURN(status=1)
  2248. ! *** completed ?
  2249. ! next record has been written:
  2250. mf%output_nrec = mf%output_nrec + 1
  2251. ! completed ?
  2252. if ( mf%output_nrec == mf%output_ntrec*mf%output_nname ) then
  2253. ! re-write status file:
  2254. call WriteStatus( mf, 'completed', status )
  2255. IF_NOTOK_RETURN(status=1)
  2256. ! ensure that new file is initialized ...
  2257. mf%output_initialised = .false.
  2258. end if
  2259. ! *** close
  2260. call Done( mf%hdf, status )
  2261. IF_NOTOK_RETURN(status=1)
  2262. ! ok
  2263. status = 0
  2264. end subroutine mf_WriteRecord_3d
  2265. end module tmm_mf_hdf