source: trunk/SRC/ReadWrite/ncdf_getmask.pro @ 325

Last change on this file since 325 was 325, checked in by pinsard, 17 years ago

modification of some headers (+some corrections) to prepare usage of the new idldoc

  • Property svn:keywords set to Id
File size: 4.7 KB
Line 
1;+
2;
3; @file_comments
4; get the land/sea mask array from a NetCDF file
5;
6; @categories
7; Read NetCDF file
8;
9; @param fileid {in}{required}{type=salar string or long}
10; if fileid is a scalar string then it is the name of the file (with
11; the full path) to be opened (in that case, the file will be opened
12; and closed within ncdf_getmask).
13; if fileid is a scalar then it is the id of the file return by a call
14; to ncdf_open outside of ncdf_getmask (in that case, the file will
15; NOT be opened and closed within ncdf_getmask)
16;
17; @keyword ADDSCL_BEFORE {default=0}{type=scalar: 0 or 1}
18; put 1 to apply add_offset ad scale factor on data before looking for
19; missing values when using USEASMASK keyword
20;
21; @keyword INVMASK {default=0}{type=scalar: 0 or 1}
22; Inverse the land/sea mask (that should have 0/1 values for land/sea): mask = 1-mask
23;
24; @keyword MASKNAME {type=string}
25; A string giving the name of the variable in the file
26; that contains the land/sea mask
27;
28; @keyword MISSING_VALUE {type=scalar}
29; To define (or redefine if the attribute is
30; already existing) the missing values used with USEASMASK
31; keyword
32;
33; @keyword USEASMASK {type=scalar string}
34; A string giving the name of the variable in the file
35; that will be used to build the land/sea mask. In this case the
36; mask is based on the first record (if record dimension
37; exists). The mask is build according to :
38;    1 the keyword missing_value if existing
39;    2 the attribute 'missing_value' if existing
40;    3 NaN values if existing
41;
42; @keyword
43; _EXTRA to be able to call ncdf_getmask with _extra keyword
44;
45; @returns
46; the land/sea mask 2D or 3D array or -1 in case of error or mask absence
47;
48; @examples
49;
50; IDL> mask = ncdf_getmask('HadISST1_1m_187001_200702_sst_reg1m.nc',useasmask = 'sst', missing_value = -1.00000e+30)
51;
52; IDL> mask = ncdf_getmask('meshmaskORCA2.nc', maskname = 'tmask')
53;
54; @history
55; August 2007: Sebastien Masson (smasson\@lodyc.jussieu.fr)
56;
57; @version
58; $Id$
59;
60;-
61FUNCTION ncdf_getmask, fileid, ADDSCL_BEFORE = addscl_before, MASKNAME = maskname, USEASMASK = useasmask $
62              , MISSING_VALUE = missing_value, INVMASK = invmask, _EXTRA = ex
63;
64  compile_opt idl2, strictarrsubs
65;
66  IF NOT (keyword_set(maskname) OR keyword_set(useasmask)) AND keyword_set(romsgrid) THEN maskname = 'mask_rho'
67  IF NOT (keyword_set(maskname) OR keyword_set(useasmask)) THEN return, -1
68;----------------------------------------------------------
69; should we open the file?
70  IF size(fileid, /type) EQ 7 THEN cdfid = ncdf_open(fileid) ELSE cdfid = fileid
71; what is inside the file
72  inq = ncdf_inquire(cdfid)
73; name of the variables
74  namevar = strarr(inq.nvars)
75  for varid = 0, inq.nvars-1 do begin
76    invar = ncdf_varinq(cdfid, varid)
77    namevar[varid] = strlowcase(invar.name)
78  ENDFOR
79;----------------------------------------------------------
80  CASE 1 OF
81    keyword_set(maskname):mskid = (where(namevar EQ strlowcase(maskname)))[0]
82    keyword_set(useasmask):mskid = (where(namevar EQ strlowcase(useasmask)))[0]
83  ENDCASE
84;
85  if mskid NE -1 THEN BEGIN
86    mskinq = ncdf_varinq(cdfid, mskid)
87; is the mask variable containing the record dimension?
88    withrcd = (where(mskinq.dim EQ inq.recdim))[0]
89    IF withrcd NE -1 THEN BEGIN
90; in order to read only the first record
91; we need to get the size of each dimension
92      count = replicate(1L, mskinq.ndims)
93      FOR d = 0, mskinq.ndims -1 DO BEGIN
94        IF d NE withrcd THEN BEGIN
95          ncdf_diminq, cdfid, mskinq.dim[d], name, size
96          count[d] = size
97        ENDIF
98      ENDFOR
99; read the variable for the first record
100      ncdf_varget, cdfid, mskid, mask, count = count
101    ENDIF ELSE ncdf_varget, cdfid, mskid, mask
102; check if we need to applay add_offset and scale factor
103    ncdf_getatt, cdfid, mskid, add_offset = add, scale_factor = scl, missing_value = miss
104    IF n_elements(missing_value) NE 0 THEN miss = missing_value
105
106    IF keyword_set(addscl_before) THEN BEGIN
107      IF scl NE 1 THEN mask = mask * scl
108      IF add NE 0 THEN mask = mask + add
109    ENDIF
110
111    IF keyword_set(useasmask)  THEN BEGIN
112      IF n_elements(miss) NE 0 THEN BEGIN
113; we have to take care of the float accuracy
114        CASE 1 OF
115          miss GE 1.e6:mask = mask LT (miss-10)
116          miss LE -1.e6:mask = mask GT (miss+10)
117          abs(miss) LE 1.e-6:mask = abs(mask) GT 1.e-6
118          ELSE:mask = mask NE miss
119        ENDCASE
120      ENDIF ELSE BEGIN
121        mask = finite(mask)
122        IF min(mask) EQ 1 THEN BEGIN
123          ras = report( 'missing or nan values not found...')
124          mask = -1
125        ENDIF
126      ENDELSE
127    ENDIF
128
129    mask = byte(round(mask))
130    if keyword_set(invmask) then mask = 1b-mask
131
132  ENDIF ELSE mask = -1
133
134  IF size(fileid, /type) EQ 7 THEN ncdf_close, cdfid
135
136  return, mask
137END
Note: See TracBrowser for help on using the repository browser.