read_sla.pro 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. ;+
  2. PRO read_sla, Files, NumObs=NumObs, $
  3. Latitudes=Latitudes, Longitudes=Longitudes, $
  4. ObsSLA=ObsSLA, ModSLA=ModSLA, SLAQC=SLAQC, $
  5. MDT=MDT, Satellites=Satellites, Types=Types, Dates=Dates, rmdi=rmdi, $
  6. iobs=iobs, jobs=jobs, mstp=mstp, quiet=quiet, track=track
  7. ;+-----------------------------------------------------------
  8. ;IDL program to read in netcdf files of altimeter data.
  9. ;
  10. ;Author: D. J. Lea - Feb 2008
  11. ;
  12. ;------------------------------------------------------------
  13. rmdi = -99999.
  14. NumFiles=n_elements(Files)
  15. ;RefDate = '1950-01-01'
  16. ;!DATE_SEPARATOR='-'
  17. RefDate=JULDAY(1,1,1950,0,0) ; should be at 0 UTC
  18. ifile2=0
  19. for ifile = 0, NumFiles-1 do begin
  20. ;------------------------------------------------------------
  21. ;1. Open the file containing the data
  22. ncid = ncdf_open(Files(ifile), /nowrite)
  23. if ncid lt 0 and not keyword_set(quiet) then print, 'Error opening file '+Files(ifile)
  24. if ncid ge 0 then begin
  25. if not keyword_set(quiet) then print, 'Opened file '+Files(ifile)+' successfully'
  26. ;------------------------------------------------------------
  27. ;2. Read in the dimensions in the file
  28. ncinfo = ncdf_inquire(ncid)
  29. ; var = ncdf_varinq( ncid, "SLA" )
  30. if not keyword_set(quiet) then print,'ncinfo.ndims ',ncinfo.ndims
  31. ncdf_diminq, ncid, 0, name, NData
  32. NData2=0
  33. cycles=1
  34. if (ncinfo.ndims gt 2) then ncdf_diminq, ncid, 2, name, NData2
  35. if (ncinfo.ndims gt 1) then ncdf_diminq, ncid, 1, name, cycles
  36. if not keyword_set(quiet) then print,NData, NData2
  37. NData=max([NData*cycles, NData2*cycles])
  38. ; print,NData, NData2
  39. if (NData gt 0) then begin
  40. ;------------------------------------------------------------
  41. ;3. Allocate the data arrays and read in the data
  42. lons = dblarr(NData)
  43. lats = dblarr(NData)
  44. obsval = fltarr(NData)
  45. modval = fltarr(NData)
  46. mdtval = fltarr(NData)
  47. bytQC = bytarr(NData)
  48. QC = fltarr(NData)
  49. Dats = dblarr(NData)
  50. trackval = intarr(NData)
  51. Sats = intarr(NData)
  52. strSats= strarr(NData)
  53. StrVal = strarr(8)
  54. ; dts = replicate(!dt_base, NData)
  55. dts = dblarr(NData)
  56. ; info,lons
  57. varid = ncdf_varid(ncid, 'LONGITUDE')
  58. scale_factor=1.
  59. if (varid eq -1) then begin
  60. varid = ncdf_varid(ncid, 'Longitudes')
  61. if (varid eq -1) then begin
  62. varid = ncdf_varid(ncid, 'longitude')
  63. endif
  64. ncdf_attget, ncid, varid, 'scale_factor', scale_factor
  65. endif
  66. ncdf_varget, ncid, varid, lons
  67. lons=lons*scale_factor
  68. ; info, lons
  69. if (cycles gt 1) then begin
  70. lonsn=lons
  71. for i=1,cycles-1 do begin
  72. lonsn=[lonsn,lons]
  73. endfor
  74. lons=lonsn
  75. endif
  76. ; info, lons
  77. ; info,lats
  78. varid = ncdf_varid(ncid, 'LATITUDE')
  79. scale_factor=1.
  80. if (varid eq -1) then begin
  81. varid = ncdf_varid(ncid, 'Latitudes')
  82. if (varid eq -1) then begin
  83. varid = ncdf_varid(ncid, 'latitude')
  84. endif
  85. ncdf_attget, ncid, varid, 'scale_factor', scale_factor
  86. endif
  87. ncdf_varget, ncid, varid, lats
  88. lats=lats*scale_factor
  89. ; info, lats
  90. if (cycles gt 1) then begin
  91. latsn=lats
  92. for i=1,cycles-1 do begin
  93. latsn=[latsn,lats]
  94. endfor
  95. lats=latsn
  96. endif
  97. ; info, lats
  98. varid = ncdf_varid(ncid, 'JULD')
  99. if (varid eq -1) then begin
  100. varid = ncdf_varid(ncid, 'time')
  101. endif
  102. if (varid ne -1) then begin
  103. ncdf_varget, ncid, varid, Dats
  104. endif else begin
  105. varid = ncdf_varid(ncid, 'BeginDates')
  106. ncdf_varget, ncid, varid, Bds
  107. ; print,bds
  108. ; info, bds
  109. if (cycles gt 1) then begin
  110. bds=transpose(bds)
  111. endif
  112. varid = ncdf_varid(ncid, 'NbPoints')
  113. ncdf_varget, ncid, varid, nbpoints
  114. ; Read in dataindexes and deltat in cls format file
  115. ; These are used to adjust dates
  116. varid = ncdf_varid(ncid, 'DeltaT')
  117. if (varid ne -1) then begin
  118. ncdf_varget, ncid, varid, deltat
  119. ncdf_attget, ncid, varid, 'scale_factor', scale_factor
  120. deltat=deltat*scale_factor
  121. varid = ncdf_varid(ncid, 'DataIndexes')
  122. ncdf_varget, ncid, varid, dataindexes
  123. endif
  124. print, 'deltat ',deltat
  125. ; print, 'dataindexes ',dataindexes
  126. ; info, nbpoints
  127. ; print,nbpoints
  128. ; info, cycles
  129. cumbds=[0]
  130. nbpointst=nbpoints
  131. dataindexest=dataindexes
  132. for i=1,cycles-1 do begin
  133. nbpointst=[nbpointst,nbpoints]
  134. dataindexest=[dataindexest,dataindexes]
  135. endfor
  136. cumbds=[0,total(nbpointst,/cumulative)]
  137. ; if not keyword_set(quiet) then print,'cumbds ',cumbds
  138. nel=n_elements(Bds)
  139. ; info,nel
  140. ; if not keyword_set(quiet) then print,n_elements(dats)
  141. for i=0,nel-1 do begin
  142. ; print,'*',i, cumbds(i+1)-cumbds(i)
  143. ; print,i,cumbds(i),cumbds(i+1)-1
  144. dats(cumbds(i):cumbds(i+1)-1)=bds(i)
  145. trackval(cumbds(i):cumbds(i+1)-1)=i
  146. dataindexest(cumbds(i):cumbds(i+1)-1)=dataindexest(cumbds(i):cumbds(i+1)-1)-dataindexest(cumbds(i))
  147. endfor
  148. ; adjust dats based on dataindex
  149. dats=dats+dataindexest*deltat/86400.
  150. ; print, dats
  151. endelse
  152. dts=RefDate + Dats
  153. varid = ncdf_varid(ncid, 'MISSION')
  154. if (varid ne -1) then begin
  155. ncdf_varget, ncid, varid, Sats
  156. ncdf_attget, ncid, varid, 'Value_0', StrVal0
  157. ncdf_attget, ncid, varid, 'Value_1', StrVal1
  158. ncdf_attget, ncid, varid, 'Value_2', StrVal2
  159. ncdf_attget, ncid, varid, 'Value_3', StrVal3
  160. ncdf_attget, ncid, varid, 'Value_4', StrVal4
  161. ncdf_attget, ncid, varid, 'Value_5', StrVal5
  162. ncdf_attget, ncid, varid, 'Value_6', StrVal6
  163. ncdf_attget, ncid, varid, 'Value_7', StrVal7
  164. StrVal=[StrVal0, StrVal1, StrVal2, StrVal3, StrVal4, $
  165. StrVal5, StrVal6, StrVal7]
  166. for ival = 0, 7 do begin
  167. pts = where(Sats eq ival, count)
  168. if count gt 0 then strSats(pts) = StrVal(ival)
  169. endfor
  170. endif
  171. varid = ncdf_varid(ncid, 'SLA')
  172. ncdf_varget, ncid, varid, obsval
  173. ncdf_attget, ncid, varid, '_FillValue', FillValue
  174. scale_factor=1.
  175. add_offset=0.
  176. ;if (ncinfo.ndims gt 2) then ncdf_attget, ncid, varid, 'scale_factor', scale_factor
  177. ncviq=ncdf_varinq(ncid, varid)
  178. for iatt=0,ncviq.natts-1 do begin
  179. ncaiq=ncdf_attname(ncid, varid, iatt)
  180. if ( ncaiq eq 'scale_factor') then ncdf_attget, ncid, varid, 'scale_factor', scale_factor
  181. if ( ncaiq eq 'add_factor') then ncdf_attget, ncid, varid, 'add_factor', add_factor
  182. endfor
  183. if not keyword_set(quiet) then print,'SLA scale factor: ',scale_factor
  184. if not keyword_set(quiet) then print,'SLA add offset: ',add_offset
  185. if not keyword_set(quiet) then print,'SLA FillValue: ',FillValue
  186. pts = where(obsval eq FillValue, count)
  187. ; if not keyword_set(quiet) then print,count
  188. obsval=obsval*scale_factor+add_offset
  189. if count gt 0 then obsval(pts) = rmdi
  190. varid = ncdf_varid(ncid, 'SLA_Hx')
  191. if (varid ne -1) then begin
  192. ncdf_varget, ncid, varid, modval
  193. ncdf_attget, ncid, varid, '_FillValue', FillValue
  194. ; if not keyword_set(quiet) then print,'SLA_Hx FillValue ',FillValue
  195. pts = where(modval eq FillValue or modval eq -9999.0, count)
  196. ; if not keyword_set(quiet) then print,count
  197. if count gt 0 then modval(pts) = rmdi
  198. endif
  199. varid = ncdf_varid(ncid, 'SLA_QC')
  200. if (varid ne -1) then begin
  201. ncdf_varget, ncid, varid, bytQC
  202. ncdf_attget, ncid, varid, '_FillValue', FillValue
  203. QC(*) = 0.
  204. pts = where(bytQC eq FillValue, count)
  205. if count gt 0 then QC(pts) = rmdi
  206. pts = where(bytQC ne 48, count)
  207. if count gt 0 then QC(pts) = 1.
  208. endif
  209. varid = ncdf_varid(ncid, 'MDT')
  210. if (varid ne -1) then ncdf_varget, ncid, varid, mdtval
  211. varid = ncdf_varid(ncid, 'data_source')
  212. if varid ne -1 then begin
  213. ncdf_varget, ncid, varid, obstyp
  214. endif else begin
  215. varid = ncdf_varid(ncid, 'MISSION')
  216. if varid ne -1 then ncdf_varget, ncid, varid, obstyp
  217. endelse
  218. ; reads in multi cycle data the wrong way round!
  219. if (cycles gt 1) then begin
  220. obsval=reform(obsval,[cycles,ndata/cycles])
  221. obsval=transpose(obsval)
  222. modval=reform(modval,[cycles,ndata/cycles])
  223. modval=transpose(modval)
  224. QC=reform(QC,[cycles,ndata/cycles])
  225. QC=transpose(QC)
  226. endif
  227. varid = ncdf_varid(ncid, 'IOBS')
  228. if (varid ne -1) then ncdf_varget, ncid, varid, iobsval
  229. varid = ncdf_varid(ncid, 'JOBS')
  230. if (varid ne -1) then ncdf_varget, ncid, varid, jobsval
  231. varid = ncdf_varid(ncid, 'MSTP')
  232. if (varid ne -1) then ncdf_varget, ncid, varid, mstpval
  233. ; info, obssla
  234. ; info, obsval
  235. ; info, modsla
  236. ; info, modval
  237. if ifile2 eq 0 then begin
  238. Latitudes = [float(lats)]
  239. Longitudes = [float(lons)]
  240. ObsSLA = [obsval(*)]
  241. ModSLA = [modval(*)]
  242. MDT = [mdtval]
  243. SLAQC = [QC]
  244. Dates = [dts]
  245. Satellites = [strSats]
  246. if (n_elements(obstyp) gt 0) then Types = [obstyp]
  247. if (n_elements(iobsval) gt 0) then iobs = [iobsval]
  248. if (n_elements(jobsval) gt 0) then jobs = [jobsval]
  249. if (n_elements(mstpval) gt 0) then mstp = [mstpval]
  250. if (n_elements(trackval) gt 0) then track = [trackval]
  251. endif else begin
  252. Latitudes = [Latitudes, float(lats)]
  253. Longitudes = [Longitudes, float(lons)]
  254. ObsSLA = [ObsSLA, obsval(*)]
  255. ModSLA = [ModSLA, modval(*)]
  256. MDT = [MDT, mdtval]
  257. SLAQC = [SLAQC, QC]
  258. Dates = [Dates, dts]
  259. Satellites = [Satellites, strSats]
  260. if (n_elements(obstyp) gt 0) then Types = [Types, obstyp]
  261. if (n_elements(iobsval) gt 0) then iobs = [iobs, iobsval]
  262. if (n_elements(jobsval) gt 0) then jobs = [jobs, jobsval]
  263. if (n_elements(mstpval) gt 0) then mstp = [mstp, mstpval]
  264. if (n_elements(trackval) gt 0) then tracks = [track, trackval]
  265. endelse
  266. ifile2=ifile2+1
  267. endif
  268. ncdf_close, ncid
  269. endif
  270. endfor ; ifile
  271. NumObs = n_elements(Latitudes)
  272. END