io_hdf.F90 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077
  1. !#################################################################
  2. !
  3. ! NAME
  4. ! io_hdf
  5. !
  6. ! Subroutines to read arrays from / write arrays to HDF 4 files
  7. !
  8. !### macro's #####################################################
  9. !
  10. #define TRACEBACK write (gol,'("in ",a," (",a,", line",i5,")")') rname, __FILE__, __LINE__; call goErr
  11. #define IF_NOTOK_RETURN(action) if (status/=0) then; TRACEBACK; action; return; end if
  12. #define IF_ERROR_RETURN(action) if (status> 0) then; TRACEBACK; action; return; end if
  13. !
  14. #include "tm5.inc"
  15. !
  16. !#################################################################
  17. module io_hdf
  18. use GO, only : gol, goPr, goErr
  19. implicit none
  20. private
  21. public :: io_write
  22. public :: io_write3d_32d, io_write2d_32d, io_write3d_32dr, io_write2d_32dr, io_write3d_32, io_write4d_32
  23. public :: io_read3d_32, io_read4d_32
  24. public :: io_read2d_i16g, io_read2d_i16d
  25. public :: io_read3d_32d, io_read2d_32g, io_read3d_32g, io_read1d_32g
  26. public :: io_read4d_32g, io_read4d_32d, io_read3d_32dr
  27. public :: io_read2d_32d, io_read2d_32dr
  28. public :: io_read2d_64g
  29. public :: io_read3d_64g
  30. public :: DFNT_INT8, DFNT_INT16, DFNT_INT32, DFNT_INT64
  31. public :: DFNT_FLOAT32, DFNT_FLOAT64
  32. public :: DFACC_READ, DFACC_CREATE, DFACC_WRITE, DFNT_CHAR
  33. public :: SUCCEED, FAIL
  34. !
  35. interface operator (+)
  36. module procedure upper_case
  37. end interface
  38. interface io_write
  39. module procedure io_write4d_32
  40. module procedure io_write3d_32
  41. module procedure io_write2d_32
  42. module procedure io_write1d_32
  43. module procedure io_write2d_i32
  44. module procedure io_write2d_i16
  45. module procedure io_write1d_i16
  46. module procedure io_write4d_i32
  47. end interface
  48. include 'hdf.f90'
  49. logical,parameter :: okdebug = .false.
  50. integer,parameter :: comp_type = 1
  51. integer,parameter,dimension(1) :: comp_prm = (/ 1 /)
  52. contains
  53. subroutine io_write4d_32( sd_id, im, labim, jm, labjm, &
  54. lm, lablm, nt, labnt, &
  55. data, name, idate,units)
  56. implicit none
  57. ! in/out:
  58. integer,intent(in) :: im
  59. integer,intent(in) :: jm
  60. integer,intent(in) :: lm
  61. integer,intent(in) :: nt
  62. integer,intent(in) :: sd_id
  63. character(len=*),intent(in) :: name
  64. character(len=*),intent(in) :: labim
  65. character(len=*),intent(in) :: labjm
  66. character(len=*),intent(in) :: lablm
  67. character(len=*),intent(in) :: labnt
  68. integer,dimension(6),optional :: idate
  69. character(len=*),optional :: units
  70. real,dimension(im,jm,lm,nt),intent(in) :: data
  71. ! local
  72. integer :: sfcreate, sfscompress, sfwdata, sfendacc
  73. integer :: sfdimid, sfsdmname, sfsnatt
  74. integer :: rank = 4 ,istat
  75. integer,dimension(4) :: start = (/ 0,0,0,0 /)
  76. integer,dimension(4) :: stride= (/1,1,1,1/)
  77. integer :: sds_id, dimid0, dimid1, dimid2, dimid3
  78. sds_id = sfcreate(sd_id,name, DFNT_FLOAT32, rank, (/im,jm,lm,nt/) )
  79. if(present(idate)) istat = sfsnatt(sds_id,'idate', DFNT_INT32, 6, idate)
  80. if(present(units)) istat = sfsnatt(sds_id,'units', DFNT_CHAR, len(trim(units)), trim(units))
  81. dimid2 = sfdimid(sds_id, 0)
  82. istat = sfsdmname(dimid2,labim)
  83. dimid1 = sfdimid(sds_id, 1)
  84. istat = sfsdmname(dimid1,labjm)
  85. dimid0 = sfdimid(sds_id, 2)
  86. istat = sfsdmname(dimid0, lablm)
  87. dimid3 = sfdimid(sds_id, 3)
  88. istat = sfsdmname(dimid3, labnt)
  89. !istat = sfscompress(sds_id,comp_type,comp_prm)
  90. istat = sfwdata( sds_id, (/0,0,0,0/), (/1,1,1,1/), (/im,jm,lm,nt/), &
  91. real(data,kind=4) )
  92. istat = sfendacc(sds_id)
  93. end subroutine io_write4d_32
  94. subroutine io_write4d_i32( sd_id, im, labim, jm, labjm, &
  95. lm, lablm, nt, labnt, &
  96. data, name, idate, units )
  97. implicit none
  98. ! in/out:
  99. integer,intent(in) :: im,jm,lm,nt
  100. integer,intent(in) :: sd_id
  101. character(len=*),intent(in) :: name
  102. character(len=*),intent(in) :: labim,labjm,lablm,labnt
  103. integer,dimension(6),optional :: idate
  104. character(len=*),optional :: units
  105. integer,dimension(im,jm,lm,nt),intent(in) :: data
  106. ! local
  107. integer :: sfcreate, sfscompress, sfwdata, sfendacc
  108. integer :: sfdimid, sfsdmname, sfsnatt
  109. integer :: rank = 4 ,istat
  110. integer,dimension(4) :: start = (/ 0,0,0,0 /)
  111. integer,dimension(4) :: stride= (/1,1,1,1/)
  112. integer :: sds_id, dimid0, dimid1, dimid2, dimid3
  113. sds_id = sfcreate(sd_id,name, DFNT_INT32, rank, (/im,jm,lm,nt/) )
  114. if(present(idate)) istat = sfsnatt(sds_id,'idate', DFNT_INT32, 6, idate)
  115. if(present(units)) istat = sfsnatt(sds_id,'units', DFNT_CHAR, len(trim(units)), trim(units))
  116. dimid2 = sfdimid(sds_id, 0)
  117. istat = sfsdmname(dimid2,labim)
  118. dimid1 = sfdimid(sds_id, 1)
  119. istat = sfsdmname(dimid1,labjm)
  120. dimid0 = sfdimid(sds_id, 2)
  121. istat = sfsdmname(dimid0, lablm)
  122. dimid3 = sfdimid(sds_id, 3)
  123. istat = sfsdmname(dimid3, labnt)
  124. !istat = sfscompress(sds_id,comp_type,comp_prm)
  125. istat = sfwdata(sds_id, (/0,0,0,0/),(/1,1,1,1/) ,(/im,jm,lm,nt/) ,data)
  126. istat = sfendacc(sds_id)
  127. end subroutine io_write4d_I32
  128. subroutine io_write3d_32(sd_id,im,labim,jm,labjm,lm,lablm,data,name,idate,units)
  129. implicit none
  130. ! in/out:
  131. integer,intent(in) :: im,jm,lm
  132. integer,intent(in) :: sd_id
  133. character(len=*),intent(in) :: name
  134. character(len=*),intent(in) :: labim,labjm,lablm
  135. real,dimension(im,jm,lm),intent(in) :: data
  136. integer,dimension(6),optional :: idate
  137. character(len=*),optional :: units
  138. ! local
  139. integer :: sfcreate, sfscompress, sfwdata, sfendacc
  140. integer :: sfdimid, sfsdmname, sfsnatt
  141. integer :: rank = 3 ,istat
  142. integer,dimension(3) :: start = (/ 0,0,0 /), stride= (/1,1,1/)
  143. integer :: sds_id, dimid0, dimid1, dimid2
  144. sds_id = sfcreate(sd_id,name, DFNT_FLOAT32, rank, (/im,jm,lm/) )
  145. if(present(idate)) istat = sfsnatt(sds_id,'idate', DFNT_INT32, 6, idate)
  146. if(present(units)) istat = sfsnatt(sds_id,'units', DFNT_CHAR, len(trim(units)), trim(units))
  147. dimid2 = sfdimid(sds_id, 0)
  148. istat = sfsdmname(dimid2,labim)
  149. dimid1 = sfdimid(sds_id, 1)
  150. istat = sfsdmname(dimid1,labjm)
  151. dimid0 = sfdimid(sds_id, 2)
  152. istat = sfsdmname(dimid0, lablm)
  153. !istat = sfscompress(sds_id,comp_type,comp_prm)
  154. istat = sfwdata( sds_id, (/0,0,0/) ,(/1,1,1/) ,(/im,jm,lm/), &
  155. real(data,kind=4) )
  156. istat = sfendacc(sds_id)
  157. end subroutine io_write3d_32
  158. subroutine io_read3d_32(sd_id,im,jm,lm,data,name,ifail)
  159. implicit none
  160. ! in/out:
  161. integer, intent(in) :: im,jm,lm
  162. integer, intent(in) :: sd_id
  163. character(len=*),intent(in) :: name
  164. integer,intent(out) :: ifail
  165. real,dimension(im,jm,lm),intent(out) :: data
  166. ! local
  167. integer, parameter :: MAX_VAR_DIMS = 32
  168. character(len=64) :: xname
  169. integer :: index, rank, sds_id
  170. integer :: istat, attributes, num_type
  171. integer :: sffinfo, sfselect, sfginfo
  172. integer :: sfendacc, sfend, sfrnatt, sfrcatt
  173. integer :: sffattr, sfrdata, sfn2index
  174. integer,dimension(MAX_VAR_DIMS) :: dim_sizes
  175. real(kind=4),dimension(:,:,:),allocatable :: hdfr
  176. ifail = 1
  177. index = sfn2index(sd_id,name)
  178. if ( index == -1 ) return
  179. sds_id = sfselect(sd_id, index)
  180. istat = sfginfo(sds_id, xname, rank, dim_sizes, num_type, attributes)
  181. write(*,*) 'io_read3d_32: name = ', name
  182. write(*,*) 'io_read3d_32: rank = ', rank
  183. write(*,*) 'io_read3d_32: dims = ', dim_sizes(1:rank)
  184. if( rank == 3 .and. &
  185. dim_sizes(1) == im .and. &
  186. dim_sizes(2) == jm .and. &
  187. dim_sizes(3) == lm ) then
  188. allocate(hdfr(im,jm,lm))
  189. istat = sfrdata(sds_id, (/0,0,0/),(/1,1,1/),(/im,jm,lm/),hdfr)
  190. if ( istat == SUCCEED ) then
  191. if(okdebug) print*, 'io_read3d_32: Successfully retrieved ' &
  192. //name//' from file'
  193. else
  194. print*, 'io_read3d_32: Failed to read '//name//' from file'
  195. return
  196. end if
  197. data = hdfr
  198. istat = sfendacc(sds_id)
  199. deallocate(hdfr)
  200. ifail = 0
  201. end if
  202. end subroutine io_read3d_32
  203. subroutine io_read4d_32(sd_id,im,jm,lm,nt,data,name,ifail)
  204. implicit none
  205. ! in/out:
  206. integer, intent(in) :: im,jm,lm,nt
  207. integer, intent(in) :: sd_id
  208. character(len=*),intent(in) :: name
  209. real,dimension(im,jm,lm,nt),intent(out) :: data
  210. integer,intent(out) :: ifail
  211. ! local
  212. integer, parameter :: MAX_VAR_DIMS = 32
  213. character(len=64) :: xname
  214. integer :: index, rank, sds_id
  215. integer :: istat, attributes, num_type
  216. integer :: sffinfo, sfselect, sfginfo
  217. integer :: sfendacc, sfend, sfrnatt, sfrcatt
  218. integer :: sffattr, sfrdata, sfn2index
  219. integer,dimension(MAX_VAR_DIMS) :: dim_sizes
  220. real(kind=4),dimension(:,:,:,:),allocatable :: hdfr
  221. ifail = 1
  222. index = sfn2index(sd_id,name)
  223. if (index == -1) return
  224. sds_id = sfselect(sd_id, index)
  225. istat = sfginfo(sds_id, xname, rank, dim_sizes, num_type, attributes)
  226. write(*,*) 'io_read4d_32: name = ', name
  227. write(*,*) 'io_read4d_32: rank = ', rank
  228. write(*,*) 'io_read4d_32: dims = ', dim_sizes(1:rank)
  229. if( rank == 4 .and. &
  230. dim_sizes(1) == im .and. &
  231. dim_sizes(2) == jm .and. &
  232. dim_sizes(3) == lm .and. &
  233. dim_sizes(4) == nt ) then
  234. allocate(hdfr(im,jm,lm,nt))
  235. istat = sfrdata(sds_id, (/0,0,0,0/),(/1,1,1,1/),(/im,jm,lm,nt/),hdfr)
  236. if ( istat == SUCCEED ) then
  237. if ( okdebug ) print*, 'io_read4d_32: Successfully retrieved '// &
  238. name//' from file'
  239. else
  240. print*, 'io_read4d_32: Failed to read '//name//' from file'
  241. return
  242. end if
  243. data = hdfr
  244. istat = sfendacc(sds_id)
  245. deallocate(hdfr)
  246. ifail = 0
  247. end if
  248. end subroutine io_read4d_32
  249. subroutine io_write1d_i16(sd_id,im,labim,data,name,idate,units)
  250. implicit none
  251. ! in/out:
  252. integer,intent(in) :: im
  253. integer,intent(in) :: sd_id
  254. character(len=*),intent(in) :: name, labim
  255. integer,dimension(6),optional :: idate
  256. integer,dimension(im),intent(in) :: data
  257. character(len=*),optional :: units
  258. ! local
  259. integer :: sfcreate, sfscompress, sfwdata, sfendacc
  260. integer :: sfdimid, sfsdmname, sfsnatt
  261. integer :: rank = 1 ,istat
  262. integer,dimension(1) :: start = (/ 0 /), stride= (/1/)
  263. integer :: sds_id, dimid1, dimid2
  264. sds_id = sfcreate(sd_id,name, DFNT_INT16, rank, (/im/) )
  265. if(present(idate)) istat = sfsnatt(sds_id,'idate', DFNT_INT32, 6, idate)
  266. if(present(units)) istat = sfsnatt(sds_id,'units', DFNT_CHAR, len(trim(units)), trim(units))
  267. dimid2 = sfdimid(sds_id, 0)
  268. istat = sfsdmname(dimid2,labim)
  269. !istat = sfscompress(sds_id,comp_type,comp_prm)
  270. istat = sfwdata(sds_id, start, stride ,(/im/) , int(data,kind=2))
  271. istat = sfendacc(sds_id)
  272. end subroutine io_write1d_i16
  273. subroutine io_write1d_32(sd_id,im,labim,data,name,idate,units)
  274. implicit none
  275. ! in/out:
  276. integer,intent(in) :: im
  277. integer,intent(in) :: sd_id
  278. character(len=*),intent(in) :: name, labim
  279. integer,dimension(6),optional :: idate
  280. character(len=*),optional :: units
  281. real,dimension(im),intent(in) :: data
  282. ! local
  283. integer :: sfcreate, sfscompress, sfwdata, sfendacc
  284. integer :: sfdimid, sfsdmname, sfsnatt
  285. integer :: rank = 1 ,istat
  286. integer,dimension(1) :: start = (/ 0 /), stride= (/1/)
  287. integer :: sds_id, dimid1, dimid2
  288. sds_id = sfcreate(sd_id,name, DFNT_FLOAT32, rank, (/im/) )
  289. if(present(idate)) istat = sfsnatt(sds_id,'idate', DFNT_INT32, 6, idate)
  290. if(present(units)) istat = sfsnatt(sds_id,'units', DFNT_CHAR, len(trim(units)), trim(units))
  291. dimid2 = sfdimid(sds_id, 0)
  292. istat = sfsdmname(dimid2,labim)
  293. !istat = sfscompress(sds_id,comp_type,comp_prm)
  294. istat = sfwdata(sds_id, start, stride ,(/im/) , real(data,kind=4))
  295. istat = sfendacc(sds_id)
  296. end subroutine io_write1d_32
  297. subroutine io_write2d_32(sd_id,im,labim,jm,labjm,data,name,idate,units)
  298. implicit none
  299. ! in/out:
  300. integer,intent(in) :: im,jm
  301. integer,intent(in) :: sd_id
  302. character(len=*),intent(in) :: name,labim,labjm
  303. integer,dimension(6),optional :: idate
  304. character(len=*),optional :: units
  305. real,dimension(im,jm),intent(in) :: data
  306. ! local
  307. integer :: sfcreate, sfscompress, sfwdata, sfendacc
  308. integer :: sfdimid, sfsdmname, sfsnatt
  309. integer :: rank = 2 ,istat
  310. integer,dimension(2) :: start = (/ 0,0 /), stride= (/1,1/)
  311. integer :: sds_id, dimid1, dimid2
  312. sds_id = sfcreate(sd_id,name, DFNT_FLOAT32, rank, (/im,jm/) )
  313. if(present(idate)) istat = sfsnatt(sds_id,'idate', DFNT_INT32, 6, idate)
  314. if(present(units)) istat = sfsnatt(sds_id,'units', DFNT_CHAR, len(trim(units)), trim(units))
  315. dimid2 = sfdimid(sds_id, 0)
  316. istat = sfsdmname(dimid2,labim)
  317. dimid1 = sfdimid(sds_id, 1)
  318. istat = sfsdmname(dimid1,labjm)
  319. !istat = sfscompress(sds_id,comp_type,comp_prm)
  320. istat = sfwdata(sds_id, start, stride ,(/im,jm/) , real(data,kind=4))
  321. istat = sfendacc(sds_id)
  322. end subroutine io_write2d_32
  323. subroutine io_read2d_I16g(sd_id,im,jm,data,name,ifail,index,idate)
  324. implicit none
  325. ! in/out:
  326. integer, intent(in) :: im,jm
  327. integer, intent(in) :: sd_id
  328. character(len=*),intent(in) :: name
  329. integer,dimension(im,jm),intent(out) :: data
  330. integer,intent(out) :: ifail
  331. integer,dimension(6), optional :: idate
  332. ! local
  333. integer,dimension(6) :: idate_file
  334. integer, parameter :: MAX_VAR_DIMS = 32
  335. character(len=64) :: xname,attr_name
  336. integer,optional :: index
  337. integer(kind=4) :: rank
  338. integer :: sds_id
  339. integer :: istat, attributes, num_type, n_values, data_type
  340. integer :: sffinfo, sfselect, sfginfo, sfrattr
  341. integer :: sfendacc, sfend, sfrnatt, sfrcatt
  342. integer :: sffattr, sfrdata, sfn2index
  343. integer :: sfgainfo,attr_index,lname
  344. integer :: num_ds,num_at,ids
  345. integer :: idebug = 0
  346. integer(kind=4),dimension(MAX_VAR_DIMS):: dim_sizes
  347. ifail = 1
  348. lname = len_trim(name)
  349. istat = sffinfo(sd_id,num_ds,num_at)
  350. if( present(index) ) then
  351. sds_id = sfselect(sd_id, index)
  352. istat = sfginfo(sds_id, xname, rank, dim_sizes, num_type, attributes)
  353. if ( idebug >= 100 ) then
  354. write(*,*) 'io_read2d_I16g: name = ', name,xname(1:lname)
  355. write(*,*) 'io_read2d_I16g: rank = ', rank
  356. write(*,*) 'io_read2d_I16g: dims = ', dim_sizes(1:rank)
  357. write(*,*) 'io_read2d_I16g: attr = ', attributes
  358. end if
  359. ! check rank and name....
  360. names: if(+xname(1:lname) == +name(1:lname) .and. &
  361. rank == 2 .and. &
  362. dim_sizes(1) == im .and. &
  363. dim_sizes(2) == jm ) then
  364. if( present(idate) ) then
  365. attr_index = sffattr(sds_id, 'idate')
  366. istat = sfgainfo(sds_id, attr_index, attr_name, &
  367. data_type, n_values)
  368. istat = sfrattr(sds_id, attr_index, idate_file)
  369. if ( idebug >= 100 ) then
  370. write(*,*) 'io_read2d_I16g: idate = ', idate,idate_file
  371. end if
  372. if( sum(abs(idate-idate_file)) == 0 ) then
  373. call read_it !everything OK....proceed
  374. !cmk index=index+1 !set index to next position
  375. return !and return
  376. end if
  377. else
  378. call read_it !everything OK....proceed
  379. !cmk index=index+1 !set index to next position
  380. return !and return
  381. end if
  382. end if names
  383. istat = sfendacc(sds_id) !close 'wrong' ds
  384. end if
  385. dsloop: do ids=0,num_ds-1
  386. sds_id = sfselect(sd_id, ids)
  387. istat = sfginfo(sds_id, xname, rank, dim_sizes, num_type, attributes)
  388. if ( idebug >= 100 ) then
  389. write(*,*) 'io_read2d_I16g: name = ', name,xname(1:lname)
  390. write(*,*) 'io_read2d_I16g: rank = ', rank
  391. write(*,*) 'io_read2d_I16g: dims = ', dim_sizes(1:rank)
  392. write(*,*) 'io_read2d_I16g: attr = ', attributes
  393. end if
  394. if(+xname(1:lname) /= +name(1:lname) .or. & !check rank and name....
  395. rank /= 2 .or. &
  396. dim_sizes(1) /= im .or. &
  397. dim_sizes(2) /= jm ) then
  398. istat = sfendacc(sds_id) !close 'wrong' ds
  399. cycle dsloop
  400. end if
  401. if(present (idate)) then
  402. attr_index = sffattr(sds_id, 'idate')
  403. istat = sfgainfo(sds_id, attr_index, attr_name, data_type, n_values)
  404. istat = sfrattr(sds_id, attr_index, idate_file)
  405. if ( idebug >= 100 ) then
  406. write(*,*) 'io_read2d_I16g: idate = ', idate,idate_file
  407. end if
  408. if( sum(abs(idate-idate_file)) /= 0 ) then
  409. istat = sfendacc(sds_id) !close 'wrong' ds
  410. cycle dsloop
  411. end if
  412. end if
  413. call read_it !everything OK
  414. ! set index to next position if present...
  415. if ( present(index) ) index=ids+1
  416. return
  417. end do dsloop
  418. print*, 'io_read2d_I16g: Could not find '//name//' from file'
  419. if ( present(idate) ) print *,'io_read2d_I16g: With date..',idate
  420. contains
  421. subroutine read_it
  422. integer :: sfrdata
  423. integer(kind=2),dimension(:,:),allocatable :: hdfr
  424. allocate(hdfr(im,jm))
  425. istat = sfrdata(sds_id, (/0,0/),(/1,1/),(/im,jm/),hdfr)
  426. if ( istat == SUCCEED ) then
  427. if ( idebug >= 100 ) &
  428. print*, 'io_read2d_I16g: Successfully retrieved '// &
  429. name//' from file'
  430. else
  431. print*, 'io_read2d_I16g: Failed to read '//name//' from file'
  432. return
  433. end if
  434. data = hdfr
  435. istat = sfendacc(sds_id)
  436. deallocate(hdfr)
  437. ifail = 0
  438. end subroutine read_it
  439. end subroutine io_read2d_I16g
  440. subroutine io_write2d_I16(sd_id,im,jm,data,name,idate,units)
  441. implicit none
  442. ! in/out:
  443. integer,intent(in) :: im,jm
  444. integer,intent(in) :: sd_id
  445. character(len=*),intent(in) :: name
  446. integer(kind=2),dimension(im,jm),intent(in) :: data
  447. integer,dimension(6),optional :: idate
  448. character(len=*),optional :: units
  449. ! local
  450. integer :: sfcreate, sfscompress, sfwdata, sfendacc, sfsnatt
  451. integer :: rank = 2 ,istat
  452. integer,dimension(2) :: start = (/ 0,0 /), stride= (/1,1/)
  453. integer :: sds_id
  454. sds_id = sfcreate(sd_id, name, DFNT_INT16, rank, (/im,jm/) )
  455. if( present(idate) ) istat = sfsnatt(sds_id,'idate', DFNT_INT32, 6, idate)
  456. if(present(units)) istat = sfsnatt(sds_id,'units', DFNT_CHAR, len(trim(units)), trim(units))
  457. !print*,'sfcreate returns dataset id:',sds_id
  458. !istat = sfscompress(sds_id,comp_type,comp_prm)
  459. !print*,'sfscompress returns:',istat
  460. istat = sfwdata(sds_id, start, stride, (/im,jm/) , data)
  461. !print*,'sfwdata returns:',istat
  462. istat = sfendacc(sds_id)
  463. !print*, 'sfendacc returns: ', istat
  464. end subroutine io_write2d_I16
  465. subroutine io_write2d_I32(sd_id,im,labim,jm,labjm,data,name,idate,units)
  466. implicit none
  467. ! in/out:
  468. integer,intent(in) :: im,jm
  469. integer,intent(in) :: sd_id
  470. character(len=*),intent(in) :: name,labim,labjm
  471. integer,dimension(im,jm),intent(in) :: data
  472. integer,dimension(6),optional :: idate
  473. character(len=*),optional :: units
  474. ! local
  475. integer :: sfcreate, sfscompress, sfwdata, sfendacc
  476. integer :: sfdimid, sfsdmname, sfsnatt
  477. integer :: rank = 2 ,istat
  478. integer,dimension(2) :: start = (/ 0,0 /), stride= (/1,1/)
  479. integer :: sds_id, dimid1, dimid2
  480. sds_id = sfcreate(sd_id,name, DFNT_INT32, rank, (/im,jm/) )
  481. if(present(idate)) istat = sfsnatt(sds_id,'idate', DFNT_INT32, 6, idate)
  482. if(present(units)) istat = sfsnatt(sds_id,'units', DFNT_CHAR, len(trim(units)), trim(units))
  483. dimid2 = sfdimid(sds_id, 0)
  484. istat = sfsdmname(dimid2,labim)
  485. dimid1 = sfdimid(sds_id, 1)
  486. istat = sfsdmname(dimid1,labjm)
  487. !istat = sfscompress(sds_id,comp_type,comp_prm)
  488. istat = sfwdata(sds_id, start, stride ,(/im,jm/) , data)
  489. istat = sfendacc(sds_id)
  490. end subroutine io_write2d_I32
  491. subroutine io_write1d_I32(sd_id,im,labim,data,name,idate,units)
  492. implicit none
  493. ! in/out:
  494. integer,intent(in) :: im
  495. integer,intent(in) :: sd_id
  496. character(len=*),intent(in) :: name,labim
  497. integer,dimension(im),intent(in) :: data
  498. integer,dimension(6),optional :: idate
  499. character(len=*),optional :: units
  500. ! local
  501. integer :: sfcreate, sfscompress, sfwdata, sfendacc
  502. integer :: sfdimid, sfsdmname, sfsnatt
  503. integer :: rank = 2 ,istat
  504. integer :: start = 0 ,stride= 1
  505. integer :: sds_id, dimid1, dimid2
  506. sds_id = sfcreate(sd_id,name, DFNT_INT32, rank, im)
  507. if(present(idate)) istat = sfsnatt(sds_id,'idate', DFNT_INT32, 6, idate)
  508. if(present(units)) istat = sfsnatt(sds_id,'units', DFNT_CHAR, len(trim(units)), trim(units))
  509. dimid2 = sfdimid(sds_id, 0)
  510. istat = sfsdmname(dimid2,labim)
  511. !istat = sfscompress(sds_id,comp_type,comp_prm)
  512. istat = sfwdata(sds_id, start, stride ,im , data)
  513. istat = sfendacc(sds_id)
  514. end subroutine io_write1d_I32
  515. subroutine io_read2d_I16D(sd_id,im,jm,data,name,index,ifail,idate)
  516. implicit none
  517. ! in/out:
  518. integer,intent(in) :: im,jm
  519. integer,intent(in) :: sd_id
  520. character(len=*),intent(in) :: name
  521. integer,dimension(im,jm),intent(out) :: data
  522. integer,intent(inout) :: index
  523. integer,intent(out) :: ifail
  524. integer,dimension(6),intent(in) :: idate
  525. ! local
  526. integer, parameter :: MAX_VAR_DIMS = 32
  527. integer,dimension(6) :: idate_file
  528. integer :: sffinfo, sfselect, sfginfo, sfrdata
  529. integer :: sfendacc, sffattr, sfgainfo, sfrattr
  530. integer :: rank = 2 ,istat
  531. integer,dimension(2) :: start = (/ 0,0 /), stride= (/1,1/)
  532. integer(kind=2),dimension(:,:),allocatable :: hdfr
  533. integer :: sds_id, ndatasets, nglobat, i
  534. integer :: xrank, xtype, natt, j
  535. integer,dimension(MAX_VAR_DIMS) :: dim_sizes
  536. character(len=64) :: xname,attr_name
  537. integer :: num_type, n_values, data_type
  538. integer :: attributes, attr_index
  539. ifail = 1
  540. sds_id = sfselect(sd_id, index)
  541. istat = sfginfo(sds_id, xname, rank, dim_sizes, num_type, attributes)
  542. write(*,*) 'io_read2d_I16D: name = ', name
  543. write(*,*) 'io_read2d_I16D: rank = ', rank
  544. write(*,*) 'io_read2d_I16D: dims = ', dim_sizes(1:rank)
  545. write(*,*) 'io_read2d_I16D: attr = ', attributes
  546. attr_index = sffattr(sds_id, 'idate')
  547. istat = sfgainfo(sds_id, attr_index, attr_name, data_type, n_values)
  548. istat = sfrattr(sds_id, attr_index, idate_file)
  549. if( sum(abs(idate_file-idate)) == 0 .and. &
  550. rank == 2 .and. &
  551. +name(1:4) == +xname(1:4) .and. &
  552. dim_sizes(1) == im .and. &
  553. dim_sizes(2) == jm ) then
  554. allocate(hdfr(im,jm))
  555. istat = sfrdata(sds_id, start,stride,(/im,jm/),hdfr)
  556. if ( istat == SUCCEED ) then
  557. if( okdebug ) print*, 'io_read2d_I16D: Successfully retrieved '// &
  558. name//' from file'
  559. else
  560. print*, 'io_read2d_I16D: Failed to read '//name//' from file'
  561. return
  562. end if
  563. data = hdfr
  564. istat = sfendacc(sds_id)
  565. deallocate(hdfr)
  566. ifail = 0
  567. index = index+1
  568. end if
  569. end subroutine io_read2d_I16D
  570. subroutine io_write3d_32d(sd_id,im,labim,jm,labjm,lm,lablm,data,name,idate,units)
  571. implicit none
  572. ! in/out:
  573. integer,intent(in) :: im,jm,lm
  574. integer,intent(in) :: sd_id
  575. character(len=*),intent(in) :: name,labim,labjm,lablm
  576. real,dimension(im,jm,lm),intent(in) :: data
  577. integer,dimension(6),intent(in) :: idate
  578. character(len=*),optional :: units
  579. ! local
  580. integer :: sfcreate, sfscompress, sfwdata, sfendacc
  581. integer :: sfdimid, sfsdmname, sfsnatt
  582. integer :: rank = 3 ,istat
  583. integer,dimension(3) :: start = (/ 0,0,0 /), stride= (/1,1,1/)
  584. integer :: sds_id, dimid0, dimid1, dimid2
  585. sds_id = sfcreate(sd_id,name, DFNT_FLOAT32, rank, (/im,jm,lm/) )
  586. istat = sfsnatt(sds_id,'idate', DFNT_INT32, 6, idate)
  587. if(present(units)) istat = sfsnatt(sds_id,'units', DFNT_CHAR, len(trim(units)), trim(units))
  588. dimid2 = sfdimid(sds_id, 0)
  589. istat = sfsdmname(dimid2,labim)
  590. dimid1 = sfdimid(sds_id, 1)
  591. istat = sfsdmname(dimid1,labjm)
  592. dimid0 = sfdimid(sds_id, 2)
  593. istat = sfsdmname(dimid0, lablm)
  594. !istat = sfscompress(sds_id,comp_type,comp_prm)
  595. istat = sfwdata( sds_id, (/0,0,0/), (/1,1,1/), (/im,jm,lm/), &
  596. real(data,kind=4) )
  597. istat = sfendacc(sds_id)
  598. end subroutine io_write3d_32d
  599. subroutine io_write3d_32dr(sd_id,im,labim,jm,labjm,lm,lablm, &
  600. data,name,idate,region,units)
  601. implicit none
  602. ! in/out:
  603. integer,intent(in) :: im,jm,lm
  604. integer,intent(in) :: sd_id
  605. character(len=*),intent(in) :: name,labim,labjm,lablm
  606. real,dimension(im,jm,lm),intent(in) :: data
  607. integer,dimension(6),intent(in) :: idate
  608. character(len=*),optional :: units
  609. integer,intent(in) :: region
  610. ! local
  611. integer :: sfcreate, sfscompress, sfwdata, sfendacc
  612. integer :: sfdimid, sfsdmname, sfsnatt
  613. integer :: rank = 3 ,istat
  614. integer,dimension(3) :: start = (/ 0,0,0 /), stride= (/1,1,1/)
  615. integer :: sds_id, dimid0, dimid1, dimid2
  616. sds_id = sfcreate(sd_id,name, DFNT_FLOAT32, rank, (/im,jm,lm/) )
  617. istat = sfsnatt(sds_id,'idate', DFNT_INT32, 6, idate)
  618. if(present(units)) istat = sfsnatt(sds_id,'units', DFNT_CHAR, len(trim(units)), trim(units))
  619. istat = sfsnatt(sds_id,'region', DFNT_INT32, 1, region)
  620. dimid2 = sfdimid(sds_id, 0)
  621. istat = sfsdmname(dimid2,labim)
  622. dimid1 = sfdimid(sds_id, 1)
  623. istat = sfsdmname(dimid1,labjm)
  624. dimid0 = sfdimid(sds_id, 2)
  625. istat = sfsdmname(dimid0, lablm)
  626. !istat = sfscompress(sds_id,comp_type,comp_prm)
  627. istat = sfwdata( sds_id, (/0,0,0/), (/1,1,1/), (/im,jm,lm/), &
  628. real(data,kind=4) )
  629. istat = sfendacc(sds_id)
  630. end subroutine io_write3d_32dr
  631. subroutine io_write2d_32d(sd_id,im,labim,jm,labjm,data,name,idate,units)
  632. implicit none
  633. ! in/out:
  634. integer,intent(in) :: im,jm
  635. integer,intent(in) :: sd_id
  636. character(len=*),intent(in) :: name,labim,labjm
  637. real,dimension(im,jm),intent(in) :: data
  638. integer,dimension(6),intent(in) :: idate
  639. character(len=*),optional :: units
  640. ! local
  641. integer :: sfcreate, sfscompress, sfwdata, sfendacc
  642. integer :: sfdimid, sfsdmname, sfsnatt
  643. integer :: rank = 2
  644. integer :: istat
  645. integer,dimension(2) :: start = (/ 0,0 /), stride= (/1,1/)
  646. integer :: sds_id, dimid1, dimid2
  647. sds_id = sfcreate(sd_id,name, DFNT_FLOAT32, rank, (/im,jm/) )
  648. istat = sfsnatt(sds_id,'idate', DFNT_INT32, 6, idate)
  649. if(present(units)) istat = sfsnatt(sds_id,'units', DFNT_CHAR, len(trim(units)), trim(units))
  650. dimid2 = sfdimid(sds_id, 0)
  651. istat = sfsdmname(dimid2,labim)
  652. dimid1 = sfdimid(sds_id, 1)
  653. istat = sfsdmname(dimid1,labjm)
  654. !istat = sfscompress(sds_id,comp_type,comp_prm)
  655. istat = sfwdata(sds_id, start, stride, (/im,jm/), real(data,kind=4))
  656. istat = sfendacc(sds_id)
  657. end subroutine io_write2d_32d
  658. subroutine io_write2d_32dr(sd_id,im,labim,jm,labjm,data,name,idate,region,units)
  659. implicit none
  660. ! in/out:
  661. integer,intent(in) :: im,jm
  662. integer,intent(in) :: sd_id
  663. character(len=*),intent(in) :: name,labim,labjm
  664. integer,dimension(6),intent(in) :: idate
  665. real,dimension(im,jm),intent(in) :: data
  666. integer,intent(in) :: region
  667. character(len=*),optional :: units
  668. ! local
  669. integer :: sfcreate, sfscompress, sfwdata, sfendacc
  670. integer :: sfdimid, sfsdmname, sfsnatt
  671. integer :: rank = 2 ,istat
  672. integer,dimension(2) :: start = (/ 0,0 /), stride= (/1,1/)
  673. integer :: sds_id, dimid1, dimid2
  674. sds_id = sfcreate(sd_id,name, DFNT_FLOAT32, rank, (/im,jm/) )
  675. istat = sfsnatt(sds_id,'idate', DFNT_INT32, 6, idate)
  676. if(present(units)) istat = sfsnatt(sds_id,'units', DFNT_CHAR, len(trim(units)), trim(units))
  677. istat = sfsnatt(sds_id,'region', DFNT_INT32, 1, region)
  678. dimid2 = sfdimid(sds_id, 0)
  679. istat = sfsdmname(dimid2,labim)
  680. dimid1 = sfdimid(sds_id, 1)
  681. istat = sfsdmname(dimid1,labjm)
  682. !istat = sfscompress(sds_id,comp_type,comp_prm)
  683. istat = sfwdata(sds_id, start, stride ,(/im,jm/) , real(data,kind=4))
  684. istat = sfendacc(sds_id)
  685. end subroutine io_write2d_32dr
  686. subroutine io_read3d_32d(sd_id,im,jm,lm,data,name,index,ifail,idate)
  687. implicit none
  688. ! in/out:
  689. integer, intent(in) :: im,jm,lm
  690. integer, intent(in) :: sd_id
  691. real,dimension(im,jm,lm),intent(out) :: data
  692. character(len=*),intent(in) :: name
  693. integer,intent(inout) :: index
  694. integer,intent(out) :: ifail
  695. integer,dimension(6),intent(in) :: idate
  696. ! local
  697. integer,dimension(6) :: idate_file
  698. integer, parameter :: MAX_VAR_DIMS = 32
  699. character(len=64) :: xname,attr_name
  700. integer(kind=4) :: rank
  701. integer :: sds_id
  702. integer :: istat, attributes, num_type
  703. integer :: n_values, data_type
  704. integer :: sffinfo, sfselect, sfginfo, sfrattr
  705. integer :: sfendacc, sfend, sfrnatt, sfrcatt
  706. integer :: sffattr, sfrdata, sfn2index
  707. integer :: sfgainfo,attr_index,lname
  708. integer :: num_ds,num_at,ids
  709. integer(kind=4),dimension(MAX_VAR_DIMS) :: dim_sizes
  710. real(kind=4),dimension(:,:,:),allocatable :: hdfr
  711. ifail = 1
  712. sds_id = sfselect(sd_id, index) !first try the suggested index.....
  713. istat = sfginfo(sds_id, xname, rank, dim_sizes, num_type, attributes)
  714. lname = len_trim(name)
  715. write(*,*) 'io_read3d_32d: name = ', name,xname(1:lname)
  716. write(*,*) 'io_read3d_32d: rank = ', rank
  717. write(*,*) 'io_read3d_32d: dims = ', dim_sizes(1:rank)
  718. write(*,*) 'io_read3d_32d: attr = ', attributes
  719. attr_index = sffattr(sds_id, 'idate')
  720. istat = sfgainfo(sds_id, attr_index, attr_name, data_type, n_values)
  721. istat = sfrattr(sds_id, attr_index, idate_file)
  722. write(*,*) 'io_read3d_32d: idate = ', idate,idate_file
  723. if( rank == 3 .and. &
  724. +xname(1:lname) == +name(1:lname) .and. &
  725. sum(abs(idate_file-idate)) == 0 .and. &
  726. dim_sizes(1) == im .and. &
  727. dim_sizes(2) == jm .and. &
  728. dim_sizes(3) == lm ) then
  729. allocate(hdfr(im,jm,lm))
  730. istat = sfrdata(sds_id, (/0,0,0/),(/1,1,1/),(/im,jm,lm/),hdfr)
  731. if ( istat == SUCCEED ) then
  732. if ( okdebug ) print*, 'io_read3d_32d: Successfully retrieved '// &
  733. name//' from file'
  734. else
  735. print*, 'io_read3d_32d: Failed to read '//name//' from file'
  736. return
  737. end if
  738. data = hdfr
  739. istat = sfendacc(sds_id)
  740. deallocate(hdfr)
  741. ifail = 0
  742. index = index+1
  743. else
  744. print *, 'io_read3d_32d: Try to find '//name//' with date ',idate
  745. istat = sfendacc(sds_id) !close 'wrong' ds
  746. istat = sffinfo(sd_id,num_ds,num_at)
  747. do ids = 0,num_ds-1
  748. sds_id = sfselect(sd_id, ids)
  749. istat = sfginfo(sds_id, xname, rank, dim_sizes, num_type, attributes)
  750. if ( +xname(1:lname) == +name(1:lname) .and. &
  751. rank == 3 .and. &
  752. dim_sizes(1) == im .and. &
  753. dim_sizes(2) == jm .and. &
  754. dim_sizes(3) == lm ) then
  755. attr_index = sffattr(sds_id, 'idate')
  756. istat = sfgainfo(sds_id, attr_index, attr_name, data_type, n_values)
  757. istat = sfrattr(sds_id, attr_index, idate_file)
  758. if ( sum(abs(idate-idate_file)) == 0 ) then
  759. allocate(hdfr(im,jm,lm))
  760. istat = sfrdata(sds_id, (/0,0,0/),(/1,1,1/),(/im,jm,lm/),hdfr)
  761. if ( istat == SUCCEED ) then
  762. print*, 'io_read3d_32d: Successfully retrieved '//name// &
  763. ' from file'
  764. else
  765. print*, 'io_read3d_32d: Failed to read '//name//' from file'
  766. return
  767. end if
  768. data = hdfr
  769. istat = sfendacc(sds_id)
  770. deallocate(hdfr)
  771. ifail = 0
  772. index = ids + 1
  773. return
  774. end if !date fit
  775. istat = sfendacc(sds_id)
  776. end if !name fit
  777. end do !ids loop
  778. print*, 'io_read3d_32d: Failed to read '//name//' from file'
  779. end if
  780. end subroutine io_read3d_32d
  781. subroutine io_read1d_32g(sd_id,jm,data,name,ifail,index,idate)
  782. implicit none
  783. ! in/out:
  784. integer, intent(in) :: jm
  785. integer, intent(in) :: sd_id
  786. character(len=*),intent(in) :: name
  787. real,dimension(jm),intent(out) :: data
  788. integer,intent(out) :: ifail
  789. integer,dimension(6), optional :: idate
  790. integer,optional :: index
  791. ! local
  792. integer,dimension(6) :: idate_file
  793. integer, parameter :: MAX_VAR_DIMS = 32
  794. character(len=64) :: xname,attr_name
  795. integer(kind=4) :: rank
  796. integer :: sds_id
  797. integer :: istat, attributes, num_type, n_values, data_type
  798. integer :: sffinfo, sfselect, sfginfo, sfrattr
  799. integer :: sfendacc, sfend, sfrnatt, sfrcatt
  800. integer :: sffattr, sfrdata, sfn2index
  801. integer :: sfgainfo,attr_index,lname
  802. integer :: num_ds,num_at,ids
  803. integer(kind=4),dimension(MAX_VAR_DIMS):: dim_sizes
  804. integer :: idebug = 0
  805. ifail = 1
  806. lname = len_trim(name)
  807. istat = sffinfo(sd_id,num_ds,num_at)
  808. if ( present(index) ) then
  809. print*,'index = ',index
  810. sds_id = sfselect(sd_id, index)
  811. istat = sfginfo(sds_id, xname, rank, dim_sizes, num_type, attributes)
  812. if ( idebug >= 100 ) then
  813. write(*,*) 'io_read1d_32g: name = ', name,xname(1:lname)
  814. write(*,*) 'io_read1d_32g: rank = ', rank
  815. write(*,*) 'io_read1d_32g: dims = ', dim_sizes(1:rank)
  816. write(*,*) 'io_read1d_32g: attr = ', attributes
  817. end if
  818. ! check rank and name....
  819. names: if( +xname(1:lname) == +name(1:lname) .and. &
  820. rank == 1 .and. &
  821. dim_sizes(1) == jm ) then
  822. if ( present(idate) ) then
  823. attr_index = sffattr(sds_id, 'idate')
  824. istat = &
  825. sfgainfo(sds_id, attr_index, attr_name, data_type, n_values)
  826. istat = sfrattr(sds_id, attr_index, idate_file)
  827. if ( idebug >= 100 ) then
  828. write(*,*) 'io_read1d_32g: idate = ', idate,idate_file
  829. end if
  830. if ( sum(abs(idate-idate_file)) == 0 ) then
  831. call read_it !everything OK....proceed
  832. return !and return
  833. end if
  834. else
  835. call read_it !everything OK....proceed
  836. return !and return
  837. end if
  838. end if names
  839. istat = sfendacc(sds_id) !close 'wrong' ds
  840. end if
  841. dsloop: do ids=0,num_ds-1
  842. sds_id = sfselect(sd_id, ids)
  843. istat = sfginfo(sds_id, xname, rank, dim_sizes, num_type, attributes)
  844. if ( idebug >= 100 ) then
  845. write(*,*) 'io_read1d_32g: name = ', name,xname(1:lname)
  846. write(*,*) 'io_read1d_32g: rank = ', rank
  847. write(*,*) 'io_read1d_32g: dims = ', dim_sizes(1:rank)
  848. write(*,*) 'io_read1d_32g: attr = ', attributes
  849. end if
  850. ! check rank and name....
  851. if ( +xname(1:lname) /= +name(1:lname) .or. &
  852. rank /= 1 .or. &
  853. dim_sizes(1) /= jm ) then
  854. istat = sfendacc(sds_id) !close 'wrong' ds
  855. cycle dsloop
  856. end if
  857. if ( present(idate) ) then
  858. attr_index = sffattr(sds_id, 'idate')
  859. istat = sfgainfo(sds_id, attr_index, attr_name, data_type, n_values)
  860. istat = sfrattr(sds_id, attr_index, idate_file)
  861. if ( idebug >= 100 ) then
  862. write(*,*) 'io_read1d_32g: idate = ', idate,idate_file
  863. end if
  864. if( sum(abs(idate-idate_file)) /= 0 ) then
  865. istat = sfendacc(sds_id) !close 'wrong' ds
  866. cycle dsloop
  867. end if
  868. end if
  869. call read_it !everything OK
  870. ! set index to next position if present...
  871. if ( present(index) ) index=ids+1
  872. return
  873. end do dsloop
  874. print*, 'io_read1d_32g: Could not find '//name//' from file'
  875. if ( present(idate) ) print *,'io_read1d_32g: With date..',idate
  876. contains
  877. subroutine read_it
  878. integer :: sfrdata
  879. real(kind=4),dimension(:),allocatable :: hdfr
  880. allocate(hdfr(jm))
  881. istat = sfrdata(sds_id, (/0/),(/1/),(/jm/),hdfr)
  882. if ( istat == SUCCEED ) then
  883. if ( idebug >= 100 ) &
  884. print*, 'io_read1d_32g: Successfully retrieved '//name//' from file'
  885. else
  886. print*, 'io_read1d_32g: Failed to read '//name//' from file'
  887. return
  888. end if
  889. data = hdfr
  890. istat = sfendacc(sds_id)
  891. deallocate(hdfr)
  892. ifail = 0
  893. end subroutine read_it
  894. end subroutine io_read1d_32g
  895. subroutine io_read2d_32g(sd_id,im,jm,data,name,ifail,index,idate)
  896. implicit none
  897. ! in/out:
  898. integer, intent(in) :: im,jm
  899. integer, intent(in) :: sd_id
  900. character(len=*),intent(in) :: name
  901. real,dimension(im,jm),intent(out) :: data
  902. integer,intent(out) :: ifail
  903. integer,dimension(6), optional :: idate
  904. integer,optional :: index
  905. ! local
  906. integer,dimension(6) :: idate_file
  907. integer, parameter :: MAX_VAR_DIMS = 32
  908. character(len=64) :: xname,attr_name
  909. integer(kind=4) :: rank
  910. integer :: sds_id
  911. integer :: istat, attributes, num_type, n_values, data_type
  912. integer :: sffinfo, sfselect, sfginfo, sfrattr
  913. integer :: sfendacc, sfend, sfrnatt, sfrcatt
  914. integer :: sffattr, sfrdata, sfn2index
  915. integer :: sfgainfo,attr_index,lname
  916. integer :: num_ds,num_at,ids
  917. integer(kind=4),dimension(MAX_VAR_DIMS):: dim_sizes
  918. integer :: idebug = 0
  919. ifail = 1
  920. lname = len_trim(name)
  921. istat = sffinfo(sd_id,num_ds,num_at)
  922. if ( present(index) ) then
  923. sds_id = sfselect(sd_id, index)
  924. istat = sfginfo(sds_id, xname, rank, dim_sizes, num_type, attributes)
  925. if ( idebug >= 100 ) then
  926. write(*,*) 'io_read2d_32g: name = ', name,xname(1:lname)
  927. write(*,*) 'io_read2d_32g: rank = ', rank
  928. write(*,*) 'io_read2d_32g: dims = ', dim_sizes(1:rank)
  929. write(*,*) 'io_read2d_32g: attr = ', attributes
  930. end if
  931. ! check rank and name....
  932. names: if( +xname(1:lname) == +name(1:lname) .and. &
  933. rank == 2 .and. &
  934. dim_sizes(1) == im .and. &
  935. dim_sizes(2) == jm ) then
  936. if ( present(idate) ) then
  937. attr_index = sffattr(sds_id, 'idate')
  938. istat = &
  939. sfgainfo(sds_id, attr_index, attr_name, data_type, n_values)
  940. istat = sfrattr(sds_id, attr_index, idate_file)
  941. if ( idebug >= 100 ) then
  942. write(*,*) 'io_read2d_32g: idate = ', idate,idate_file
  943. end if
  944. if ( sum(abs(idate-idate_file)) == 0 ) then
  945. call read_it !everything OK....proceed
  946. return !and return
  947. end if
  948. else
  949. call read_it !everything OK....proceed
  950. return !and return
  951. end if
  952. end if names
  953. istat = sfendacc(sds_id) !close 'wrong' ds
  954. end if
  955. dsloop: do ids=0,num_ds-1
  956. sds_id = sfselect(sd_id, ids)
  957. istat = sfginfo(sds_id, xname, rank, dim_sizes, num_type, attributes)
  958. if ( idebug >= 100 ) then
  959. write(*,*) 'io_read2d_32g: name = ', name,xname(1:lname)
  960. write(*,*) 'io_read2d_32g: rank = ', rank
  961. write(*,*) 'io_read2d_32g: dims = ', dim_sizes(1:rank)
  962. write(*,*) 'io_read2d_32g: attr = ', attributes
  963. end if
  964. ! check rank and name....
  965. if ( +xname(1:lname) /= +name(1:lname) .or. &
  966. rank /= 2 .or. &
  967. dim_sizes(1) /= im .or. &
  968. dim_sizes(2) /= jm ) then
  969. istat = sfendacc(sds_id) !close 'wrong' ds
  970. cycle dsloop
  971. end if
  972. if ( present(idate) ) then
  973. attr_index = sffattr(sds_id, 'idate')
  974. istat = sfgainfo(sds_id, attr_index, attr_name, data_type, n_values)
  975. istat = sfrattr(sds_id, attr_index, idate_file)
  976. if ( idebug >= 100 ) then
  977. write(*,*) 'io_read2d_32g: idate = ', idate,idate_file
  978. end if
  979. if( sum(abs(idate-idate_file)) /= 0 ) then
  980. istat = sfendacc(sds_id) !close 'wrong' ds
  981. cycle dsloop
  982. end if
  983. end if
  984. call read_it !everything OK
  985. ! set index to next position if present...
  986. if ( present(index) ) index=ids+1
  987. return
  988. end do dsloop
  989. print *, 'io_read2d_32g: Could not find requested data set in hdf file:'
  990. print *, 'io_read2d_32g: name : ', trim(name)
  991. print *, 'io_read2d_32g: shape : ', im, jm
  992. if (present(idate)) print *, 'io_read2d_32g: idate : ', idate
  993. if (present(index)) print *, 'io_read2d_32g: index : ', index
  994. contains
  995. subroutine read_it
  996. integer :: sfrdata
  997. real(kind=4),dimension(:,:),allocatable :: hdfr
  998. allocate(hdfr(im,jm))
  999. istat = sfrdata(sds_id, (/0,0/),(/1,1/),(/im,jm/),hdfr)
  1000. if ( istat == SUCCEED ) then
  1001. if ( idebug >= 100 ) &
  1002. print*, 'io_read2d_32g: Successfully retrieved '//name//' from file'
  1003. else
  1004. print*, 'io_read2d_32g: Failed to read '//name//' from file'
  1005. return
  1006. end if
  1007. data = hdfr
  1008. istat = sfendacc(sds_id)
  1009. deallocate(hdfr)
  1010. ifail = 0
  1011. end subroutine read_it
  1012. end subroutine io_read2d_32g
  1013. subroutine io_read2d_64g(sd_id,im,jm,data,name,ifail,index,idate)
  1014. implicit none
  1015. ! in/out:
  1016. integer, intent(in) :: im,jm
  1017. integer, intent(in) :: sd_id
  1018. character(len=*),intent(in) :: name
  1019. real,dimension(im,jm),intent(out) :: data
  1020. integer,intent(out) :: ifail
  1021. integer,dimension(6), optional :: idate
  1022. integer,optional :: index
  1023. ! local
  1024. integer,dimension(6) :: idate_file
  1025. integer, parameter :: MAX_VAR_DIMS = 32
  1026. character(len=64) :: xname,attr_name
  1027. integer(kind=4) :: rank
  1028. integer :: sds_id
  1029. integer :: istat, attributes, num_type, n_values, data_type
  1030. integer :: sffinfo, sfselect, sfginfo, sfrattr
  1031. integer :: sfendacc, sfend, sfrnatt, sfrcatt
  1032. integer :: sffattr, sfrdata, sfn2index
  1033. integer :: sfgainfo,attr_index,lname
  1034. integer :: num_ds,num_at,ids
  1035. integer(kind=4),dimension(MAX_VAR_DIMS):: dim_sizes
  1036. integer :: idebug = 0
  1037. ifail = 1
  1038. lname = len_trim(name)
  1039. istat = sffinfo(sd_id,num_ds,num_at)
  1040. if ( present(index) ) then
  1041. sds_id = sfselect(sd_id, index)
  1042. istat = sfginfo(sds_id, xname, rank, dim_sizes, num_type, attributes)
  1043. if ( idebug >= 100 ) then
  1044. write(*,*) 'io_read2d_64g: name = ', name,xname(1:lname)
  1045. write(*,*) 'io_read2d_64g: rank = ', rank
  1046. write(*,*) 'io_read2d_64g: dims = ', dim_sizes(1:rank)
  1047. write(*,*) 'io_read2d_64g: attr = ', attributes
  1048. end if
  1049. ! check rank and name....
  1050. names: if( +xname(1:lname) == +name(1:lname) .and. &
  1051. rank == 2 .and. &
  1052. dim_sizes(1) == im .and. &
  1053. dim_sizes(2) == jm ) then
  1054. if ( present(idate) ) then
  1055. attr_index = sffattr(sds_id, 'idate')
  1056. istat = &
  1057. sfgainfo(sds_id, attr_index, attr_name, data_type, n_values)
  1058. istat = sfrattr(sds_id, attr_index, idate_file)
  1059. if ( idebug >= 100 ) then
  1060. write(*,*) 'io_read2d_64g: idate = ', idate,idate_file
  1061. end if
  1062. if ( sum(abs(idate-idate_file)) == 0 ) then
  1063. call read_it !everything OK....proceed
  1064. return !and return
  1065. end if
  1066. else
  1067. call read_it !everything OK....proceed
  1068. return !and return
  1069. end if
  1070. end if names
  1071. istat = sfendacc(sds_id) !close 'wrong' ds
  1072. end if
  1073. dsloop: do ids=0,num_ds-1
  1074. sds_id = sfselect(sd_id, ids)
  1075. istat = sfginfo(sds_id, xname, rank, dim_sizes, num_type, attributes)
  1076. if ( idebug >= 100 ) then
  1077. write(*,*) 'io_read2d_64g: name = ', name,xname(1:lname)
  1078. write(*,*) 'io_read2d_64g: rank = ', rank
  1079. write(*,*) 'io_read2d_64g: dims = ', dim_sizes(1:rank)
  1080. write(*,*) 'io_read2d_64g: attr = ', attributes
  1081. end if
  1082. ! check rank and name....
  1083. if ( +xname(1:lname) /= +name(1:lname) .or. &
  1084. rank /= 2 .or. &
  1085. dim_sizes(1) /= im .or. &
  1086. dim_sizes(2) /= jm ) then
  1087. istat = sfendacc(sds_id) !close 'wrong' ds
  1088. cycle dsloop
  1089. end if
  1090. if ( present(idate) ) then
  1091. attr_index = sffattr(sds_id, 'idate')
  1092. istat = sfgainfo(sds_id, attr_index, attr_name, data_type, n_values)
  1093. istat = sfrattr(sds_id, attr_index, idate_file)
  1094. if ( idebug >= 100 ) then
  1095. write(*,*) 'io_read2d_64g: idate = ', idate,idate_file
  1096. end if
  1097. if( sum(abs(idate-idate_file)) /= 0 ) then
  1098. istat = sfendacc(sds_id) !close 'wrong' ds
  1099. cycle dsloop
  1100. end if
  1101. end if
  1102. call read_it !everything OK
  1103. ! set index to next position if present...
  1104. if ( present(index) ) index=ids+1
  1105. return
  1106. end do dsloop
  1107. print*, 'io_read2d_64g: Could not find '//name//' from file'
  1108. if ( present(idate) ) print *,'io_read2d_64g: With date..',idate
  1109. contains
  1110. subroutine read_it
  1111. integer :: sfrdata
  1112. real ,dimension(:,:),allocatable :: hdfr
  1113. allocate(hdfr(im,jm))
  1114. istat = sfrdata(sds_id, (/0,0/),(/1,1/),(/im,jm/),hdfr)
  1115. if ( istat == SUCCEED ) then
  1116. if ( idebug >= 100 ) &
  1117. print*, 'io_read2d_64g: Successfully retrieved '//name//' from file'
  1118. else
  1119. print*, 'io_read2d_64g: Failed to read '//name//' from file'
  1120. return
  1121. end if
  1122. data = hdfr
  1123. istat = sfendacc(sds_id)
  1124. deallocate(hdfr)
  1125. ifail = 0
  1126. end subroutine read_it
  1127. end subroutine io_read2d_64g
  1128. subroutine io_read3d_64g(sd_id,im,jm,lm,data,name,ifail,index,idate)
  1129. implicit none
  1130. ! in/out:
  1131. integer, intent(in) :: im,jm,lm
  1132. integer, intent(in) :: sd_id
  1133. character(len=*),intent(in) :: name
  1134. real,dimension(im,jm,lm),intent(out) :: data
  1135. integer,intent(out) :: ifail
  1136. integer,dimension(6), optional :: idate
  1137. integer,optional :: index
  1138. ! local
  1139. integer,dimension(6) :: idate_file
  1140. integer, parameter :: MAX_VAR_DIMS = 32
  1141. character(len=64) :: xname,attr_name
  1142. integer(kind=4) :: rank
  1143. integer :: sds_id
  1144. integer :: istat, attributes, num_type, n_values, data_type
  1145. integer :: sffinfo, sfselect, sfginfo, sfrattr
  1146. integer :: sfendacc, sfend, sfrnatt, sfrcatt
  1147. integer :: sffattr, sfrdata, sfn2index
  1148. integer :: sfgainfo,attr_index,lname
  1149. integer :: num_ds,num_at,ids
  1150. integer(kind=4),dimension(MAX_VAR_DIMS):: dim_sizes
  1151. integer :: idebug = 0
  1152. ifail = 1
  1153. lname = len_trim(name)
  1154. istat = sffinfo(sd_id,num_ds,num_at)
  1155. if ( present(index) ) then
  1156. sds_id = sfselect(sd_id, index)
  1157. istat = sfginfo(sds_id, xname, rank, dim_sizes, num_type, attributes)
  1158. if ( idebug >= 100 ) then
  1159. write(*,*) 'io_read3d_64g: name = ', name,xname(1:lname)
  1160. write(*,*) 'io_read3d_64g: rank = ', rank
  1161. write(*,*) 'io_read3d_64g: dims = ', dim_sizes(1:rank)
  1162. write(*,*) 'io_read3d_64g: attr = ', attributes
  1163. end if
  1164. ! check rank and name....
  1165. names: if( +xname(1:lname) == +name(1:lname) .and. &
  1166. rank == 3 .and. &
  1167. dim_sizes(1) == im .and. &
  1168. dim_sizes(2) == jm .and. &
  1169. dim_sizes(3) == lm ) then
  1170. if ( present(idate) ) then
  1171. attr_index = sffattr(sds_id, 'idate')
  1172. istat = &
  1173. sfgainfo(sds_id, attr_index, attr_name, data_type, n_values)
  1174. istat = sfrattr(sds_id, attr_index, idate_file)
  1175. if ( idebug >= 100 ) then
  1176. write(*,*) 'io_read3d_64g: idate = ', idate,idate_file
  1177. end if
  1178. if ( sum(abs(idate-idate_file)) == 0 ) then
  1179. call read_it !everything OK....proceed
  1180. return !and return
  1181. end if
  1182. else
  1183. call read_it !everything OK....proceed
  1184. return !and return
  1185. end if
  1186. end if names
  1187. istat = sfendacc(sds_id) !close 'wrong' ds
  1188. end if
  1189. dsloop: do ids=0,num_ds-1
  1190. sds_id = sfselect(sd_id, ids)
  1191. istat = sfginfo(sds_id, xname, rank, dim_sizes, num_type, attributes)
  1192. if ( idebug >= 100 ) then
  1193. write(*,*) 'io_read3d_64g: name = ', name,xname(1:lname)
  1194. write(*,*) 'io_read3d_64g: rank = ', rank
  1195. write(*,*) 'io_read3d_64g: dims = ', dim_sizes(1:rank)
  1196. write(*,*) 'io_read3d_64g: attr = ', attributes
  1197. end if
  1198. ! check rank and name....
  1199. if ( +xname(1:lname) /= +name(1:lname) .or. &
  1200. rank /= 3 .or. &
  1201. dim_sizes(1) /= im .or. &
  1202. dim_sizes(2) /= jm .or. &
  1203. dim_sizes(3) /= lm ) then
  1204. istat = sfendacc(sds_id) !close 'wrong' ds
  1205. cycle dsloop
  1206. end if
  1207. if ( present(idate) ) then
  1208. attr_index = sffattr(sds_id, 'idate')
  1209. istat = sfgainfo(sds_id, attr_index, attr_name, data_type, n_values)
  1210. istat = sfrattr(sds_id, attr_index, idate_file)
  1211. if ( idebug >= 100 ) then
  1212. write(*,*) 'io_read3d_64g: idate = ', idate,idate_file
  1213. end if
  1214. if( sum(abs(idate-idate_file)) /= 0 ) then
  1215. istat = sfendacc(sds_id) !close 'wrong' ds
  1216. cycle dsloop
  1217. end if
  1218. end if
  1219. call read_it !everything OK
  1220. ! set index to next position if present...
  1221. if ( present(index) ) index=ids+1
  1222. return
  1223. end do dsloop
  1224. print*, 'io_read3d_64g: Could not find '//name//' from file'
  1225. if ( present(idate) ) print *,'io_read3d_64g: With date..',idate
  1226. contains
  1227. subroutine read_it
  1228. integer :: sfrdata
  1229. real ,dimension(:,:,:),allocatable :: hdfr
  1230. allocate(hdfr(im,jm,lm))
  1231. istat = sfrdata(sds_id, (/0,0,0/),(/1,1,1/),(/im,jm,lm/),hdfr)
  1232. if ( istat == SUCCEED ) then
  1233. if ( idebug >= 100 ) &
  1234. print*, 'io_read3d_64g: Successfully retrieved '//name//' from file'
  1235. else
  1236. print*, 'io_read3d_64g: Failed to read '//name//' from file'
  1237. return
  1238. end if
  1239. data = hdfr
  1240. istat = sfendacc(sds_id)
  1241. deallocate(hdfr)
  1242. ifail = 0
  1243. end subroutine read_it
  1244. end subroutine io_read3d_64g
  1245. subroutine io_read3d_32g(sd_id,im,jm,lm,data,name,ifail,index,idate)
  1246. implicit none
  1247. ! in/out:
  1248. integer, intent(in) :: im,jm,lm
  1249. integer, intent(in) :: sd_id
  1250. character(len=*),intent(in) :: name
  1251. real,dimension(im,jm,lm),intent(out) :: data
  1252. integer,intent(out) :: ifail
  1253. integer,optional :: index
  1254. integer,dimension(6), optional :: idate
  1255. ! local
  1256. integer,dimension(6):: idate_file
  1257. integer, parameter :: MAX_VAR_DIMS = 32
  1258. character(len=64) :: xname,attr_name
  1259. integer(kind=4) :: rank
  1260. integer :: sds_id
  1261. integer :: istat, attributes, num_type, n_values, data_type
  1262. integer :: sffinfo, sfselect, sfginfo, sfrattr
  1263. integer :: sfendacc, sfend, sfrnatt, sfrcatt
  1264. integer :: sffattr, sfrdata, sfn2index
  1265. integer :: sfgainfo,attr_index,lname
  1266. integer :: num_ds,num_at,ids
  1267. integer(kind=4),dimension(MAX_VAR_DIMS):: dim_sizes
  1268. integer :: idebug = 0
  1269. ifail = 1
  1270. lname = len_trim(name)
  1271. istat = sffinfo(sd_id,num_ds,num_at)
  1272. if ( present(index) ) then
  1273. sds_id = sfselect(sd_id, index)
  1274. istat = sfginfo(sds_id, xname, rank, dim_sizes, num_type, attributes)
  1275. if ( idebug >= 100 ) then
  1276. write(*,*) 'io_read3d_32g: name = ', name,xname(1:lname)
  1277. write(*,*) 'io_read3d_32g: rank = ', rank
  1278. write(*,*) 'io_read3d_32g: dims = ', dim_sizes(1:rank)
  1279. write(*,*) 'io_read3d_32g: attr = ', attributes
  1280. end if
  1281. !check rank and name....
  1282. names: if ( +xname(1:lname) == +name(1:lname) .and. &
  1283. rank == 3 .and. &
  1284. dim_sizes(1) == im .and. &
  1285. dim_sizes(2) == jm .and. &
  1286. dim_sizes(3) == lm ) then
  1287. if ( present(idate) ) then
  1288. attr_index = sffattr(sds_id, 'idate')
  1289. istat = sfgainfo(sds_id, attr_index, attr_name, data_type, n_values)
  1290. istat = sfrattr(sds_id, attr_index, idate_file)
  1291. if ( idebug >= 100 ) then
  1292. write(*,*) 'io_read3d_32g: idate = ', idate,idate_file
  1293. end if
  1294. if ( sum(abs(idate-idate_file)) == 0 ) then
  1295. call read_it !everything OK....proceed
  1296. return !and return
  1297. end if
  1298. else
  1299. call read_it !everything OK....proceed
  1300. return !and return
  1301. end if
  1302. end if names
  1303. istat = sfendacc(sds_id) !close 'wrong' ds
  1304. end if
  1305. dsloop: do ids=0,num_ds-1
  1306. sds_id = sfselect(sd_id, ids)
  1307. istat = sfginfo(sds_id, xname, rank, dim_sizes, num_type, attributes)
  1308. if ( idebug >= 100 ) then
  1309. write(*,*) 'io_read3d_32g: name = ', name,xname(1:lname)
  1310. write(*,*) 'io_read3d_32g: rank = ', rank
  1311. write(*,*) 'io_read3d_32g: dims = ', dim_sizes(1:rank)
  1312. write(*,*) 'io_read3d_32g: attr = ', attributes
  1313. end if
  1314. !check rank and name....
  1315. if( +xname(1:lname) /= +name(1:lname) .or. &
  1316. rank /= 3 .or. &
  1317. dim_sizes(1) /= im .or. &
  1318. dim_sizes(2) /= jm .or. &
  1319. dim_sizes(3) /= lm ) then
  1320. istat = sfendacc(sds_id) !close 'wrong' ds
  1321. cycle dsloop
  1322. end if
  1323. if(present (idate)) then
  1324. attr_index = sffattr(sds_id, 'idate')
  1325. istat = sfgainfo(sds_id, attr_index, attr_name, data_type, n_values)
  1326. istat = sfrattr(sds_id, attr_index, idate_file)
  1327. if ( idebug >= 100 ) then
  1328. write(*,*) 'io_read3d_32g: idate = ', idate,idate_file
  1329. end if
  1330. if(sum(abs(idate-idate_file)) /= 0) then
  1331. istat = sfendacc(sds_id) !close 'wrong' ds
  1332. cycle dsloop
  1333. end if
  1334. end if
  1335. call read_it !everything OK
  1336. if(present(index)) index=ids+1 !set index to next position if present
  1337. return
  1338. end do dsloop
  1339. print *, 'io_read2d_32g: Could not find requested data set in hdf file:'
  1340. print *, 'io_read3d_32g: name : ', trim(name)
  1341. print *, 'io_read3d_32g: shape : ', im, jm, lm
  1342. if (present(idate)) print *, 'io_read3d_32g: idate : ', idate
  1343. if (present(index)) print *, 'io_read3d_32g: index : ', index
  1344. contains
  1345. subroutine read_it
  1346. integer :: sfrdata
  1347. real(kind=4),dimension(:,:,:),allocatable :: hdfr
  1348. allocate(hdfr(im,jm,lm))
  1349. istat = sfrdata(sds_id, (/0,0,0/),(/1,1,1/),(/im,jm,lm/),hdfr)
  1350. if (istat == SUCCEED) then
  1351. if ( idebug >= 100 ) &
  1352. print*, 'io_read3d_32g: Successfully retrieved '// &
  1353. name//' from file'
  1354. else
  1355. print*, 'io_read3d_32g: Failed to read '//name//' from file'
  1356. return
  1357. end if
  1358. data = hdfr
  1359. istat = sfendacc(sds_id)
  1360. deallocate(hdfr)
  1361. ifail = 0
  1362. end subroutine read_it
  1363. end subroutine io_read3d_32g
  1364. subroutine io_read4d_32g(sd_id,im,jm,lm,nt,data,name,ifail,index,idate)
  1365. implicit none
  1366. ! in/out:
  1367. integer, intent(in) :: im,jm,lm,nt
  1368. integer, intent(in) :: sd_id
  1369. character(len=*),intent(in) :: name
  1370. real,dimension(im,jm,lm,nt),intent(out) :: data
  1371. integer,intent(out) :: ifail
  1372. integer,dimension(6), optional :: idate
  1373. ! local
  1374. integer,dimension(6):: idate_file
  1375. integer, parameter :: MAX_VAR_DIMS = 32
  1376. character(len=64) :: xname,attr_name
  1377. integer,optional :: index
  1378. integer(kind=4) :: rank
  1379. integer :: sds_id
  1380. integer :: istat, attributes, num_type, n_values, data_type
  1381. integer :: sfselect, sfginfo, sfrattr
  1382. integer :: sfendacc, sfend, sfrnatt, sfrcatt
  1383. integer :: sffattr, sfrdata, sfn2index
  1384. integer :: sfgainfo,attr_index,lname
  1385. integer :: sffinfo,num_ds,num_at,ids
  1386. integer(kind=4),dimension(MAX_VAR_DIMS):: dim_sizes
  1387. integer :: idebug = 0
  1388. ifail = 1
  1389. lname = len_trim(name)
  1390. istat = sffinfo(sd_id,num_ds,num_at)
  1391. if ( idebug >= 100 ) then
  1392. print *, 'io_read4d_32g: # ds & att',sd_id,num_ds,num_at
  1393. end if
  1394. if ( present(index) ) then
  1395. sds_id = sfselect(sd_id, index)
  1396. if ( sds_id < 0 ) then
  1397. write(*,*) 'io_read4d_32g: could not select sds for index ', index
  1398. ifail=-1; return
  1399. end if
  1400. istat = sfginfo(sds_id, xname, rank, dim_sizes, num_type, attributes)
  1401. if ( idebug >= 100 ) then
  1402. write(*,*) 'io_read4d_32g: name = ', name,xname(1:lname)
  1403. write(*,*) 'io_read4d_32g: rank = ', rank
  1404. write(*,*) 'io_read4d_32g: dims = ', dim_sizes(1:rank)
  1405. write(*,*) 'io_read4d_32g: attr = ', attributes
  1406. end if
  1407. !check rank and name....
  1408. names: if( +xname(1:lname) == +name(1:lname) .and. &
  1409. rank == 4 .and. &
  1410. dim_sizes(1) == im .and. &
  1411. dim_sizes(2) == jm .and. &
  1412. dim_sizes(4) == nt .and. &
  1413. dim_sizes(3) == lm ) then
  1414. if ( present(idate) ) then
  1415. attr_index = sffattr(sds_id, 'idate')
  1416. istat = sfgainfo(sds_id, attr_index, attr_name, data_type, n_values)
  1417. istat = sfrattr(sds_id, attr_index, idate_file)
  1418. if (idebug >= 100 ) then
  1419. write(*,*) 'io_read4d_32g: idate = ', idate,idate_file
  1420. end if
  1421. if( sum(abs(idate-idate_file)) == 0 ) then
  1422. call read_it
  1423. return !and return
  1424. end if
  1425. else
  1426. call read_it
  1427. return !and return
  1428. end if
  1429. end if names
  1430. istat = sfendacc(sds_id) !close 'wrong' ds
  1431. end if
  1432. dsloop: do ids=0,num_ds-1
  1433. sds_id = sfselect(sd_id, ids)
  1434. istat = sfginfo(sds_id, xname, rank, dim_sizes, num_type, attributes)
  1435. if (idebug >= 100 ) then
  1436. write(*,*) 'io_read4d_32g: name = ', name,xname(1:lname)
  1437. write(*,*) 'io_read4d_32g: rank = ', rank
  1438. write(*,*) 'io_read4d_32g: dims = ', dim_sizes(1:rank)
  1439. write(*,*) 'io_read4d_32g: attr = ', attributes
  1440. end if
  1441. !check rank and name....
  1442. if ( +xname(1:lname) /= +name(1:lname) .or. &
  1443. rank /= 4 .or. &
  1444. dim_sizes(1) /= im .or. &
  1445. dim_sizes(2) /= jm .or. &
  1446. dim_sizes(4) /= nt .or. &
  1447. dim_sizes(3) /= lm ) then
  1448. istat = sfendacc(sds_id) !close 'wrong' ds
  1449. cycle dsloop
  1450. end if
  1451. if ( present(idate) ) then
  1452. attr_index = sffattr(sds_id, 'idate')
  1453. istat = sfgainfo(sds_id, attr_index, attr_name, data_type, n_values)
  1454. istat = sfrattr(sds_id, attr_index, idate_file)
  1455. if ( idebug >= 100 ) then
  1456. write(*,*) 'io_read4d_32g: idate = ', idate,idate_file
  1457. end if
  1458. if ( sum(abs(idate-idate_file)) /= 0 ) then
  1459. istat = sfendacc(sds_id) !close 'wrong' ds
  1460. cycle dsloop
  1461. end if
  1462. end if
  1463. call read_it
  1464. return
  1465. end do dsloop
  1466. print*, 'io_read4d_32g: Could not find '//name//' from file'
  1467. print*, 'io_read4d_32g: With dimensions',im,jm,lm,nt
  1468. if ( present(idate) ) print *,'io_read4d_32g: With date..',idate
  1469. contains
  1470. subroutine read_it
  1471. integer :: sfrdata
  1472. real(kind=4),dimension(:,:,:,:),allocatable :: hdfr
  1473. allocate(hdfr(im,jm,lm,nt))
  1474. istat = sfrdata(sds_id, (/0,0,0,0/),(/1,1,1,1/),(/im,jm,lm,nt/),hdfr)
  1475. if ( istat == SUCCEED ) then
  1476. if ( idebug >= 100 ) &
  1477. print*, 'io_read4d_32g: Successfully retrieved '// &
  1478. name//' from file'
  1479. else
  1480. print*, 'io_read4d_32g: Failed to read '//name//' from file'
  1481. return
  1482. end if
  1483. data = hdfr
  1484. istat = sfendacc(sds_id)
  1485. deallocate(hdfr)
  1486. ifail = 0
  1487. end subroutine read_it
  1488. end subroutine io_read4d_32g
  1489. subroutine io_read4d_32d(sd_id,im,jm,lm,ntrace,data,name,index,ifail,idate)
  1490. implicit none
  1491. ! in/out:
  1492. integer, intent(in) :: im,jm,lm,ntrace
  1493. integer, intent(in) :: sd_id
  1494. real,dimension(im,jm,lm,ntrace),intent(out) :: data
  1495. character(len=*), intent(in) :: name
  1496. integer,intent(inout) :: index
  1497. integer,intent(out) :: ifail
  1498. integer,dimension(6),intent(in) :: idate
  1499. ! local
  1500. integer,dimension(6) :: idate_file
  1501. integer, parameter :: MAX_VAR_DIMS = 32
  1502. character(len=64) :: xname,attr_name
  1503. integer(kind=4) :: rank
  1504. integer :: sds_id
  1505. integer :: istat, attributes, num_type, n_values, data_type
  1506. integer :: sffinfo, sfselect, sfginfo, sfrattr
  1507. integer :: sfendacc, sfend, sfrnatt, sfrcatt
  1508. integer :: sffattr, sfrdata, sfn2index
  1509. integer :: sfgainfo,attr_index,lname
  1510. integer :: num_ds,num_at,ids
  1511. integer(kind=4),dimension(MAX_VAR_DIMS) :: dim_sizes
  1512. real(kind=4),dimension(:,:,:,:),allocatable :: hdfr
  1513. ifail = 1
  1514. sds_id = sfselect(sd_id, index)
  1515. istat = sfginfo(sds_id, xname, rank, dim_sizes, num_type, attributes)
  1516. lname = len_trim(name)
  1517. write(*,*) 'io_read4d_32d: name = ', name
  1518. write(*,*) 'io_read4d_32d: rank = ', rank
  1519. write(*,*) 'io_read4d_32d: dims = ', dim_sizes(1:rank)
  1520. write(*,*) 'io_read4d_32d: attr = ', attributes
  1521. attr_index = sffattr(sds_id, 'idate')
  1522. istat = sfgainfo(sds_id, attr_index, attr_name, data_type, n_values)
  1523. istat = sfrattr(sds_id, attr_index, idate_file)
  1524. !if( sum(abs(idate_file-idate)) == 0 .and. &
  1525. print*,'io_read4d_32d: ****',im,jm,lm,ntrace,dim_sizes(1:4)
  1526. if( &
  1527. rank == 4 .and. &
  1528. dim_sizes(1) == im .and. &
  1529. dim_sizes(2) == jm .and. &
  1530. dim_sizes(3) == lm .and. &
  1531. dim_sizes(4) == ntrace ) then
  1532. allocate(hdfr(im,jm,lm,ntrace))
  1533. istat = sfrdata(sds_id, (/0,0,0,0/),(/1,1,1,1/),(/im,jm,lm,ntrace/),hdfr)
  1534. if ( istat == SUCCEED ) then
  1535. print*, 'io_read4d_32d: Successfully retrieved '//name//' from file'
  1536. else
  1537. print*, 'io_read4d_32d: Failed to read '//name//' from file'
  1538. return
  1539. end if
  1540. data = hdfr
  1541. istat = sfendacc(sds_id)
  1542. deallocate(hdfr)
  1543. ifail = 0
  1544. index = index+1
  1545. end if
  1546. end subroutine io_read4d_32d
  1547. subroutine io_read3d_32dr(sd_id,im,jm,lm,data,name,index,ifail,idate,region)
  1548. implicit none
  1549. ! in/out:
  1550. integer, intent(in) :: im,jm,lm
  1551. integer, intent(in) :: sd_id
  1552. real,dimension(im,jm,lm),intent(out) :: data
  1553. character(len=*),intent(in) :: name
  1554. integer,intent(inout) :: index
  1555. integer,intent(out) :: ifail
  1556. integer,dimension(6),intent(in) :: idate
  1557. integer,intent(in) :: region
  1558. ! local
  1559. integer,dimension(6) :: idate_file
  1560. integer :: region_file
  1561. integer, parameter :: MAX_VAR_DIMS = 32
  1562. character(len=64) :: xname,attr_name
  1563. integer :: rank, sds_id
  1564. integer :: istat, attributes, num_type, n_values, data_type
  1565. integer :: sffinfo, sfselect, sfginfo, sfrattr
  1566. integer :: sfendacc, sfend, sfrnatt, sfrcatt
  1567. integer :: sffattr, sfrdata, sfn2index
  1568. integer :: sfgainfo,attr_index
  1569. integer,dimension(MAX_VAR_DIMS) :: dim_sizes
  1570. real(kind=4),dimension(:,:,:),allocatable :: hdfr
  1571. ifail = 1
  1572. sds_id = sfselect(sd_id, index)
  1573. istat = sfginfo(sds_id, xname, rank, dim_sizes, num_type, attributes)
  1574. !write(*,*) 'name = ', name
  1575. !write(*,*) 'rank = ', rank
  1576. !write(*,*) 'dims = ', dim_sizes(1:rank)
  1577. !write(*,*) 'attr = ', attributes
  1578. attr_index = sffattr(sds_id, 'region')
  1579. istat = sfgainfo(sds_id, attr_index , attr_name, data_type, n_values)
  1580. !print*,'attr_name',attr_name
  1581. !print*,'n_values',n_values
  1582. istat = sfrattr(sds_id, attr_index, region_file)
  1583. attr_index = sffattr(sds_id, 'idate')
  1584. istat = sfgainfo(sds_id, attr_index, attr_name, data_type, n_values)
  1585. !print*,'attr_name',attr_name
  1586. !print*,'n_values',n_values
  1587. istat = sfrattr(sds_id, attr_index, idate_file)
  1588. !print *,idate_file,idate,region_file,region
  1589. if ( sum(abs(idate_file-idate)) == 0 .and. &
  1590. region_file == region .and. &
  1591. rank == 3 .and. &
  1592. dim_sizes(1) == im .and. &
  1593. dim_sizes(2) == jm .and. &
  1594. dim_sizes(3) == lm ) then
  1595. allocate(hdfr(im,jm,lm))
  1596. istat = sfrdata(sds_id, (/0,0,0/),(/1,1,1/),(/im,jm,lm/),hdfr)
  1597. if ( istat == SUCCEED ) then
  1598. print*, 'io_read3d_32dr: Successfully retrieved '//name//' from file'
  1599. else
  1600. print*, 'io_read3d_32dr: Failed to read '//name//' from file'
  1601. return
  1602. end if
  1603. data = hdfr
  1604. istat = sfendacc(sds_id)
  1605. deallocate(hdfr)
  1606. ifail = 0
  1607. index = index+1
  1608. end if
  1609. end subroutine io_read3d_32dr
  1610. subroutine io_read2d_32d(sd_id,im,jm,data,name,index,ifail,idate)
  1611. implicit none
  1612. ! in/out:
  1613. integer, intent(in) :: im,jm
  1614. integer, intent(in) :: sd_id
  1615. real,dimension(im,jm),intent(out) :: data
  1616. character(len=*), intent(in) :: name
  1617. integer,intent(inout) :: index
  1618. integer,intent(out) :: ifail
  1619. integer,dimension(6),intent(in) :: idate
  1620. ! local
  1621. integer,dimension(6) :: idate_file
  1622. integer, parameter :: MAX_VAR_DIMS = 32
  1623. character(len=64) :: xname,attr_name
  1624. integer :: rank, sds_id
  1625. integer :: istat, attributes, num_type, n_values, data_type
  1626. integer :: sffinfo, sfselect, sfginfo, sfrattr
  1627. integer :: sfendacc, sfend, sfrnatt, sfrcatt
  1628. integer :: sffattr, sfrdata, sfn2index
  1629. integer :: sfgainfo,attr_index
  1630. integer,dimension(MAX_VAR_DIMS) :: dim_sizes
  1631. real(kind=4),dimension(:,:),allocatable :: hdfr
  1632. ifail = 1
  1633. sds_id = sfselect(sd_id, index)
  1634. istat = sfginfo(sds_id, xname, rank, dim_sizes, num_type, attributes)
  1635. write(*,*) 'io_read2d_32d: name = ', name
  1636. write(*,*) 'io_read2d_32d: rank = ', rank
  1637. write(*,*) 'io_read2d_32d: dims = ', dim_sizes(1:rank)
  1638. write(*,*) 'io_read2d_32d: attr = ', attributes
  1639. attr_index = sffattr(sds_id, 'idate')
  1640. istat = sfgainfo(sds_id, attr_index, attr_name, data_type, n_values)
  1641. istat = sfrattr(sds_id, attr_index, idate_file)
  1642. if( &
  1643. rank == 2 .and. &
  1644. dim_sizes(1) == im .and. &
  1645. dim_sizes(2) == jm ) then
  1646. allocate(hdfr(im,jm))
  1647. istat = sfrdata(sds_id, (/0,0/),(/1,1/),(/im,jm/),hdfr)
  1648. if ( istat == SUCCEED ) then
  1649. print*, 'io_read2d_32d: Successfully retrieved '//name//' from file'
  1650. else
  1651. print*, 'io_read2d_32d: Failed to read '//name//' from file'
  1652. return
  1653. end if
  1654. data = hdfr
  1655. istat = sfendacc(sds_id)
  1656. deallocate(hdfr)
  1657. ifail = 0
  1658. index = index+1
  1659. end if
  1660. end subroutine io_read2d_32d
  1661. subroutine io_read2d_32dr(sd_id,im,jm,data,name,index,ifail,idate,region)
  1662. implicit none
  1663. ! in/out:
  1664. integer,intent(in) :: im,jm
  1665. integer,intent(in) :: sd_id
  1666. real,dimension(im,jm),intent(out) :: data
  1667. character(len=*),intent(in) :: name
  1668. integer,intent(inout) :: index
  1669. integer,intent(out) :: ifail
  1670. integer,dimension(6),intent(in) :: idate
  1671. integer,intent(in) :: region
  1672. ! local
  1673. integer,dimension(6) :: idate_file
  1674. integer :: region_file
  1675. integer, parameter :: MAX_VAR_DIMS = 32
  1676. character(len=64) :: xname,attr_name
  1677. integer :: rank, sds_id
  1678. integer :: istat, attributes, num_type, n_values, data_type
  1679. integer :: sffinfo, sfselect, sfginfo, sfrattr
  1680. integer :: sfendacc, sfend, sfrnatt, sfrcatt
  1681. integer :: sffattr, sfrdata, sfn2index
  1682. integer :: sfgainfo,attr_index
  1683. integer,dimension(MAX_VAR_DIMS) :: dim_sizes
  1684. real(kind=4),dimension(:,:),allocatable :: hdfr
  1685. ifail = 1
  1686. sds_id = sfselect(sd_id, index)
  1687. istat = sfginfo(sds_id, xname, rank, dim_sizes, num_type, attributes)
  1688. !write(*,*) 'name = ', name
  1689. !write(*,*) 'rank = ', rank
  1690. !write(*,*) 'dims = ', dim_sizes(1:rank)
  1691. !write(*,*) 'attr = ', attributes
  1692. attr_index = sffattr(sds_id, 'region')
  1693. istat = sfgainfo(sds_id, attr_index , attr_name, data_type, n_values)
  1694. !print*,'attr_name',attr_name
  1695. !print*,'n_values',n_values
  1696. istat = sfrattr(sds_id, attr_index, region_file)
  1697. attr_index = sffattr(sds_id, 'idate')
  1698. istat = sfgainfo(sds_id, attr_index, attr_name, data_type, n_values)
  1699. !print*,'attr_name',attr_name
  1700. !print*,'n_values',n_values
  1701. istat = sfrattr(sds_id, attr_index, idate_file)
  1702. !print *,idate_file,idate,region_file,region
  1703. if( sum(abs(idate_file-idate)) == 0 .and. &
  1704. region_file == region .and. &
  1705. rank == 2 .and. &
  1706. dim_sizes(1) == im .and. &
  1707. dim_sizes(2) == jm ) then
  1708. allocate(hdfr(im,jm))
  1709. istat = sfrdata(sds_id, (/0,0/),(/1,1/),(/im,jm/),hdfr)
  1710. if ( istat == SUCCEED ) then
  1711. print*, 'io_read2d_32dr: Successfully retrieved '//name//' from file'
  1712. else
  1713. print*, 'io_read2d_32dr: Failed to read '//name//' from file'
  1714. return
  1715. end if
  1716. data = hdfr
  1717. istat = sfendacc(sds_id)
  1718. deallocate(hdfr)
  1719. ifail = 0
  1720. index = index+1
  1721. end if
  1722. end subroutine io_read2d_32dr
  1723. function upper_case (old) result (new)
  1724. !
  1725. ! returns the upper-case version of the input string
  1726. !
  1727. implicit none
  1728. character(len = *), intent(in) ::old
  1729. character(len = 64) ::new
  1730. integer :: i
  1731. new = old
  1732. do i=1,len_trim(old)
  1733. if( lge (old(i:i), 'a') .and. &
  1734. lle (old(i:i), 'z') ) &
  1735. new(i:i) = achar (iachar(old(i:i)) - 32)
  1736. end do
  1737. end function upper_case
  1738. end module io_hdf