read_feedback.pro 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. PRO read_feedback, Files, DepRange=DepRange, VarName=VarNames, NumData=NumData, $
  2. OutLats=OutLats, OutLons=OutLons, $
  3. OutInstruments=OutInst, OutPlatform=OutPlatform, $
  4. OutDeps=OutDeps, $
  5. OutObs1=OutObs1, OutObs2=OutObs2, $
  6. OutMod1=OutMod1, OutMod2=OutMod2, $
  7. OutQC1=OutQC1, OutQC2=OutQC2, $
  8. MDT=MDT, OutDates=OutDates, $
  9. rmdi=rmdi, quiet=quiet, $
  10. OutProfileNum=OutProfileNum
  11. ;------------------------------------------------------------------------------------------
  12. ;Program to read in data from a feedback format file
  13. ;
  14. ; Inputs:
  15. ; File => File name to be read in
  16. ; DepRange => (optional) Range of depths for which the data is to be extracted.
  17. ;
  18. ; Outputs:
  19. ;
  20. ;Author: Matt Martin. 29/09/09
  21. ;------------------------------------------------------------------------------------------
  22. rmdi = 99999.
  23. if NOT keyword_set(DepRange) then DepRange=[0.,1.e1] ;Default to top 10 metres.
  24. NumFiles=n_elements(Files)
  25. ifile2=0
  26. numdata=0
  27. for ifile = 0, NumFiles-1 do begin
  28. File = Files(ifile)
  29. ;1. Open the netcdf file
  30. ncid = ncdf_open(File, /nowrite)
  31. if ncid lt 0 and not keyword_set(quiet) then print, 'Error opening file '+File
  32. if ncid ge 0 then begin
  33. if not keyword_set(quiet) then print, 'Opened file '+File+' successfully'
  34. ;2. Get info about file
  35. ncinfo = ncdf_inquire(ncid)
  36. ;3. Read in the relevant dimensions
  37. NumExtra = 0
  38. for idim = 0, ncinfo.ndims-1 do begin
  39. ncdf_diminq, ncid, idim, name, dimlen
  40. if name eq 'N_OBS' then NumObs = dimlen $
  41. else if name eq 'N_LEVELS' then NumLevels = dimlen $
  42. else if name eq 'N_VARS' then NumVars = dimlen $
  43. else if name eq 'N_QCF' then NumFlags = dimlen $
  44. else if name eq 'N_ENTRIES' then NumEntries = dimlen $
  45. else if name eq 'N_EXTRA' then NumExtra = dimlen $
  46. else if name eq 'STRINGNAM' then strnam = dimlen $
  47. else if name eq 'STRINGGRID' then strgrid = dimlen $
  48. else if name eq 'STRINGWMO' then strwmo = dimlen $
  49. else if name eq 'STRINGTYP' then strtyp = dimlen $
  50. else if name eq 'STRINGJULD' then strjuld = dimlen
  51. endfor
  52. ;4. Set up arrays
  53. if (NumObs gt 0) then begin
  54. ByteNam = intarr(strnam, NumVars)
  55. if (n_elements(NumEntries) eq 1) then ByteEntries = intarr(strnam, NumEntries)
  56. ByteId = intarr(strwmo, NumObs)
  57. ByteType = intarr(strtyp, NumObs)
  58. ByteJulDRef = intarr(strjuld)
  59. VarNames = strarr(NumVars)
  60. if (n_elements(NumEntries) eq 1) then Entries = strarr(NumEntries)
  61. Id = strarr(NumObs)
  62. Type = strarr(NumObs)
  63. if NumExtra gt 0 then begin
  64. ByteExtra = intarr(strnam, NumExtra)
  65. Extra = strarr(NumExtra)
  66. endif
  67. Longitude = fltarr(NumObs)
  68. Latitude = fltarr(NumObs)
  69. Depth = fltarr(NumLevels, NumObs)
  70. DepQC = intarr(NumLevels, NumObs)
  71. JulD = dblarr(NumObs)
  72. ObsQC = intarr(NumObs)
  73. PosQC = intarr(NumObs)
  74. JulDQC = intarr(NumObs)
  75. Obs = fltarr(NumLevels, NumObs, NumVars)
  76. Hx = fltarr(NumLevels, NumObs, NumVars)
  77. VarQC = intarr(NumObs, NumVars)
  78. LevelQC = intarr(NumLevels, NumObs, NumVars)
  79. ;5. Read in the data
  80. varid = ncdf_varid(ncid, 'VARIABLES')
  81. ncdf_varget, ncid, varid, ByteNam
  82. ; info, VarNames
  83. ; info, ByteNam
  84. for ivar= 0, NumVars-1 do begin
  85. VarNames(ivar) = string(ByteNam(*,ivar))
  86. endfor
  87. if ifile2 eq 0 then VarName = VarNames(0)
  88. if VarName ne VarNames(0) then message, 'Can only read in from files containing the same variables'
  89. varid = ncdf_varid(ncid, 'JULD_REFERENCE')
  90. ncdf_varget, ncid, varid, ByteJulDRef
  91. JulDRef = string(ByteJulDRef)
  92. RefDate = JULDAY(fix(strmid(JulDRef,4,2)), fix(strmid(JulDRef,6,2)), fix(strmid(JulDRef,0,4)), $
  93. fix(strmid(JulDRef,8,2)), fix(strmid(JulDRef,10,2)), fix(strmid(JulDRef,12,2)))
  94. print, 'RefDate: ',RefDate
  95. varid = ncdf_varid(ncid, 'JULD')
  96. ncdf_varget, ncid, varid, JulD
  97. ; print, JulD
  98. Dates = dblarr(NumLevels, NumObs)
  99. for iob = 0L, long(NumObs)-1L do begin
  100. dt = RefDate + JulD(iob)
  101. ; print,dt, JulD(iob)
  102. Dates(0:NumLevels-1, iob) = replicate(dt, NumLevels)
  103. endfor
  104. varid = ncdf_varid(ncid, 'STATION_IDENTIFIER')
  105. ncdf_varget, ncid, varid, ByteId
  106. Identifier = string(ByteId)
  107. varid = ncdf_varid(ncid, 'STATION_TYPE')
  108. ncdf_varget, ncid, varid, ByteType
  109. Type = string(ByteType)
  110. varid = ncdf_varid(ncid, 'LONGITUDE')
  111. ncdf_varget, ncid, varid, Longitude
  112. varid = ncdf_varid(ncid, 'LATITUDE')
  113. ncdf_varget, ncid, varid, Latitude
  114. varid = ncdf_varid(ncid, 'DEPTH')
  115. ncdf_varget, ncid, varid, Depth
  116. ncdf_attget, ncid, varid, '_Fillvalue', FillVal
  117. pts = where(Depth eq FillVal, count)
  118. if count gt 0 then Depth(pts) = rmdi
  119. varid = ncdf_varid(ncid, 'OBSERVATION_QC')
  120. ncdf_varget, ncid, varid, ObsQC
  121. ncdf_attget, ncid, varid, '_Fillvalue', FillVal
  122. pts = where(ObsQC eq FillVal, count)
  123. if count gt 0 then ObsQC(pts) = rmdi
  124. for ivar = 0, NumVars-1 do begin
  125. varid = ncdf_varid(ncid, strtrim(VarNames(ivar))+'_OBS')
  126. ncdf_varget, ncid, varid, tmp
  127. Obs(*,*,ivar) = tmp
  128. ncdf_attget, ncid, varid, '_Fillvalue', FillValObs
  129. varid = ncdf_varid(ncid, strtrim(VarNames(ivar))+'_Hx')
  130. if (varid gt -1) then begin
  131. ncdf_varget, ncid, varid, tmp
  132. Hx(*,*,ivar) = tmp
  133. ncdf_attget, ncid, varid, '_Fillvalue', FillValHx
  134. endif else begin
  135. FillValHx=0
  136. endelse
  137. varid = ncdf_varid(ncid, strtrim(VarNames(ivar))+'_QC')
  138. ncdf_varget, ncid, varid, tmp
  139. VarQC(*,ivar) = tmp
  140. ncdf_attget, ncid, varid, '_Fillvalue', FillValQC
  141. varid = ncdf_varid(ncid, strtrim(VarNames(ivar))+'_LEVEL_QC')
  142. ncdf_varget, ncid, varid, tmp
  143. LevelQC(*,*,ivar) = tmp
  144. ncdf_attget, ncid, varid, '_Fillvalue', FillValLevQC
  145. endfor
  146. ; print,' DJL levelqc(*,218,0) ',levelqc(*,218,0)
  147. ; print,' DJL levelqc(*,218,1) ',levelqc(*,218,1)
  148. pts = where(Obs eq FillValObs, count)
  149. if count gt 0 then Obs(pts) = rmdi
  150. pts = where(Hx eq FillValHx, count)
  151. if count gt 0 then Hx(pts) = rmdi
  152. pts = where(VarQC eq FillValQC, count)
  153. if count gt 0 then VarQC(pts) = rmdi
  154. pts = where(LevelQC eq FillValLevQC, count)
  155. if count gt 0 then LevelQC(pts) = rmdi
  156. Obs1 = Obs(*,*,0)
  157. Hx1 = Hx(*,*,0)
  158. VarQC1=LevelQC(*,*,0)
  159. if NumVars gt 1 then begin
  160. Obs2 = Obs(*,*,1)
  161. Hx2 = Hx(*,*,1)
  162. VarQC2=LevelQC(*,*,1)
  163. endif
  164. if strtrim(VarName,2) eq 'SLA' then begin
  165. MDT = fltarr(NumLevels, NumObs)
  166. print, NumLevels, NumObs
  167. varid = ncdf_varid(ncid, 'MDT')
  168. ncdf_varget, ncid, varid, MDT
  169. ncdf_attget, ncid, varid, '_Fillvalue', FillVal
  170. pts = where(MDT eq FillVal, count)
  171. if count gt 0 then MDT(pts) = rmdi
  172. Obs2 = MDT
  173. endif
  174. ;6. Put the data into the correct form to be output from this routine
  175. Platformsa = strarr(NumLevels, NumObs)
  176. Instrumentsa = strarr(NumLevels, NumObs)
  177. Lons = fltarr(NumLevels,NumObs)
  178. Lats = fltarr(NumLevels,NumObs)
  179. ProfileNum = lonarr(NumLevels,NumObs)
  180. for i=0,NumLevels-1 do begin
  181. Platformsa(i,*)=Type
  182. Instrumentsa(i,*)=Identifier
  183. endfor
  184. for i=0L,long(NumObs)-1L do begin
  185. Lats(*,i)=Latitude(i)
  186. Lons(*,i)=Longitude(i)
  187. ProfileNum(*,i) = i+1
  188. endfor
  189. ;
  190. pts = where(Depth ge DepRange(0) and Depth le DepRange(1), NumDataInc)
  191. NumData=NumData+NumDataInc
  192. ;
  193. if ifile2 eq 0 then begin
  194. OutObs1 = [Obs1(pts)]
  195. OutMod1 = [Hx1(pts)]
  196. OutQC1 = [VarQC1(pts)]
  197. OutDeps = [Depth(pts)]
  198. OutLats = [Lats(pts)]
  199. OutLons = [Lons(pts)]
  200. OutDates= [Dates(pts)]
  201. OutInst= [Instrumentsa(pts)]
  202. OutPlatform= [Platformsa(pts)]
  203. OutProfileNum = [ProfileNum(pts)]
  204. if NumVars eq 2 then begin
  205. OutObs2 = [Obs2(pts)]
  206. OutMod2 = [Hx2(pts)]
  207. OutQC2 = [VarQC2(pts)]
  208. endif
  209. endif else begin
  210. OutObs1 = [OutObs1, Obs1(pts)]
  211. OutMod1 = [OutMod1, Hx1(pts)]
  212. OutQC1 = [OutQC1, VarQC1(pts)]
  213. OutDeps = [OutDeps, Depth(pts)]
  214. OutLats = [OutLats, Lats(pts)]
  215. OutLons = [OutLons, Lons(pts)]
  216. OutDates= [OutDates, Dates(pts)]
  217. OutInst= [OutInst, Instrumentsa(pts)]
  218. OutPlatform= [OutPlatform, Platformsa(pts)]
  219. OutProfileNum = [OutProfileNum, ProfileNum(pts)]
  220. if NumVars eq 2 then begin
  221. OutObs2 = [OutObs2, Obs2(pts)]
  222. OutMod2 = [OutMod2, Hx2(pts)]
  223. OutQC2 = [OutQC2, VarQC2(pts)]
  224. endif
  225. endelse
  226. if NumVars eq 1 then begin
  227. if VarName ne 'SLA' then OutObs2 = fltarr(n_elements(OutObs1)) + rmdi
  228. OutMod2 = fltarr(n_elements(OutMod1)) + rmdi
  229. OutQC2 = fltarr(n_elements(OutQC1)) + rmdi
  230. endif
  231. ;
  232. ifile2=ifile2+1
  233. endif
  234. ncdf_close, ncid
  235. endif
  236. endfor ;ifile
  237. pts1 = where(OutQC1 eq 1, count1)
  238. pts2 = where(OutQC1 ne 1, count2)
  239. if count1 gt 0 then OutQc1(pts1) = 0
  240. if count2 gt 0 then OutQc1(pts2) = 1
  241. if NumVars gt 1 then begin
  242. pts1 = where(OutQC2 eq 1, count1)
  243. pts2 = where(OutQC2 ne 1, count2)
  244. if count1 gt 0 then OutQc2(pts1) = 0
  245. if count2 gt 0 then OutQc2(pts2) = 1
  246. endif
  247. END