tmm_mf_ncep_gfs.F90 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. !###############################################################################
  2. !
  3. ! Interface to NCEP Global Forecast System data
  4. !
  5. ! Database
  6. ! --------
  7. !
  8. ! From Phil Rasch and Dani Bundi (NCAR)
  9. !
  10. ! NCEP Cray Binary files
  11. ! ----------------------
  12. !
  13. ! See http://dss.ucar.edu/pub/reanalysis//CRAY_bin.html .
  14. !
  15. ! Grib files
  16. ! ----------
  17. !
  18. ! For grib codes, see:
  19. ! http://www.nco.ncep.noaa.gov/pmb/docs/on388/
  20. !
  21. ! Spectral fields
  22. ! -----------
  23. !
  24. ! From the 'discription' in the NetCDF files in the CDC archive
  25. ! it was concluded that 'ncep spectral' / sqrt(2) = 'ecmwf spectral' .
  26. !
  27. !### macro's ###################################################################
  28. !
  29. #define TRACEBACK write (gol,'("in ",a," (",a,", line",i5,")")') rname, __FILE__, __LINE__; call goErr
  30. #define IF_NOTOK_RETURN(action) if (status/=0) then; TRACEBACK; action; return; end if
  31. #define IF_ERROR_RETURN(action) if (status> 0) then; TRACEBACK; action; return; end if
  32. !
  33. #include "tmm.inc"
  34. !
  35. !###############################################################################
  36. module tmm_mf_ncep_gfs
  37. use GO , only : gol, goErr, goPr
  38. use GO , only : TDate
  39. implicit none
  40. ! --- in/out ----------------------------
  41. private
  42. public :: TMeteoFile_ncep_gfs
  43. public :: Init, Done
  44. public :: Get
  45. public :: ReadRecord
  46. ! --- const ------------------------------
  47. character(len=*), parameter :: mname = 'tmm_mf_ncep_gfs'
  48. !--- type ---------------------------------
  49. type TMeteoFile_ncep_gfs
  50. ! field collection
  51. character(len=256) :: paramkeys
  52. type(TDate) :: trange(2)
  53. ! file names
  54. character(len=256) :: dir
  55. character(len=256) :: fname
  56. character(len=16) :: ext
  57. ! time resolution
  58. character(len=32) :: treskey
  59. type(TDate) :: tref
  60. end type TMeteoFile_ncep_gfs
  61. ! --- interfaces -------------------------
  62. interface Init
  63. module procedure mf_Init
  64. end interface
  65. interface Done
  66. module procedure mf_Done
  67. end interface
  68. interface Get
  69. module procedure mf_Get
  70. end interface
  71. interface ReadRecord
  72. module procedure mf_ReadRecord
  73. end interface
  74. ! --- adhoc ----------------------------------------
  75. integer :: adhoc_fu = 10
  76. character(len=256) :: adhoc_fname = 'none'
  77. contains
  78. ! ==============================================================
  79. subroutine mf_Init( mf, dir, archivekeys, paramkey, &
  80. tref, t1, t2, status )
  81. use GO, only : TDate, IsAnyDate, IncrDate, operator(+)
  82. use GO, only : goVarValue
  83. ! --- in/out ----------------------------
  84. type(TMeteoFile_ncep_gfs), intent(out) :: mf
  85. character(len=*), intent(in) :: dir
  86. character(len=*), intent(in) :: archivekeys
  87. character(len=*), intent(in) :: paramkey
  88. type(TDate), intent(in) :: tref, t1, t2
  89. integer, intent(out) :: status
  90. ! --- const --------------------------------------
  91. character(len=*), parameter :: rname = mname//'/mf_Init'
  92. ! --- local --------------------------------------
  93. ! --- begin --------------------------------
  94. ! store directory:
  95. mf%dir = trim(dir)//'/'
  96. !
  97. ! extract fields from archivekey :
  98. ! tres=fc024up1tr3
  99. !
  100. !mf%mdir = 'no_mdir'
  101. !call goVarValue( archivekeys, ';', 'mdir', '=', mf%mdir, status )
  102. !if (status>0) then; write (gol,'("in ",a)') rname; call goErr; status=1; return; end if
  103. ! one specific set only yet ...
  104. mf%treskey = 'fc024up1tr3'
  105. ! store reftime:
  106. mf%tref = tref
  107. ! set file specific stuff:
  108. ! o list of parameters in file
  109. ! o file extension
  110. select case ( paramkey )
  111. ! * ncep cray binary files
  112. case ( 'oro', 'LNSP', 'Tv', 'D', 'VO', 'Q' )
  113. mf%paramkeys = '-oro-LNSP-Tv-D-VO-Q-'
  114. mf%ext = 'SF'
  115. ! * ncep grib files
  116. case ( 'lsm', 'albedo', 'sr', 'sps', 'ci', 'skt', 'sst', &
  117. 'u10m', 'v10m', 'slhf', 'sshf', 'sstr', 'lsp', 'cp', &
  118. 'blh' )
  119. mf%paramkeys = '-'
  120. mf%paramkeys = trim(mf%paramkeys)//'lsm-albedo-sr-sps-ci-skt-sst-'
  121. mf%paramkeys = trim(mf%paramkeys)//'u10m-v10m-slhf-sshf-sstr-lsp-cp-'
  122. mf%paramkeys = trim(mf%paramkeys)//'blh-'
  123. mf%ext = 'SFLUXGrbF'
  124. ! * error ...
  125. case default
  126. write (gol,'("do not know file for param ",a)') paramkey; call goErr
  127. write (gol,'("in ",a)') rname; call goErr; status=1; return
  128. end select
  129. ! files are valid for instant times only;
  130. ! averaged fields are stored at the end of the interval (t2) thus valid for (t1,t2]
  131. if ( IsAnyDate(t1) .and. IsAnyDate(t2) ) then
  132. mf%trange(1) = tref
  133. mf%trange(2) = tref
  134. else
  135. mf%trange(1) = t1 + IncrDate( mili=1 )
  136. mf%trange(2) = t2
  137. end if
  138. ! ok
  139. status = 0
  140. end subroutine mf_Init
  141. ! ***
  142. subroutine mf_Done( mf, status )
  143. ! --- in/out ------------------------------------
  144. type(TMeteoFile_ncep_gfs), intent(inout) :: mf
  145. integer, intent(out) :: status
  146. ! --- const --------------------------------------
  147. character(len=*), parameter :: rname = mname//'/mf_Done'
  148. ! --- begin -------------------------------------
  149. ! files have been closed in ReadRecord/WriteRecord
  150. ! ok
  151. status = 0
  152. end subroutine mf_Done
  153. ! ***
  154. subroutine mf_Get( mf, status, trange1, trange2, paramkeys )
  155. use GO, only : TDate
  156. ! --- in/out ----------------------------
  157. type(TMeteoFile_ncep_gfs), intent(in) :: mf
  158. integer, intent(out) :: status
  159. type(TDate), intent(out), optional :: trange1, trange2
  160. character(len=*), intent(out), optional :: paramkeys
  161. ! --- const --------------------------------------
  162. character(len=*), parameter :: rname = mname//'/mf_Get'
  163. ! --- local --------------------------------
  164. ! --- begin --------------------------------
  165. ! time range:
  166. if ( present(trange1) ) trange1 = mf%trange(1)
  167. if ( present(trange2) ) trange2 = mf%trange(2)
  168. ! parameter names:
  169. if ( present(paramkeys) ) paramkeys = mf%paramkeys
  170. ! ok
  171. status = 0
  172. end subroutine mf_Get
  173. ! ***
  174. !
  175. ! time arguments:
  176. ! tref , any, any : oro and other constant fields
  177. ! dummy, t1 , t1 : file name with t1
  178. ! dummy, t1 , t2 : file name with t2
  179. !
  180. subroutine GetTime( treskey, tref, t1, t2, status, tfile, fcst_hr, aver_hr, taver )
  181. use go, only : TDate, Get, NewDate, IncrDate, IsAnyDate, operator(>), operator(-), operator(+)
  182. ! --- in/out ----------------------------------
  183. character(len=*), intent(in) :: treskey
  184. type(TDate), intent(in) :: tref, t1, t2
  185. integer, intent(out) :: status
  186. type(TDate), intent(out), optional :: tfile
  187. integer, intent(out), optional :: fcst_hr
  188. integer, intent(out), optional :: aver_hr
  189. type(TDate), intent(out), optional :: taver
  190. ! --- const --------------------------------------
  191. character(len=*), parameter :: rname = mname//'/GetTime'
  192. ! --- local -----------------------------------
  193. integer :: year, month, day, hour
  194. integer :: dd, h0, dh, dha
  195. type(TDate) :: tfc
  196. ! --- begin -----------------------------------
  197. !
  198. ! set time values
  199. !
  200. ! resolution depends on data set:
  201. select case ( treskey )
  202. !case ( 'rasch-bundy' )
  203. !
  204. ! ! set forecast hour and time step:
  205. ! select case ( hour )
  206. ! case ( 00 ) ; h0 = 00 ; dh = 00
  207. ! case ( 03 ) ; h0 = 00 ; dh = 03
  208. ! case ( 06 ) ; h0 = 06 ; dh = 00
  209. ! case ( 09 ) ; h0 = 06 ; dh = 03
  210. ! case ( 12 ) ; h0 = 12 ; dh = 00
  211. ! case ( 15 ) ; h0 = 12 ; dh = 03
  212. ! case ( 18 ) ; h0 = 12 ; dh = 06
  213. ! case ( 21 ) ; h0 = 12 ; dh = 09
  214. ! case default
  215. ! write (gol,'("unsupported hour : ",i3)') hour; call goErr
  216. ! write (gol,'("in ",a)') rname; call goErr; status=1; return
  217. ! end select
  218. !
  219. ! ** Rasch-Bundy set ; combination of 'forecast_0' and forecast_1 files
  220. !
  221. ! instant field : analysis or forecast
  222. ! interval [t1,t2] : forecast file stamped at t2
  223. !
  224. case ( 'fc024up1tr3' )
  225. if ( IsAnyDate(t1) .and. IsAnyDate(t2) ) then
  226. ! constant field; extract time stuff from tref:
  227. call Get( tref, year=year, month=month, day=day, hour=hour )
  228. else
  229. ! extract time stuff from t2, which is either t1 or end of interval
  230. call Get( t2, year=year, month=month, day=day, hour=hour )
  231. ! accumulated field for 12:00 is in files 12.24 ...
  232. if ( (t2>t1) .and. (hour==12) ) hour = 12+24
  233. end if
  234. ! set forecast hour, time step:
  235. select case ( hour )
  236. case ( 12 ) ; dd = 0 ; h0 = 12 ; dh = 00 ; dha=00
  237. case ( 15 ) ; dd = 0 ; h0 = 12 ; dh = 03 ; dha=00
  238. case ( 18 ) ; dd = 0 ; h0 = 12 ; dh = 06 ; dha=00
  239. case ( 21 ) ; dd = 0 ; h0 = 12 ; dh = 09 ; dha=06
  240. case ( 00 ) ; dd = 1 ; h0 = 12 ; dh = 12 ; dha=06
  241. case ( 03 ) ; dd = 1 ; h0 = 12 ; dh = 15 ; dha=12
  242. case ( 06 ) ; dd = 1 ; h0 = 12 ; dh = 18 ; dha=12
  243. case ( 09 ) ; dd = 1 ; h0 = 12 ; dh = 21 ; dha=18
  244. case ( 12+24 ) ; dd = 1 ; h0 = 12 ; dh = 24 ; dha=18
  245. case default
  246. write (gol,'("unsupported hour : ",i3)') hour; call goErr
  247. write (gol,'("in ",a)') rname; call goErr; status=1; return
  248. end select
  249. case default
  250. write (gol,'("unsupported time resolution key : ",a)') treskey; call goErr
  251. write (gol,'("in ",a)') rname; call goErr; status=1; return
  252. end select
  253. ! fill start of forecast:
  254. tfc = NewDate( year=year, month=month, day=day, hour=h0 ) - IncrDate(day=dd)
  255. !
  256. ! return output
  257. !
  258. ! return time for filename ?
  259. if ( present(tfile) ) tfile = tfc
  260. ! return time step ?
  261. if ( present(fcst_hr) ) fcst_hr = dh
  262. ! return start of averaging interval ?
  263. if ( present(aver_hr) ) aver_hr = dha
  264. ! start of aver interval ?
  265. if ( present(taver) ) taver = tfc + IncrDate(hour=dha)
  266. ! ok
  267. status = 0
  268. end subroutine GetTime
  269. ! ***
  270. ! Return a field given parameter name, time, etc.
  271. ! Only one of grid types is filled!
  272. subroutine mf_ReadRecord( mf, paramkey, t1, t2, nuv, nw, &
  273. gridtype, levi, &
  274. lli, ll, sp_ll, &
  275. ggi, gg, sp_gg, &
  276. shi, sh, lnsp_sh, &
  277. tmi, status )
  278. use parray , only : pa_Init, pa_Done
  279. use GO , only : TDate
  280. use Grid , only : TLevelInfo
  281. use Grid , only : TllGridInfo, TggGridInfo, TshGridInfo
  282. use tmm_info , only : TMeteoInfo
  283. ! --- in/out -------------------------------
  284. type(TMeteoFile_ncep_gfs), intent(inout) :: mf
  285. character(len=*), intent(in) :: paramkey
  286. type(TDate), intent(in) :: t1, t2
  287. character(len=1), intent(in) :: nuv
  288. character(len=1), intent(in) :: nw
  289. character(len=2), intent(out) :: gridtype
  290. type(TLevelInfo), intent(out) :: levi
  291. type(TllGridInfo), intent(inout) :: lli
  292. real, pointer :: ll(:,:,:)
  293. real, pointer :: sp_ll(:,:)
  294. type(TggGridInfo), intent(inout) :: ggi
  295. real, pointer :: gg(:,:)
  296. real, pointer :: sp_gg(:)
  297. type(TshGridInfo), intent(inout) :: shi
  298. complex, pointer :: sh(:,:)
  299. complex, pointer :: lnsp_sh(:)
  300. type(TMeteoInfo), intent(out) :: tmi
  301. integer, intent(out) :: status
  302. ! --- const --------------------------------------
  303. character(len=*), parameter :: rname = mname//'/mf_ReadRecord'
  304. ! --- local -------------------------------
  305. real, pointer :: ll2(:,:,:)
  306. real, pointer :: gg2(:,:)
  307. complex, pointer :: sh2(:,:)
  308. ! --- begin ---------------------------------
  309. ! combined or other special field ?
  310. select case ( paramkey )
  311. !
  312. ! total surface stress : sstr^2 = ewss^2 + nsss^2
  313. !
  314. case ( 'sstr' )
  315. ! ! read first field:
  316. ! call mf_ReadRecord_1( mf, 'ewss', t1, t2, nuv, nw, gridtype, levi, &
  317. ! lli, ll, sp_ll, ggi, gg, sp_gg, shi, sh, lnsp_sh, &
  318. ! tmi, status )
  319. ! IF_NOTOK_RETURN(status=1)
  320. !
  321. ! ! init pointer:
  322. ! call pa_Init( ll2 ) ; call pa_Init( gg2 ) ; call pa_Init( sh2 )
  323. !
  324. ! ! read second field:
  325. ! call mf_ReadRecord_1( mf, 'nsss', t1, t2, nuv, nw, gridtype, levi, &
  326. ! lli, ll2, sp_ll, ggi, gg2, sp_gg, shi, sh2, lnsp_sh, &
  327. ! tmi, status )
  328. ! IF_NOTOK_RETURN(status=1)
  329. !
  330. ! ! process:
  331. ! select case ( gridtype )
  332. ! case ( 'll' ) ; ll = sqrt( ll**2 + ll2**2 )
  333. ! case ( 'gg' ) ; gg = sqrt( gg**2 + gg2**2 )
  334. ! case default
  335. ! write (gol,'("unsupported gridtype for sstr :",a)') gridtype; call goErr
  336. ! write (gol,'("in ",a)') rname; call goErr; status=1; return
  337. ! end select
  338. !
  339. ! ! clear pointers:
  340. ! call pa_Done( ll2 ) ; call pa_Done( gg2 ) ; call pa_Done( sh2 )
  341. ! read other time average field to setup output grid:
  342. call mf_ReadRecord_1( mf, 'slhf', t1, t2, nuv, nw, gridtype, levi, &
  343. lli, ll, sp_ll, ggi, gg, sp_gg, shi, sh, lnsp_sh, &
  344. tmi, status )
  345. IF_NOTOK_RETURN(status=1)
  346. ! dummy ...
  347. write (*,'(" WARNING - no surface stress in rasch-bundy set; use constant value ...")')
  348. gg = 0.1 ! N/m2
  349. !
  350. ! sea surface temperature : skin temperture * sea-mask
  351. !
  352. case ( 'sst' )
  353. ! read skin temperature:
  354. call mf_ReadRecord_1( mf, 'skt', t1, t2, nuv, nw, gridtype, levi, &
  355. lli, ll, sp_ll, ggi, gg, sp_gg, shi, sh, lnsp_sh, &
  356. tmi, status )
  357. IF_NOTOK_RETURN(status=1)
  358. ! init pointers:
  359. call pa_Init( ll2 ) ; call pa_Init( gg2 ) ; call pa_Init( sh2 )
  360. ! read land-sea mask:
  361. call mf_ReadRecord_1( mf, 'lsm', t1, t2, nuv, nw, gridtype, levi, &
  362. lli, ll2, sp_ll, ggi, gg2, sp_gg, shi, sh2, lnsp_sh, &
  363. tmi, status )
  364. IF_NOTOK_RETURN(status=1)
  365. ! process:
  366. select case ( gridtype )
  367. case ( 'll' ) ; ll = ll * max( 0.0, 1.0 - ll2 )
  368. case ( 'gg' ) ; gg = gg * max( 0.0, 1.0 - gg2 )
  369. case default
  370. write (gol,'("unsupported gridtype for sst :",a)') gridtype; call goErr
  371. write (gol,'("in ",a)') rname; call goErr; status=1; return
  372. end select
  373. ! clear pointers:
  374. call pa_Done( ll2 ) ; call pa_Done( gg2 ) ; call pa_Done( sh2 )
  375. !
  376. ! not available ...
  377. !
  378. case ( 'sr' )
  379. ! read land-sea mask to setup grids:
  380. call mf_ReadRecord_1( mf, 'lsm', t1, t2, nuv, nw, gridtype, levi, &
  381. lli, ll, sp_ll, ggi, gg, sp_gg, shi, sh, lnsp_sh, &
  382. tmi, status )
  383. IF_NOTOK_RETURN(status=1)
  384. ! dummy ...
  385. write (*,'(" WARNING - no surface roughness in rasch-bundy set; use constant value ...")')
  386. gg = 0.001 ! m
  387. !
  388. ! no specials ...
  389. !
  390. case default
  391. call mf_ReadRecord_1( mf, paramkey, t1, t2, nuv, nw, gridtype, levi, &
  392. lli, ll, sp_ll, ggi, gg, sp_gg, shi, sh, lnsp_sh, &
  393. tmi, status )
  394. IF_NOTOK_RETURN(status=1)
  395. end select
  396. ! ok
  397. status = 0
  398. end subroutine mf_ReadRecord
  399. ! ***
  400. subroutine mf_ReadRecord_1( mf, paramkey, t1, t2, nuv, nw, &
  401. gridtype, levi, &
  402. lli, ll, sp_ll, &
  403. ggi, gg, sp_gg, &
  404. shi, sh, lnsp_sh, &
  405. tmi, status )
  406. use parray , only : pa_Init, pa_Done
  407. use GO , only : TDate, IncrDate, operator(<), operator(+), operator(-), rTotal, wrtgol
  408. use Grid , only : TLevelInfo
  409. use Grid , only : TllGridInfo, TggGridInfo, TshGridInfo
  410. use tmm_info , only : TMeteoInfo
  411. ! --- in/out -------------------------------
  412. type(TMeteoFile_ncep_gfs), intent(inout) :: mf
  413. character(len=*), intent(in) :: paramkey
  414. type(TDate), intent(in) :: t1, t2
  415. character(len=1), intent(in) :: nuv
  416. character(len=1), intent(in) :: nw
  417. character(len=2), intent(out) :: gridtype
  418. type(TLevelInfo), intent(out) :: levi
  419. type(TllGridInfo), intent(inout) :: lli
  420. real, pointer :: ll(:,:,:)
  421. real, pointer :: sp_ll(:,:)
  422. type(TggGridInfo), intent(inout) :: ggi
  423. real, pointer :: gg(:,:)
  424. real, pointer :: sp_gg(:)
  425. type(TshGridInfo), intent(inout) :: shi
  426. complex, pointer :: sh(:,:)
  427. complex, pointer :: lnsp_sh(:)
  428. type(TMeteoInfo), intent(out) :: tmi
  429. integer, intent(out) :: status
  430. ! --- const --------------------------------------
  431. character(len=*), parameter :: rname = mname//'/mf_ReadRecord_1'
  432. ! --- local -------------------------------
  433. type(TDate) :: ta
  434. real, pointer :: ll1(:,:,:)
  435. real, pointer :: gg1(:,:)
  436. complex, pointer :: sh1(:,:)
  437. real :: dt, dt1
  438. integer :: aver_hr
  439. ! --- begin ---------------------------------
  440. ! accumulated field ?
  441. select case ( paramkey )
  442. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  443. ! accumulated fields
  444. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  445. case ( 'lsp', 'cp', 'sf', 'sshf', 'slhf', 'ssr', 'ewss', 'nsss' )
  446. ! should be a time interval ...
  447. if ( .not. (t1 < t2) ) then
  448. write (gol,'("accumulated fields requires time interval:")'); call goErr
  449. write (gol,'(" paramkey : ",a)') paramkey; call goErr
  450. call wrtgol( ' t1 : ', t1 ); call goErr
  451. call wrtgol( ' t2 : ', t2 ); call goErr
  452. write (gol,'("in ",a)') rname; call goErr; status=1; return
  453. end if
  454. ! fields are averaged over intervals of 3 or 6 hour; set begin of averaging interval:
  455. call GetTime( mf%treskey, mf%tref, t1, t2, status, taver=ta )
  456. ! read field accumulated over [ta,t2] :
  457. call mf_ReadRecord_2( mf, paramkey, ta, t2, nuv, nw, &
  458. gridtype, levi, &
  459. lli, ll, sp_ll, &
  460. ggi, gg, sp_gg, &
  461. shi, sh, lnsp_sh, &
  462. tmi, status )
  463. IF_NOTOK_RETURN(status=1)
  464. ! substract [tf1,t1] if necessary:
  465. if ( ta < t1 ) then
  466. ! init pointer:
  467. call pa_Init( ll1 )
  468. call pa_Init( gg1 )
  469. call pa_Init( sh1 )
  470. ! read field accumulated over [ta,t1] :
  471. call mf_ReadRecord_2( mf, paramkey, ta, t1, nuv, nw, &
  472. gridtype, levi, &
  473. lli, ll1, sp_ll, &
  474. ggi, gg1, sp_gg, &
  475. shi, sh1, lnsp_sh, &
  476. tmi, status )
  477. IF_NOTOK_RETURN(status=1)
  478. ! ll contains aver over [ta,t2], ll1 contains aver over [ta,t1]
  479. dt = rTotal( t2 - ta, 'sec' )
  480. dt1 = rTotal( t1 - ta, 'sec' )
  481. ! substract accumulations, return average:
  482. select case ( gridtype )
  483. case ( 'll' ) ; ll = ( ll*dt - ll1*dt1 ) / rTotal( t2 - t1, 'sec' )
  484. case ( 'gg' ) ; gg = ( gg*dt - gg1*dt1 ) / rTotal( t2 - t1, 'sec' )
  485. case ( 'sh' ) ; sh = ( sh*dt - sh1*dt1 ) / rTotal( t2 - t1, 'sec' )
  486. case default
  487. write (gol,'("unsupported gridtype for substract :",a)') gridtype; call goErr
  488. write (gol,'("in ",a)') rname; call goErr; status=1; return
  489. end select
  490. ! clear pointers:
  491. call pa_Done( ll1 )
  492. call pa_Done( gg1 )
  493. call pa_Done( sh1 )
  494. end if
  495. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  496. ! instantaneous fields
  497. ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  498. case default
  499. call mf_ReadRecord_2( mf, paramkey, t1, t2, nuv, nw, &
  500. gridtype, levi, &
  501. lli, ll, sp_ll, &
  502. ggi, gg, sp_gg, &
  503. shi, sh, lnsp_sh, &
  504. tmi, status )
  505. IF_NOTOK_RETURN(status=1)
  506. end select
  507. ! ok
  508. status = 0
  509. end subroutine mf_ReadRecord_1
  510. ! ***
  511. ! Return a field given parameter name, time, etc.
  512. ! Only one of grid types is filled!
  513. subroutine mf_ReadRecord_2( mf, paramkey, t1, t2, nuv, nw, &
  514. gridtype, levi, &
  515. lli, ll, sp_ll, &
  516. ggi, gg, sp_gg, &
  517. shi, sh, lnsp_sh, &
  518. tmi, status )
  519. use PArray , only : pa_Init, pa_Done, pa_SetShape
  520. use binas , only : grav
  521. use GO , only : TDate, Get
  522. use Grid , only : TLevelInfo, Init, Done
  523. use Grid , only : TllGridInfo, TggGridInfo, TshGridInfo
  524. use Grid , only : Interpol
  525. use tmm_info , only : TMeteoInfo, Init, AddHistory
  526. ! --- in/out -------------------------------
  527. type(TMeteoFile_ncep_gfs), intent(inout) :: mf
  528. character(len=*), intent(in) :: paramkey
  529. type(TDate), intent(in) :: t1, t2
  530. character(len=1), intent(in) :: nuv
  531. character(len=1), intent(in) :: nw
  532. character(len=2), intent(out) :: gridtype
  533. type(TLevelInfo), intent(out) :: levi
  534. type(TllGridInfo), intent(inout) :: lli
  535. real, pointer :: ll(:,:,:)
  536. real, pointer :: sp_ll(:,:)
  537. type(TggGridInfo), intent(inout) :: ggi
  538. real, pointer :: gg(:,:)
  539. real, pointer :: sp_gg(:)
  540. type(TshGridInfo), intent(inout) :: shi
  541. complex, pointer :: sh(:,:)
  542. complex, pointer :: lnsp_sh(:)
  543. type(TMeteoInfo), intent(out) :: tmi
  544. integer, intent(out) :: status
  545. ! --- const --------------------------------------
  546. character(len=*), parameter :: rname = mname//'/mf_ReadRecord_2'
  547. ! --- local -------------------------------
  548. type(TDate) :: tfile
  549. integer :: year, month, day, hour
  550. integer :: fcst_hr
  551. complex, pointer :: sh1(:)
  552. real, pointer :: gg1(:)
  553. character(len=64) :: key
  554. ! --- begin ---------------------------------
  555. ! no fluxes through boundaries ...
  556. if ( nuv /= 'n' ) then
  557. write (gol,'("unsupported nuv key : ",a)') nuv; call goErr
  558. write (gol,'("in ",a)') rname; call goErr; status=1; return
  559. end if
  560. ! init info; example of history: model==msc;sh==159;nlev==60
  561. call Init( tmi, paramkey, 'unknown', status )
  562. call AddHistory( tmi, 'model==ncep gfs', status )
  563. call AddHistory( tmi, 'archive==ncar', status )
  564. !
  565. ! write filename
  566. !
  567. ! extract times used in filename:
  568. call GetTime( mf%treskey, mf%tref, t1, t2, status, tfile=tfile, fcst_hr=fcst_hr )
  569. IF_NOTOK_RETURN(status=1)
  570. ! write filename:
  571. ! <dir>/20040614/20040614.00.00.SF
  572. ! <dir>/20040614/20040614.00.00.SFLUXGrbF
  573. !
  574. call Get( tfile, year=year, month=month, day=day, hour=hour )
  575. write (mf%fname,'(i4.4,2i2.2,"-",i4.4,2i2.2,".",i2.2,".",i2.2,".",a)') &
  576. year, month, day, year, month, day, hour, fcst_hr, trim(mf%ext)
  577. !
  578. ! read from cray binary or grib file
  579. !
  580. select case ( paramkey )
  581. !
  582. ! * 2d spectral fields
  583. !
  584. case ( 'oro', 'LNSP' )
  585. ! output is spectral field
  586. gridtype = 'sh'
  587. ! read 2d field
  588. call Read_Bin_2d( trim(mf%dir)//trim(mf%fname), paramkey, t1, shi, sh1, status )
  589. IF_NOTOK_RETURN(status=1)
  590. ! copy from rank1 array:
  591. call pa_SetShape( sh, shi%np, 1 )
  592. sh(:,1) = sh1
  593. call pa_Done( sh1 )
  594. ! unit conversion:
  595. select case ( paramkey )
  596. !
  597. ! factor for conversion from cbar to Pa :
  598. ! [Pa] = [cbar] * 1e-2 [bar/cbar] * 1e5 [Pa/bar] = [mbar] * 1e3
  599. ! add to first complex coeff:
  600. ! sp * fac = exp( lnsp + nlog(fac) )
  601. ! = exp( {sum_i=1,n c_i p_i} + nlog(fac) )
  602. ! = exp( c_1 + {sum_i=2,n c_i p_i} + nlog(fac) )
  603. case ( 'LNSP' )
  604. sh(1,1) = sh(1,1) + cmplx(log(1.0e3),0.0)
  605. !
  606. ! ncep oro in in [m], should be [m][g] = [m m/s2]
  607. case ( 'oro' )
  608. sh(:,1) = sh(:,1) * grav
  609. end select
  610. ! dummy levels
  611. call Init( levi, 1, (/0.0,0.0/), (/1.0,0.0/), status )
  612. IF_NOTOK_RETURN(status=1)
  613. ! info ...
  614. call AddHistory( tmi, 'file=='//trim(mf%fname), status )
  615. write (key,'("sh==",i4.4)') shi%T
  616. call AddHistory( tmi, trim(key), status )
  617. !
  618. ! * 3d spectral fields
  619. !
  620. case ( 'VO', 'D', 'Tv', 'Q' )
  621. ! output is spectral field
  622. gridtype = 'sh'
  623. ! read 3d field
  624. call Read_Bin_3d( trim(mf%dir)//trim(mf%fname), paramkey, t1, levi, shi, sh, status )
  625. IF_NOTOK_RETURN(status=1)
  626. ! unit conversion:
  627. select case ( paramkey )
  628. ! For some reason, the u/v/w from VO/D needs a factor -1 ...
  629. ! The minus is probably caused by the upwards coordinate system of ncep
  630. ! instead of the downward from ecmwf.
  631. case ( 'VO', 'D' )
  632. sh = - sh
  633. end select
  634. ! read lnsp:
  635. call Read_Bin_2d( trim(mf%dir)//trim(mf%fname), 'LNSP', t1, shi, lnsp_sh, status )
  636. IF_NOTOK_RETURN(status=1)
  637. ! unit conversion:
  638. lnsp_sh(1) = lnsp_sh(1) + cmplx(log(1.0e3),0.0)
  639. ! info ...
  640. call AddHistory( tmi, 'file=='//trim(mf%fname), status )
  641. write (key,'("sh==",i4.4)') shi%T
  642. call AddHistory( tmi, trim(key), status )
  643. !
  644. ! * 2d surface fields
  645. !
  646. case ( 'lsm', 'albedo', 'sr', 'sps', 'ci', 'skt', 'u10m', 'v10m', 'slhf', 'sshf', 'ewss', 'nsss', 'lsp', 'cp' )
  647. ! output is gaussian grid
  648. gridtype = 'gg'
  649. ! read 2d field
  650. call Read_Grib_2d( mf, paramkey, t1, t2, ggi, gg1, status )
  651. IF_NOTOK_RETURN(status=1)
  652. ! copy from rank1 array:
  653. call pa_SetShape( gg, ggi%np, 1 )
  654. gg(:,1) = gg1
  655. call pa_Done( gg1 )
  656. ! unit conversion:
  657. select case ( paramkey )
  658. !
  659. ! for some probably historical reaseon, TM expects land/sea mask in % ...
  660. case ( 'lsm' )
  661. gg(:,1) = gg(:,1) * 100.0 ! 0-1 -> %
  662. !
  663. ! fluxes downward (ecmwf direction) instead of upward (ncep direction)
  664. case ( 'slhf', 'sshf' )
  665. gg(:,1) = - gg(:,1)
  666. !
  667. ! kg water / m2 / s -> m water / s
  668. ! With density of 998 kg water / m3 : kg/m2/s / (kg/m3) = m/s
  669. case ( 'lsp', 'cp' )
  670. gg(:,1) = gg(:,1) / 998.0 ! m water / s
  671. end select
  672. ! dummy levels
  673. call Init( levi, 1, (/0.0,0.0/), (/1.0,0.0/), status )
  674. IF_NOTOK_RETURN(status=1)
  675. ! info ...
  676. call AddHistory( tmi, 'file=='//trim(mf%fname), status )
  677. write (key,'("gg==",i4.4)') ggi%N
  678. call AddHistory( tmi, trim(key), status )
  679. !
  680. ! * ????
  681. !
  682. case default
  683. write (gol,'("unsupported paramkey : ",a)') paramkey; call goErr
  684. write (gol,'("in ",a)') rname; call goErr; status=1; return
  685. end select
  686. !
  687. ! ~~~ end
  688. !
  689. ! ok
  690. status = 0
  691. end subroutine mf_ReadRecord_2
  692. ! ***
  693. subroutine Check_Bin_Time( ncb, t, status )
  694. use go , only : TDate, NewDate, IncrDate, operator(+), operator(/=), IsAnyDate, wrtgol
  695. use file_ncb, only : TNcepCrayBin
  696. ! --- in/out -----------------------------------
  697. type(TNcepCrayBin), intent(in) :: ncb
  698. type(TDate), intent(in) :: t
  699. integer, intent(out) :: status
  700. ! --- const --------------------------------------
  701. character(len=*), parameter :: rname = mname//'/Check_Bin_Time'
  702. ! --- local -----------------------------------
  703. type(TDate) :: tf
  704. ! --- begin -----------------------------------
  705. ! trap any date ..
  706. if ( IsAnyDate(t) ) then
  707. ! ok
  708. status = 0; return
  709. end if
  710. ! time in bin file:
  711. tf = NewDate( time4=ncb%idate ) + IncrDate( hour=ncb%fcst_hr )
  712. ! check:
  713. if ( tf /= t ) then
  714. write (gol,'("wrong time in binary file:")'); call goErr
  715. call wrtgol( ' file : ', tf ); call goErr
  716. call wrtgol( ' t : ', t ); call goErr
  717. write (gol,'("in ",a)') rname; call goErr; status=1; return
  718. end if
  719. ! ok
  720. status = 0
  721. end subroutine Check_Bin_Time
  722. ! ***
  723. subroutine Read_Bin_2d( fname, paramkey, t1, shi, sh, status )
  724. use parray , only : pa_SetShape
  725. use go , only : TDate
  726. use grid , only : TshGridInfo, Init
  727. use file_ncb, only : TNcepCrayBin, Init, Done, ReadRecord
  728. ! --- in/out -----------------------------------
  729. character(len=*), intent(in) :: fname
  730. character(len=*), intent(in) :: paramkey
  731. type(TDate), intent(in) :: t1
  732. type(TshGridInfo), intent(out) :: shi
  733. complex, pointer :: sh(:)
  734. integer, intent(out) :: status
  735. ! --- const --------------------------------------
  736. character(len=*), parameter :: rname = mname//'/Read_Bin_2d'
  737. ! --- local -----------------------------------
  738. type(TNcepCrayBin) :: ncb
  739. ! --- begin -----------------------------------
  740. ! open file
  741. call Init( ncb, fname, status )
  742. IF_NOTOK_RETURN(status=1)
  743. ! check time:
  744. call Check_Bin_Time( ncb, t1, status )
  745. IF_NOTOK_RETURN(status=1)
  746. ! init output grid definition:
  747. call Init( shi, ncb%shT, status )
  748. IF_NOTOK_RETURN(status=1)
  749. ! allocate output grid:
  750. call pa_SetShape( sh, shi%np )
  751. ! read record:
  752. call ReadRecord( ncb, paramkey, sh, status )
  753. IF_NOTOK_RETURN(status=1)
  754. ! convert from NCEP spectral coeff to ECMWF spectral coeff:
  755. sh = sh / sqrt(2.0)
  756. ! close file:
  757. call Done( ncb, status )
  758. IF_NOTOK_RETURN(status=1)
  759. ! ok
  760. status = 0
  761. end subroutine Read_Bin_2d
  762. ! ***
  763. subroutine Read_Bin_3d( fname, paramkey, t1, levi, shi, sh, status )
  764. use parray , only : pa_SetShape
  765. use go , only : TDate
  766. use grid , only : TshGridInfo, Init
  767. use grid , only : TLevelInfo, Init
  768. use file_ncb, only : TNcepCrayBin, Init, Done, ReadRecord
  769. ! --- in/out -----------------------------------
  770. character(len=*), intent(in) :: fname
  771. character(len=*), intent(in) :: paramkey
  772. type(TDate), intent(in) :: t1
  773. type(TLevelInfo), intent(out) :: levi
  774. type(TshGridInfo), intent(out) :: shi
  775. complex, pointer :: sh(:,:)
  776. integer, intent(out) :: status
  777. ! --- const --------------------------------------
  778. character(len=*), parameter :: rname = mname//'/Read_Bin_2d'
  779. ! --- local -----------------------------------
  780. type(TNcepCrayBin) :: ncb
  781. ! --- begin -----------------------------------
  782. ! open file
  783. call Init( ncb, fname, status )
  784. IF_NOTOK_RETURN(status=1)
  785. ! check time:
  786. call Check_Bin_Time( ncb, t1, status )
  787. IF_NOTOK_RETURN(status=1)
  788. ! init levels
  789. call Init( levi, ncb%nlev, ncb%sigma_half*0.0, ncb%sigma_half, status )
  790. IF_NOTOK_RETURN(status=1)
  791. ! init output grid definition:
  792. call Init( shi, ncb%shT, status )
  793. IF_NOTOK_RETURN(status=1)
  794. ! allocate output grid:
  795. call pa_SetShape( sh, shi%np, ncb%nlev )
  796. ! read record:
  797. call ReadRecord( ncb, paramkey, sh, status )
  798. IF_NOTOK_RETURN(status=1)
  799. ! convert from NCEP spectral coeff to ECMWF spectral coeff:
  800. sh = sh / sqrt(2.0)
  801. ! close file:
  802. call Done( ncb, status )
  803. IF_NOTOK_RETURN(status=1)
  804. ! ok
  805. status = 0
  806. end subroutine Read_Bin_3d
  807. ! ***
  808. subroutine Read_Grib_2d( mf, paramkey, t1, t2, ggi, gg, status )
  809. use parray , only : pa_SetShape
  810. use go , only : TDate, iTotal, Get, operator(==), operator(-), IsAnyDate
  811. use grid , only : TggGridInfo, Init
  812. use file_ncg, only : TNcepGrib, Init, Done, ReadRecord, Get
  813. use file_ncg, only : CheckRecord
  814. ! --- in/out -----------------------------------
  815. type(TMeteoFile_ncep_gfs), intent(inout) :: mf
  816. character(len=*), intent(in) :: paramkey
  817. type(TDate), intent(in) :: t1, t2
  818. type(TggGridInfo), intent(out) :: ggi
  819. real, pointer :: gg(:)
  820. integer, intent(out) :: status
  821. ! --- const --------------------------------------
  822. character(len=*), parameter :: rname = mname//'/Read_Grib_2d'
  823. ! --- local -----------------------------------
  824. type(TNcepGrib) :: ncg
  825. character(len=5) :: param
  826. type(TDate) :: tref
  827. integer :: reftime(5)
  828. integer :: timerange(4), dh, dha
  829. character(len=4) :: levtype
  830. integer :: level
  831. integer :: ggN
  832. integer :: irec
  833. ! --- begin -----------------------------------
  834. !! open file
  835. !call Init( ncg, trim(mf%dir)//trim(mf%fname), status )
  836. !IF_NOTOK_RETURN(status=1)
  837. ! For some reason, opening a new with the same file unit
  838. ! (while the old one is closed) in the same program gives errors ...
  839. ! Thus, new file unit for each new file name ...
  840. if ( trim(mf%dir)//trim(mf%fname) /= adhoc_fname ) then
  841. adhoc_fu = adhoc_fu + 1
  842. adhoc_fname = trim(mf%dir)//trim(mf%fname)
  843. end if
  844. call Init( ncg, adhoc_fu, trim(mf%dir)//trim(mf%fname), status )
  845. IF_NOTOK_RETURN(status=1)
  846. ! set grib param key
  847. select case ( paramkey )
  848. case ( 'lsm' ) ; param = 'LAND' ; levtype = 'SFC' ; level = 0 ; irec=32
  849. case ( 'sps' ) ; param = 'PRES' ; levtype = 'SFC' ; level = 0 ; irec=38
  850. case ( 'ci' ) ; param = 'ICEC' ; levtype = 'SFC' ; level = 0 ; irec=33
  851. case ( 'albedo' ) ; param = 'ALBDO' ; levtype = 'SFC' ; level = 0 ; irec=48
  852. case ( 'skt' ) ; param = 'TMP' ; levtype = 'SFC' ; level = 0 ; irec=5
  853. case ( 'u10m' ) ; param = 'UGRD' ; levtype = 'HTGL' ; level = 10 ; irec=34
  854. case ( 'v10m' ) ; param = 'VGRD' ; levtype = 'HTGL' ; level = 10 ; irec=35
  855. case ( 'slhf' ) ; param = 'LHTFL' ; levtype = 'SFC' ; level = 0 ; irec=4
  856. case ( 'sshf' ) ; param = 'SHTFL' ; levtype = 'SFC' ; level = 0 ; irec=3
  857. !case ( 'ewss' ) ; param = ''
  858. !case ( 'nsss' ) ; param = ''
  859. case ( 'lsp' ) ; param = 'PRATE' ; levtype = 'SFC' ; level = 0 ; irec=29
  860. case ( 'cp' ) ; param = 'CPRAT' ; levtype = 'SFC' ; level = 0 ; irec=30
  861. !case ( 'blh' ) ; param = 'HPBL'
  862. case default
  863. write (gol,'("unsupported paramkey ",a)') paramkey; call goErr
  864. write (gol,'("in ",a)') rname; call goErr; status=1; return
  865. end select
  866. ! no time at all, instant time or time average ?
  867. if ( IsAnyDate(t1) .and. IsAnyDate(t2) ) then
  868. ! no search ...
  869. reftime = -1
  870. timerange = -1
  871. else if ( t1 == t2 ) then
  872. ! extract ref time and time step:
  873. call GetTime( mf%treskey, t1, t1, t2, status, tfile=tref, fcst_hr=dh )
  874. IF_NOTOK_RETURN(status=1)
  875. ! set reftime and timerange arrays:
  876. call Get( tref, time5=reftime )
  877. timerange = (/1,dh,0,10/) ! hours, P1, P2, valid for reftime + P1
  878. else
  879. ! extract ref time and time step:
  880. call GetTime( mf%treskey, t1, t1, t2, status, tfile=tref, fcst_hr=dh, aver_hr=dha )
  881. IF_NOTOK_RETURN(status=1)
  882. ! set reftime and timerange arrays:
  883. call Get( tref, time5=reftime )
  884. timerange = (/1,dha,dh,3/) ! hours, P1, P2, aver over reftime + [P1,P2]
  885. end if
  886. ! >>> for some reason, this failes ... problems with w3 library ?
  887. !! search requested record:
  888. !call ReadRecord( ncg, status, param=param, reftime=reftime, timerange=timerange, &
  889. ! levtype=levtype, level=level )
  890. !IF_NOTOK_RETURN(status=1)
  891. !<<<
  892. ! >>> not so nice, but effective ...
  893. ! read record given specified message number:
  894. call ReadRecord( ncg, status, irec=irec )
  895. IF_NOTOK_RETURN(status=1)
  896. ! check contents:
  897. call CheckRecord( ncg, status, param=param, reftime=reftime, timerange=timerange, &
  898. levtype=levtype, level=level )
  899. IF_NOTOK_RETURN(status=1)
  900. ! <<<
  901. ! extract Gaussian grid number :
  902. call Get( ncg, status, ggN=ggN )
  903. IF_NOTOK_RETURN(status=1)
  904. ! init output grid, no reduced grid:
  905. call Init( ggi, ggN, .false., status )
  906. IF_NOTOK_RETURN(status=1)
  907. ! allocate output grid:
  908. call pa_SetShape( gg, ggi%np )
  909. ! extract field:
  910. call Get( ncg, status, gg=gg )
  911. IF_NOTOK_RETURN(status=1)
  912. ! close file:
  913. call Done( ncg, status )
  914. IF_NOTOK_RETURN(status=1)
  915. ! ok
  916. status = 0
  917. end subroutine Read_Grib_2d
  918. end module tmm_mf_ncep_gfs