source: trunk/SRC/ToBeReviewed/LECTURE/read_ncdf.pro @ 366

Last change on this file since 366 was 366, checked in by pinsard, 16 years ago

typo

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 17.9 KB
RevLine 
[2]1;+
[231]2;
[150]3; @file_comments
4; Reading function for the file net_cdf.
[226]5; This program is less universal than ncdf_lec (it appeal to declared
[150]6; variables in common.pro) but it is very easier to be used. It considerate
7; the declaration of the different zooms which have been defined
8; (ixminmesh...premierx...), the declaration of the variable key_shift...
9; To put it in a nutshell, the result of read_ncdf can be directly used in plt...
[226]10; This is also this program which is used by default in our reading widgets.
[2]11;
[150]12; @categories
[157]13; Reading
[226]14;
[163]15; @param NAME {in}{required}{type=string}
16; It define the field to be read.
[2]17;
[150]18; @param BEGINNING {in}{required}
19; Relative with the time axis.
20; These can be
[365]21;  - 2 dates of the type yyyymmdd and in this case, we select data
[150]22;  which are included between these two dates.
[365]23;  - 2 indexes which define between which and which time steps we have
[163]24;  to extract the temporal dimension.
[2]25;
[365]26; @param ENDING {in}{required}
[150]27; Relative with the time axis.
28; See BEGINNING.
[226]29;
[174]30; @param COMPATIBILITY {in}{optional}
31; Useless, defined for compatibility
[226]32;
[271]33; @keyword ADDSCL_BEFORE {default=0}{type=scalar: 0 or 1}
[366]34; put 1 to apply add_offset and scale factor on data before looking for
[271]35; missing values
36;
[226]37; @keyword BOXZOOM
38; Contain the boxzoom on which we have to do the reading
39;
[174]40; @keyword CALLITSELF {default=0}{type=scalar: 0 or 1}
[366]41; For ROMS outputs. Use by read_ncdf itself to access auxilliary data
42; (h and zeta).
[226]43;
[276]44; @keyword DIREC
45; a string used to specify the direction along which we want to make
46; spatial and/or temporal mean. It could be: 'x' 'y' 'z' 't' 'xy' 'xz'
[365]47; 'yz' 'xyz' 'xt' 'yt' 'zt' 'xyt' 'xzt' 'yzt' or 'xyzt'
[276]48;
[174]49; @keyword FILENAME {required}{type=string}
[295]50; It contains the file's name.
[226]51;
[174]52; @keyword INIT {default=0}{type=scalar: 0 or 1}
[365]53; To call automatically <pro>initncdf</pro> with filename as input argument
[366]54; and thus redefine all the grid parameters
[226]55;
[150]56; @keyword GRID
[163]57; ='[UTVWF]' to specify the type of grid. Default is (1)
[150]58; based on the name of the file if the file ends by
59; GRID[._][TUVFW].NC (not case sensible) or (2) T if case (1)
60; is not found.
[226]61;
[174]62; @keyword TIMESTEP {default=0}{type=scalar: 0 or 1}
[366]63; Specify that BEGINNING and ENDING refer to indexes of the time axis and not
64; to dates
[2]65;
[174]66; @keyword TOUT {default=0}{type=scalar: 0 or 1}
[226]67; We activate it if we want to read the file on the whole domain without
68; considerate the sub-domain defined by the boxzoom or
[150]69; lon1,lon2,lat1,lat2,vert1,vert2.
[226]70;
[174]71; @keyword NOSTRUCT {default=0}{type=scalar: 0 or 1}
[226]72; We activate it if we do not want that read_ncdf send back a structure
[163]73; but only the array referring to the field.
[226]74;
[174]75; @keyword ZETAFILENAME {default=FILENAME}{type=string}
[238]76; For ROMS outputs. The filename of the file where zeta variable should be read
[174]77;
78; @keyword ZETAZERO {default=0}{type=scalar: 0 or 1}
79; For ROMS outputs. To define zeta to 0. instead of reading it
80;
[336]81; @keyword ZINVAR {type=named variable}
82; Set this keyword to a named variable in which 1 is returned if a
83; vertical dimension is found in the variable. Returns 0 otherwise
84;
[150]85; @keyword _EXTRA
[271]86; Used to pass keywords to <pro>isafile</pro>, <pro>initncdf</pro>,
[337]87; <pro>ncdf_gettime</pro>, <pro>ncdf_getatt</pro> and <pro>domdef</pro>
[2]88;
[150]89; @returns
[366]90; Structure readable by <pro>litchamp</pro> or an array if NOSTRUCT is
91; activated.
92;
[150]93; @uses
94; common.pro
[226]95;
[150]96; @restrictions
97; The field must have a temporal dimension.
[226]98;
[150]99; @history
[157]100; Sebastien Masson (smasson\@lodyc.jussieu.fr)
[226]101;                      15/10/1999
102;
[150]103; @version
[226]104; $Id$
[325]105;
[2]106;-
[327]107FUNCTION read_ncdf, name, beginning, ending, compatibility $
[336]108                    , BOXZOOM = boxzoom, FILENAME = filename $
109                    , PARENTIN = parentin, TIMESTEP = timestep $
110                    , ADDSCL_BEFORE = addscl_before $
[366]111                    , TOUT = tout, NOSTRUCT = nostruct $
112                    , CONT_NOFILL = CONT_NOFILL, INIT = init $
[336]113                    , GRID = grid, CALLITSELF = callitself, DIREC = direc $
114                    , ZETAFILENAME = zetafilename, ZETAZERO = zetazero $
115                    , ZINVAR = zinvar, _EXTRA = ex
[114]116;
117  compile_opt idl2, strictarrsubs
118;
[44]119@cm_4mesh
120@cm_4data
121@cm_4cal
122  IF NOT keyword_set(key_forgetold) THEN BEGIN
123@updatenew
124@updatekwd
125  ENDIF
[2]126;------------------------------------------------------------
127; we find the filename.
128;------------------------------------------------------------
[226]129;  print,filename
[2]130; is parent a valid widget ?
[271]131  IF keyword_set(parentin) THEN BEGIN
[44]132    parent = long(parentin)
133    parent = parent*widget_info(parent, /managed)
134  ENDIF
135  filename = isafile(filename = filename, IODIRECTORY = iodir, _EXTRA = ex)
[2]136;------------------------------------------------------------
[150]137; Opening of the name file
[2]138;------------------------------------------------------------
[271]139  IF size(filename, /type) NE 7 THEN $
[336]140     return, report('read_ncdf cancelled')
[44]141  IF !version.OS_FAMILY EQ 'unix' THEN spawn, '\file '+filename+' > /dev/null'
142  cdfid = ncdf_open(filename)
[271]143  inq = ncdf_inquire(cdfid)
[2]144;------------------------------------------------------------
145; we check if the variable name exists in the file.
146;------------------------------------------------------------
[271]147  IF ncdf_varid(cdfid, name) EQ -1 THEN BEGIN
[44]148    ncdf_close, cdfid
[2]149    return, report('variable '+name+' !C not found in the file '+filename)
[44]150  ENDIF
[271]151  varinq = ncdf_varinq(cdfid, name)
152  IF varinq.ndims LT 2 THEN return, report('read_ncdf cannot read scalar or 1D data')
[172]153; look for the dimension names
[271]154  dimnames = strarr(varinq.ndims)
155  FOR i = 0, varinq.ndims-1 DO BEGIN
156    ncdf_diminq, cdfid, varinq.dim[i], tmp, dimsize
[172]157    dimnames[i] = tmp
[226]158  ENDFOR
[2]159;------------------------------------------------------------
[365]160; we check if the variable has a vertical dimension and/or
161; a record dimension. This is useful to define boxzoom
[336]162; keyword in domdef
163;------------------------------------------------------------
[365]164  dummy = where(varinq.dim EQ inq.recdim, tinvar)
[336]165; check the presence of a vertical dimension according to the
166; number of dimensions and the presence of a record dimension
167  zinvar = (varinq.ndims EQ 3 AND tinvar NE 1) OR varinq.ndims EQ 4
168;------------------------------------------------------------
[44]169; shall we redefine the grid parameters
170;------------------------------------------------------------
[271]171  IF keyword_set(init) THEN initncdf, filename, _extra = ex
[44]172;------------------------------------------------------------
[150]173; check the time axis and the debut and ending dates
[2]174;------------------------------------------------------------
[271]175  IF n_elements(beginning) EQ 0 THEN BEGIN
[284]176    beginning = 0L
177    timestep = 1L
[271]178  ENDIF
179; define time and jpt
180  CASE 1 OF
[365]181    keyword_set(timestep):BEGIN
[284]182      firsttps = long(beginning[0])
183      IF n_elements(ending) NE 0 THEN lasttps = long(ending[0]) ELSE lasttps = firsttps
[271]184      jpt = lasttps-firsttps+1
185      IF NOT keyword_set(callitself) then time = julday(1, 1, 1) + lindgen(jpt)
186    END
[365]187    keyword_set(parent):BEGIN
[44]188      widget_control, parent, get_uvalue = top_uvalue
189      filelist = extractatt(top_uvalue, 'filelist')
190      IF filelist[0] EQ 'many !' THEN filelist = filename
191      currentfile = (where(filelist EQ filename))[0]
192      time = (*(extractatt(top_uvalue, 'fileparameters'))[currentfile]).time_counter
[298]193      date1 = date2jul(beginning[0])
194      if n_elements(ending) NE 0 then date2 = date2jul(ending[0]) ELSE date2 = date1
[336]195      firsttps = (where(abs(time - date1) LT 0.9d/86400.d))[0] ; beware of rounding errors...
[271]196      lasttps = (where(abs(time - date2) LT 0.9d/86400.d))[0]
197      jpt = lasttps-firsttps+1
198    END
[365]199    ELSE:BEGIN
[271]200      time = ncdf_gettime(filename, cdfid, caller = 'read_ncdf', err = err, _extra = ex)
201      IF n_elements(err) NE 0 THEN BEGIN
202        dummy = report(err)
[44]203        ncdf_close, cdfid
[271]204        return, -1
[44]205      ENDIF
[271]206; date1
[298]207      date1 = date2jul(beginning[0])
[271]208; date2
[298]209      if n_elements(ending) NE 0 then date2 = date2jul(ending[0]) ELSE date2 = date1
[271]210; firsttps
211      firsttps = where(time GE (date1 - 0.9d/86400.d)) & firsttps = firsttps[0]
[44]212      if firsttps EQ -1 THEN BEGIN
213        ncdf_close, cdfid
214        return, report('date 1: '+strtrim(jul2date(date1), 1)+' is not found in the time axis.')
[74]215      ENDIF
[271]216; lasttps
217      lasttps = where(time LE (date2 + 0.9d/86400.d)) & lasttps = lasttps[n_elements(lasttps)-1]
218      if lasttps EQ -1 THEN BEGIN
[44]219        ncdf_close, cdfid
[198]220        return, report('the time axis has no date before date 2: '+strtrim(jul2date(date2), 1))
[44]221      endif
222      if lasttps LT firsttps then BEGIN
223        ncdf_close, cdfid
[198]224        return, report('the time axis has no dates between date1 and  date 2: '+strtrim(jul2date(date1), 1)+' '+strtrim(jul2date(date2), 1))
[44]225      endif
[271]226      time = time[firsttps:lasttps]
227      jpt = lasttps-firsttps+1
228    END
229  ENDCASE
[2]230;------------------------------------------------------------
[150]231; Name of the grid on which the field refer to.
[2]232;------------------------------------------------------------
[44]233  IF keyword_set(grid) THEN vargrid = strupcase(grid) ELSE BEGIN
234    vargrid = 'T'               ; default definition
[69]235    IF finite(glamu[0]) EQ 1 THEN BEGIN
[172]236; are we in one of the case corresponding to ROMS conventions?
237      CASE 1 OF
[336]238        dimnames[2 < (varinq.ndims-1)] EQ 's_w':vargrid = 'W'
[172]239        dimnames[0] EQ 'xi_rho' AND dimnames[1] EQ 'eta_rho':vargrid = 'T'
240        dimnames[0] EQ 'xi_u'   AND dimnames[1] EQ 'eta_u'  :vargrid = 'U'
241        dimnames[0] EQ 'xi_v'   AND dimnames[1] EQ 'eta_v'  :vargrid = 'V'
242        dimnames[0] EQ 'xi_psi' AND dimnames[1] EQ 'eta_psi':vargrid = 'F'
243        dimnames[0] EQ 'xi_rho' AND dimnames[1] EQ 'eta_v'  :vargrid = 'V'
244        dimnames[0] EQ 'xi_u'   AND dimnames[1] EQ 'eta_rho':vargrid = 'U'
245        dimnames[0] EQ 'xi_u'   AND dimnames[1] EQ 'eta_v'  :vargrid = 'F'
[226]246        ELSE:BEGIN
[172]247; could we define the grid type from the file name??
248          pattern = ['GRID.', 'GRID_', 'GRID', 'UPID_', '30ID_']
249          gdtype = ['T', 'U', 'V', 'W', 'F']
250          fnametest = strupcase(filename)
251          FOR i = 0, n_elements(pattern)-1 DO BEGIN
252            FOR j = 0, n_elements(gdtype)-1 DO BEGIN
253              substr = pattern[i]+gdtype[j]
254              pos = strpos(fnametest, substr)
255              IF pos NE -1 THEN $
256                 vargrid = strmid(fnametest, pos+strlen(substr)-1, 1)
257            ENDFOR
258          ENDFOR
259        END
260      ENDCASE
[336]261    ENDIF
[44]262  ENDELSE
[2]263;---------------------------------------------------------------
[365]264; redefinition of the domain
[2]265;---------------------------------------------------------------
[271]266  IF keyword_set(tout) THEN BEGIN
[44]267    nx = jpi
268    ny = jpj
269    nz = jpk
270    firstx = 0
271    firsty = 0
272    firstz = 0
273    lastx = jpi-1
274    lasty = jpj-1
275    lastz = jpk-1
276    case strupcase(vargrid) of
277      'T':mask = tmask
278      'U':mask = umask()
279      'V':mask = vmask()
280      'W':mask = tmask
281      'F':mask = fmask()
282    endcase
283  ENDIF ELSE BEGIN
[336]284    IF keyword_set(boxzoom) then BEGIN
285      CASE N_Elements(Boxzoom) Of
286        1:bte = [lon1, lon2, lat1, lat2, 0., boxzoom[0]]
287        2:bte = [lon1, lon2, lat1, lat2, boxzoom[0], boxzoom[1]]
288        4:bte = [Boxzoom, vert1, vert2]
289        5:bte = [Boxzoom[0:3], 0, Boxzoom[4]]
290        6:bte = Boxzoom
291        ELSE: BEGIN
[44]292          ncdf_close, cdfid
293          return, report('Wrong Definition of Boxzoom')
[336]294        END
[44]295      ENDCASE
[336]296      IF zinvar EQ 1 THEN domdef, bte, GRIDTYPE = ['T', vargrid], _extra = ex $
297      ELSE domdef, bte[0:3], GRIDTYPE = ['T', vargrid], _extra = ex
[44]298    ENDIF
299    grille, mask, glam, gphi, gdep, nx, ny, nz, firstx, firsty, firstz, lastx, lasty, lastz
[226]300    undefine, glam & undefine, gphi & ; We liberate some memory!
[44]301  ENDELSE
[2]302;---------------------------------------------------------------------
[295]303; We initialize ixmindta, iymindta if needed
[2]304;---------------------------------------------------------------------
[44]305  if n_elements(jpidta) EQ 0 THEN jpidta = jpiglo
306  if n_elements(jpjdta) EQ 0 THEN jpjdta = jpjglo
307  if n_elements(jpkdta) EQ 0 THEN jpkdta = jpkglo
308  if n_elements(ixmindta) EQ 0 THEN ixmindta = 0
309  if n_elements(ixmaxdta) EQ 0 then ixmaxdta = jpidta-1
310  if ixmindta EQ -1 THEN ixmindta = 0
311  IF ixmaxdta EQ -1 then ixmaxdta = jpidta-1
312  if n_elements(iymindta) EQ 0 THEN iymindta = 0
313  IF n_elements(iymaxdta) EQ 0 then iymaxdta = jpjdta-1
314  if iymindta EQ -1 THEN iymindta = 0
315  IF iymaxdta EQ -1 then iymaxdta = jpjdta-1
316  if n_elements(izmindta) EQ 0 THEN izmindta = 0
317  IF n_elements(izmaxdta) EQ 0 then izmaxdta = jpkdta-1
318  if izmindta EQ -1 THEN izmindta = 0
319  IF izmaxdta EQ -1 then izmaxdta = jpkdta-1
[2]320;----------------------------------------------------------------
[150]321; We will read the file
[2]322;---------------------------------------------------------------
[44]323  if n_elements(key_stride) LE 2 then key_stride = [1, 1, 1]
324  key_stride = 1l > long(key_stride)
325;---------------------------------------------------------------------
326;---------------------------------------------------------------------
[2]327@read_ncdf_varget
328;---------------------------------------------------------------------
[44]329;---------------------------------------------------------------------
[271]330; We define common (cm_4data) variables associated with the variable.
[2]331;---------------------------------------------------------------------
332; varname
[172]333  IF NOT keyword_set(callitself) THEN varname = name
[271]334; varunit and others...
[337]335  ncdf_getatt, cdfid, name, add_offset = add_offset, scale_factor = scale_factor, missing_value = missing_value, units = units, _extra = ex
[271]336  IF NOT keyword_set(callitself) THEN varunit = units
[2]337; vardate
[150]338; We make a legible date in function of the specified language.
339  year = long(beginning[0])/10000
340  month = (long(beginning[0])/100) MOD 100
341  day = (long(beginning[0]) MOD 100)
[44]342  vardate = string(format = '(C(CMoA))', 31*(month-1))+' '+strtrim(day, 1)+', '+strtrim(year, 1)
[74]343  varexp = file_basename(filename)
[150]344; We apply the value valmask on land points.
[44]345  if NOT keyword_set(cont_nofill) then begin
[271]346    valmask = 1.e20
[44]347    case 1 of
[275]348      varinq.ndims eq 2:                                               earth = where(mask[*, *, 0] EQ 0) ;xy   array
349      varinq.ndims eq 3 AND (where(varinq.dim EQ inq.recdim))[0] EQ -1:earth = where(mask EQ 0)          ;xyz  array
[365]350      varinq.ndims eq 3 AND (where(varinq.dim EQ inq.recdim))[0] NE -1:earth = where(mask[*, *, 0] EQ 0) ;xyt  array
[275]351      varinq.ndims eq 4:                                               earth = where(mask EQ 0)          ;xyzt array
[271]352    ENDCASE
[44]353  ENDIF ELSE earth = -1
354; we look for  missing_value
[271]355; we apply add_offset, scale_factor and missing_value
356  IF keyword_set(addscl_before) THEN BEGIN
[275]357    IF scale_factor NE 1 THEN res = temporary(res)*scale_factor
358    IF add_offset   NE 0 THEN res = temporary(res)+add_offset
[271]359  ENDIF
[275]360  IF size(missing_value, /type) NE 7 THEN BEGIN
[271]361    IF finite(missing_value) EQ 1 THEN BEGIN
[336]362      CASE 1 OF
363        missing_value GT 1.e6:missing = where(res GT missing_value/10.)
364        missing_value LT -1.e6:missing = where(res LT missing_value/10.)
365        abs(missing_value) LT 1.e-6:missing = where(abs(res) LT 1.e-6)
366        ELSE:missing = where(res EQ missing_value)
367      ENDCASE
[271]368    ENDIF ELSE missing = where(finite(res) EQ 0)
[44]369  ENDIF ELSE missing = -1
[271]370  IF NOT keyword_set(addscl_before) THEN BEGIN
371    if scale_factor NE 1 then res = temporary(res)*scale_factor
372    if add_offset NE 0 then res = temporary(res)+add_offset
373  ENDIF
[275]374  IF missing[0] NE -1 THEN res[temporary(missing)] = !values.f_nan
[365]375  IF earth[0] NE -1 THEN BEGIN
376    IF varinq.ndims eq 3 AND (where(varinq.dim EQ inq.recdim))[0] NE -1 THEN $ ;xyt array
[275]377       earth = earth#replicate(1, jpt)+replicate(nx*ny, n_elements(earth))#lindgen(jpt)
378    IF varinq.ndims eq 4 THEN earth = earth#replicate(1, jpt)+replicate(nx*ny*nz, n_elements(earth))#lindgen(jpt)
379    res[temporary(earth)] = 1.e20
[365]380  ENDIF
[2]381;---------------------------------------------------------------------
[172]382; if it is roms outputs, we need to get additionals infos...
383  IF NOT keyword_set(callitself) THEN BEGIN
384    IF strmid(dimnames[0], 0, 3) EQ 'xi_' AND strmid(dimnames[1], 0, 4) EQ 'eta_' THEN BEGIN
385      ncdf_attget, cdfid, 'theta_s', theta_s, /global
386      ncdf_attget, cdfid, 'theta_b', theta_b, /global
387      ncdf_attget, cdfid, 'hc', hc, /global
[174]388; look for all variables names
[271]389      allvarnames = strarr(inq.nvars)
390      FOR i = 0, inq.nvars-1 DO BEGIN
[174]391        tmp = ncdf_varinq( cdfid, i)
392        allvarnames[i] = tmp.name
393      ENDFOR
394      CASE 1 OF
395        keyword_set(zetazero):zeta = fltarr(nx, ny, jpt)
396        keyword_set(zetafilename):  $
397           zeta = read_ncdf('zeta', firsttps, lasttps, FILENAME = zetafilename $
398                            , /TIMESTEP, /NOSTRUCT, CONT_NOFILL = CONT_NOFILL $
399                            , GRID = vargrid, /CALLITSELF, _EXTRA = ex)
[181]400        (where(allvarnames EQ 'zeta'))[0] NE -1: $
[174]401           zeta = read_ncdf('zeta', firsttps, lasttps, FILENAME = filename $
402                            , /TIMESTEP, /NOSTRUCT, CONT_NOFILL = CONT_NOFILL $
403                            , GRID = vargrid, /CALLITSELF, _EXTRA = ex)
404        ELSE:return, report('The variable zeta was not found in the file, please use the keyword ZETAFILENAME to specify the name of a file containing zeta or use  keyword ZETAZERO to define zeta to 0.')
405      ENDCASE
[192]406      romszinfos = {h:romszinfos.h, zeta:temporary(zeta), theta_s:theta_s, theta_b:theta_b, hc:hc}
[172]407    ENDIF ELSE romszinfos = {h:-1, zeta:-1, theta_s:-1, theta_b:-1, hc:-1}
408  ENDIF
409;---------------------------------------------------------------------
[276]410  IF keyword_set(direc) THEN BEGIN
[310]411    IF jpt EQ 1 THEN res = moyenne(temporary(res), direc, _extra = ex) $
[276]412    ELSE BEGIN
[310]413      res = grossemoyenne(temporary(res), direc, _extra = ex)
[276]414      IF ( strpos(strlowcase(direc), 't') ge 0 ) THEN BEGIN
415        vardate = strtrim(jul2date(time[0]), 1)+' - '+strtrim(jul2date(time[jpt-1]), 1)
416        time = total(time)/float(jpt)
417        jpt = 1
418      ENDIF
[365]419    ENDELSE
[276]420  ENDIF
421;---------------------------------------------------------------------
[44]422  ncdf_close, cdfid
[172]423  IF keyword_set(nostruct) THEN return, res
424  IF keyword_set(key_forgetold) THEN BEGIN
[226]425    return, {arr:temporary(res), grid:vargrid, unit:varunit, experiment:varexp, name:varname}
426  ENDIF ELSE BEGIN
[172]427    return, {tab:temporary(res), grille:vargrid, unite:varunit, experience:varexp, nom:varname}
428  ENDELSE
429
[44]430END
Note: See TracBrowser for help on using the repository browser.