tmm_mf_ecmwf_mars.F90 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887
  1. !###############################################################################
  2. !
  3. ! Input/output of meteofiles : grib version.
  4. !
  5. !### macro's ###################################################################
  6. !
  7. #define TRACEBACK write (gol,'("in ",a," (",a,", line",i5,")")') rname, __FILE__, __LINE__; call goErr
  8. #define IF_NOTOK_RETURN(action) if (status/=0) then; TRACEBACK; action; return; end if
  9. #define IF_ERROR_RETURN(action) if (status> 0) then; TRACEBACK; action; return; end if
  10. !
  11. #include "tmm.inc"
  12. !
  13. !###############################################################################
  14. module tmm_mf_ecmwf_mars
  15. use GO , only : gol, goErr, goPr
  16. use GO , only : TDate
  17. implicit none
  18. ! --- in/out ----------------------------
  19. private
  20. public :: TMeteoFile_ecmwf_mars
  21. public :: Init, Done
  22. public :: Get
  23. public :: ReadRecord
  24. ! --- const ------------------------------
  25. character(len=*), parameter :: mname = 'tmm_mf_ecmwf_mars'
  26. !--- type ---------------------------------
  27. type TMeteoFile_ecmwf_mars
  28. ! file name:
  29. character(len=256) :: dir
  30. character(len=256) :: fname
  31. ! time range covered by file:
  32. type(TDate) :: trange(2)
  33. ! other time keys for this file:
  34. character(len=16) :: treskey
  35. ! current time range covered by grib record:
  36. type(TDate) :: tref, t1, t2
  37. !
  38. ! file description
  39. !
  40. character(len=16) :: ec_class, ec_type, ec_levs
  41. integer :: ec_sh, ec_gg
  42. character(len=256) :: paramkeys
  43. !
  44. ! adhoc flag:
  45. logical :: slhf_for_convec
  46. end type TMeteoFile_ecmwf_mars
  47. ! --- interfaces -------------------------
  48. interface Init
  49. module procedure mf_Init
  50. end interface
  51. interface Done
  52. module procedure mf_Done
  53. end interface
  54. interface Get
  55. module procedure mf_Get
  56. end interface
  57. interface ReadRecord
  58. module procedure mf_ReadRecord
  59. end interface
  60. contains
  61. ! ==============================================================
  62. subroutine mf_Init( mf, dir, archivekeys, paramkey, &
  63. tday, t1, t2, status )
  64. use GO, only : TDate
  65. use GO, only : goVarValue
  66. ! --- in/out ----------------------------
  67. type(TMeteoFile_ecmwf_mars), intent(out) :: mf
  68. character(len=*), intent(in) :: dir
  69. character(len=*), intent(in) :: archivekeys
  70. character(len=*), intent(in) :: paramkey
  71. type(TDate), intent(in) :: tday, t1, t2
  72. integer, intent(out) :: status
  73. ! --- const --------------------------------------
  74. character(len=*), parameter :: rname = mname//'/mf_Init'
  75. ! --- local --------------------------------
  76. ! --- begin --------------------------------
  77. ! store
  78. mf%dir = dir
  79. !
  80. ! extract fields from archivekey :
  81. ! form=tmpp;class=od;type=fg;levs=ml60;sh=159;gg=80;tres=_fg006up4tr3
  82. !
  83. mf%ec_class = 'od'
  84. call goVarValue( archivekeys, ';', 'class', '=', mf%ec_class, status )
  85. IF_ERROR_RETURN(status=1)
  86. !
  87. mf%ec_type = 'fc'
  88. call goVarValue( archivekeys, ';', 'type', '=', mf%ec_type, status )
  89. IF_ERROR_RETURN(status=1)
  90. !
  91. mf%ec_levs = 'ml60'
  92. call goVarValue( archivekeys, ';', 'levs', '=', mf%ec_levs, status )
  93. IF_ERROR_RETURN(status=1)
  94. !
  95. mf%ec_sh = 159
  96. call goVarValue( archivekeys, ';', 'sh', '=', mf%ec_sh, status )
  97. IF_ERROR_RETURN(status=1)
  98. !
  99. mf%ec_gg = 80
  100. call goVarValue( archivekeys, ';', 'gg', '=', mf%ec_gg, status )
  101. IF_ERROR_RETURN(status=1)
  102. !
  103. mf%treskey = '_fc012up2tr3'
  104. call goVarValue( archivekeys, ';', 'tres', '=', mf%treskey, status )
  105. IF_ERROR_RETURN(status=1)
  106. ! adhoc settings immplemented for surface heat fluxes for convection; enable ?
  107. mf%slhf_for_convec = .false.
  108. call goVarValue( archivekeys, ';', 'slhf_for_convec', '=', mf%slhf_for_convec, status )
  109. IF_ERROR_RETURN(status=1)
  110. ! specials
  111. select case ( paramkey )
  112. case ( 'oro', 'lsm' )
  113. ! overwrite timeresolutionkey, used later on to set trange
  114. mf%treskey = 'const'
  115. ! tmm_convec tries to read oro using the default sourcekey,
  116. ! which probably contains type=fc ; force to use an for oro ...
  117. mf%ec_type = 'an'
  118. end select
  119. ! single parameter in a file:
  120. mf%paramkeys = '-'//trim(paramkey)//'-'
  121. ! extract time range:
  122. call GetGribTime( mf, mf%treskey, tday, t1, t2, status, trange=mf%trange )
  123. IF_NOTOK_RETURN(status=1)
  124. ! dummy filename, might be used in error diagnose
  125. write (mf%fname,'("ecmwf mars grib file for param ",a)') trim(paramkey)
  126. ! ok
  127. status = 0
  128. end subroutine mf_Init
  129. ! ***
  130. subroutine mf_Done( mf, status )
  131. ! --- in/out ------------------------------------
  132. type(TMeteoFile_ecmwf_mars), intent(inout) :: mf
  133. integer, intent(out) :: status
  134. ! --- const --------------------------------------
  135. character(len=*), parameter :: rname = mname//'/mf_Done'
  136. ! --- begin -------------------------------------
  137. ! nothing to be done ...
  138. ! ok
  139. status = 0
  140. end subroutine mf_Done
  141. ! ***
  142. subroutine mf_Get( mf, status, trange1, trange2, paramkeys )
  143. use GO, only : TDate
  144. ! --- in/out ----------------------------
  145. type(TMeteoFile_ecmwf_mars), intent(in) :: mf
  146. integer, intent(out) :: status
  147. type(TDate), intent(out), optional :: trange1, trange2
  148. character(len=*), intent(out), optional :: paramkeys
  149. ! --- const --------------------------------------
  150. character(len=*), parameter :: rname = mname//'/mf_Get'
  151. ! --- local --------------------------------
  152. ! --- begin --------------------------------
  153. ! time range:
  154. if ( present(trange1) ) trange1 = mf%trange(1)
  155. if ( present(trange2) ) trange2 = mf%trange(2)
  156. ! contents:
  157. if ( present(paramkeys) ) paramkeys = trim(mf%paramkeys)
  158. ! ok
  159. status = 0
  160. end subroutine mf_Get
  161. ! ***
  162. subroutine mf_ReadRecord( mf, paramkey, tday, t1, t2, nuv, nw, &
  163. gridtype, levi, &
  164. lli, ll, sp_ll, &
  165. ggi, gg, sp_gg, &
  166. shi, sh, lnsp_sh, &
  167. tmi, status )
  168. use parray , only : pa_Init, pa_Done
  169. use GO , only : TDate
  170. use Grid , only : TLevelInfo
  171. use Grid , only : TllGridInfo, TggGridInfo, TshGridInfo
  172. use tmm_info , only : TMeteoInfo
  173. ! --- in/out -------------------------------
  174. type(TMeteoFile_ecmwf_mars), intent(inout) :: mf
  175. character(len=*), intent(in) :: paramkey
  176. type(TDate), intent(in) :: tday, t1, t2
  177. character(len=1), intent(in) :: nuv
  178. character(len=1), intent(in) :: nw
  179. character(len=2), intent(out) :: gridtype
  180. type(TLevelInfo), intent(out) :: levi
  181. type(TllGridInfo), intent(inout) :: lli
  182. real, pointer :: ll(:,:,:)
  183. real, pointer :: sp_ll(:,:)
  184. type(TggGridInfo), intent(inout) :: ggi
  185. real, pointer :: gg(:,:)
  186. real, pointer :: sp_gg(:)
  187. type(TshGridInfo), intent(inout) :: shi
  188. complex, pointer :: sh(:,:)
  189. complex, pointer :: lnsp_sh(:)
  190. type(TMeteoInfo), intent(out) :: tmi
  191. integer, intent(out) :: status
  192. ! --- const --------------------------------------
  193. character(len=*), parameter :: rname = mname//'/mf_ReadRecord'
  194. ! --- local -------------------------------
  195. real, pointer :: ll2(:,:,:)
  196. real, pointer :: gg2(:,:)
  197. complex, pointer :: sh2(:,:)
  198. integer :: iveg
  199. logical :: unit_change
  200. real :: unit_fac
  201. ! --- begin ---------------------------------
  202. ! combined field ?
  203. select case ( paramkey )
  204. ! *** surface stress
  205. case ( 'sstr' )
  206. ! read first field:
  207. call mf_ReadRecord_1( mf, 'ewss', tday, t1, t2, nuv, nw, &
  208. gridtype, levi, &
  209. lli, ll, sp_ll, &
  210. ggi, gg, sp_gg, &
  211. shi, sh, lnsp_sh, &
  212. tmi, status )
  213. IF_NOTOK_RETURN(status=1)
  214. ! init pointer:
  215. call pa_Init( ll2 )
  216. call pa_Init( gg2 )
  217. call pa_Init( sh2 )
  218. ! read second field:
  219. call mf_ReadRecord_1( mf, 'nsss', tday, t1, t2, nuv, nw, &
  220. gridtype, levi, &
  221. lli, ll2, sp_ll, &
  222. ggi, gg2, sp_gg, &
  223. shi, sh2, lnsp_sh, &
  224. tmi, status )
  225. IF_NOTOK_RETURN(status=1)
  226. ! process:
  227. select case ( gridtype )
  228. case ( 'll' ) ; ll = sqrt( ll**2 + ll2**2 )
  229. case ( 'gg' ) ; gg = sqrt( gg**2 + gg2**2 )
  230. case ( 'sh' ) ; sh = sqrt( sh**2 + sh2**2 )
  231. case default
  232. write (gol,'("unsupported gridtype for surface stress :",a)') gridtype; call goErr
  233. TRACEBACK; status=1; return
  234. end select
  235. ! clear pointers:
  236. call pa_Done( ll2 )
  237. call pa_Done( gg2 )
  238. call pa_Done( sh2 )
  239. ! *** vegetation types
  240. case ( 'tv01', 'tv02', 'tv03', 'tv04', 'tv05', 'tv06', 'tv07', 'tv08', 'tv09', 'tv10', &
  241. 'tv11', 'tv12', 'tv13', 'tv14', 'tv15', 'tv16', 'tv17', 'tv18', 'tv19', 'tv20' )
  242. ! extract number from name
  243. read (paramkey(3:4),'(i2)') iveg
  244. ! low vegetation types:
  245. call mf_ReadRecord_1( mf, 'tvl', tday, t1, t2, nuv, nw, &
  246. gridtype, levi, &
  247. lli, ll, sp_ll, &
  248. ggi, gg, sp_gg, &
  249. shi, sh, lnsp_sh, &
  250. tmi, status )
  251. IF_NOTOK_RETURN(status=1)
  252. ! set elements that match requested vegetation type to 100%, zero elsewhere
  253. select case ( gridtype )
  254. case ( 'gg' )
  255. where ( nint(gg(:,1)) == iveg )
  256. gg(:,1) = 100.0 ! %
  257. elsewhere
  258. gg(:,1) = 0.0
  259. end where
  260. case default
  261. write (gol,'("unsupported gridtype for vegetation fractions :",a)') gridtype; call goErr
  262. TRACEBACK; status=1; return
  263. end select
  264. ! init pointer:
  265. call pa_Init( ll2 )
  266. call pa_Init( gg2 )
  267. call pa_Init( sh2 )
  268. ! high vegetation types:
  269. call mf_ReadRecord_1( mf, 'tvh', tday, t1, t2, nuv, nw, &
  270. gridtype, levi, &
  271. lli, ll2, sp_ll, &
  272. ggi, gg2, sp_gg, &
  273. shi, sh2, lnsp_sh, &
  274. tmi, status )
  275. IF_NOTOK_RETURN(status=1)
  276. ! set elements that match requested vegetation type to 100%:
  277. select case ( gridtype )
  278. case ( 'gg' )
  279. where ( nint(gg2(:,1)) == iveg )
  280. gg(:,1) = 100.0 ! %
  281. end where
  282. case default
  283. write (gol,'("unsupported gridtype for vegetation fractions :",a)') gridtype; call goErr
  284. TRACEBACK; status=1; return
  285. end select
  286. ! clear pointers:
  287. call pa_Done( ll2 )
  288. call pa_Done( gg2 )
  289. call pa_Done( sh2 )
  290. ! *** default
  291. case default
  292. call mf_ReadRecord_1( mf, paramkey, tday, t1, t2, nuv, nw, &
  293. gridtype, levi, &
  294. lli, ll, sp_ll, &
  295. ggi, gg, sp_gg, &
  296. shi, sh, lnsp_sh, &
  297. tmi, status )
  298. IF_NOTOK_RETURN(status=1)
  299. end select
  300. ! unit change ?
  301. unit_change = .true.
  302. select case ( paramkey )
  303. case ( 'lsm' ) ; unit_fac = 100.0 ! 0-1 -> 0-100%
  304. case default ; unit_change = .false.
  305. end select
  306. ! apply ?
  307. if ( unit_change ) then
  308. select case ( gridtype )
  309. case ( 'll' ) ; ll = ll * unit_fac
  310. case ( 'gg' ) ; gg = gg * unit_fac
  311. !case ( 'sh' ) ; sh = sh * unit_fac
  312. case default
  313. write (gol,'("unsupported gridtype for unit change :",a)') gridtype; call goErr
  314. TRACEBACK; status=1; return
  315. end select
  316. end if
  317. ! ok
  318. status = 0
  319. end subroutine mf_ReadRecord
  320. ! ***
  321. subroutine mf_ReadRecord_1( mf, paramkey, tday, t1, t2, nuv, nw, &
  322. gridtype, levi, &
  323. lli, ll, sp_ll, &
  324. ggi, gg, sp_gg, &
  325. shi, sh, lnsp_sh, &
  326. tmi, status )
  327. use parray , only : pa_Init, pa_Done
  328. use GO , only : TDate, TIncrDate, operator(<), operator(-), rTotal, wrtgol
  329. use Grid , only : TLevelInfo
  330. use Grid , only : TllGridInfo, TggGridInfo, TshGridInfo
  331. use tmm_info , only : TMeteoInfo
  332. ! --- in/out -------------------------------
  333. type(TMeteoFile_ecmwf_mars), intent(inout) :: mf
  334. character(len=*), intent(in) :: paramkey
  335. type(TDate), intent(in) :: tday, t1, t2
  336. character(len=1), intent(in) :: nuv
  337. character(len=1), intent(in) :: nw
  338. character(len=2), intent(out) :: gridtype
  339. type(TLevelInfo), intent(out) :: levi
  340. type(TllGridInfo), intent(inout) :: lli
  341. real, pointer :: ll(:,:,:)
  342. real, pointer :: sp_ll(:,:)
  343. type(TggGridInfo), intent(inout) :: ggi
  344. real, pointer :: gg(:,:)
  345. real, pointer :: sp_gg(:)
  346. type(TshGridInfo), intent(inout) :: shi
  347. complex, pointer :: sh(:,:)
  348. complex, pointer :: lnsp_sh(:)
  349. type(TMeteoInfo), intent(out) :: tmi
  350. integer, intent(out) :: status
  351. ! --- const --------------------------------------
  352. character(len=*), parameter :: rname = mname//'/mf_ReadRecord_1'
  353. ! --- local -------------------------------
  354. type(TDate) :: tref
  355. type(TIncrDate) :: tshift
  356. type(TDate) :: trefs, t1s, t2s
  357. real, pointer :: ll1(:,:,:)
  358. real, pointer :: gg1(:,:)
  359. complex, pointer :: sh1(:,:)
  360. real :: dt_sec
  361. ! --- begin ---------------------------------
  362. !write (gol,'("mf_ReadRecord_1: paramkey : ",a)') trim(paramkey); call goPr
  363. !call wrtgol( 'mf_ReadRecord_1: tday : ', tday ); call goPr
  364. !call wrtgol( 'mf_ReadRecord_1: t1 : ', t1 ); call goPr
  365. !call wrtgol( 'mf_ReadRecord_1: t2 : ', t2 ); call goPr
  366. ! accumulated field ?
  367. select case ( paramkey )
  368. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  369. ! accumulated fields
  370. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  371. case ( 'lsp', 'cp', 'sf', 'sshf', 'slhf', &
  372. 'ssr', 'ssrd', 'str', 'strd', &
  373. 'ewss', 'nsss', &
  374. 'UDMF', 'UDDR', 'DDMF', 'DDDR' )
  375. ! get reference time for requested time interval:
  376. call GetGribTime( mf, mf%treskey, tday, t1, t2, status, tref=tref )
  377. IF_NOTOK_RETURN(status=1)
  378. ! should be a time interval ...
  379. if ( .not. (t1 < t2) ) then
  380. write (gol,'("accumulated fields requires time interval:")'); call goErr
  381. write (gol,'(" paramkey : ",a)') paramkey; call goErr
  382. call wrtgol( ' t1 : ', t1 ); call goErr
  383. call wrtgol( ' t2 : ', t2 ); call goErr
  384. TRACEBACK; status=1; return
  385. end if
  386. ! read field accumulated over [tref,t2] :
  387. call wrtgol( ' accum ', tref, ' - ', t2 ); call goPr
  388. call mf_ReadRecord_2( mf, paramkey, tref, t2, t2, nuv, nw, &
  389. gridtype, levi, &
  390. lli, ll, sp_ll, &
  391. ggi, gg, sp_gg, &
  392. shi, sh, lnsp_sh, &
  393. tmi, status )
  394. IF_NOTOK_RETURN(status=1)
  395. ! substract [tref,t1] if necessary:
  396. if ( tref < t1 ) then
  397. ! init pointer:
  398. call pa_Init( ll1 )
  399. call pa_Init( gg1 )
  400. call pa_Init( sh1 )
  401. ! read field accumulated over [tref,t1] :
  402. call wrtgol( ' accum ', tref, ' - ', t1 ); call goPr
  403. call mf_ReadRecord_2( mf, paramkey, tref, t1, t1, nuv, nw, &
  404. gridtype, levi, &
  405. lli, ll1, sp_ll, &
  406. ggi, gg1, sp_gg, &
  407. shi, sh1, lnsp_sh, &
  408. tmi, status )
  409. IF_NOTOK_RETURN(status=1)
  410. ! substract:
  411. select case ( gridtype )
  412. case ( 'll' ) ; ll = ll - ll1
  413. case ( 'gg' ) ; gg = gg - gg1
  414. case ( 'sh' ) ; sh = sh - sh1
  415. case default
  416. write (gol,'("unsupported gridtype for substract :",a)') gridtype; call goErr
  417. TRACEBACK; status=1; return
  418. end select
  419. ! clear pointers:
  420. call pa_Done( ll1 )
  421. call pa_Done( gg1 )
  422. call pa_Done( sh1 )
  423. end if
  424. ! return time averages only:
  425. dt_sec = rTotal( t2 - t1, 'sec' )
  426. select case ( gridtype )
  427. case ( 'll' ) ; ll = ll / dt_sec
  428. case ( 'gg' ) ; gg = gg / dt_sec
  429. case ( 'sh' ) ; sh = sh / dt_sec
  430. case default
  431. write (gol,'("unsupported gridtype for time average :",a)') gridtype; call goErr
  432. TRACEBACK; status=1; return
  433. end select
  434. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  435. ! emission fields
  436. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  437. case ( 'ch4fire' )
  438. ! get reference time for requested time interval:
  439. call GetGribTime( mf, mf%treskey, tday, t1, t2, status, tref=tref )
  440. IF_NOTOK_RETURN(status=1)
  441. ! times in gribfile: 00:30+[0,0], ...
  442. trefs = tref
  443. t1s = tref
  444. t2s = tref
  445. ! just read ..
  446. call mf_ReadRecord_2( mf, paramkey, trefs, t1s, t2s, nuv, nw, &
  447. gridtype, levi, &
  448. lli, ll, sp_ll, &
  449. ggi, gg, sp_gg, &
  450. shi, sh, lnsp_sh, &
  451. tmi, status )
  452. IF_NOTOK_RETURN(status=1)
  453. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  454. ! instantaneous fields
  455. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  456. case default
  457. ! get reference time for requested time interval;
  458. ! eventually shift for analysed fields in case of forecasts:
  459. call GetGribTime( mf, mf%treskey, tday, t1, t2, status, tref=tref, tshift=tshift )
  460. IF_NOTOK_RETURN(status=1)
  461. ! shift times (might be zero):
  462. trefs = tref - tshift
  463. t1s = t1 - tshift
  464. t2s = t2 - tshift
  465. ! just read ..
  466. call mf_ReadRecord_2( mf, paramkey, trefs, t1s, t2s, nuv, nw, &
  467. gridtype, levi, &
  468. lli, ll, sp_ll, &
  469. ggi, gg, sp_gg, &
  470. shi, sh, lnsp_sh, &
  471. tmi, status )
  472. IF_NOTOK_RETURN(status=1)
  473. end select
  474. ! ok
  475. status = 0
  476. end subroutine mf_ReadRecord_1
  477. ! ***
  478. subroutine mf_ReadRecord_2( mf, paramkey, tref, t1, t2, nuv, nw, &
  479. gridtype, levi, &
  480. lli, ll, sp_ll, &
  481. ggi, gg, sp_gg, &
  482. shi, sh, lnsp_sh, &
  483. tmi, status )
  484. use GO , only : TDate, wrtgol, Get, NewDate, operator(>)
  485. use GO , only : goWriteKeyNum
  486. use Grid , only : Init, Done
  487. use Grid , only : TLevelInfo
  488. use Grid , only : TllGridInfo, TggGridInfo, TshGridInfo
  489. use Grid , only : Interpol
  490. use file_grib , only : TGribFile
  491. use file_grib , only : Init, Done, ReadRecord, Get, Check
  492. use file_grib , only : levtype_sfc, levtype_hyb, levtype_land
  493. use file_grib , only : gridtype_ll, gridtype_gg, gridtype_sh
  494. use grib_table, only : GetPid
  495. use PArray , only : pa_Init, pa_Done, pa_SetShape
  496. use tmm_info , only : TMeteoInfo, Init, AddHistory
  497. ! --- in/out -------------------------------
  498. type(TMeteoFile_ecmwf_mars), intent(inout) :: mf
  499. character(len=*), intent(in) :: paramkey
  500. type(TDate), intent(in) :: tref, t1, t2
  501. character(len=1), intent(in) :: nuv
  502. character(len=1), intent(in) :: nw
  503. character(len=2), intent(out) :: gridtype
  504. type(TLevelInfo), intent(out) :: levi
  505. type(TllGridInfo), intent(inout) :: lli
  506. real, pointer :: ll(:,:,:)
  507. real, pointer :: sp_ll(:,:)
  508. type(TggGridInfo), intent(inout) :: ggi
  509. real, pointer :: gg(:,:)
  510. real, pointer :: sp_gg(:)
  511. type(TshGridInfo), intent(inout) :: shi
  512. complex, pointer :: sh(:,:)
  513. complex, pointer :: lnsp_sh(:)
  514. type(TMeteoInfo), intent(out) :: tmi
  515. integer, intent(out) :: status
  516. ! --- const --------------------------------------
  517. character(len=*), parameter :: rname = mname//'/mf_ReadRecord_2'
  518. ! --- local -------------------------------
  519. character(len=16) :: ec_class, ec_type
  520. character(len=16) :: ec_grid, gridN, gridT
  521. character(len=16) :: levs
  522. character(len=16) :: treskey
  523. logical :: constant
  524. type(TGribFile) :: grib
  525. logical :: do_spm
  526. character(len=256) :: spm_fname
  527. type(TGribFile) :: spm_grib
  528. logical :: spm_lnsp
  529. logical :: spm_lnsp2sp
  530. integer :: pid
  531. character(len=7) :: gribcode
  532. character(len=16) :: spm_levs, spm_paramkey, ec_paramkey
  533. type(TDate) :: tfile
  534. integer :: ccyy, mm, dd, hh, mn
  535. type(TDate) :: tc
  536. logical :: exist
  537. logical :: isfirst
  538. logical :: reopened
  539. integer :: pid, tabid
  540. integer :: nlev, glevtype, glevel
  541. integer :: level
  542. integer :: nlev_out, level_out
  543. integer :: ggridtype
  544. real :: lon_first, lon_inc
  545. integer :: lon_n
  546. real :: lat_first, lat_inc
  547. integer :: lat_n
  548. integer :: ggN
  549. integer :: shT
  550. integer :: greftime(5), gtimerange(4)
  551. character(len=64) :: key
  552. integer :: ilat
  553. real, pointer :: pat(:,:)
  554. type(TshGridInfo) :: tmp_shi
  555. complex, pointer :: tmp_sh(:)
  556. ! --- begin ---------------------------------
  557. !write (gol,'("mf_ReadRecord_2: paramkey : ",a)') trim(paramkey); call goPr
  558. !call wrtgol( 'mf_ReadRecord_2: tref : ', tref ); call goPr
  559. !call wrtgol( 'mf_ReadRecord_2: t1 : ', t1 ); call goPr
  560. !call wrtgol( 'mf_ReadRecord_2: t2 : ', t2 ); call goPr
  561. ! no fluxes through boundaries ...
  562. if ( nuv /= 'n' ) then
  563. write (gol,'("unsupported nuv key : ",a)') nuv; call goErr
  564. TRACEBACK; status=1; return
  565. end if
  566. ! limitted support of fluxes ..
  567. !if ( nw /= 'n' ) then
  568. ! write (gol,'("unsupported nw key : ",a)') nw; call goErr
  569. ! TRACEBACK; status=1; return
  570. !end if
  571. ! init pointer arrays:
  572. call pa_Init( pat )
  573. !
  574. ! ~~~ 3d field or 2d stored in 3d array
  575. !
  576. ! grid : T159, N80, etc
  577. call goWriteKeyNum( gridT, 'T', mf%ec_sh )
  578. call goWriteKeyNum( gridN, 'N', mf%ec_gg )
  579. ! defaults
  580. ec_paramkey = paramkey
  581. ec_class = mf%ec_class
  582. ec_type = mf%ec_type
  583. levs = mf%ec_levs
  584. ec_grid = gridN
  585. treskey = mf%treskey
  586. constant = .false.
  587. do_spm = .false.
  588. spm_lnsp = .false.
  589. spm_lnsp2sp = .false.
  590. ! specials
  591. select case ( paramkey )
  592. case ( 'LNSP' )
  593. ec_grid = gridT
  594. case ( 'VO', 'D' )
  595. ec_grid = gridT
  596. do_spm = .true.
  597. spm_lnsp = .true.
  598. case ( 'T', 'W', 'Q', 'CLWC', 'CIWC', 'CC', 'UDMF', 'UDDR', 'DDMF', 'DDDR' )
  599. do_spm = .true.
  600. spm_lnsp2sp = (ec_class == 'e4') .or. (ec_class == 'ei')
  601. case ( 'oro', 'lsm' )
  602. levs = 'sfc'
  603. constant = .true.
  604. treskey = 'const'
  605. ! tmm_convec tries to read oro using the default sourcekey,
  606. ! which probably contains type=fc ; force to use an for oro ...
  607. ec_type = 'an'
  608. ! case ( 'cvl', 'cvh', 'tvl', 'tvh', 'sr', 'albedo', 'lsrh' )
  609. ! ec_type = 'an'
  610. ! levs = 'sfc'
  611. case ( 'ci', 'sst', 'swvl1', 'swvl2', 'swvl3', 'swvl4', '10fg', 'sd', 'lsp', &
  612. 'cp', 'sf', 'sshf', 'slhf', 'blh', 'u10m', 'v10m', 't2m', 'd2m', &
  613. 'ssr', 'ewss', 'nsss', 'sstr' ,'src', 'skt' )
  614. levs = 'sfc'
  615. case ( 'sp' )
  616. spm_lnsp2sp = (ec_class == 'e4') .or. (ec_class == 'ei')
  617. if ( spm_lnsp2sp ) then
  618. ec_paramkey = 'LNSP'
  619. else
  620. levs = 'sfc'
  621. end if
  622. end select
  623. ! write gribcode
  624. call GetPid( 'ec', ec_paramkey, pid, tabid, status )
  625. IF_NOTOK_RETURN(status=1)
  626. write (gribcode,'(i3,".",i3.3)') pid, tabid
  627. gribcode = adjustl(gribcode)
  628. ! convert input times to file name times:
  629. call GetGribTime( mf, treskey, tref, t1, t2, status, tfile=tfile )
  630. IF_NOTOK_RETURN(status=1)
  631. !call wrtgol( 'mf_ReadRecord_2: tfile : ', tfile ); call goPr
  632. ! extract time values:
  633. call Get( tfile, year=ccyy, month=mm, day=dd, hour=hh, min=mn )
  634. ! create file name:
  635. ! dir/od-fc-20000101-1200-ml60-138-T159.gb
  636. !
  637. ! filename includes date:
  638. write (mf%fname,'(a,"/",a,"-",a,"-",i4.4,2i2.2,"-",2i2.2,"-",a,"-",a,"-",a,".gb")') &
  639. trim(mf%dir), &
  640. trim(ec_class), trim(ec_type), ccyy, mm, dd, hh, mn, &
  641. trim(levs), trim(gribcode), trim(ec_grid)
  642. ! exist ?
  643. inquire( file=mf%fname, exist=exist )
  644. if ( .not. exist ) then
  645. write (gol,'("grib file does not exist:")'); call goErr
  646. write (gol,'(" ",a)') trim(mf%fname); call goErr
  647. TRACEBACK; status=1; return
  648. end if
  649. ! open grib file
  650. call Init( grib, mf%fname, 'r', status )
  651. IF_NOTOK_RETURN(status=1)
  652. ! arrays and grids not defined yet
  653. isfirst = .true.
  654. reopened = .false.
  655. ! loop over records
  656. level = 0
  657. do
  658. !
  659. ! read gribsection in file buffer
  660. !
  661. call ReadRecord( grib, status )
  662. select case ( status )
  663. case ( 0 )
  664. ! no error
  665. case ( 1 )
  666. ! eof
  667. if ( .not. reopened ) then
  668. !write (gol,'("grib read record: re-open ...")'); call goPr
  669. ! close:
  670. call Done( grib, status )
  671. IF_NOTOK_RETURN(status=1)
  672. ! reopen:
  673. call Init( grib, mf%fname, 'r', status )
  674. IF_NOTOK_RETURN(status=1)
  675. reopened = .true.
  676. cycle
  677. else
  678. write (gol,'("reached eof before requested record was found")'); call goErr
  679. write (gol,'(" file : ",a)') trim(mf%fname); call goErr
  680. write (gol,'(" paramkey : ",a)') trim(paramkey); call goErr
  681. call wrtgol( ' tref : ', tref ); call goErr
  682. call wrtgol( ' t1 : ', t1 ); call goErr
  683. call wrtgol( ' t2 : ', t2 ); call goErr
  684. write (gol,'("tips:")'); call goErr
  685. write (gol,'(" o grib file corrupted or zero ?")'); call goErr
  686. write (gol,'(" o if accumulatd field,")'); call goErr
  687. write (gol,'(" check list of accumulated fields in mf_ReadRecord_1")'); call goErr
  688. TRACEBACK; status=1; return
  689. end if
  690. case default
  691. write (gol,'("error from grib ReadRecord; status=",i6)') status; call goErr
  692. TRACEBACK; status=1; return
  693. end select
  694. !
  695. ! checks ...
  696. !
  697. ! get param id for the requested field from grib table:
  698. select case ( paramkey )
  699. case ( 'spm' ) ; call GetPid( 'ec', 'SP' , pid, tabid, status )
  700. case default ; call GetPid( 'ec', ec_paramkey, pid, tabid, status )
  701. end select
  702. IF_NOTOK_RETURN(status=1)
  703. ! check parameter; continue if not ok:
  704. call Check( grib, pid=pid, debug=0, status=status )
  705. if (status/=0) cycle
  706. ! fill times ?
  707. if ( .not. constant ) then
  708. ! extract time fields from grib, store in mf%tref/mf%t1/mf%t2
  709. call SetTime( mf, grib, status )
  710. IF_NOTOK_RETURN(status=1)
  711. ! check time:
  712. call CheckTime( mf, tref, t1, t2, status )
  713. if (status/=0) then
  714. !write (gol,'("grib read record: wrong time; skip ...")'); call goPr
  715. !write (gol,'(" paramkey : ",a)') paramkey; call goPr
  716. !call wrtgol( ' req. tref : ', tref ); call goPr
  717. !call wrtgol( ' t1 : ', t1 ); call goPr
  718. !call wrtgol( ' t2 : ', t2 ); call goPr
  719. !call wrtgol( ' grib tref : ', mf%tref ); call goPr
  720. !call wrtgol( ' t1 : ', mf%t1 ); call goPr
  721. !call wrtgol( ' t2 : ', mf%t2 ); call goPr
  722. !write (gol,'(" grib file : ",a)') trim(mf%fname); call goPr
  723. cycle
  724. end if
  725. end if ! time checking
  726. ! extract level stuff:
  727. call Get( grib, nlev=nlev, levtype=glevtype, level=glevel, status=status )
  728. IF_NOTOK_RETURN(status=1)
  729. ! check level type:
  730. select case ( glevtype )
  731. case ( levtype_sfc, levtype_land )
  732. ! surface field
  733. nlev = 1
  734. glevel = 1
  735. case ( levtype_hyb )
  736. select case ( paramkey )
  737. case ( 'LNSP', 'sp' )
  738. nlev = 1
  739. case default
  740. ! level in 3d field
  741. end select
  742. case default
  743. write (gol,'("found unexpected level type: ")'); call goErr
  744. write (gol,'(" leveltype : ",i3)') glevtype; call goErr
  745. write (gol,'(" paramkey : ",a)') paramkey; call goErr
  746. write (gol,'(" grib file : ",a)') trim(mf%fname); call goErr
  747. TRACEBACK; status=1; return
  748. end select
  749. ! check level:
  750. level = level + 1
  751. if ( glevel /= level ) then
  752. write (gol,'("found unexpected level: ")'); call goErr
  753. write (gol,'(" paramkey : ",a)') paramkey; call goErr
  754. write (gol,'(" req. level : ",i6)') level; call goErr
  755. write (gol,'(" grib level : ",i6)') glevel; call goErr
  756. write (gol,'(" grib nlev : ",i6)') nlev; call goErr
  757. write (gol,'(" grib file : ",a)') trim(mf%fname); call goErr
  758. TRACEBACK; status=1; return
  759. end if
  760. ! number of output levels:
  761. nlev_out = nlev
  762. if ( nw == 'w' ) nlev_out = nlev_out + 1
  763. !
  764. ! define grids and arrays
  765. !
  766. if ( isfirst ) then
  767. !
  768. ! * info
  769. !
  770. ! example of history:
  771. ! model=ecmwf;class=od;type=fc;tref=2000,12,31,12,00; ...
  772. ! trange=001,234,240,001;sh=159;nlev=60
  773. !
  774. call Init( tmi, paramkey, 'unknown', status )
  775. call AddHistory( tmi, 'model==ecmwf', status )
  776. call AddHistory( tmi, 'class=='//trim(mf%ec_class), status )
  777. call AddHistory( tmi, 'type=='//trim(mf%ec_type) , status )
  778. !
  779. call Get( grib, status, reftime=greftime, timerange=gtimerange )
  780. IF_NOTOK_RETURN(status=1)
  781. write (key,'("tref==",i4.4,4(",",i2.2))') greftime
  782. call AddHistory( tmi, trim(key), status )
  783. write (key,'("trange==",i3.3,3(",",i3.3))') gtimerange
  784. call AddHistory( tmi, trim(key), status )
  785. !
  786. write (key,'("nlev==",i3.3)') nlev
  787. call AddHistory( tmi, trim(key), status )
  788. !
  789. ! * define horizontal grid:
  790. !
  791. ! extract grid type:
  792. call Get( grib, status, gridtype=ggridtype )
  793. IF_NOTOK_RETURN(status=1)
  794. ! setup:
  795. select case ( ggridtype )
  796. ! o lat/lon
  797. case ( gridtype_ll )
  798. ! routine returns lat/lon grid:
  799. gridtype = 'll'
  800. ! grib storage is north pole to south pole:
  801. call Get( grib, status, &
  802. lon_first=lon_first, lon_inc=lon_inc, lon_n=lon_n, &
  803. lat_last =lat_first, lat_inc=lat_inc, lat_n=lat_n )
  804. IF_NOTOK_RETURN(status=1)
  805. ! define grid structure:
  806. call Init( lli, lon_first, lon_inc, lon_n, &
  807. lat_first, lat_inc, lat_n, status )
  808. IF_NOTOK_RETURN(status=1)
  809. ! init array to store 2d field from grib file (north-south order):
  810. call pa_SetShape( pat, lon_n, lat_n )
  811. ! allocate output:
  812. call pa_SetShape( ll, lon_n, lat_n, nlev_out )
  813. ll = 0.0
  814. ! add to history:
  815. write (key,'("longrid==",f7.2,",",f6.2,",",i4)') lon_first, lon_inc, lon_n
  816. call AddHistory( tmi, trim(key), status )
  817. write (key,'("latgrid==",f7.2,",",f6.2,",",i4)') lat_first, lat_inc, lat_n
  818. call AddHistory( tmi, trim(key), status )
  819. ! o gaussian grid
  820. case ( gridtype_gg )
  821. ! routine returns gg grid:
  822. gridtype = 'gg'
  823. ! extract grid number:
  824. call Get( grib, status, N=ggN )
  825. IF_NOTOK_RETURN(status=1)
  826. ! define grid structure:
  827. call Init( ggi, ggN, .true., status )
  828. IF_NOTOK_RETURN(status=1)
  829. ! allocate output:
  830. call pa_SetShape( gg, ggi%np, nlev_out )
  831. gg = 0.0
  832. ! add to history:
  833. write (key,'("gg==",i4.4)') ggN
  834. call AddHistory( tmi, trim(key), status )
  835. ! o spectral field:
  836. case ( gridtype_sh )
  837. ! routine returns sh grid:
  838. gridtype = 'sh'
  839. ! extract spectral truncation:
  840. call Get( grib, status, T=shT )
  841. IF_NOTOK_RETURN(status=1)
  842. ! intialize spherical harmonic field info:
  843. call Init( shi, shT, status )
  844. IF_NOTOK_RETURN(status=1)
  845. ! allocate output:
  846. call pa_SetShape( sh, shi%np, nlev_out )
  847. sh = cmplx(0.0,0.0)
  848. ! add to history:
  849. write (key,'("sh==",i4.4)') shT
  850. call AddHistory( tmi, trim(key), status )
  851. case default
  852. write (gol,'("unsupported gridtype for setup : ",i6)') ggridtype; call goErr
  853. TRACEBACK; status=1; return
  854. end select
  855. !
  856. ! * levels
  857. !
  858. select case ( nlev )
  859. case ( 1 )
  860. call Init( levi, nlev, (/0.0,0.0/), (/0.0,0.0/), status )
  861. IF_NOTOK_RETURN(status=1)
  862. case ( 60 )
  863. call Init( levi, 'ec60', status )
  864. IF_NOTOK_RETURN(status=1)
  865. case ( 91 )
  866. call Init( levi, 'ec91', status )
  867. IF_NOTOK_RETURN(status=1)
  868. case default
  869. write (gol,'("do not how to init levi for nlev = ",i6)') nlev; call goErr
  870. TRACEBACK; status=1; return
  871. end select
  872. ! not again ...
  873. isfirst = .false.
  874. end if ! isfirst (grid definition and allocation)
  875. !
  876. ! store
  877. !
  878. ! layers numbered 1..nlev, half levels numberd 1..nlev+1
  879. ! top-down, thus 1 is space and nlev+1 is surface
  880. if ( nw == 'w' ) then
  881. ! store half levels
  882. select case ( paramkey )
  883. ! store in upper half level of a layer:
  884. case ( 'UDMF', 'DDMF' )
  885. level_out = level
  886. !! store in lower half level of a layer:
  887. !case ( 'dummy' )
  888. ! level_out = level + 1
  889. ! to be implemented ...
  890. case default
  891. write (gol,'("do not if data is on upper or lower half level ...")'); call goErr
  892. write (gol,'(" paramkey : ",a)') paramkey; call goErr
  893. TRACEBACK; status=1; return
  894. end select
  895. else
  896. ! store full levels
  897. level_out = level
  898. end if
  899. select case ( ggridtype )
  900. case ( gridtype_ll )
  901. ! read 2d pat from grib; storred from north to south
  902. call Get( grib, status, ll=pat )
  903. IF_NOTOK_RETURN(status=1)
  904. ! store from south to north:
  905. do ilat = 1, lat_n
  906. ll(:,ilat,level_out) = pat(:,lat_n+1-ilat)
  907. end do
  908. case ( gridtype_gg )
  909. ! read 2d pat from grib:
  910. call Get( grib, status, gg=gg(:,level_out) )
  911. IF_NOTOK_RETURN(status=1)
  912. ! convert from lnsp to sp ?
  913. if ( paramkey == 'sp' .and. spm_lnsp2sp ) gg(:,level_out) = exp(gg(:,level_out)) ! Pa
  914. case ( gridtype_sh )
  915. ! read 2d pat from grib:
  916. call Get( grib, status, sh=sh(:,level_out) )
  917. IF_NOTOK_RETURN(status=1)
  918. case default
  919. write (gol,'("unsupported gridtype for 2d pat : ",i6)') gridtype; call goErr
  920. TRACEBACK; status=1; return
  921. end select
  922. ! last record for this field ?
  923. if ( glevel == nlev ) exit
  924. end do ! records
  925. ! close grib file
  926. call Done( grib, status )
  927. IF_NOTOK_RETURN(status=1)
  928. !
  929. ! ~~~ surface pressure
  930. !
  931. if ( do_spm ) then
  932. ! read lnsp and covert to sp ?
  933. if ( spm_lnsp .or. spm_lnsp2sp) then
  934. spm_levs = levs
  935. spm_paramkey = 'LNSP'
  936. else
  937. spm_levs = 'sfc'
  938. spm_paramkey = 'SP'
  939. end if
  940. ! write gribcode:
  941. call GetPid( 'ec', spm_paramkey, pid, tabid, status )
  942. IF_NOTOK_RETURN(status=1)
  943. write (gribcode,'(i3,".128")') pid
  944. gribcode = adjustl(gribcode)
  945. ! create file name:
  946. ! dir/od-fc-2000-01-ml60-T159-T_20000101_fg006up4tr3.gb
  947. write (spm_fname,'(a,"/",a,"-",a,"-",i4.4,2i2.2,"-",2i2.2,"-",a,"-",a,"-",a,".gb")') &
  948. trim(mf%dir), &
  949. trim(ec_class), trim(ec_type), ccyy, mm, dd, hh, mn, &
  950. trim(spm_levs), trim(gribcode), trim(ec_grid)
  951. ! exist ?
  952. inquire( file=spm_fname, exist=exist )
  953. if ( .not. exist ) then
  954. write (gol,'("grib file does not exist:")'); call goErr
  955. write (gol,'(" ",a)') trim(spm_fname); call goErr
  956. TRACEBACK; status=1; return
  957. end if
  958. ! open grib file
  959. call Init( spm_grib, spm_fname, 'r', status )
  960. IF_NOTOK_RETURN(status=1)
  961. ! loop over time records
  962. do
  963. ! read gribsection in file buffer
  964. call ReadRecord( spm_grib, status )
  965. IF_NOTOK_RETURN(status=1)
  966. ! fill times
  967. call SetTime( mf, spm_grib, status )
  968. IF_NOTOK_RETURN(status=1)
  969. ! check time:
  970. call CheckTime( mf, tref, t1, t2, status )
  971. if (status/=0) then
  972. !write (gol,'("grib read record: spm wrong time; skip ...")'); call goPr
  973. cycle
  974. !write (gol,'("found unexpected times in grib file:")'); call goErr
  975. !write (gol,'(" paramkey : ",a)') paramkey; call goErr
  976. !call wrtgol( ' req. t1 : ', t1 ); call goErr
  977. !call wrtgol( ' t2 : ', t2 ); call goErr
  978. !call wrtgol( ' grib t1 : ', mf%t1 ); call goErr
  979. !call wrtgol( ' t2 : ', mf%t2 ); call goErr
  980. !write (gol,'(" grib file : ",a)') trim(spm_fname); call goErr
  981. !TRACEBACK; status=1; return
  982. end if
  983. ! time ok
  984. exit
  985. end do ! time loop
  986. ! set param id:
  987. select case ( ggridtype )
  988. case ( gridtype_ll )
  989. call GetPid( 'ec', 'SP', pid, tabid, status )
  990. IF_NOTOK_RETURN(status=1)
  991. case ( gridtype_gg )
  992. if ( spm_lnsp2sp ) then
  993. call GetPid( 'ec', 'LNSP', pid, tabid, status )
  994. IF_NOTOK_RETURN(status=1)
  995. else
  996. call GetPid( 'ec', 'SP', pid, tabid, status )
  997. IF_NOTOK_RETURN(status=1)
  998. end if
  999. case ( gridtype_sh )
  1000. call GetPid( 'ec', 'LNSP', pid, tabid, status )
  1001. IF_NOTOK_RETURN(status=1)
  1002. case default
  1003. write (gol,'("unsupported gridtype for setup sp/lnsp : ",i6)') ggridtype; call goErr
  1004. TRACEBACK; status=1; return
  1005. end select
  1006. ! check parameter:
  1007. call Check( spm_grib, pid=pid, debug=1, status=status )
  1008. IF_NOTOK_RETURN(status=1)
  1009. ! check level:
  1010. call Get( spm_grib, levtype=glevtype, level=glevel, status=status )
  1011. IF_NOTOK_RETURN(status=1)
  1012. select case ( ggridtype )
  1013. case ( gridtype_ll )
  1014. if ( glevtype /= levtype_sfc ) then
  1015. write (gol,'("found unexpected level type ")'); call goErr
  1016. write (gol,'(" paramkey : ",a)') paramkey; call goErr
  1017. write (gol,'(" sfc level type : ",i6)') levtype_sfc; call goErr
  1018. write (gol,'(" grib level type : ",i6)') glevtype; call goErr
  1019. write (gol,'(" grib file : ",a)') trim(spm_fname); call goErr
  1020. TRACEBACK; status=1; return
  1021. end if
  1022. case ( gridtype_gg )
  1023. if ( spm_lnsp2sp ) then
  1024. if ( (glevtype /= levtype_hyb) .or. (glevel /= 1) ) then
  1025. write (gol,'("found unexpected level type (lnsp for 3d gg)")'); call goErr
  1026. write (gol,'(" paramkey : ",a)') paramkey; call goErr
  1027. write (gol,'(" hyb level type : ",i6)') levtype_hyb; call goErr
  1028. write (gol,'(" grib level type : ",i6)') glevtype; call goErr
  1029. write (gol,'(" grib level : ",i6)') glevel; call goErr
  1030. write (gol,'(" grib file : ",a)') trim(spm_fname); call goErr
  1031. TRACEBACK; status=1; return
  1032. end if
  1033. else
  1034. if ( glevtype /= levtype_sfc ) then
  1035. write (gol,'("found unexpected level type ")'); call goErr
  1036. write (gol,'(" paramkey : ",a)') paramkey; call goErr
  1037. write (gol,'(" sfc level type : ",i6)') levtype_sfc; call goErr
  1038. write (gol,'(" grib level type : ",i6)') glevtype; call goErr
  1039. write (gol,'(" grib file : ",a)') trim(spm_fname); call goErr
  1040. TRACEBACK; status=1; return
  1041. end if
  1042. end if
  1043. case ( gridtype_sh )
  1044. if ( (glevtype /= levtype_hyb) .or. (glevel /= 1) ) then
  1045. write (gol,'("found unexpected level type ")'); call goErr
  1046. write (gol,'(" paramkey : ",a)') paramkey; call goErr
  1047. write (gol,'(" hyb level type : ",i6)') levtype_hyb; call goErr
  1048. write (gol,'(" grib level type : ",i6)') glevtype; call goErr
  1049. write (gol,'(" grib level : ",i6)') glevel; call goErr
  1050. write (gol,'(" grib file : ",a)') trim(spm_fname); call goErr
  1051. TRACEBACK; status=1; return
  1052. end if
  1053. case default
  1054. write (gol,'("unsupported gridtype for sp/lnsp levs : ",i6)') ggridtype; call goErr
  1055. TRACEBACK; status=1; return
  1056. end select
  1057. ! read and store surface pressure field:
  1058. select case ( ggridtype )
  1059. case ( gridtype_ll )
  1060. ! allocate storage
  1061. call pa_SetShape( sp_ll, lon_n, lat_n )
  1062. ! read 2d pat from grib; storred from north to south
  1063. call Get( spm_grib, status, ll=pat )
  1064. IF_NOTOK_RETURN(status=1)
  1065. ! store from south to north:
  1066. do ilat = 1, lat_n
  1067. sp_ll(:,ilat) = pat(:,lat_n+1-ilat)
  1068. end do
  1069. case ( gridtype_gg )
  1070. ! allocate storage
  1071. call pa_SetShape( sp_gg, ggi%np )
  1072. ! read gg field from grib:
  1073. call Get( spm_grib, status, gg=sp_gg )
  1074. IF_NOTOK_RETURN(status=1)
  1075. ! convert from lnsp to sp ?
  1076. if ( spm_lnsp2sp ) sp_gg = exp(sp_gg) ! Pa
  1077. case ( gridtype_sh )
  1078. ! allocate storage
  1079. call pa_SetShape( lnsp_sh, shi%np )
  1080. ! read spectral coeff:
  1081. call Get( spm_grib, status, sh=lnsp_sh )
  1082. IF_NOTOK_RETURN(status=1)
  1083. case default
  1084. write (gol,'("unsupported gridtype for reading sp/lnsp : ",i6)') ggridtype; call goErr
  1085. TRACEBACK; status=1; return
  1086. end select
  1087. ! close grib file
  1088. call Done( spm_grib, status )
  1089. IF_NOTOK_RETURN(status=1)
  1090. end if
  1091. !
  1092. ! ~~~ end
  1093. !
  1094. ! deallocate arrays
  1095. call pa_Done( pat )
  1096. ! ok
  1097. status = 0
  1098. end subroutine mf_ReadRecord_2
  1099. ! ****************************************************************************
  1100. !
  1101. ! In gribfile:
  1102. ! reftime : for example time at which forecast is made
  1103. ! timerange: increment or interval
  1104. !
  1105. ! arguments: ok if:
  1106. !
  1107. ! time1 == time2 time1==time2 == reftime+timerange
  1108. !
  1109. ! time1 == 0 time2 == reftime+timerange
  1110. !
  1111. ! time2 == 0 time1 == reftime
  1112. !
  1113. ! time1 < time2 time1 == refitme, time2 == reftime+timerange
  1114. !
  1115. !
  1116. ! grib [t1-----------t2]
  1117. ! status
  1118. ! o time1/2 record too old 1
  1119. ! o time1/2 ok 0
  1120. ! o time1/2 record too new 2
  1121. !
  1122. !
  1123. ! SetTime( mf, status )
  1124. !
  1125. ! Extracts time values from current grib record,
  1126. ! store in mf%t1, mf%t2
  1127. !
  1128. ! return status:
  1129. ! 0 : ok
  1130. ! other : some error
  1131. !
  1132. ! CheckTime( mf, time1, time2, status )
  1133. !
  1134. ! return status:
  1135. ! 0 : times match
  1136. ! 1 : times do not match, try next record
  1137. ! 2 : current record is newer than requested (reopen ?)
  1138. ! 3 : some error
  1139. !
  1140. ! ***
  1141. !
  1142. ! Return time parameters for grib files:
  1143. ! o tfile : date in filename
  1144. ! o trange : time interval covered by fields in file
  1145. ! o tref : reference time (forecast start?) for tday,[t1,t2]
  1146. !
  1147. ! Called as:
  1148. ! call GetGribTime( treskey, tday, t1, t2, status, tref=tref )
  1149. ! call GetGribTime( treskey, tref, t1, t2, status, tfile=tfile )
  1150. !
  1151. subroutine GetGribTime( mf, treskey, t0, t1, t2, status, tfile, trange, tref, tshift )
  1152. use GO, only : TDate, TIncrDate, Get, Set, wrtgol, NewDate, IncrDate
  1153. use GO, only : operator(+), operator(-), operator(<), rTotal, iTotal
  1154. use GO, only : AnyDate, IsAnyDate
  1155. use GO, only : wrtgol
  1156. ! --- in/out --------------------------------
  1157. type(TMeteoFile_ecmwf_mars), intent(in) :: mf ! for adhoc flags ...
  1158. character(len=*), intent(in) :: treskey
  1159. type(TDate), intent(in) :: t0, t1, t2
  1160. integer, intent(out) :: status
  1161. type(TDate), intent(out), optional :: tfile
  1162. type(TDate), intent(out), optional :: trange(2)
  1163. type(TDate), intent(out), optional :: tref
  1164. type(TIncrDate), intent(out), optional :: tshift
  1165. ! --- const --------------------------------------
  1166. character(len=*), parameter :: rname = mname//'/GetGribTime'
  1167. ! --- local --------------------------------
  1168. integer :: hour0, hour1, dhour2, time6(6)
  1169. integer :: dd, hh, mn, step
  1170. integer :: anhh
  1171. real :: ddr
  1172. ! --- begin --------------------------------
  1173. !! debug ...
  1174. !write (gol,'(" GetGribTime: treskey : ",a)') trim(treskey); call goPr
  1175. !call wrtgol( ' GetGribTime: t0 : ', t0 ); call goPr
  1176. !call wrtgol( ' GetGribTime: t1 : ', t1 ); call goPr
  1177. !call wrtgol( ' GetGribTime: t2 : ', t2 ); call goPr
  1178. ! files opend upon reading, thus no particular time range for which file is valid:
  1179. if ( present(trange) ) then
  1180. trange(1) = AnyDate()
  1181. trange(2) = AnyDate()
  1182. !! debug ...
  1183. !call wrtgol( ' GetGribTime: set trange to : ', trange(1), ' - ', trange(2) ); call goPr
  1184. end if
  1185. ! zero shift by default
  1186. if ( present(tshift) ) then
  1187. tshift = IncrDate(hour=0)
  1188. !! debug ...
  1189. !call wrtgol( ' GetGribTime: set tshift to : ', tshift ); call goPr
  1190. endif
  1191. ! set day shift, start hour, and step
  1192. select case ( treskey )
  1193. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1194. ! constant field
  1195. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1196. case ( 'const' )
  1197. ! only t0 is usefull ...
  1198. if ( present(tfile ) ) tfile = t0
  1199. !if ( present(trange) ) trange = (/t1,t2/) ! any, any
  1200. if ( present(tref ) ) tref = t0 ! dummy ...
  1201. ! take analysed fields always at least 24 hour old,
  1202. ! since these are the only analysed fields available in forecast mode
  1203. if ( present(tshift) ) then
  1204. tshift = IncrDate(hour=24)
  1205. ! FIX for start of ml91 test suite; try if 12 is ok too ...
  1206. !tshift = IncrDate(hour=12)
  1207. end if
  1208. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1209. ! fc, 3 hourly
  1210. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1211. case ( '_fc012up2tr3' )
  1212. ! hour of t0:
  1213. call Get( t0, hour=hour0 )
  1214. ! end hour counted from t0 00:00
  1215. dhour2 = iTotal( t2 - t0, 'hour' )
  1216. !! debug ...
  1217. !write (gol,'(" GetGribTime: slhf4cnv : ",l2)') mf%slhf_for_convec; call goPr
  1218. !write (gol,'(" GetGribTime: hour0 : ",i3)') hour0; call goPr
  1219. !write (gol,'(" GetGribTime: dhour2 : ",i3)') dhour2; call goPr
  1220. ! set forecast start time and step given hour:
  1221. ! ~~ first the unusual stuf ...
  1222. if ( mf%slhf_for_convec .and. (hour0 == 0) .and. (dhour2 == 3) ) then
  1223. ! (00,03] fc day 0 (slhf for convec) take from (-12,03]
  1224. dd = -1 ; hh = 12 ; step = 15
  1225. else if ( mf%slhf_for_convec .and. (hour0 == 12) .and. (dhour2 == 3) ) then
  1226. ! (12,15] fc day 0 (slhf for convec) take from (00,15]
  1227. dd = 0 ; hh = 00 ; step = 15
  1228. ! ~~ now the usual settings ...
  1229. else if ( dhour2 == 0 ) then
  1230. ! 00:00 fc day 0
  1231. dd = -1 ; hh = 12 ; step = 12
  1232. else if ( (dhour2 <= 12) .and. (modulo(dhour2,3) == 0) ) then
  1233. ! (00,12] fc day 0
  1234. dd = 0
  1235. hh = 00
  1236. step = dhour2
  1237. else if ( ( (t0 < NewDate(year=2006,month=03,day=14) ) .and. &
  1238. ( ((dhour2 <= 12+ 72) .and. (modulo(dhour2,3) == 0)) .or. &
  1239. ((dhour2 <= 12+240) .and. (modulo(dhour2,6) == 0)) ) ) &
  1240. .or. ( ( ((dhour2 <= 12+ 96) .and. (modulo(dhour2,3) == 0)) .or. &
  1241. ((dhour2 <= 12+240) .and. (modulo(dhour2,6) == 0)) ) ) ) then
  1242. ! (12,240] fc days 1-10
  1243. dd = 0
  1244. hh = 12
  1245. step = dhour2 - 12
  1246. else
  1247. write (gol,'("unsupported hour :")'); call goErr
  1248. write (gol,'(" hour0 : ",i3)') hour0; call goErr
  1249. write (gol,'(" dhour2 : ",i3)') dhour2; call goErr
  1250. write (gol,'(" treskey : ",a )') treskey; call goErr
  1251. call wrtgol( ' time1 : ', t1 ); call goErr
  1252. write (gol,'(" timslhf_for_convec : ",l2)') mf%slhf_for_convec; call goErr
  1253. TRACEBACK; status=1; return
  1254. end if
  1255. !! fields valid for hh+(00,12] :
  1256. !if ( present(trange) ) then
  1257. ! trange(1) = t2
  1258. ! call Set( trange(1), hour=hh, min=0, sec=0, mili=0 ) ! hh:00
  1259. ! ! trap 00:00, this should be previous day:
  1260. ! call Get( t2, time6=time6 )
  1261. ! if ( all(time6(4:6)==0) ) trange(1) = trange(1) - IncrDate(day=1)
  1262. ! ! complete (00,12]
  1263. ! trange(2) = trange(1) + IncrDate(hour=12) ! 24:00
  1264. ! call Set( trange(1), mili=1 ) ! > 00:00
  1265. !end if
  1266. ! reference time = start of forecast
  1267. if ( present(tref) ) then
  1268. tref = t0 + IncrDate(day=dd,hour=hh)
  1269. !! debug ...
  1270. !write (gol,*) ' GetGribTime: dd, hh, step : ', dd, hh, step; call goPr
  1271. !call wrtgol( ' GetGribTime: set tref to : ', tref ); call goPr
  1272. end if
  1273. ! adhoc: if tfile is requested, probably the 'tref' returned before
  1274. ! is now in input 't0' ...
  1275. if ( present(tfile) ) then
  1276. tfile = t0 ! .. is tref !
  1277. !! debug ...
  1278. !call wrtgol( ' GetGribTime: set tfile to : ', tfile ); call goPr
  1279. end if
  1280. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1281. ! analysis, files for hours 0, 6, 12, and 18
  1282. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1283. case ( '_an0tr6' )
  1284. ! reference time = analysis time
  1285. if ( present(tref) ) then
  1286. tref = t1
  1287. end if
  1288. ! t0 t1,t2 ->
  1289. ! -+-----------------------+-----------------------+--->
  1290. ! 00 06 12 18 00 06 12 18 00
  1291. ! a
  1292. ! a a
  1293. ! a a(-----------] 00 analysis/forecast
  1294. ! a a(--------------> 12 forecast
  1295. !
  1296. ! -24 -24 -24 -24 -48 shift
  1297. ! take analysed fields always at least 24 hour old,
  1298. ! since these are the only analysed fields available in forecast mode,
  1299. ! and to obtain a contineous time line
  1300. ! t0 is always 00:00
  1301. if ( present(tshift) ) then
  1302. ! difference between t1 and t0 00:00 in fraction of days:
  1303. ddr = rTotal( t1 - t0, 'day' )
  1304. ! set time shift in days:
  1305. tshift = IncrDate(day=floor(ddr)+1)
  1306. !! FIX for start of ml91 test suite; try if 12 is ok too ...
  1307. !tshift = IncrDate(day=floor(ddr),hour=12)
  1308. end if
  1309. ! one file for each time:
  1310. if ( present(tfile) ) then
  1311. tfile = t1
  1312. end if
  1313. !! fields in file valid for instant time:
  1314. !if ( present(trange) ) then
  1315. ! trange(1) = t1
  1316. ! trange(2) = t1
  1317. !end if
  1318. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1319. ! fc, 1 hourly emissions
  1320. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1321. case ( '_fc000up24tr1' )
  1322. ! macc emisisons
  1323. ! fields in grib file have time stamps 00:30, 01:30, etc
  1324. ! reference time = start of forecast
  1325. if ( present(tref) ) then
  1326. ! end hour counted from t0 00:00
  1327. dhour2 = iTotal( t2 - t0, 'hour' )
  1328. ! field from current day, 'forecast time' 00:30 etc:
  1329. tref = t0 + IncrDate(day=0,hour=dhour2-1,min=30)
  1330. ! no steps in this timing convention:
  1331. step = 0
  1332. !! debug ...
  1333. !write (gol,*) ' GetGribTime: dd, hh, step : ', dd, hh, step; call goPr
  1334. !call wrtgol( ' GetGribTime: set tref to : ', tref ); call goPr
  1335. end if
  1336. ! adhoc: if tfile is requested, probably the 'tref' returned before
  1337. ! is now in input 't0' ...
  1338. if ( present(tfile) ) then
  1339. tfile = t0 ! .. is tref !
  1340. !! debug ...
  1341. !call wrtgol( ' GetGribTime: set tfile to : ', tfile ); call goPr
  1342. end if
  1343. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1344. ! ???
  1345. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1346. case default
  1347. write (gol,'("unsupported time resolution key:")'); call goErr
  1348. write (gol,'(" `",a,"`")') trim(treskey); call goErr
  1349. TRACEBACK; status=1; return
  1350. end select
  1351. ! ok
  1352. status = 0
  1353. end subroutine GetGribTime
  1354. ! ***
  1355. !
  1356. ! Extract time fields of current grib record,
  1357. ! store in mf%tref, mf%t1, mf%t2
  1358. !
  1359. subroutine SetTime( mf, grib, status )
  1360. use GO, only : TDate, NewDate, IncrDate, operator(+), wrtgol
  1361. use file_grib, only : TGribFile, Check, Get
  1362. ! --- const -------------------------------------
  1363. character(len=*), parameter :: rname = mname//'/SetTime'
  1364. ! --- in/out -------------------------------
  1365. type(TMeteoFile_ecmwf_mars), intent(inout) :: mf
  1366. type(TGribFile), intent(inout) :: grib
  1367. integer, intent(out) :: status
  1368. ! --- local -------------------------------
  1369. integer :: reftime(5), timerange(4)
  1370. ! --- begin -------------------------------
  1371. ! extract time fields from grib record:
  1372. call Get( grib, status, reftime=reftime, timerange=timerange )
  1373. IF_NOTOK_RETURN(status=1)
  1374. ! Fill t1 and t2 with the time information; might be equal.
  1375. ! Check time range indicator (WMO code table 5):
  1376. select case ( timerange(4) )
  1377. case ( 0, 1 )
  1378. !
  1379. ! 0 = Forecast product valid for reference time + P1 (P1>0),
  1380. ! or uninitialized analysis product for reference time (P1=0)
  1381. !
  1382. ! 1 = Initialized analysis product for reference time (P1=0).
  1383. !
  1384. ! fill reference time:
  1385. mf%tref = NewDate( time5=reftime )
  1386. ! fill t1 with reftime+timerange;
  1387. ! add P1 in hours; check time unit (WMO code table 4)
  1388. mf%t1 = NewDate( time5=reftime )
  1389. select case ( timerange(1) )
  1390. case ( 1 ) ! hours
  1391. mf%t1 = mf%t1 + IncrDate( hour=timerange(2) )
  1392. case default
  1393. write (gol,'("grib timerange units other than hours not supported yet")'); call goErr
  1394. write (gol,'(" reftime : ",i4,4i3)') reftime; call goErr
  1395. write (gol,'(" timerange : ",4i3)') timerange; call goErr
  1396. write (gol,'(" file : ",a)') trim(grib%fname); call goErr
  1397. TRACEBACK; status=1; return
  1398. end select
  1399. ! instant time:
  1400. mf%t2 = mf%t1
  1401. case ( 2 )
  1402. !
  1403. ! 2 = Product with a valid time ranging between
  1404. ! reference time + P1 and reference time + P2
  1405. !
  1406. ! fill reftime:
  1407. mf%tref = NewDate( time5=reftime )
  1408. ! fill t1 with reftime+P1;
  1409. ! add P1 in hours; check time unit (WMO code table 4)
  1410. mf%t1 = mf%tref
  1411. select case ( timerange(1) )
  1412. case ( 1 ) ! hours
  1413. mf%t1 = mf%t1 + IncrDate( hour=timerange(2) )
  1414. case default
  1415. write (gol,'("grib timerange units other than hours not supported yet")'); call goErr
  1416. write (gol,'(" file : ",a)') trim(grib%fname); call goErr
  1417. TRACEBACK; status=1; return
  1418. end select
  1419. ! fill t2 with reftime+P2;
  1420. ! add P2 in hours; check time unit (WMO code table 4)
  1421. mf%t2 = mf%tref
  1422. select case ( timerange(1) )
  1423. case ( 1 ) ! hours
  1424. mf%t2 = mf%t2 + IncrDate( hour=timerange(3) )
  1425. case default
  1426. write (gol,'("grib timerange units other than hours not supported yet")'); call goErr
  1427. write (gol,'(" file : ",a)') trim(grib%fname); call goErr
  1428. TRACEBACK; status=1; return
  1429. end select
  1430. case default
  1431. write (gol,'("unsupported time range indicator:")'); call goErr
  1432. write (gol,'(" indicator : ",i6)') timerange(4); call goErr
  1433. write (gol,'(" file : ",a)') trim(grib%fname); call goErr
  1434. TRACEBACK; status=1; return
  1435. end select
  1436. ! ok
  1437. status = 0
  1438. end subroutine SetTime
  1439. ! ***
  1440. subroutine CheckTime( mf, tref, t1, t2, status )
  1441. use GO
  1442. use file_grib, only : Check, Get
  1443. ! --- const -------------------------------------
  1444. character(len=*), parameter :: rname = mname//'/CheckTime'
  1445. ! --- in/out -------------------------------
  1446. type(TMeteoFile_ecmwf_mars), intent(in) :: mf
  1447. type(TDate), intent(in) :: tref, t1, t2
  1448. integer, intent(out) :: status
  1449. ! --- local -------------------------------
  1450. integer :: year1, year2
  1451. ! --- begin -------------------------------
  1452. !call wrtgol( 'CheckTime: (', tref, ') ', t1, ' - ', t2 ); call goPr
  1453. !call wrtgol( 'CheckTime: (', mf%tref, ') ', mf%t1, ' - ', mf%t2 ); call goPr
  1454. ! requested year zero ? always ok
  1455. call Get( t1, year=year1 )
  1456. call Get( t2, year=year2 )
  1457. if ( (year1 == 0) .and. (year2 == 0) ) then
  1458. ! requested constant field, always ok
  1459. status = 0; return ! ok
  1460. end if
  1461. if ( year1 == 0 ) then
  1462. ! do not test t1, only t2
  1463. if ( t2 == mf%t2 ) then
  1464. status = 0; return ! ok
  1465. else
  1466. status = 1; return ! not ok, try next
  1467. end if
  1468. else if ( year2 == 0 ) then
  1469. ! do not test t2, only t1
  1470. if ( t1 == mf%t1 ) then
  1471. status = 0; return ! ok
  1472. else
  1473. status = 1; return ! not ok, try next
  1474. end if
  1475. end if
  1476. ! ! interval or instant time
  1477. ! if ( t1 < t2 ) then
  1478. !
  1479. ! !! time interval: [t1,t2] should be inside [t1,t2]
  1480. ! !if ( (t1 >= mf%t1) .and. (t2 <= mf%t2) ) then
  1481. ! ! status = 0; return ! ok
  1482. ! !else
  1483. ! ! status = 1; return ! try next
  1484. ! !end if
  1485. !
  1486. ! ! time interval: [t1,t2] should be equal to [t1,t2]
  1487. ! if ( (t1 == mf%t1) .and. (t2 == mf%t2) ) then
  1488. ! status = 0; return ! ok
  1489. ! else
  1490. ! status = 1; return ! try next
  1491. ! end if
  1492. !
  1493. ! else if ( t1 == t2 ) then
  1494. !
  1495. ! ! instant time: t2 should match t2
  1496. ! if ( t2 == mf%t2 ) then
  1497. ! status = 0; return ! ok
  1498. ! else
  1499. ! status = 1; return ! try next
  1500. ! end if
  1501. !
  1502. ! else
  1503. !
  1504. ! write (gol,'("t1 should not exceed t2:")'); call goErr
  1505. ! call wrtgol( ' t1 : ', t1 ); call goErr
  1506. ! call wrtgol( ' t2 : ', t2 ); call goErr
  1507. ! TRACEBACK; status=3; return
  1508. !
  1509. ! end if
  1510. ! compare all:
  1511. if ( (tref == mf%tref) .and. (t1 == mf%t1) .and. (t2 == mf%t2) ) then
  1512. status = 0; return ! ok
  1513. else
  1514. status = 1; return ! try next
  1515. end if
  1516. ! some error ...
  1517. status = 1
  1518. end subroutine CheckTime
  1519. end module tmm_mf_ecmwf_mars