read_enact.pro 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. PRO read_enact, Files, DepRange=DepRange, $
  2. NumData=NumData, $
  3. OutLats=OutLats, OutLons=OutLons, Instruments=OutInst, $
  4. Platform=OutPlatform, $
  5. OutDeps=OutDeps, OutSObs=OutSObs, OutSQC=OutSQC, $
  6. OutTObs=OutTObs, OutTQC=OutTQC, OutDates=OutDates, $
  7. OutTMod=OutTMod, OutSMod=OutSMod, rmdi=rmdi, quiet=quiet, $
  8. iobs=outiobs, jobs=outjobs, kobs=outkobs, $
  9. ProfileNum=OutProfileNum
  10. ;------------------------------------------------------------------------------------------
  11. ;Program to read in data from an ENACT format file containing profile data.
  12. ;
  13. ; Inputs:
  14. ; File => File name to be read in
  15. ; DepRange => (optional) Range of depths for which the data is to be extracted.
  16. ;
  17. ; Outputs:
  18. ; NumProfs => Number of profiles -Integer
  19. ; Latitudes => Latitudes(NumProfs) -Real
  20. ; Longitudes => Longitudes(NumProfs) -Real
  21. ; Dates => Dates(NumProfs) -Date/time structure.
  22. ; Instruments => Instrument type (NumProfs) -String
  23. ; Depths => Depths of each ob (NumProfs) -Real
  24. ; Salinity => Salinity (psu) (NumProfs) -Real
  25. ; SalQC => QC flag for salinity (NumProfs) 1=> Good -Int
  26. ; Temperature => Temperature (deg C) (NumProfs) -Real
  27. ; TempQC => QC flag for temperature (NumProfs) 1=> Good -Int
  28. ; ModelT => Model temperature (deg C) (NumProfs) -Real
  29. ; ModelS => Model salinity (psu) (NumProfs) -Real
  30. ; rmdi => Missing data indicator -Real
  31. ;
  32. ;Author: Matt Martin. 5/10/07.
  33. ;------------------------------------------------------------------------------------------
  34. rmdi = 99999.
  35. if NOT keyword_set(DepRange) then DepRange=[0.,1.e1] ;Default to top 10 metres.
  36. NumFiles=n_elements(Files)
  37. ifile2=0
  38. for ifile = 0, NumFiles-1 do begin
  39. File = Files(ifile)
  40. ;1. Open the netcdf file
  41. ncid = ncdf_open(File, /nowrite)
  42. if ncid lt 0 and not keyword_set(quiet) then print, 'Error opening file '+File
  43. if ncid ge 0 then begin
  44. if not keyword_set(quiet) then print, 'Opened file '+File+' successfully'
  45. ;2. Get info about file
  46. ncinfo = ncdf_inquire(ncid)
  47. ; if not keyword_set(quiet) then , ncinfo
  48. ;3. Read in the relevant dimensions
  49. for idim = 0, ncinfo.ndims-1 do begin
  50. ncdf_diminq, ncid, idim, name, dimlen
  51. if name eq 'N_PROF' then NumProfs = dimlen $
  52. else if name eq 'N_LEVELS' then NumLevels = dimlen $
  53. else if name eq 'STRING4' then str4 = dimlen
  54. endfor
  55. ;4. Set up arrays and read in the data
  56. if (NumProfs gt 0) then begin
  57. JulDays = fltarr(NumProfs)
  58. Latitudes = fltarr(NumProfs)
  59. Longitudes = fltarr(NumProfs)
  60. BytInsts = intarr(4, NumProfs)
  61. Instruments = strarr(NumProfs)
  62. Platforms = strarr(NumProfs)
  63. Instrumentsa = strarr(NumLevels, NumProfs)
  64. Platformsa = strarr(NumLevels, NumProfs)
  65. Depths = fltarr(NumLevels, NumProfs)
  66. Salinity = fltarr(NumLevels, NumProfs)
  67. SalQC = intarr(NumLevels, NumProfs)
  68. Temperature = fltarr(NumLevels, NumProfs)
  69. TempQC = intarr(NumLevels, NumProfs)
  70. ModelT = fltarr(NumLevels, NumProfs)
  71. ModelS = fltarr(NumLevels, NumProfs)
  72. ProfileNum = replicate(1,NumLevels)#indgen(NumProfs)
  73. varid = ncdf_varid(ncid, 'JULD')
  74. ncdf_varget, ncid, varid, JulDays
  75. ; print,JulDays
  76. ; BaseDate = var_to_dt(1950,1,1)
  77. ; BaseDate=JULDAY(1,1,1950,0,0)
  78. BaseDate=double(JULDAY(1,1,1950,0,0)) ;should be at 0 UTC
  79. ; info,BaseDate
  80. Dates = replicate(BaseDate, NumLevels,NumProfs)
  81. ; info,dates
  82. for iprof = 0L, NumProfs-1 do begin
  83. secs = JulDays(iprof)* 24. * 60. * 60.
  84. ; dt = dt_add(BaseDate, second=secs)
  85. dt=BaseDate+JulDays(iprof)
  86. ; CALDAT, dt, mon, day, year, hour, minute, second
  87. ; print,dt-BaseDate, mon, day, year, hour, minute, second
  88. Dates(0:NumLevels-1,iprof) = replicate(dt,NumLevels)
  89. endfor
  90. varid = ncdf_varid(ncid, 'LONGITUDE')
  91. ncdf_varget, ncid, varid, Longitudes
  92. varid = ncdf_varid(ncid, 'LATITUDE')
  93. ncdf_varget, ncid, varid, Latitudes
  94. varid = ncdf_varid(ncid, 'DEPH_CORRECTED')
  95. ncdf_varget, ncid, varid, Depths
  96. res = ncdf_attinq( ncid, varid , '_FillValue')
  97. if res.length gt 0 and res.datatype ne "UNKNOWN" then ncdf_attget, ncid, varid, '_FillValue', FillVal $
  98. else begin
  99. res = ncdf_attinq( ncid, varid , '_fillvalue')
  100. ncdf_attget, ncid, varid, '_fillvalue', FillVal
  101. endelse
  102. pts = where(Depths eq FillVal, count)
  103. if count gt 0 then Depths(pts) = rmdi
  104. ; if keyword_set(Salinity) then begin
  105. varid = ncdf_varid(ncid, 'PSAL_CORRECTED')
  106. if (varid ne -1) then begin
  107. ; if not keyword_set(quiet) then print,'reading psal_corrected'
  108. ncdf_varget, ncid, varid, Salinity
  109. res = ncdf_attinq( ncid, varid , '_FillValue')
  110. if res.length gt 0 and res.datatype ne "UNKNOWN" then ncdf_attget, ncid, varid, '_FillValue', FillVal $
  111. else begin
  112. res = ncdf_attinq( ncid, varid , '_fillvalue')
  113. ncdf_attget, ncid, varid, '_fillvalue', FillVal
  114. endelse
  115. pts = where(Salinity eq FillVal, count)
  116. if count gt 0 then Salinity(pts) = rmdi
  117. endif
  118. varid = ncdf_varid(ncid, 'PSAL_CORRECTED_QC')
  119. ncdf_varget, ncid, varid, SalQC
  120. res = ncdf_attinq( ncid, varid , '_FillValue')
  121. if res.length gt 0 and res.datatype ne "UNKNOWN" then ncdf_attget, ncid, varid, '_FillValue', FillVal $
  122. else begin
  123. res = ncdf_attinq( ncid, varid , '_fillvalue')
  124. ncdf_attget, ncid, varid, '_fillvalue', FillVal
  125. endelse
  126. SalQC = SalQC - 48
  127. ; endif
  128. ; if keyword_set(Temperature) then begin
  129. varid = ncdf_varid(ncid, 'POTM_CORRECTED')
  130. if (varid ne -1) then begin
  131. ; if not keyword_set(quiet) then print,'reading potm_corrected'
  132. ncdf_varget, ncid, varid, Temperature
  133. res = ncdf_attinq( ncid, varid , '_FillValue')
  134. if res.length gt 0 and res.datatype ne "UNKNOWN" then ncdf_attget, ncid, varid, '_FillValue', FillVal $
  135. else begin
  136. res = ncdf_attinq( ncid, varid , '_fillvalue')
  137. ncdf_attget, ncid, varid, '_fillvalue', FillVal
  138. endelse
  139. ; if not keyword_set(quiet) then print,'Temp fill value ',fillval
  140. pts = where(Temperature eq FillVal, count)
  141. if count gt 0 then Temperature(pts) = rmdi
  142. endif
  143. varid = ncdf_varid(ncid, 'POTM_CORRECTED_QC')
  144. ncdf_varget, ncid, varid, TempQC
  145. TempQC = TempQC - 48
  146. ; endif
  147. ; if keyword_set(OutTMod) then begin
  148. varid = ncdf_varid(ncid, 'POTM_Hx')
  149. if (varid ne -1) then begin
  150. ncdf_varget, ncid, varid, ModelT
  151. res = ncdf_attinq( ncid, varid , '_FillValue')
  152. if res.length gt 0 and res.datatype ne "UNKNOWN" then ncdf_attget, ncid, varid, '_FillValue', FillVal $
  153. else begin
  154. res = ncdf_attinq( ncid, varid , '_fillvalue')
  155. ncdf_attget, ncid, varid, '_fillvalue', FillVal
  156. endelse
  157. pts = where(ModelT eq FillVal, count)
  158. if count gt 0 then ModelT(pts) = rmdi
  159. endif else begin
  160. ModelT=rmdi
  161. endelse
  162. ; endif
  163. ; if keyword_set(OutSMod) then begin
  164. varid = ncdf_varid(ncid, 'PSAL_Hx')
  165. if (varid ne -1) then begin
  166. ncdf_varget, ncid, varid, ModelS
  167. res = ncdf_attinq( ncid, varid , '_FillValue')
  168. if res.length gt 0 and res.datatype ne "UNKNOWN" then ncdf_attget, ncid, varid, '_FillValue', FillVal $
  169. else begin
  170. res = ncdf_attinq( ncid, varid , '_fillvalue')
  171. ncdf_attget, ncid, varid, '_fillvalue', FillVal
  172. endelse
  173. pts = where(ModelS eq FillVal, count)
  174. if count gt 0 then ModelS(pts) = rmdi
  175. endif else begin
  176. ModelS=rmdi
  177. endelse
  178. ; endif
  179. varid = ncdf_varid(ncid, 'WMO_INST_TYPE')
  180. ncdf_varget, ncid, varid, BytInsts
  181. pts = where(BytInsts(0,*) eq 32 and $
  182. BytInsts(1,*) eq 56 and $
  183. BytInsts(2,*) eq 50 and $
  184. BytInsts(3,*) eq 48, count)
  185. if count gt 0 then Instruments(pts) = 'BUOYS' ; 820
  186. pts = where(BytInsts(0,*) eq 32 and $
  187. BytInsts(1,*) eq 52 and $
  188. BytInsts(2,*) eq 48 and $
  189. BytInsts(3,*) eq 49, count)
  190. if count gt 0 then Instruments(pts) = 'XBT' ; 401
  191. pts = where(BytInsts(0,*) eq 32 and $
  192. BytInsts(1,*) eq 55 and $
  193. BytInsts(2,*) eq 52 and $
  194. BytInsts(3,*) eq 49, count)
  195. if count gt 0 then Instruments(pts) = 'TESAC' ; 741
  196. pts = where(BytInsts(0,*) eq 32 and $
  197. BytInsts(1,*) eq 56 and $
  198. BytInsts(2,*) eq 51 and $
  199. BytInsts(3,*) eq 49, count)
  200. if count gt 0 then Instruments(pts) = 'ARGO' ; 831
  201. pts = where(Instruments eq '', count)
  202. if count gt 0 then Instruments(pts) = 'UNKNOWN'
  203. varid = ncdf_varid(ncid, 'PLATFORM_NUMBER')
  204. ncdf_varget, ncid, varid, platforms
  205. platforms=string(platforms)
  206. for i=0,numlevels-1 do begin
  207. platformsa(i,*)=platforms
  208. instrumentsa(i,*)=instruments
  209. endfor
  210. varid = ncdf_varid(ncid, 'IOBS')
  211. if (varid ne -1) then ncdf_varget, ncid, varid, iobsval
  212. varid = ncdf_varid(ncid, 'JOBS')
  213. if (varid ne -1) then ncdf_varget, ncid, varid, jobsval
  214. varid = ncdf_varid(ncid, 'KOBS')
  215. if (varid ne -1) then ncdf_varget, ncid, varid, kobsval
  216. ;Select those obs in the required depth range
  217. ; Lats = replv(Latitudes(*), [NumLevels,NumProfs],1)
  218. ; Lons = replv(Longitudes(*), [NumLevels,NumProfs],1)
  219. Lons=fltarr(NumLevels,NumProfs)
  220. Lats=fltarr(NumLevels,NumProfs)
  221. iobs=fltarr(NumLevels,NumProfs)
  222. jobs=fltarr(NumLevels,NumProfs)
  223. for i=0L,NumProfs-1 do begin
  224. Lats(*,i)=Latitudes(i)
  225. Lons(*,i)=Longitudes(i)
  226. if (n_elements(iobsval) gt 0) then $
  227. iobs(*,i)=iobsval(i)
  228. if (n_elements(jobsval) gt 0) then $
  229. jobs(*,i)=jobsval(i)
  230. endfor
  231. pts = where(Depths ge DepRange(0) and Depths le DepRange(1), NumData)
  232. if ifile2 eq 0 then begin
  233. OutTObs = [Temperature(pts)]
  234. OutSObs = [Salinity(pts)]
  235. OutTMod = [ModelT(pts)]
  236. OutSMod = [ModelS(pts)]
  237. OutTQC = [TempQC(pts)]
  238. OutSQC = [SalQC(pts)]
  239. OutDeps = [Depths(pts)]
  240. OutLats = [Lats(pts)]
  241. OutLons = [Lons(pts)]
  242. OutDates= [Dates(pts)]
  243. OutInst= [Instrumentsa(pts)]
  244. OutPlatform= [Platformsa(pts)]
  245. OutProfileNum = [ProfileNum(pts)]
  246. if (n_elements(iobsval) gt 0) then outiobs = [iobs(pts)]
  247. if (n_elements(jobsval) gt 0) then outjobs = [jobs(pts)]
  248. if (n_elements(kobsval) gt 0) then outkobs = [kobsval(pts)]
  249. endif else begin
  250. OutTObs = [OutTObs,Temperature(pts)]
  251. OutSObs = [OutSObs,Salinity(pts)]
  252. OutTMod = [OutTMod,ModelT(pts)]
  253. OutSMod = [OutSMod,ModelS(pts)]
  254. OutTQC = [OutTQC,TempQC(pts)]
  255. OutSQC = [OutSQC,SalQC(pts)]
  256. OutDeps = [OutDeps,Depths(pts)]
  257. OutLats = [OutLats,Lats(pts)]
  258. OutLons = [OutLons,Lons(pts)]
  259. OutDates= [OutDates,Dates(pts)]
  260. OutInst= [OutInst,Instrumentsa(pts)]
  261. OutPlatform = [OutPlatform, Platformsa(pts)]
  262. OutProfileNum = [OutProfileNum, ProfileNum(pts)]
  263. if (n_elements(iobsval) gt 0) then outiobs = [outiobs, iobs(pts)]
  264. if (n_elements(jobsval) gt 0) then outjobs = [outjobs, jobs(pts)]
  265. if (n_elements(kobsval) gt 0) then outkobs = [outkobs, kobsval(pts)]
  266. endelse
  267. ifile2=ifile2+1
  268. endif
  269. ncdf_close, ncid
  270. endif
  271. endfor ;ifile
  272. END