source: trunk/procs/nc_read.pro @ 122

Last change on this file since 122 was 121, checked in by ericg, 16 years ago

mask value now correct when scale_factor is present in netCDF file

File size: 4.9 KB
RevLine 
[2]1;--------------------------- 
[86]2; Reading any netcdf file
[2]3;--------------------------- 
[86]4FUNCTION nc_read, file_name, var_name, ncdf_db, BOXZOOM = boxzoom, TIME_1 = time_1,  TIME_2 =  time_2, ALL_DATA = all_data, NO_MEAN = no_mean
[2]5
6; arguments = file_name, varname
7; ncdf_db=<location>:<path> or just <path>
8; mot clef = iodir time_1 time_2
9
10@common
11@com_eg
[86]12 
[2]13; inits
[86]14   IF debug_w THEN print, '   '
15   IF debug_w THEN print, '   ENTER nc_read...'
[41]16
[2]17   IF NOT keyword_set(TIME_1) THEN time_1 =  1
18   IF NOT keyword_set(TIME_2) THEN time_2 =  time_1
[117]19   IF debug_w THEN print, '  Before read_ncdf,  key_yreverse=', key_yreverse 
[92]20;
[10]21
[86]22; decide which subdomain of data to read
23   IF debug_w THEN print, '     keyword_set(ALL_DATA) : ', keyword_set(ALL_DATA)
[2]24   IF keyword_set(ALL_DATA) THEN BEGIN
[86]25      tout = 1
26   ENDIF ELSE tout = 0
27   CASE vert_type OF
28      'z' : zindex = 0
29      'level' : zindex = 1
30      ELSE: zindex = 0
31   ENDCASE
[2]32
33; local directory
[86]34   IF strpos(ncdf_db, ':') GE 1 THEN directory = (strsplit(ncdf_db, ':', /EXTRACT))[1] $
[2]35    ELSE directory = ncdf_db
36
[86]37; Find the variable's attribute
38   ncdf_getatt, directory+file_name, var_name, add_offset = add_offset, scale_factor = scale_factor, $
39    missing_value = missing_value, units = units, calendar = calendar, long_name = long_name
[2]40
[86]41; By default units for the Z axis are meters
42   IF n_elements(gdept) GT 1 THEN BEGIN
43      IF name_level NE '-' THEN $
44       ncdf_getatt, directory+file_name, name_level, units = units_depth ELSE units_depth = 'm'
45   ENDIF ELSE units_depth = ''
[2]46
[86]47; Check consistency between time_2 computed from param in cmdline and the max number
48; of time steps found in the file.
49   jpt_max = find_jptmax(file_name, IODIRECTORY = directory)
50   IF time_2-time_1+1 GT jpt_max AND jpt_max NE -1 THEN time_2 = time_1+jpt_max-1
[2]51
[86]52; Read the data with a call to read_ncdf
53   IF debug_w THEN print, '     '
54   IF debug_w THEN print, '     Reading raw data from ',  file_name
[2]55
[86]56   lec_data = read_ncdf(var_name, time_1-1, time_2-1, BOXZOOM = boxzoom, FILENAME = directory+file_name, $
[111]57                        /TIMESTEP, TOUT = tout, /NOSTRUCT, /CONT_NOFILL, GRID = vargrid, $
[86]58                        ZINVAR = zinvar, ZINDEX = zindex)
59   field_dim = (size(lec_data))[0]
60   IF field_dim LE 0 THEN stop,  '  Something wrong happened in read_ncdf '
[2]61
[41]62   IF debug_w THEN print, '     '
[121]63   IF debug_w AND keyword_set(boxzoom) THEN print,  '    boxzoom=', boxzoom
[86]64   IF debug_w THEN print,  '    firstxt,firstyt,firstzt=', firstxt, firstyt, firstzt
65   IF debug_w THEN print,  '    lastxt,lastyt,lastzt=', firstxt, firstyt, firstzt   
66   IF debug_w THEN print,  '    zinvar=', zinvar
67   IF debug_w THEN print, '     jpt=', jpt
68   IF debug_w THEN print, '     key_shift=', key_shift
[117]69   IF debug_w THEN print, '     After read_ncdf, key_yreverse=',key_yreverse
[41]70   IF debug_w THEN print, '     '
[2]71   
[86]72; Average data along vertical if needed and update some features
73; needed for plt (data_direc, name_suff)
74   name_suff = ''
75   data_direc = ''
76   update_data, TAB = lec_data, VNAME = var_name, BOXZOOM = boxzoom, ZUNITS = units_depth, $
77    ZINVAR = zinvar, SUFFIX = name_suff, D_DIREC = data_direc, $
78    TIME_1 = time_1, TIME_2 =  time_2, NO_MEAN = no_mean
79   field_dim = (size(lec_data))[0]
[2]80
[86]81; Field attributes
[2]82   field = {name: '', data: lec_data, legend: '', units: '', origin: '', dim: 0, direc: data_direc}
83   field.name = var_name
84   field.origin = directory+file_name
[86]85   field.legend = long_name+name_suff
86   field.units = units
87   field.dim = field_dim
[2]88
[92]89; replace NaN with valmask
[121]90;   idx_t = where (~finite(field.data))
91;   IF idx_t(0) NE -1 THEN field.data(idx_t) = valmask
[92]92
[86]93; get valmask (might need valmask = float(string(valmask))
[117]94   valmask = 1.e20
95   IF size(missing_value, /TYPE) EQ 4 OR size(missing_value, /TYPE) EQ 5 THEN BEGIN
[86]96      valmask = missing_value
[117]97      ; ensure valmask is positive
[86]98      IF valmask LT 0 THEN BEGIN
99         print, '      *** Warning valmask is negative - changing sign: ', valmask
100         idx_t = where (field.data EQ valmask)
[117]101         IF idx_t[0] NE -1 THEN field.data(idx_t) = -valmask
[86]102         valmask = -valmask
103         idx_t=0                ; free memory
104      ENDIF
[10]105   ENDIF
106
[121]107   IF debug_w THEN print, '     valmask=',valmask
108
[92]109; if pseudo 3d mask, read mask file and set masked points to valmask
110; still to do
111
[2]112; min/max
113
114   chardim = ' - dim = '
115   FOR i = 1, (size(field.data))[0] DO BEGIN
116      chardim = chardim+string((size(field.data))[i], format = '(I5)')
117   ENDFOR 
118
[117]119   idx_valmsk = (where (field.data EQ valmask))
120   idx_novalmsk = (where (field.data NE valmask))
121   minf = idx_valmsk[0] EQ -1 ? min(field.data) : min(field.data(idx_novalmsk))
122   maxf = idx_valmsk[0] EQ -1 ? max(field.data) : max(field.data(idx_novalmsk))
[2]123
124   print, '      = ',field.legend, '    [min/max = ',minf , maxf,'  ', field.units,' - masked values = ',valmask, chardim, ']'
125
[41]126   IF debug_w THEN print,  '  ...EXIT nc_read'
127   IF debug_w THEN print,  '  '
[86]128   
[2]129   return, field
130
131END
Note: See TracBrowser for help on using the repository browser.