tmm_mf_tm5_hdf.F90 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233
  1. !###############################################################################
  2. !
  3. ! Input/output of meteofiles produced by TM5 : HDF version.
  4. !
  5. !###############################################################################
  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_tm5_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_tm5_hdf
  22. public :: TMM_MF_TM5_HDF_Init, TMM_MF_TM5_HDF_Done
  23. public :: Init, Done
  24. public :: Get
  25. public :: ReadRecord
  26. public :: WriteRecord
  27. ! --- const ------------------------------
  28. character(len=*), parameter :: mname = 'tmm_mf_tm5_hdf'
  29. ! ~~~ output keys and defaults
  30. ! current format version
  31. character(len=*), parameter :: output_format = 'tm5-hdf'
  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_tm5_hdf
  42. ! input/output ?
  43. character(len=1) :: io
  44. !
  45. ! field collection
  46. !
  47. character(len=256) :: fname
  48. type(THdfFile) :: hdf
  49. character(len=256) :: paramkeys
  50. type(TDate) :: trange(2)
  51. !
  52. ! file
  53. !
  54. type(TSds) :: sds
  55. logical :: selected
  56. character(len=16) :: paramkey
  57. !
  58. ! pw specials
  59. !
  60. logical :: mfw_redir
  61. !
  62. ! surface stress specials
  63. !
  64. logical :: sstr_to_ewss_nsss
  65. !
  66. ! surface pressure field
  67. !
  68. logical :: spm_load ! load spm ?
  69. logical :: spm_selected ! data set filled ?
  70. type(TSds) :: spm_sds ! data set for sp
  71. logical :: spm_incl ! record included in main file ?
  72. logical :: spm_extr ! record in external spm file ?
  73. character(len=256) :: spm_fname
  74. type(THdfFile) :: spm_hdf
  75. logical :: spm_n_to_uv
  76. !
  77. ! output
  78. !
  79. logical :: output_initialised
  80. integer :: output_nrec
  81. integer :: output_ntrec
  82. character(len=20) :: output_names(20)
  83. integer :: output_nname
  84. !
  85. ! adhoc ...
  86. integer :: fixyear
  87. logical :: qad
  88. end type TMeteoFile_tm5_hdf
  89. ! --- interfaces -------------------------
  90. interface Init
  91. module procedure mf_Init
  92. end interface
  93. interface Done
  94. module procedure mf_Done
  95. end interface
  96. interface Get
  97. module procedure mf_Get
  98. end interface
  99. interface ReadRecord
  100. module procedure mf_ReadRecord
  101. end interface
  102. interface WriteRecord
  103. module procedure mf_WriteRecord_2d
  104. module procedure mf_WriteRecord_3d
  105. end interface
  106. ! --- var --------------------------------------
  107. ! timer id's:
  108. integer :: itim_readrecord
  109. contains
  110. ! ==============================================================
  111. subroutine TMM_MF_TM5_HDF_Init( rcf, status )
  112. use GO, only : TrcFile
  113. use GO, only : GO_Timer_Def
  114. ! --- in/out ---------------------------------
  115. type(TRcFile), intent(in) :: rcf
  116. integer, intent(out) :: status
  117. ! --- const ----------------------------------
  118. character(len=*), parameter :: rname = mname//'/TMM_MF_TM5_HDF_Init'
  119. ! --- local ----------------------------------
  120. ! --- begin ----------------------------------
  121. ! define timers:
  122. call GO_Timer_Def( itim_readrecord, 'tmm tm5-hdf readrecord', status )
  123. IF_NOTOK_RETURN(status=1)
  124. ! ok
  125. status = 0
  126. end subroutine TMM_MF_TM5_HDF_Init
  127. ! ***
  128. subroutine TMM_MF_TM5_HDF_Done( status )
  129. ! --- in/out ---------------------------------
  130. integer, intent(out) :: status
  131. ! --- const ----------------------------------
  132. character(len=*), parameter :: rname = mname//'/TMM_MF_TM5_HDF_Done'
  133. ! --- local ----------------------------------
  134. ! --- begin ----------------------------------
  135. ! ok
  136. status = 0
  137. end subroutine TMM_MF_TM5_HDF_Done
  138. ! ==============================================================
  139. subroutine mf_Init( mf, io, dir, archivekeys, paramkey, &
  140. tref, t1, t2, status )
  141. use GO, only : TDate, Set, Get, NewDate, AnyDate, IsAnyDate
  142. use GO, only : rTotal, operator(-), operator(>=)
  143. use GO, only : goVarValue, goWriteKeyNum, goReplace
  144. ! --- in/out ----------------------------
  145. type(TMeteoFile_tm5_hdf), intent(out) :: mf
  146. character(len=1), intent(in) :: io
  147. character(len=*), intent(in) :: dir
  148. character(len=*), intent(in) :: archivekeys
  149. character(len=*), intent(in) :: paramkey
  150. type(TDate), intent(in) :: tref, t1, t2
  151. integer, intent(out) :: status
  152. ! --- const --------------------------------------
  153. character(len=*), parameter :: rname = mname//'/mf_Init'
  154. ! --- local --------------------------------
  155. character(len=64) :: mf_mdir, mf_tres
  156. character(len=64) :: mf_class, mf_type, mf_grid
  157. character(len=1) :: mf_psep, mf_nsep
  158. character(len=64) :: mf_filekey
  159. character(len=4) :: mf_fckey
  160. type(TDate) :: tfile
  161. integer :: ccyy, mm, dd, dh
  162. type(TDate) :: tc
  163. logical :: exist
  164. ! --- begin --------------------------------
  165. ! store i/o :
  166. mf%io = io
  167. ! default flags:
  168. mf%sstr_to_ewss_nsss = .false.
  169. mf%mfw_redir = .false.
  170. !
  171. ! extract fields from archivekey :
  172. ! mdir=ec-fg_3h-ml60-glb3x2;tres=_21p06
  173. !
  174. mf%fixyear = -1
  175. call goVarValue( archivekeys, ';', 'fixyear', '=', mf%fixyear, status )
  176. if (status>0) then; TRACEBACK; status=1; return; end if
  177. mf_mdir = 'no_mdir'
  178. call goVarValue( archivekeys, ';', 'mdir', '=', mf_mdir, status )
  179. if (status>0) then; TRACEBACK; status=1; return; end if
  180. !
  181. mf_tres = 'no_tres'
  182. call goVarValue( archivekeys, ';', 'tres', '=', mf_tres, status )
  183. if (status>0) then; TRACEBACK; status=1; return; end if
  184. !
  185. ! path seperation character:
  186. mf_psep = '/'
  187. call goVarValue( archivekeys, ';', 'pathsep', '=', mf_psep, status )
  188. if (status>0) then; TRACEBACK; status=1; return; end if
  189. !
  190. ! name seperation character:
  191. mf_nsep = '-'
  192. call goVarValue( archivekeys, ';', 'namesep', '=', mf_nsep, status )
  193. if (status>0) then; TRACEBACK; status=1; return; end if
  194. !
  195. ! adhoc flag
  196. mf%sstr_to_ewss_nsss = .false.
  197. call goVarValue( archivekeys, ';', 'sstr', '=', mf%sstr_to_ewss_nsss, status )
  198. if (status>0) then; TRACEBACK; status=1; return; end if
  199. !
  200. ! main file
  201. !
  202. ! by default, no surface pressure stuff ...
  203. mf%spm_load = .false.
  204. mf%spm_incl = .false. ! no spm fields included in hdf file
  205. mf%spm_extr = .false. ! no extra spm file
  206. mf%spm_n_to_uv = .false. ! no interpolation from 'n' to 'u' or 'v'
  207. ! * set mf_filekey (uvsp,t,etc) and parmeters:
  208. select case ( paramkey )
  209. case ( 'sp', 'tsp' )
  210. mf_filekey = paramkey
  211. mf%paramkeys = '-'//trim(paramkey)//'-'
  212. mf%spm_load = .false.
  213. case ( 'spm' )
  214. mf_filekey = 'spm'
  215. mf_tres = '_00p06'
  216. mf%paramkeys = '-spm-'
  217. case ( 'mfu', 'mfv' )
  218. mf_filekey = 'mfuv'
  219. mf%paramkeys = '-mfu-mfv-'
  220. mf%spm_load = .true.
  221. case ( 'mfw' )
  222. mf_filekey = 'mfw'
  223. mf%paramkeys = '-mfw-'
  224. mf%spm_load = .true.
  225. case ( 'T' )
  226. mf_filekey = 't'
  227. mf%paramkeys = '-T-'
  228. mf%spm_load = .true.
  229. case ( 'Q' )
  230. mf_filekey = 'q'
  231. mf%paramkeys = '-Q-'
  232. mf%spm_load = .true.
  233. case ( 'CLWC', 'CIWC', 'CC', 'CCO', 'CCU', &
  234. 'clwc', 'ciwc', 'cc', 'cco', 'ccu' )
  235. mf_filekey = 'cld'
  236. mf%paramkeys = '-CLWC-CIWC-CC-CCO-CCU-'
  237. mf%spm_load = .true.
  238. case ( 'eu', 'ed', 'du', 'dd', 'cloud_base', 'cloud_top', 'cloud_lfs' )
  239. mf_filekey = 'sub'
  240. mf%paramkeys = '-eu-ed-du-dd-cloud_base-cloud_top-cloud_lfs-'
  241. mf%spm_load = .true.
  242. case ( 'K' )
  243. mf_filekey = 'diffus'
  244. mf%paramkeys = '-K-'
  245. mf%spm_load = .true.
  246. ! o constant fields
  247. case ( 'oro', 'lsm' )
  248. mf_tres = 'constant'
  249. mf_filekey = trim(paramkey)
  250. mf%paramkeys = '-'//trim(paramkey)//'-'
  251. mf%spm_load = .false.
  252. ! o monthly fields:
  253. case ( 'srols' )
  254. mf_tres = 'month'
  255. mf_filekey = trim(paramkey)
  256. mf%paramkeys = '-'//trim(paramkey)//'-'
  257. mf%spm_load = .false.
  258. ! o vegetation fields
  259. case ( 'cvl', 'cvh', &
  260. 'tv01', 'tv02', 'tv03', 'tv04', 'tv05', &
  261. 'tv06', 'tv07', 'tv08', 'tv09', 'tv10', &
  262. 'tv11', 'tv12', 'tv13', 'tv14', 'tv15', &
  263. 'tv16', 'tv17', 'tv18', 'tv19', 'tv20' )
  264. mf_filekey = 'veg'
  265. mf%paramkeys= '-'//&
  266. 'cvl-cvh-'//&
  267. 'tv01-tv02-tv03-tv04-tv05-'//&
  268. 'tv06-tv07-tv08-tv09-tv10-'//&
  269. 'tv11-tv12-tv13-tv14-tv15-'//&
  270. 'tv16-tv17-tv18-tv19-tv20-'
  271. mf%spm_load = .false.
  272. ! o each surface file in a seperate file;
  273. ! might be collected in surf_*.tar files afterwards
  274. case ( 'sr', 'srmer', &
  275. 'swvl1', &
  276. 'albedo', 'lsrh', 'ci', 'g10m', 'fg10','u10m', 'v10m', 'sd', &
  277. 'lsp', 'cp', 'sf', 'sshf', 'slhf', 'blh', &
  278. 't2m', 'd2m', 'ssr', 'ssrd', 'str', 'strd', &
  279. 'sstr', 'src', 'raero', 'ustar', &
  280. 'sst', 'sps', 'skt', 'ch4fire' )
  281. mf_filekey = trim(paramkey)
  282. mf%paramkeys = '-'//trim(paramkey)//'-'
  283. mf%spm_load = .false.
  284. case ( 'ewss', 'nsss' )
  285. if ( mf%sstr_to_ewss_nsss ) then
  286. mf_filekey = 'sstr'
  287. mf%paramkeys = '-sstr-'
  288. else
  289. mf_filekey = trim(paramkey)
  290. mf%paramkeys = '-'//trim(paramkey)//'-'
  291. end if
  292. mf%spm_load = .false.
  293. case default
  294. write (gol,'("unsupported paramkey `",a,"`")') paramkey; call goErr
  295. TRACEBACK; status=1; return
  296. end select
  297. ! eventually replace by other value:
  298. call goVarValue( archivekeys, ';', 'filekey', '=', mf_filekey, status )
  299. if (status>0) then; TRACEBACK; status=1; return; end if
  300. ! convert input times to file name times:
  301. call GetTime( mf_filekey, mf_tres, tref, t1, t2, status, &
  302. tfile=tfile, trange=mf%trange )
  303. IF_NOTOK_RETURN(status=1)
  304. ! adhoc: fixed year ?
  305. if ( mf%fixyear > 0 ) call Set( tfile, year=mf%fixyear )
  306. ! extract time values:
  307. call Get( tfile, year=ccyy, month=mm, day=dd )
  308. ! replace time values in directory name:
  309. call goReplace( mf_mdir, '<yyyy>', '(i4.4)', ccyy, status )
  310. IF_NOTOK_RETURN(status=1)
  311. call goReplace( mf_mdir, '<mm>', '(i2.2)', mm, status )
  312. IF_NOTOK_RETURN(status=1)
  313. ! special data set: trap change from fg to fc data:
  314. if ( mf_tres == '_fg006up4tr3' ) then
  315. tc = NewDate( 2000, 09, 12 )
  316. if ( tfile >= tc ) mf_tres = '_fc012up2tr3'
  317. end if
  318. ! forecast key: '', 'f1', .., 'f10' ;
  319. ! no key for constant files (t1 and t2 are anydate)
  320. ! or month files (t1 is begin of month thus probably < tref)
  321. mf_fckey = ''
  322. if ( (.not. IsAnyDate(t1)) .and. (t1 >= tref) ) then
  323. dh = floor( rTotal( t1 - tref, 'day' ) )
  324. if ( dh > 0 ) call goWriteKeyNum( mf_fckey, 'f', dh )
  325. end if
  326. ! create file name:
  327. ! dir / ec_od-ml60-T159 - oro.hdf
  328. ! dir / ec_od-ml60-T159 - T_20000101_fg006up4tr3.hdf
  329. select case ( mf_tres )
  330. case ( 'constant' )
  331. ! filename without date:
  332. write (mf%fname,'(a,a,a,a,a,".hdf")') trim(dir), mf_psep, trim(mf_mdir), mf_nsep, trim(mf_filekey)
  333. case ( 'month' )
  334. ! filename without day and forecast key:
  335. write (mf%fname,'(a,a,a,a,a,"_",i4.4,i2.2,a)') &
  336. trim(dir), mf_psep, trim(mf_mdir), mf_nsep, trim(mf_filekey), ccyy, mm, '.hdf'
  337. case default
  338. ! filename including date:
  339. write (mf%fname,'(a,a,a,a,a,"_",i4.4,2i2.2,a,a,a)') &
  340. trim(dir), mf_psep, trim(mf_mdir), mf_nsep, &
  341. trim(mf_filekey), ccyy, mm, dd, trim(mf_fckey), trim(mf_tres), '.hdf'
  342. ! trap request for sps ; might be in sp file ...
  343. if ( trim(mf_filekey) == 'sps' ) then
  344. inquire( file=trim(mf%fname), exist=exist )
  345. if ( .not. exist ) then
  346. write (mf%fname,'(a,a,a,a,a,"_",i4.4,2i2.2,a,a,a)') &
  347. trim(dir), mf_psep, trim(mf_mdir), mf_nsep, &
  348. 'sp', ccyy, mm, dd, trim(mf_fckey), trim(mf_tres), '.hdf'
  349. end if
  350. end if
  351. end select
  352. ! spm is included in files ...
  353. mf%spm_incl = mf%spm_load ! used to be external, now included
  354. mf%spm_fname = mf%fname ! same file
  355. mf%spm_extr = .false. ! not external for sure ...
  356. ! in case of output, not initialised yet ...
  357. mf%output_initialised = .false.
  358. mf%output_nname = 0
  359. ! number of expected time records in a file:
  360. call GetTime( mf_filekey, mf_tres, tref, t1, t2, status, nrec=mf%output_ntrec )
  361. IF_NOTOK_RETURN(status=1)
  362. ! ok
  363. status = 0
  364. end subroutine mf_Init
  365. ! ***
  366. subroutine mf_Done( mf, status )
  367. !use file_hdf, only : Done
  368. ! --- in/out ------------------------------------
  369. type(TMeteoFile_tm5_hdf), intent(inout) :: mf
  370. integer, intent(out) :: status
  371. ! --- const --------------------------------------
  372. character(len=*), parameter :: rname = mname//'/mf_Done'
  373. ! --- begin -------------------------------------
  374. ! files have been closed in ReadRecord/WriteRecord
  375. ! ok
  376. status = 0
  377. end subroutine mf_Done
  378. ! ***
  379. subroutine mf_Get( mf, status, trange1, trange2, paramkeys, filename )
  380. use GO, only : TDate
  381. ! --- in/out ----------------------------
  382. type(TMeteoFile_tm5_hdf), intent(in) :: mf
  383. integer, intent(out) :: status
  384. type(TDate), intent(out), optional :: trange1, trange2
  385. character(len=*), intent(out), optional :: paramkeys
  386. character(len=*), intent(out), optional :: filename
  387. ! --- const --------------------------------------
  388. character(len=*), parameter :: rname = mname//'/mf_Get'
  389. ! --- local --------------------------------
  390. ! --- begin --------------------------------
  391. ! time range:
  392. if ( present(trange1) ) trange1 = mf%trange(1)
  393. if ( present(trange2) ) trange2 = mf%trange(2)
  394. ! parameter names:
  395. if ( present(paramkeys) ) paramkeys = trim(mf%paramkeys)
  396. ! file name:
  397. if ( present(filename) ) filename = trim(mf%fname)
  398. ! ok
  399. status = 0
  400. end subroutine mf_Get
  401. ! ******************************************************************
  402. ! ***
  403. ! *** time range, parameters, file names
  404. ! ***
  405. ! ******************************************************************
  406. !
  407. ! Return time parameters:
  408. ! o tfile : date in filename
  409. ! o trange : time interval covered by fields in file
  410. !
  411. subroutine GetTime( filekey, tres, tref, t1, t2, status, tfile, trange, nrec )
  412. use GO, only : TDate, NewDate, AnyDate, Get, Set, wrtgol, IncrDate, IsAnyDate
  413. use GO, only : operator(<), operator(+), operator(-), rTotal
  414. ! --- in/out --------------------------------
  415. character(len=*), intent(in) :: filekey
  416. character(len=*), intent(in) :: tres
  417. type(TDate), intent(in) :: tref, t1, t2
  418. integer, intent(out) :: status
  419. type(TDate), intent(out), optional :: tfile
  420. type(TDate), intent(out), optional :: trange(2)
  421. integer, intent(out), optional :: nrec
  422. ! --- const --------------------------------------
  423. character(len=*), parameter :: rname = mname//'/GetTime'
  424. ! --- local --------------------------------
  425. integer :: year, month
  426. integer :: hour1, time6(6)
  427. integer :: dd, hh, step
  428. logical :: interval
  429. real :: dhr
  430. ! --- begin --------------------------------
  431. ! set day shift, start hour, and step
  432. select case ( tres )
  433. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  434. ! tmpp [21,21]
  435. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  436. case ( '_21p06', '_21p03', '_av21' )
  437. ! routine is called with tref,t1,t2:
  438. ! t1,t1,t2
  439. ! t1,any,any (oro and other constant fields)
  440. ! thus use tref to construct the file times
  441. if ( IsAnyDate(t1) ) then
  442. call Get( tref, hour=hour1 )
  443. interval = .false.
  444. else
  445. call Get( t1, hour=hour1 )
  446. interval = t1 < t2
  447. end if
  448. ! file ccyymmdd contains fields for (21,21];
  449. ! only uvsp is valid for [21,21] since it contains surface pressure for 21:00
  450. if ( present(tfile) ) then
  451. tfile = tref
  452. call Set( tfile, hour=0, min=0, sec=0 )
  453. if ( (hour1 > 21) .or. ((interval .or. filekey=='uvsp') .and. hour1==21) ) then
  454. tfile = tfile + IncrDate(day=1)
  455. end if
  456. end if
  457. ! fields by default valid for (21,21];
  458. ! only uvsp is valid for [21,21] since it contains surface pressure for 21:00
  459. if ( present(trange) ) then
  460. trange(1) = tref
  461. call Set( trange(1), hour=0, min=0, sec=0 ) ! 00:00 today
  462. if ( (hour1 > 21) .or. ((interval .or. filekey=='uvsp') .and. hour1==21) ) then
  463. trange(1) = trange(1) + IncrDate(day=1)
  464. end if
  465. trange(1) = trange(1) - IncrDate(hour=3) ! previous 21:00
  466. trange(2) = trange(1) + IncrDate(day=1) ! next 21:00
  467. ! boundary not included in most cases:
  468. if ( filekey /= 'uvsp' ) trange(1) = trange(1) + IncrDate(mili=1)
  469. end if
  470. ! number of records in file:
  471. if ( present(nrec) ) then
  472. select case ( tres )
  473. case ( '_21p06' ) ; nrec = 24/6
  474. case ( '_21p03' ) ; nrec = 24/3
  475. case ( '_av21' ) ; nrec = 24/24
  476. case default
  477. write (gol,'("unsupported tres for setting nrec : ",a)') tres; call goErr
  478. TRACEBACK; status=1; return
  479. end select
  480. end if
  481. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  482. ! tm5 constant
  483. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  484. case ( 'constant' )
  485. ! no date in filename ...
  486. if ( present(tfile) ) tfile = AnyDate()
  487. ! fields always valid ...
  488. if ( present(trange) ) then
  489. trange(1) = AnyDate()
  490. trange(2) = AnyDate()
  491. end if
  492. ! only one output record in constant file:
  493. if ( present(nrec) ) nrec = 1
  494. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  495. ! tm5 monthly file
  496. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  497. case ( 'month' )
  498. ! file ccyymmdd contains fields for this month:
  499. if ( present(tfile) ) then
  500. call Get( t1, year=year, month=month )
  501. tfile = NewDate( year=year, month=month, day=1 )
  502. end if
  503. ! field valid from begin to end of month:
  504. if ( present(trange) ) then
  505. call Get( t1, year=year, month=month )
  506. trange(1) = NewDate( year=year, month=month, day=1, hour=00 )
  507. month = month + 1
  508. if ( month > 12 ) then
  509. year = year + 1
  510. month = 1
  511. end if
  512. trange(2) = NewDate( year=year, month=month, day=1, hour=00 )
  513. end if
  514. ! only one output record in month file:
  515. if ( present(nrec) ) nrec = 1
  516. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  517. ! tm5 [00,24]
  518. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  519. case ( '_00p06', '_00p03', '_an0tr6', '_fg006up4tr3', '_fc012up2tr3', '_00p01', '_00p24' )
  520. ! file ccyymmdd contains fields for [00,24) :
  521. if ( present(tfile) ) then
  522. tfile = t1
  523. call Set( tfile, hour=0, min=0, sec=0 )
  524. end if
  525. ! fields valid for [00,24) :
  526. if ( present(trange) ) then
  527. trange(1) = t1
  528. call Set( trange(1), hour=0, min=0, sec=0 ) ! 00:00 today
  529. trange(2) = trange(1) + IncrDate(hour=24) - IncrDate(mili=1)
  530. end if
  531. ! number of records in file:
  532. if ( present(nrec) ) then
  533. select case ( tres )
  534. case ( '_00p06' ) ; nrec = 24/6
  535. case ( '_an0tr6' ) ; nrec = 24/6
  536. case ( '_00p03', '_fg006up4tr3', '_fc012up2tr3' )
  537. ! by default: 3 hourly files
  538. ! for forecasts after 12+72, only 6 hourly available:
  539. ! f0 [ 00, 24) : 00 03 06 09 12 15 18 21 : nrec=8
  540. ! f1 [ 24, 48) : 00 03 06 09 12 15 18 21 : nrec=8
  541. ! f2 [ 48, 72) : 00 03 06 09 12 15 18 21 : nrec=8
  542. ! f3 [ 72, 96) : 00 03 06 09 12 18 : nrec=6
  543. ! f4 [ 96,120) : 00 06 12 18 : nrec=4
  544. ! :
  545. ! f9 [192,216) : 00 06 12 18 : nrec=4
  546. ! f10 [216,240) : 00 06 12 : nrec=3
  547. dhr = rTotal( t1 - tref, 'hour' )
  548. if ( dhr < 72.0 ) then
  549. nrec = 8
  550. else if ( dhr < 96 ) then
  551. nrec = 6
  552. else if ( dhr < 216 ) then
  553. nrec = 4
  554. else
  555. nrec = 3
  556. end if
  557. case ( '_00p01' ) ; nrec = 24/1
  558. case ( '_00p24' ) ; nrec = 24/24
  559. case default
  560. write (gol,'("unsupported tres for setting nrec : ",a)') tres; call goErr
  561. TRACEBACK; status=1; return
  562. end select
  563. end if
  564. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  565. ! ???
  566. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  567. case default
  568. write (gol,'("unsupported time resolution key:")'); call goErr
  569. write (gol,'(" ",a)') trim(tres); call goErr
  570. TRACEBACK; status=1; return
  571. end select
  572. ! ok
  573. status = 0
  574. end subroutine GetTime
  575. ! ******************************************************************
  576. ! ***
  577. ! *** input
  578. ! ***
  579. ! ******************************************************************
  580. subroutine mf_SelectRecord( mf, paramkey, t1, t2, status )
  581. use GO, only : TDate, NewDate, IncrDate, wrtgol, Set, Get, rTotal
  582. use GO, only : operator(-), operator(+), operator(/), operator(==), operator(/=)
  583. use GO, only : operator(>=), operator(<=), operator(>)
  584. use file_hdf, only : GetInfo
  585. use file_hdf, only : TSds, Select, CheckInfo, CheckAttribute, ReadAttribute
  586. ! --- in/out --------------------------------
  587. type(TMeteoFile_tm5_hdf), intent(inout) :: mf
  588. character(len=*), intent(in) :: paramkey
  589. type(TDate), intent(in) :: t1, t2
  590. integer, intent(out) :: status
  591. ! --- const --------------------------------------
  592. character(len=*), parameter :: rname = mname//'/mf_SelectRecord'
  593. ! --- local -------------------------------
  594. integer :: nsds, isds
  595. integer :: time1(6), time2(6)
  596. type(TDate) :: tmid, treq, thelp
  597. type(TDate) :: t_spm
  598. integer :: hour, dhour
  599. integer :: status1, status2
  600. integer :: time1s(6), time2s(6)
  601. type(TDate) :: tim1s, tim2s
  602. ! --- begin -------------------------------
  603. mf%selected = .false.
  604. ! initial no record is found ...
  605. status = 1
  606. ! number of data sets:
  607. call GetInfo( mf%hdf, status, num_datasets=nsds )
  608. IF_NOTOK_RETURN(status=1)
  609. ! loop over all data sets
  610. do isds = 1, nsds
  611. call Select( mf%sds, mf%hdf, isds-1, status )
  612. IF_NOTOK_RETURN(status=1)
  613. ! correct param ?
  614. status=-1; call CheckInfo( mf%sds, status, name=paramkey )
  615. if (status>0) then; TRACEBACK; status=1; return; end if
  616. ! adhoc: some data sets have other name ...
  617. if ( status < 0 ) then
  618. select case ( paramkey )
  619. case ( 'sp', 'spm' )
  620. status=-1; call CheckInfo( mf%sds, status, name='ps' )
  621. case ( 'sps' )
  622. status=-1; call CheckInfo( mf%sds, status, name='sp' )
  623. case ( 'mfu' )
  624. status=-1; call CheckInfo( mf%sds, status, name='pu' )
  625. case ( 'mfv' )
  626. status=-1; call CheckInfo( mf%sds, status, name='pv' )
  627. case ( 'mfw' )
  628. status=-1; call CheckInfo( mf%sds, status, name='pw' )
  629. case ( 'PVo' )
  630. status=-1; call CheckInfo( mf%sds, status, name='PV' )
  631. case ( 'sr' )
  632. status=-1; call CheckInfo( mf%sds, status, name='sr_ecm' )
  633. case ( 'srols' )
  634. status=-1; call CheckInfo( mf%sds, status, name='sr_ols' )
  635. case ( 'srmer' )
  636. status=-1; call CheckInfo( mf%sds, status, name='sr_mer' )
  637. case ( 'albedo' )
  638. status=-1; call CheckInfo( mf%sds, status, name='al' )
  639. case ( 'ewss', 'nsss' )
  640. if ( mf%sstr_to_ewss_nsss ) then
  641. status=-1; call CheckInfo( mf%sds, status, name='sstr' )
  642. end if
  643. end select
  644. if (status>0) then; TRACEBACK; status=1; return; end if
  645. ! try other data set ?
  646. if ( status < 0 ) cycle
  647. end if
  648. ! correct time ?
  649. select case ( paramkey )
  650. case ( 'oro' )
  651. ! constant field, skip time check
  652. case ( 'srols' )
  653. ! monthly field:
  654. ! in TMPP files: daily samples, valid for [21,21]
  655. ! in TM5 files: monthly value
  656. ! read time2 :
  657. call ReadAttribute( mf%sds, 'time2', time2s, status )
  658. IF_NOTOK_RETURN(status=1)
  659. tim2s = NewDate( time6=time2s )
  660. ! [t1,t2] covers month; time2 should be in this month:
  661. if ( (t1 >= tim2s) .and. (tim2s <= t2) ) then
  662. ! ok
  663. status = 0
  664. end if
  665. case default
  666. ! extract time arrays:
  667. call Get( t1, time6=time1 )
  668. call Get( t2, time6=time2 )
  669. !
  670. ! replace year ?
  671. if ( mf%fixyear > 0 ) then
  672. ! year is valid for time interval:
  673. ! [ fixyear/01/01 00:00 , fixyear/12/31 24:00 )
  674. if ( t1 == t2 ) then
  675. ! instant time
  676. time1(1) = mf%fixyear
  677. time2 = time1
  678. else
  679. ! time interval
  680. time1(1) = mf%fixyear
  681. thelp = NewDate( time6=time1 ) + (t2-t1)
  682. call Get( thelp, time6=time2 )
  683. endif
  684. end if
  685. !
  686. if ( mf%qad ) then
  687. ! test if times match exactly:
  688. status1=-1 ; call CheckAttribute( mf%sds, 'time1', time1, status1 )
  689. if (status1>0) then; TRACEBACK; status=1; return; end if
  690. status2=-1 ; call CheckAttribute( mf%sds, 'time2', time2, status2 )
  691. if (status2>0) then; TRACEBACK; status=1; return; end if
  692. status = status1 + status2
  693. ! times do not match ?
  694. if ( status < 0 ) then
  695. ! read time1 and time2 :
  696. call ReadAttribute( mf%sds, 'time1', time1s, status )
  697. IF_NOTOK_RETURN(status=1)
  698. call ReadAttribute( mf%sds, 'time2', time2s, status )
  699. IF_NOTOK_RETURN(status=1)
  700. ! hours should match ...
  701. if ( (time1s(4) /= time1(4)) .or. (time2s(4) /= time2(4)) ) then
  702. status = -1
  703. cycle
  704. end if
  705. ! warning ...
  706. write (gol,'("WARNING - weak time check passed:")'); call goPr
  707. write (gol,'("WARNING - parameter : ",a)') trim(paramkey); call goPr
  708. call wrtgol( 'WARNING - t1 : ', t1 ); call goPr
  709. call wrtgol( 'WARNING - t2 : ', t2 ); call goPr
  710. write (gol,'("WARNING - file : ",a)') trim(mf%fname); call goPr
  711. ! ok
  712. status = 0
  713. end if
  714. ! weak time check passed
  715. else
  716. !! tmpp fields valid for [21,21] should be read as valid for [00,24] ...
  717. !if ( rTotal(t2-t1,'hour') == 24.0 ) then
  718. ! tim1s = t1 + IncrDate(hour=3)
  719. ! tim2s = t2 + IncrDate(hour=3)
  720. ! call Get( tim1s, time6=time1 )
  721. ! call Get( tim2s, time6=time2 )
  722. !end if
  723. ! first try wether times exactely match;
  724. status1=-1 ; call CheckAttribute( mf%sds, 'time1', time1, status1 )
  725. if (status1>0) then; TRACEBACK; status=1; return; end if
  726. status2=-1 ; call CheckAttribute( mf%sds, 'time2', time2, status2 )
  727. if (status2>0) then; TRACEBACK; status=1; return; end if
  728. status = status1 + status2
  729. ! try mid time ?
  730. if ( status < 0 ) then
  731. if ( t1 == t2 ) then
  732. ! instant time requested; check wether this is mid of times in file:
  733. call ReadAttribute( mf%sds, 'time1', time1s, status )
  734. IF_NOTOK_RETURN(status=1)
  735. tim1s = NewDate( time6=time1s )
  736. call ReadAttribute( mf%sds, 'time2', time2s, status )
  737. IF_NOTOK_RETURN(status=1)
  738. tim2s = NewDate( time6=time2s )
  739. ! mid of times in file:
  740. tmid = tim1s + (tim2s-tim1s)/2
  741. ! requested time:
  742. treq = t1
  743. if ( mf%fixyear > 0 ) call Set( treq, year=mf%fixyear )
  744. ! not ok ? then try again
  745. if ( treq /= tmid ) then
  746. status = -1
  747. cycle
  748. end if
  749. else
  750. ! interval requested; check wether time in file is mid time:
  751. tmid = t1 + (t2-t1)/2
  752. if ( mf%fixyear > 0 ) call Set( tmid, year=mf%fixyear )
  753. call Get( tmid, time6=time1 )
  754. status1=-1 ; call CheckAttribute( mf%sds, 'time1', time1, status1 )
  755. if (status1>0) then; TRACEBACK; status=1; return; end if
  756. status2=-1 ; call CheckAttribute( mf%sds, 'time2', time1, status2 )
  757. if (status2>0) then; TRACEBACK; status=1; return; end if
  758. status = status1 + status2
  759. if ( status < 0 ) cycle
  760. end if
  761. end if
  762. ! time check passed
  763. end if
  764. end select
  765. ! found!
  766. exit
  767. end do
  768. ! not found ?
  769. if ( status /= 0 ) then
  770. write (gol,'("Unable to locate field in hdf file:")'); call goErr
  771. write (gol,'(" parameter : ",a)') trim(paramkey); call goErr
  772. call wrtgol( ' t1 : ', t1 ); call goErr
  773. call wrtgol( ' t2 : ', t2 ); call goErr
  774. call wrtgol( ' mf%tr(1) : ', mf%trange(1) ); call goErr
  775. call wrtgol( ' mf%tr(2) : ', mf%trange(2) ); call goErr
  776. write (gol,'(" file : ",a)') trim(mf%fname); call goErr
  777. TRACEBACK; status = 1; return
  778. end if
  779. ! store paramkey
  780. mf%paramkey = paramkey
  781. ! ok
  782. mf%selected = .true.
  783. ! ~~~ surface pressure ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  784. if ( mf%spm_load ) then
  785. mf%spm_selected = .false.
  786. if ( paramkey /= 'sp' ) then
  787. ! only hours 00, 06, 12, 18
  788. dhour = nint(rTotal(t2-t1,'hour'))
  789. select case ( dhour )
  790. case ( 0 )
  791. t_spm = t1
  792. case ( 3 )
  793. call Get( t1, hour=hour )
  794. if ( modulo(hour,6) == 0 ) then
  795. t_spm = t1
  796. else
  797. t_spm = t2
  798. end if
  799. case ( 6 )
  800. t_spm = t1 + IncrDate( hour=3 )
  801. case default
  802. write (gol,'("do not know how to form time for mid surface pressure:")'); call goErr
  803. call wrtgol( ' t1 : ', t1 ); call goErr
  804. call wrtgol( ' t2 : ', t2 ); call goErr
  805. TRACEBACK; status = 1; return
  806. end select
  807. ! replace year ?
  808. if ( mf%fixyear > 0 ) then
  809. call Set( t_spm, year=mf%fixyear )
  810. end if
  811. ! initial no record is found ...
  812. status = 1
  813. ! number of data sets:
  814. if ( mf%spm_extr ) then
  815. call GetInfo( mf%spm_hdf, status, num_datasets=nsds )
  816. else
  817. call GetInfo( mf%hdf, status, num_datasets=nsds )
  818. end if
  819. IF_NOTOK_RETURN(status=1)
  820. ! loop over all data sets:
  821. do isds = 1, nsds
  822. ! select data set:
  823. if ( mf%spm_extr ) then
  824. call Select( mf%spm_sds, mf%spm_hdf, isds-1, status )
  825. else
  826. call Select( mf%spm_sds, mf%hdf, isds-1, status )
  827. end if
  828. IF_NOTOK_RETURN(status=1)
  829. ! correct param ?
  830. status=-1; call CheckInfo( mf%spm_sds, status, name='ps' )
  831. if (status>0) then; TRACEBACK; status=1; return; end if
  832. ! not found ? also try other names:
  833. if ( status < 0) then
  834. select case ( paramkey )
  835. case ( 'mfu', 'pu' )
  836. status=-1; call CheckInfo( mf%spm_sds, status, name='spu' )
  837. case ( 'mfv', 'pv' )
  838. status=-1; call CheckInfo( mf%spm_sds, status, name='spv' )
  839. case default
  840. status=-1; call CheckInfo( mf%spm_sds, status, name='sp' )
  841. end select
  842. if (status>0) then; TRACEBACK; status=1; return; end if
  843. ! try next ?
  844. if ( status < 0 ) cycle
  845. end if
  846. ! correct time ?
  847. call Get( t1, time6=time1 )
  848. status=-1; call CheckAttribute( mf%spm_sds, 'time1', time1, status )
  849. if (status>0) then; TRACEBACK; status=1; return; end if
  850. if (status==0) then
  851. call Get( t2, time6=time1 )
  852. status=-1; call CheckAttribute( mf%spm_sds, 'time2', time2, status )
  853. if (status>0) then; TRACEBACK; status=1; return; end if
  854. else
  855. ! try special spm times
  856. call Get( t_spm, time6=time1 )
  857. status=-1; call CheckAttribute( mf%spm_sds, 'time1', time1, status )
  858. if (status<0) cycle
  859. if (status>0) then; TRACEBACK; status=1; return; end if
  860. ! try special spm times
  861. call Get( t_spm, time6=time2 )
  862. status=-1; call CheckAttribute( mf%spm_sds, 'time2', time2, status )
  863. if (status<0) cycle
  864. if (status>0) then; TRACEBACK; status=1; return; end if
  865. end if
  866. ! found!
  867. exit
  868. end do
  869. ! not found ?
  870. if ( status /= 0 ) then
  871. write (gol,'("Unable to locate surface pressure field in hdf file:")'); call goErr
  872. call wrtgol( ' t1 - t2 : ', t1, ' - ', t2 ); call goErr
  873. call wrtgol( ' t_spm : ', t_spm ); call goErr
  874. write (gol,'(" file : ",a)') trim(mf%spm_fname); call goErr
  875. TRACEBACK; status = 1; return
  876. end if
  877. ! ok
  878. mf%spm_selected = .true.
  879. end if
  880. end if
  881. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  882. ! ok
  883. status = 0
  884. end subroutine mf_SelectRecord
  885. ! ***
  886. !
  887. ! initialiase grid info from sds
  888. !
  889. subroutine lli_Init_mf( lli, nuv, mf, status )
  890. use file_hdf, only : ReadAttribute, CheckAttribute
  891. use Grid, only : TllGridInfo, Init
  892. ! --- in/out ----------------------------------
  893. type(TllGridInfo), intent(inout) :: lli
  894. character(len=1), intent(in) :: nuv
  895. type(TMeteoFile_tm5_hdf), intent(in) :: mf
  896. integer, intent(out) :: status
  897. ! --- const --------------------------------------
  898. character(len=*), parameter :: rname = mname//'/lli_Init_mf'
  899. ! --- local -----------------------------------
  900. real :: lon_deg, dlon_deg
  901. integer :: nlon
  902. real :: lat_deg, dlat_deg
  903. integer :: nlat
  904. integer :: stat
  905. ! --- begin ------------------------------------
  906. ! record not selected ? then return
  907. if ( .not. mf%selected ) then
  908. write (gol,'("no record selected ...")'); call goErr
  909. TRACEBACK; status = 1; return
  910. end if
  911. ! check ...
  912. call CheckAttribute( mf%sds, 'gridtype' , 'll', status )
  913. if ( status /= 0 ) then
  914. write (gol,'("sds does not seem to contain ll grid")'); call goErr
  915. TRACEBACK; status=1; return
  916. end if
  917. ! extract grid position parameters from sds:
  918. call ReadAttribute( mf%sds, 'lon_first', lon_deg , status )
  919. IF_NOTOK_RETURN(status=1)
  920. call ReadAttribute( mf%sds, 'lon_inc ', dlon_deg, status )
  921. IF_NOTOK_RETURN(status=1)
  922. call ReadAttribute( mf%sds, 'lon_n ', nlon , status )
  923. IF_NOTOK_RETURN(status=1)
  924. call ReadAttribute( mf%sds, 'lat_first', lat_deg , status )
  925. IF_NOTOK_RETURN(status=1)
  926. call ReadAttribute( mf%sds, 'lat_inc ', dlat_deg, status )
  927. IF_NOTOK_RETURN(status=1)
  928. call ReadAttribute( mf%sds, 'lat_n ', nlat , status )
  929. IF_NOTOK_RETURN(status=1)
  930. ! the just read values might define points on the cell boundaries,
  931. ! while lli should define the cell centers;
  932. ! fill correct values using nuv :
  933. select case ( nuv )
  934. case ( 'n' )
  935. ! sds attributes defined for lat/lon of center
  936. call Init( lli, lon_deg, dlon_deg, nlon, &
  937. lat_deg, dlat_deg, nlat, status )
  938. IF_NOTOK_RETURN(status=1)
  939. case ( 'u' )
  940. ! sds attributes defined for lat/lon of east/west bound
  941. call Init( lli, lon_deg+0.5*dlon_deg, dlon_deg, nlon-1, &
  942. lat_deg , dlat_deg, nlat , status )
  943. IF_NOTOK_RETURN(status=1)
  944. case ( 'v' )
  945. ! sds attributes defined for lat/lon of south/north bound
  946. call Init( lli, lon_deg , dlon_deg, nlon , &
  947. lat_deg+0.5*dlat_deg, dlat_deg, nlat-1, status )
  948. IF_NOTOK_RETURN(status=1)
  949. case default
  950. write (gol,'("unsupported nuv `",a,"`")') nuv; call goErr
  951. TRACEBACK; status=1; return
  952. end select
  953. ! ok
  954. status = 0
  955. end subroutine lli_Init_mf
  956. ! ***
  957. !
  958. ! initialiase level info from sds
  959. !
  960. subroutine levi_Init_mf( levi, mf, status )
  961. use file_hdf, only : GetInfo, ReadAttribute
  962. use Grid, only : TLevelInfo, Init
  963. ! --- in/out ----------------------------------
  964. type(TLevelInfo), intent(out) :: levi
  965. type(TMeteoFile_tm5_hdf), intent(in) :: mf
  966. integer, intent(out) :: status
  967. ! --- const --------------------------------------
  968. character(len=*), parameter :: rname = mname//'/levi_Init_mf'
  969. ! --- local -----------------------------------
  970. integer :: data_rank
  971. integer :: lm
  972. real, allocatable :: a(:), b(:)
  973. ! --- begin ------------------------------------
  974. ! record not selected ? then return
  975. if ( .not. mf%selected ) then
  976. write (gol,'("no record selected ...")'); call goErr
  977. TRACEBACK; status = 1; return
  978. end if
  979. ! extract size of data array:
  980. call GetInfo( mf%sds, status, data_rank=data_rank )
  981. if ( status/= 0 ) then; TRACEBACK; status=1; return; end if
  982. ! 2D or 3D
  983. select case ( data_rank )
  984. case ( 2 )
  985. ! set dummy values ...
  986. call Init( levi, 1, (/0.0,0.0/), (/0.0,0.0/), status )
  987. if ( status/= 0 ) then; TRACEBACK; status=1; return; end if
  988. case ( 3 )
  989. ! read number of levels and hybride parameters:
  990. call ReadAttribute( mf%sds, 'lm', lm, status )
  991. ! extract hybride coeff
  992. allocate( a(lm+1), b(lm+1) )
  993. call ReadAttribute( mf%sds, 'at', a, status )
  994. IF_NOTOK_RETURN(status=1)
  995. call ReadAttribute( mf%sds, 'bt', b, status )
  996. IF_NOTOK_RETURN(status=1)
  997. ! fill ...
  998. call Init( levi, lm, a, b, status )
  999. IF_NOTOK_RETURN(status=1)
  1000. ! done
  1001. deallocate( a, b )
  1002. case default
  1003. write (gol,'("unsupported data rank : ",i6)') data_rank; call goErr
  1004. TRACEBACK; status=1; return
  1005. end select
  1006. ! ok
  1007. status = 0
  1008. end subroutine levi_Init_mf
  1009. ! ***
  1010. subroutine mf_GetField( mf, status, gridtype, ll, spm )
  1011. use PArray, only : pa_SetShape
  1012. use file_hdf, only : GetInfo, ReadData, ReadAttribute
  1013. ! --- in/out --------------------------------
  1014. type(TMeteoFile_tm5_hdf), intent(inout) :: mf
  1015. integer, intent(out) :: status
  1016. character(len=*), intent(out), optional :: gridtype
  1017. real, pointer, optional :: ll(:,:,:)
  1018. real, pointer, optional :: spm(:,:)
  1019. ! --- const --------------------------------------
  1020. character(len=*), parameter :: rname = mname//'/mf_GetField'
  1021. ! --- local -------------------------------
  1022. integer :: data_rank
  1023. integer :: data_dims(3)
  1024. ! --- begin -------------------------------
  1025. ! record not selected ? then return
  1026. if ( .not. mf%selected ) then
  1027. write (gol,'("no record selected ...")'); call goErr
  1028. TRACEBACK; status = 1; return
  1029. end if
  1030. ! initial data is not extracted ...
  1031. status = 1
  1032. ! return grid type ?
  1033. if ( present(gridtype) ) then
  1034. call ReadAttribute( mf%sds, 'gridtype', gridtype, status )
  1035. IF_NOTOK_RETURN(status=1)
  1036. end if
  1037. ! return 3d data array ?
  1038. if ( present(ll) ) then
  1039. ! extract data rank and shape:
  1040. call GetInfo( mf%sds, status, data_rank=data_rank, data_dims=data_dims )
  1041. IF_NOTOK_RETURN(status=1)
  1042. ! extract data array:
  1043. select case ( data_rank )
  1044. case ( 2 )
  1045. data_dims(3) = 1
  1046. call pa_SetShape( ll, data_dims )
  1047. call ReadData( mf%sds, ll(:,:,1), status )
  1048. IF_NOTOK_RETURN(status=1)
  1049. case ( 3 )
  1050. call pa_SetShape( ll, data_dims )
  1051. call ReadData( mf%sds, ll, status )
  1052. IF_NOTOK_RETURN(status=1)
  1053. case default
  1054. write (gol,'("unsupported data rank:",i6)') data_rank; call goErr
  1055. TRACEBACK; status=1; return
  1056. end select
  1057. ! ReadData breaks on error ...
  1058. status = 0
  1059. end if
  1060. ! return surface pressure array ?
  1061. if ( present(spm) ) then
  1062. if ( .not. mf%spm_selected ) then
  1063. write (gol,'("no spm record selected ...")'); call goErr
  1064. TRACEBACK; status = 1; return
  1065. end if
  1066. ! extract data rank and shape:
  1067. call GetInfo( mf%spm_sds, status, data_rank=data_rank, data_dims=data_dims )
  1068. IF_NOTOK_RETURN(status=1)
  1069. ! extract data array:
  1070. select case ( data_rank )
  1071. case ( 2 )
  1072. call pa_SetShape( spm, data_dims(1:2) )
  1073. call ReadData( mf%spm_sds, spm, status )
  1074. IF_NOTOK_RETURN(status=1)
  1075. case default
  1076. write (gol,'("unsupported data rank for spm:",i6)') data_rank; call goErr
  1077. TRACEBACK; status=1; return
  1078. end select
  1079. ! ReadData breaks on error ...
  1080. status = 0
  1081. end if
  1082. ! no arguments processed ?
  1083. ! probably subroutine is called incorrectly ...
  1084. if ( status /= 0 ) then
  1085. write (gol,'("no arguments processed; wrong call ?")'); call goErr
  1086. TRACEBACK; status = 1; return
  1087. end if
  1088. ! ok
  1089. status = 0
  1090. end subroutine mf_GetField
  1091. ! ***
  1092. subroutine mf_ReadRecord( mf, paramkey, t1, t2, nuv, nw, &
  1093. gridtype, levi, &
  1094. lli, ll, sp_ll, &
  1095. status )
  1096. use PArray, only : pa_Done
  1097. use GO, only : TDate
  1098. use GO, only : GO_Timer_Start, GO_Timer_End
  1099. use Grid, only : TllGridInfo, TLevelInfo
  1100. use file_hdf_base, only : Init, Done !needed on aster
  1101. ! --- in/out -------------------------------
  1102. type(TMeteoFile_tm5_hdf), intent(inout) :: mf
  1103. character(len=*), intent(in) :: paramkey
  1104. type(TDate), intent(in) :: t1, t2
  1105. character(len=1), intent(in) :: nuv
  1106. character(len=2), intent(out) :: gridtype
  1107. type(TLevelInfo), intent(out) :: levi
  1108. character(len=1), intent(in) :: nw
  1109. type(TllGridInfo), intent(inout) :: lli
  1110. real, pointer :: ll(:,:,:)
  1111. real, pointer :: sp_ll(:,:)
  1112. integer, intent(out) :: status
  1113. ! --- const --------------------------------------
  1114. character(len=*), parameter :: rname = mname//'/mf_ReadRecord'
  1115. ! --- local -------------------------------
  1116. logical :: exist
  1117. real, allocatable :: mfw_n(:,:,:)
  1118. real, allocatable :: sp_ll_n(:,:)
  1119. integer :: i, j
  1120. ! --- begin ---------------------------------
  1121. ! start timing:
  1122. call GO_Timer_Start( itim_readrecord, status )
  1123. IF_NOTOK_RETURN(status=1)
  1124. ! input ?
  1125. if ( mf%io /= 'i' ) then
  1126. write (gol,'("file should have been opened for input, but io=",a)') mf%io; call goErr
  1127. TRACEBACK; status=1; return
  1128. end if
  1129. ! open for reading:
  1130. ! if it is opened in Init, to many hdf files would be open:
  1131. !
  1132. inquire( file=trim(mf%fname), exist=exist )
  1133. if ( .not. exist ) then
  1134. write (gol,'("main file does not exist:")'); call goErr
  1135. write (gol,'(" ",a)') trim(mf%fname); call goErr
  1136. TRACEBACK; status=1; return
  1137. end if
  1138. !
  1139. call Init( mf%hdf, trim(mf%fname), 'read', status )
  1140. IF_NOTOK_RETURN(status=1)
  1141. !
  1142. call Init( mf%sds, status )
  1143. IF_NOTOK_RETURN(status=1)
  1144. !
  1145. mf%selected = .false.
  1146. ! open spm file if necessary;
  1147. ! if it is opened in Init, to many hdf files would be open:
  1148. if ( mf%spm_load ) then
  1149. !
  1150. if ( mf%spm_extr ) then
  1151. !
  1152. inquire( file=trim(mf%spm_fname), exist=exist )
  1153. if ( .not. exist ) then
  1154. write (gol,'("spm file does not exist:")'); call goErr
  1155. write (gol,'(" ",a)') trim(mf%spm_fname); call goErr
  1156. TRACEBACK; status=1; return
  1157. end if
  1158. !
  1159. call Init( mf%spm_hdf, trim(mf%spm_fname), 'read', status )
  1160. IF_NOTOK_RETURN(status=1)
  1161. !
  1162. end if
  1163. !
  1164. call Init( mf%spm_sds, status )
  1165. IF_NOTOK_RETURN(status=1)
  1166. !
  1167. mf%spm_selected = .false.
  1168. end if
  1169. ! select records in hdf files:
  1170. call mf_SelectRecord( mf, paramkey, t1, t2, status )
  1171. if ( status/= 0 ) then; TRACEBACK; status=1; return; end if
  1172. ! always regular lat/lon grid ..
  1173. gridtype = 'll'
  1174. ! setup grid definition:
  1175. call lli_Init_mf( lli, nuv, mf, status )
  1176. if ( status/= 0 ) then; TRACEBACK; status=1; return; end if
  1177. ! setup level definition:
  1178. call levi_Init_mf( levi, mf, status )
  1179. if ( status/= 0 ) then; TRACEBACK; status=1; return; end if
  1180. ! fill data array:
  1181. call mf_GetField( mf, status, ll=ll )
  1182. if ( status/= 0 ) then; TRACEBACK; status=1; return; end if
  1183. ! ***
  1184. ! special treatment of pw fields ?
  1185. if ( (paramkey == 'mfw') .or. (paramkey == 'pw') ) then
  1186. ! add extra top level ?
  1187. if ( size(ll,3) == levi%nlev ) then
  1188. ! copy current pw ('n' levels)
  1189. allocate( mfw_n(lli%im,lli%jm,levi%nlev) )
  1190. mfw_n = ll
  1191. ! renew target array
  1192. deallocate( ll )
  1193. allocate( ll(lli%nlon,lli%nlat,levi%nlev+1) )
  1194. ! store new pw:
  1195. ll(:,:,1:levi%nlev) = mfw_n
  1196. ll(:,:,levi%nlev+1) = 0.0 ! kg/s
  1197. ! clear
  1198. deallocate( mfw_n )
  1199. end if
  1200. ! change flux direction ?
  1201. if ( mf%mfw_redir ) ll = - ll ! upwards (increasing level)
  1202. end if
  1203. ! ***
  1204. ! special treatment of surface stress fields ?
  1205. if ( ((paramkey=='ewss') .or. (paramkey=='nsss')) .and. mf%sstr_to_ewss_nsss ) then
  1206. ! sstr**2 = (sstr**2)/2 + (sstr**2)/2 = nsss**2 + ewss**2
  1207. ! thus nsss = sqrt( (sstr**2)/2.0 )
  1208. ll = sqrt( (ll**2) / 2.0 )
  1209. end if
  1210. ! ***
  1211. ! fill surface pressure field ?
  1212. if ( mf%spm_load .and. mf%spm_selected ) then
  1213. ! read field:
  1214. call mf_GetField( mf, status, spm=sp_ll )
  1215. if ( status/= 0 ) then; TRACEBACK; status=1; return; end if
  1216. ! convert from 'n' to 'u'/'v' ?
  1217. if ( mf%spm_n_to_uv ) then
  1218. select case ( nuv )
  1219. case ( 'n' )
  1220. ! no interpolation
  1221. case ( 'u' )
  1222. ! copy current sp ('n')
  1223. allocate( sp_ll_n(lli%im,lli%jm) )
  1224. sp_ll_n = sp_ll
  1225. ! renew size of output array:
  1226. deallocate( sp_ll )
  1227. allocate( sp_ll(0:lli%im,lli%jm) )
  1228. ! interpol:
  1229. sp_ll(0,:) = 1.5*sp_ll_n(1,:) - 0.5*sp_ll_n(2,:)
  1230. do i = 1, lli%im-1
  1231. sp_ll(i,:) = 1.5*sp_ll_n(i,:) + 0.5*sp_ll_n(i+1,:)
  1232. end do
  1233. sp_ll(lli%im,:) = -0.5*sp_ll_n(lli%im-1,:) + 1.5*sp_ll_n(lli%im,:)
  1234. ! clear
  1235. deallocate( sp_ll_n )
  1236. case ( 'v' )
  1237. ! copy current sp ('n')
  1238. allocate( sp_ll_n(lli%im,lli%jm) )
  1239. sp_ll_n = sp_ll
  1240. ! renew size of output array:
  1241. deallocate( sp_ll )
  1242. allocate( sp_ll(lli%im,0:lli%jm) )
  1243. ! interpol:
  1244. sp_ll(:,0) = 1.5*sp_ll_n(:,1) - 0.5*sp_ll_n(:,2)
  1245. do j = 1, lli%jm-1
  1246. sp_ll(:,j) = 1.5*sp_ll_n(:,j) + 0.5*sp_ll_n(:,j+1)
  1247. end do
  1248. sp_ll(:,lli%jm) = -0.5*sp_ll_n(:,lli%jm-1) + 1.5*sp_ll_n(:,lli%jm)
  1249. ! clear
  1250. deallocate( sp_ll_n )
  1251. case default
  1252. write (gol,'("unsupported nuv `",a,"`")') nuv; call goErr
  1253. TRACEBACK; status=1; return
  1254. end select
  1255. end if
  1256. else
  1257. call pa_Done( sp_ll )
  1258. end if
  1259. ! close surface pressure file ?
  1260. if ( mf%spm_load ) then
  1261. call Done( mf%spm_sds, status )
  1262. IF_NOTOK_RETURN(status=1)
  1263. !
  1264. if ( mf%spm_extr ) then
  1265. call Done( mf%spm_hdf, status )
  1266. IF_NOTOK_RETURN(status=1)
  1267. end if
  1268. end if
  1269. ! close field file:
  1270. call Done( mf%sds, status )
  1271. IF_NOTOK_RETURN(status=1)
  1272. call Done( mf%hdf, status )
  1273. IF_NOTOK_RETURN(status=1)
  1274. ! end timing:
  1275. call GO_Timer_End( itim_readrecord, status )
  1276. IF_NOTOK_RETURN(status=1)
  1277. ! ok
  1278. status = 0
  1279. end subroutine mf_ReadRecord
  1280. ! ******************************************************************
  1281. ! ***
  1282. ! *** output
  1283. ! ***
  1284. ! ******************************************************************
  1285. subroutine WriteHeader( mf, lli, status, levi )
  1286. use Binas, only : grav, ae
  1287. use Grid, only : TllGridInfo, TLevelInfo
  1288. use file_hdf, only : WriteAttribute
  1289. ! --- in/out -------------------------------
  1290. type(TMeteoFile_tm5_hdf), intent(inout) :: mf
  1291. type(TllGridInfo), intent(in) :: lli
  1292. integer, intent(out) :: status
  1293. type(TLevelInfo), intent(in), optional :: levi
  1294. ! --- const --------------------------------------
  1295. character(len=*), parameter :: rname = mname//'/WriteHeader'
  1296. ! --- begin ---------------------------------
  1297. ! write header:
  1298. call WriteAttribute( mf%hdf, 'fname' , trim(mf%fname), status )
  1299. IF_NOTOK_RETURN(status=1)
  1300. call WriteAttribute( mf%hdf, 'format' , output_format, status )
  1301. IF_NOTOK_RETURN(status=1)
  1302. call WriteAttribute( mf%hdf, 'gridtype', 'll' , status )
  1303. IF_NOTOK_RETURN(status=1)
  1304. ! save first and last lon/lat (center) for use with HIPHOP
  1305. call WriteAttribute( mf%hdf, 'lonmin', lli%lon_deg(1) , status, knd=rknd )
  1306. IF_NOTOK_RETURN(status=1)
  1307. call WriteAttribute( mf%hdf, 'lonmax', lli%lon_deg(lli%nlon), status, knd=rknd )
  1308. IF_NOTOK_RETURN(status=1)
  1309. call WriteAttribute( mf%hdf, 'latmin', lli%lat_deg(1) , status, knd=rknd )
  1310. IF_NOTOK_RETURN(status=1)
  1311. call WriteAttribute( mf%hdf, 'latmax', lli%lat_deg(lli%nlat), status, knd=rknd )
  1312. IF_NOTOK_RETURN(status=1)
  1313. ! other useful stuff ...
  1314. call WriteAttribute( mf%hdf, 'grav' , grav , status, knd=rknd )
  1315. IF_NOTOK_RETURN(status=1)
  1316. call WriteAttribute( mf%hdf, 'ae' , ae , status, knd=rknd )
  1317. IF_NOTOK_RETURN(status=1)
  1318. call WriteAttribute( mf%hdf, 'area_m2', lli%area_m2, status, knd=rknd )
  1319. IF_NOTOK_RETURN(status=1)
  1320. ! level stuff
  1321. if ( present(levi) ) then
  1322. call WriteAttribute( mf%hdf, 'lm', levi%nlev, status, knd=iknd )
  1323. IF_NOTOK_RETURN(status=1)
  1324. call WriteAttribute( mf%hdf, 'at', levi%a , status, knd=rknd )
  1325. IF_NOTOK_RETURN(status=1)
  1326. call WriteAttribute( mf%hdf, 'bt', levi%b , status, knd=rknd )
  1327. IF_NOTOK_RETURN(status=1)
  1328. end if
  1329. ! ok
  1330. status = 0
  1331. end subroutine WriteHeader
  1332. ! ***
  1333. subroutine WriteSdsHeader( sds, tmi, unit, tref, t1, t2, lli, nuv, status, &
  1334. levi, nw, nlev )
  1335. use Binas , only : p_global
  1336. use file_hdf, only : TSds
  1337. use file_hdf, only : SetDim, WriteAttribute, Compress
  1338. use GO , only : TDate, Get, rTotal, operator(-), IsAnyDate
  1339. use Grid , only : TllGridInfo, TLevelInfo
  1340. use tmm_info, only : TMeteoInfo
  1341. ! --- in/out -----------------------------
  1342. type(TSds), intent(inout) :: sds
  1343. type(TMeteoInfo), intent(in) :: tmi
  1344. character(len=*), intent(in) :: unit
  1345. type(TDate), intent(in) :: tref, t1, t2
  1346. type(TllGridInfo), intent(in) :: lli
  1347. character(len=1), intent(in) :: nuv
  1348. integer, intent(out) :: status
  1349. type(TLevelInfo), intent(in), optional :: levi
  1350. character(len=1), intent(in), optional :: nw
  1351. integer, intent(in), optional :: nlev
  1352. ! --- const --------------------------------------
  1353. character(len=*), parameter :: rname = mname//'/WriteSdsHeader'
  1354. ! --- local -----------------------------
  1355. integer :: time6(6)
  1356. integer :: dhour
  1357. ! --- begin -----------------------------
  1358. ! *** history
  1359. ! write history of meteo field:
  1360. if ( len_trim(tmi%history) < 1 ) then
  1361. call WriteAttribute( sds, 'history', '-', status )
  1362. IF_NOTOK_RETURN(status=1)
  1363. else
  1364. call WriteAttribute( sds, 'history', trim(tmi%history), status )
  1365. IF_NOTOK_RETURN(status=1)
  1366. end if
  1367. ! *** unit
  1368. ! write unit attribute
  1369. call WriteAttribute( sds, 'unit', unit, status )
  1370. IF_NOTOK_RETURN(status=1)
  1371. ! *** time
  1372. ! time interval in hours
  1373. if ( IsAnyDate(t1) .or. IsAnyDate(t2) ) then
  1374. dhour = 0
  1375. else
  1376. dhour = nint(rTotal( t2 - t1, 'hour' ))
  1377. end if
  1378. ! write old time attributes
  1379. call Get( t1, time6=time6 )
  1380. call WriteAttribute( sds, 'idate', time6, status, knd=iknd )
  1381. IF_NOTOK_RETURN(status=1)
  1382. call WriteAttribute( sds, 'dthrs', dhour, status, knd=iknd ) ! hours
  1383. IF_NOTOK_RETURN(status=1)
  1384. ! write new time attributes
  1385. call Get( tref, time6=time6 )
  1386. call WriteAttribute( sds, 'tref', time6, status, knd=iknd )
  1387. IF_NOTOK_RETURN(status=1)
  1388. call Get( t1, time6=time6 )
  1389. call WriteAttribute( sds, 'time1', time6, status, knd=iknd )
  1390. IF_NOTOK_RETURN(status=1)
  1391. call Get( t2, time6=time6 )
  1392. call WriteAttribute( sds, 'time2', time6, status, knd=iknd )
  1393. IF_NOTOK_RETURN(status=1)
  1394. time6 = 0
  1395. time6(4) = dhour
  1396. call WriteAttribute( sds, 'dtime', time6, status, knd=iknd )
  1397. IF_NOTOK_RETURN(status=1)
  1398. ! *** grid
  1399. select case ( nuv )
  1400. case ( 'n' )
  1401. ! sds dimensions:
  1402. call SetDim( sds, 1-1, 'LON' , 'deg', lli%lon_deg, status, knd=rknd )
  1403. IF_NOTOK_RETURN(status=1)
  1404. call SetDim( sds, 2-1, 'LAT' , 'deg', lli%lat_deg, status, knd=rknd )
  1405. IF_NOTOK_RETURN(status=1)
  1406. ! sds attributes:
  1407. call WriteAttribute( sds, 'gridtype' , 'll' , status )
  1408. IF_NOTOK_RETURN(status=1)
  1409. call WriteAttribute( sds, 'lon_first', lli%lon_deg(1), status, knd=rknd )
  1410. IF_NOTOK_RETURN(status=1)
  1411. call WriteAttribute( sds, 'lon_inc ', lli%dlon_deg , status, knd=rknd )
  1412. IF_NOTOK_RETURN(status=1)
  1413. call WriteAttribute( sds, 'lon_n ', lli%nlon , status, knd=iknd )
  1414. IF_NOTOK_RETURN(status=1)
  1415. call WriteAttribute( sds, 'lat_first', lli%lat_deg(1), status, knd=rknd )
  1416. IF_NOTOK_RETURN(status=1)
  1417. call WriteAttribute( sds, 'lat_inc ', lli%dlat_deg , status, knd=rknd )
  1418. IF_NOTOK_RETURN(status=1)
  1419. call WriteAttribute( sds, 'lat_n ', lli%nlat , status, knd=iknd )
  1420. IF_NOTOK_RETURN(status=1)
  1421. case ( 'u' )
  1422. ! sds dimensions:
  1423. call SetDim( sds, 1-1, 'LONP1' , 'deg', lli%blon_deg, status, knd=rknd )
  1424. IF_NOTOK_RETURN(status=1)
  1425. call SetDim( sds, 2-1, 'LAT' , 'deg', lli%lat_deg , status, knd=rknd )
  1426. IF_NOTOK_RETURN(status=1)
  1427. ! sds attributes:
  1428. call WriteAttribute( sds, 'gridtype' , 'll', status )
  1429. IF_NOTOK_RETURN(status=1)
  1430. call WriteAttribute( sds, 'lon_first', lli%blon_deg(0), status, knd=rknd )
  1431. IF_NOTOK_RETURN(status=1)
  1432. call WriteAttribute( sds, 'lon_inc ', lli%dlon_deg , status, knd=rknd )
  1433. IF_NOTOK_RETURN(status=1)
  1434. call WriteAttribute( sds, 'lon_n ', lli%im+1 , status, knd=iknd )
  1435. IF_NOTOK_RETURN(status=1)
  1436. call WriteAttribute( sds, 'lat_first', lli%lat_deg(1) , status, knd=rknd )
  1437. IF_NOTOK_RETURN(status=1)
  1438. call WriteAttribute( sds, 'lat_inc ', lli%dlat_deg , status, knd=rknd )
  1439. IF_NOTOK_RETURN(status=1)
  1440. call WriteAttribute( sds, 'lat_n ', lli%nlat , status, knd=iknd )
  1441. IF_NOTOK_RETURN(status=1)
  1442. case ( 'v' )
  1443. ! sds dimensions:
  1444. call SetDim( sds, 1-1, 'LON' , 'deg', lli%lon_deg , status, knd=rknd )
  1445. IF_NOTOK_RETURN(status=1)
  1446. call SetDim( sds, 2-1, 'LATP1' , 'deg', lli%blat_deg, status, knd=rknd )
  1447. IF_NOTOK_RETURN(status=1)
  1448. ! sds attributes:
  1449. call WriteAttribute( sds, 'gridtype' , 'll', status )
  1450. call WriteAttribute( sds, 'lon_first', lli%lon_deg(1) , status, knd=rknd )
  1451. IF_NOTOK_RETURN(status=1)
  1452. call WriteAttribute( sds, 'lon_inc ', lli%dlon_deg , status, knd=rknd )
  1453. IF_NOTOK_RETURN(status=1)
  1454. call WriteAttribute( sds, 'lon_n ', lli%nlon , status, knd=iknd )
  1455. IF_NOTOK_RETURN(status=1)
  1456. call WriteAttribute( sds, 'lat_first', lli%blat_deg(0), status, knd=rknd )
  1457. IF_NOTOK_RETURN(status=1)
  1458. call WriteAttribute( sds, 'lat_inc ', lli%dlat_deg , status, knd=rknd )
  1459. IF_NOTOK_RETURN(status=1)
  1460. call WriteAttribute( sds, 'lat_n ', lli%jm+1 , status, knd=iknd )
  1461. IF_NOTOK_RETURN(status=1)
  1462. case default
  1463. write (gol,'("unsupported nuv `",a,"`")') nuv; call goErr
  1464. TRACEBACK; status=1; return
  1465. end select
  1466. ! *** levels
  1467. if ( present(levi) ) then
  1468. if ( .not. present(nw) ) then
  1469. write (gol,'("optional levi requires nw")'); call goErr
  1470. TRACEBACK; status=1; return
  1471. end if
  1472. ! sds dimensions:
  1473. select case ( nw )
  1474. case ( '*' )
  1475. if ( present(nlev) ) then
  1476. call SetDim( sds, 3-1, 'HYBRID_SELECTED', 'Pa' , levi%fp0(1:nlev), status, knd=rknd )
  1477. IF_NOTOK_RETURN(status=1)
  1478. end if
  1479. case ( 'n' )
  1480. call SetDim( sds, 3-1, 'HYBRID', 'Pa' , levi%fp0, status, knd=rknd )
  1481. IF_NOTOK_RETURN(status=1)
  1482. case ( 'w' )
  1483. call SetDim( sds, 3-1, 'HYBRIDh', 'Pa', levi%p0, status, knd=rknd )
  1484. IF_NOTOK_RETURN(status=1)
  1485. case default
  1486. write (gol,'("unsupported nw `",a,"` xxx")') nw; call goErr
  1487. TRACEBACK; status=1; return
  1488. end select
  1489. ! sds attributes:
  1490. call WriteAttribute( sds, 'lm', levi%nlev, status, knd=iknd )
  1491. IF_NOTOK_RETURN(status=1)
  1492. call WriteAttribute( sds, 'at', levi%a , status, knd=rknd )
  1493. IF_NOTOK_RETURN(status=1)
  1494. call WriteAttribute( sds, 'bt', levi%b , status, knd=rknd )
  1495. IF_NOTOK_RETURN(status=1)
  1496. end if
  1497. ! *** data compression
  1498. call Compress( sds, compression, status, deflate_level=deflate_level )
  1499. IF_NOTOK_RETURN(status=1)
  1500. ! *** end
  1501. ! ok
  1502. status = 0
  1503. end subroutine WriteSdsHeader
  1504. ! ***
  1505. subroutine WriteStatus( mf, msg, status )
  1506. ! --- in/out -------------------------------
  1507. type(TMeteoFile_tm5_hdf), intent(inout) :: mf
  1508. character(len=*), intent(in) :: msg
  1509. integer, intent(out) :: status
  1510. ! --- const --------------------------------------
  1511. character(len=*), parameter :: rname = mname//'/WriteStatus'
  1512. ! --- local ------------------------------
  1513. integer :: fu
  1514. logical :: opened
  1515. ! --- begin ---------------------------------
  1516. ! select unused file unit:
  1517. fu = 1234
  1518. do
  1519. inquire( unit=fu, opened=opened )
  1520. if ( .not. opened ) exit
  1521. fu = fu + 1
  1522. end do
  1523. ! open:
  1524. open( fu, file=trim(mf%fname)//'.status', form='formatted', iostat=status )
  1525. if (status/=0) then
  1526. write (gol,'("opening status file:")'); call goErr
  1527. write (gol,'(" file : ",a)') trim(mf%fname); call goErr
  1528. TRACEBACK; status=1; return
  1529. end if
  1530. ! write message:
  1531. write (fu,'(a)',iostat=status) msg
  1532. if (status/=0) then
  1533. write (gol,'("writing status:")'); call goErr
  1534. write (gol,'(" file : ",a)') trim(mf%fname); call goErr
  1535. write (gol,'(" msg : ",a)') msg; call goErr
  1536. TRACEBACK; status=1; return
  1537. end if
  1538. ! done:
  1539. close( fu, iostat=status )
  1540. if (status/=0) then
  1541. write (gol,'("closing status file:")'); call goErr
  1542. write (gol,'(" file : ",a)') trim(mf%fname); call goErr
  1543. TRACEBACK; status=1; return
  1544. end if
  1545. ! ok
  1546. status = 0
  1547. end subroutine WriteStatus
  1548. ! ***
  1549. subroutine AddOutputName( mf, name, status )
  1550. ! --- in/out -------------------------------
  1551. type(TMeteoFile_tm5_hdf), intent(inout) :: mf
  1552. character(len=*), intent(in) :: name
  1553. integer, intent(out) :: status
  1554. ! --- const --------------------------------------
  1555. character(len=*), parameter :: rname = mname//'/AddOuputName'
  1556. ! --- local ------------------------------
  1557. integer :: iname
  1558. ! --- begin ---------------------------------
  1559. ! if not present yet, add name
  1560. iname = 1
  1561. do
  1562. ! add ?
  1563. if ( iname > mf%output_nname ) then
  1564. ! place to store ?
  1565. if ( iname > size(mf%output_names) ) then
  1566. write (gol,'("length of mf%output_names array too small:")'); call goErr
  1567. do iname = 1, size(mf%output_names)
  1568. write (gol,'(" ",i3," ",a)') iname, trim(mf%output_names(iname)); call goErr
  1569. end do
  1570. TRACEBACK; status=1; return
  1571. end if
  1572. ! long enough for name ?
  1573. if ( len(mf%output_names(iname)) < len(name) ) then
  1574. write (gol,'("length of mf%output_names too small:")'); call goErr
  1575. write (gol,'(" len(mf%output_names(i)) : ",i4)') len(mf%output_names(iname)); call goErr
  1576. write (gol,'(" len(mname) : ",i4)') len(name); call goErr
  1577. TRACEBACK; status=1; return
  1578. end if
  1579. ! store:
  1580. mf%output_names(iname) = name
  1581. ! increase counter:
  1582. mf%output_nname = iname
  1583. ! leave:
  1584. exit
  1585. end if
  1586. ! found ? then leave loop
  1587. if ( mf%output_names(iname) == name ) exit
  1588. ! next index
  1589. iname = iname + 1
  1590. end do
  1591. ! ok
  1592. status = 0
  1593. end subroutine AddOutputName
  1594. ! ***
  1595. subroutine mf_WriteRecord_2d( mf, tmi, paramkey, unit, tref, t1, t2, &
  1596. lli, nuv, ll, status )
  1597. use GO , only : TDate
  1598. use Grid , only : TllGridInfo
  1599. use file_hdf, only : TSds, WriteData
  1600. use file_hdf_base, only : Init, Done ! needed on aster
  1601. use tmm_info, only : TMeteoInfo
  1602. ! --- in/out -------------------------------
  1603. type(TMeteoFile_tm5_hdf), intent(inout) :: mf
  1604. type(TMeteoInfo), intent(in) :: tmi
  1605. character(len=*), intent(in) :: paramkey, unit
  1606. type(TDate), intent(in) :: tref, t1, t2
  1607. type(TllGridInfo), intent(in) :: lli
  1608. character(len=1), intent(in) :: nuv
  1609. real, intent(in) :: ll(:,:)
  1610. integer, intent(out) :: status
  1611. ! --- const --------------------------------------
  1612. character(len=*), parameter :: rname = mname//'/mf_WriteRecord_2d'
  1613. ! --- local ------------------------------
  1614. type(TSds) :: sds
  1615. ! --- begin ---------------------------------
  1616. ! output ?
  1617. if ( mf%io /= 'o' ) then
  1618. write (gol,'("file should have been opened for output, but io=",a)') mf%io; call goErr
  1619. TRACEBACK; status=1; return
  1620. end if
  1621. ! new or existing ?
  1622. if ( .not. mf%output_initialised ) then
  1623. ! open new file, destroy old:
  1624. call Init( mf%hdf, trim(mf%fname), 'create', status )
  1625. IF_NOTOK_RETURN(status=1)
  1626. ! write file header:
  1627. call WriteHeader( mf, lli, status )
  1628. IF_NOTOK_RETURN(status=1)
  1629. ! status new
  1630. call WriteStatus( mf, 'in-progress', status )
  1631. IF_NOTOK_RETURN(status=1)
  1632. ! no records written yet:
  1633. mf%output_nrec = 0
  1634. ! now the file is initialised
  1635. mf%output_initialised = .true.
  1636. else
  1637. ! re-open file:
  1638. call Init( mf%hdf, trim(mf%fname), 'write', status )
  1639. IF_NOTOK_RETURN(status=1)
  1640. endif
  1641. ! *** data set
  1642. ! add record name:
  1643. call AddOutputName( mf, paramkey, status )
  1644. IF_NOTOK_RETURN(status=1)
  1645. ! init data set:
  1646. call Init( sds, mf%hdf, paramkey, shape(ll), 'real', status, knd=rknd_ds )
  1647. IF_NOTOK_RETURN(status=1)
  1648. ! write unit, time, and grid info:
  1649. call WriteSdsHeader( sds, tmi, unit, tref, t1, t2, lli, nuv, status )
  1650. IF_NOTOK_RETURN(status=1)
  1651. ! write grid
  1652. call WriteData( sds, ll, status )
  1653. IF_NOTOK_RETURN(status=1)
  1654. ! done:
  1655. call Done( sds, status )
  1656. IF_NOTOK_RETURN(status=1)
  1657. ! *** completed ?
  1658. ! next record has been written:
  1659. mf%output_nrec = mf%output_nrec + 1
  1660. ! completed ? then re-write status file:
  1661. if ( mf%output_nrec == mf%output_ntrec*mf%output_nname ) then
  1662. call WriteStatus( mf, 'completed', status )
  1663. IF_NOTOK_RETURN(status=1)
  1664. end if
  1665. ! *** close
  1666. call Done( mf%hdf, status )
  1667. IF_NOTOK_RETURN(status=1)
  1668. ! ok
  1669. status = 0
  1670. end subroutine mf_WriteRecord_2d
  1671. ! ***
  1672. subroutine mf_WriteRecord_3d( mf, tmi, spname, paramkey, unit, tref, t1, t2, &
  1673. lli, nuv, levi, nw, ps, ll, status, &
  1674. nlev )
  1675. use GO , only : TDate
  1676. use Grid , only : TllGridInfo, TLevelInfo
  1677. use file_hdf, only : TSds, WriteData
  1678. use file_hdf_base, only : Init, Done !needed on aster
  1679. use tmm_info, only : TMeteoInfo
  1680. ! --- in/out -------------------------------
  1681. type(TMeteoFile_tm5_hdf), intent(inout) :: mf
  1682. type(TMeteoInfo), intent(in) :: tmi
  1683. character(len=*), intent(in) :: spname, paramkey, unit
  1684. type(TDate), intent(in) :: tref, t1, t2
  1685. type(TllGridInfo), intent(in) :: lli
  1686. character(len=1), intent(in) :: nuv
  1687. type(TLevelInfo), intent(in) :: levi
  1688. character(len=1), intent(in) :: nw
  1689. real, intent(in) :: ps(:,:)
  1690. real, intent(in) :: ll(:,:,:)
  1691. integer, intent(out) :: status
  1692. integer, intent(in), optional :: nlev
  1693. ! --- const --------------------------------------
  1694. character(len=*), parameter :: rname = mname//'/mf_WriteRecord_3d'
  1695. ! --- local ------------------------------
  1696. type(TSds) :: sds
  1697. integer :: iname
  1698. ! --- begin ---------------------------------
  1699. ! output ?
  1700. if ( mf%io /= 'o' ) then
  1701. write (gol,'("file should have been opened for output, but io=",a)') mf%io; call goErr
  1702. TRACEBACK; status=1; return
  1703. end if
  1704. ! new or existing ?
  1705. if ( .not. mf%output_initialised ) then
  1706. ! open new file, destroy old:
  1707. call Init( mf%hdf, trim(mf%fname), 'create', status )
  1708. IF_NOTOK_RETURN(status=1)
  1709. ! write file header:
  1710. call WriteHeader( mf, lli, status, levi )
  1711. IF_NOTOK_RETURN(status=1)
  1712. ! status new
  1713. call WriteStatus( mf, 'in-progress', status )
  1714. IF_NOTOK_RETURN(status=1)
  1715. ! no records written yet:
  1716. mf%output_nrec = 0
  1717. ! now the file is initialised
  1718. mf%output_initialised = .true.
  1719. else
  1720. ! re-open file:
  1721. call Init( mf%hdf, trim(mf%fname), 'write', status )
  1722. IF_NOTOK_RETURN(status=1)
  1723. endif
  1724. ! *** data set
  1725. ! add record name:
  1726. call AddOutputName( mf, paramkey, status )
  1727. IF_NOTOK_RETURN(status=1)
  1728. ! init data set:
  1729. call Init( sds, mf%hdf, paramkey, shape(ll), 'real', status, knd=rknd_ds )
  1730. IF_NOTOK_RETURN(status=1)
  1731. ! write unit, time, and grid info:
  1732. call WriteSdsHeader( sds, tmi, unit, tref, t1, t2, lli, nuv, status, levi, nw )
  1733. IF_NOTOK_RETURN(status=1)
  1734. ! write grid
  1735. call WriteData( sds, ll, status )
  1736. IF_NOTOK_RETURN(status=1)
  1737. ! done:
  1738. call Done( sds, status )
  1739. IF_NOTOK_RETURN(status=1)
  1740. ! *** surface pressure
  1741. ! init data set:
  1742. call Init( sds, mf%hdf, spname, shape(ps), 'real', status, knd=rknd_ds )
  1743. IF_NOTOK_RETURN(status=1)
  1744. ! write unit, time, and grid info:
  1745. call WriteSdsHeader( sds, tmi, 'Pa', tref, t1, t2, lli, nuv, status )
  1746. IF_NOTOK_RETURN(status=1)
  1747. ! write grid
  1748. call WriteData( sds, ps, status )
  1749. IF_NOTOK_RETURN(status=1)
  1750. ! done:
  1751. call Done( sds, status )
  1752. IF_NOTOK_RETURN(status=1)
  1753. ! *** completed ?
  1754. ! next record has been written:
  1755. mf%output_nrec = mf%output_nrec + 1
  1756. ! completed ? then re-write status file:
  1757. if ( mf%output_nrec == mf%output_ntrec*mf%output_nname ) then
  1758. call WriteStatus( mf, 'completed', status )
  1759. IF_NOTOK_RETURN(status=1)
  1760. end if
  1761. ! *** close
  1762. call Done( mf%hdf, status )
  1763. IF_NOTOK_RETURN(status=1)
  1764. ! ok
  1765. status = 0
  1766. end subroutine mf_WriteRecord_3d
  1767. end module tmm_mf_tm5_hdf