tmm_mf_tmpp.F90 69 KB

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