[272] | 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} |
---|
[341] | 29 | ; To define (or redefine if the attribute is already existing) the |
---|
| 30 | ; missing values used with USEASMASK keyword. Note that this value is |
---|
| 31 | ; not used if TESTOP keyword is given and contains 2 words. |
---|
[272] | 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 |
---|
[341] | 37 | ; exists). The mask is build according to operator defined by TESTOP |
---|
| 38 | ; keyword (default NE) and the testing values defined as |
---|
| 39 | ; 1) the second word of TESTOP if existing |
---|
| 40 | ; 2) MISSING_VALUE keyword |
---|
| 41 | ; 3) attribute missing_value or _fillvalue of the variable USEASMASK |
---|
| 42 | ; 4) !Values.f_nan (can be used only with NE and EQ operators) |
---|
[272] | 43 | ; |
---|
[341] | 44 | ; @keyword TESTOP {default='NE'} {type=scalar string, for example 'GT 0.5'} |
---|
| 45 | ; a string describing the type of test that will be done to define the |
---|
| 46 | ; mask. The test is performed on the variable specified by USEASMASK |
---|
| 47 | ; keyword. |
---|
| 48 | ; TESTOP can contain 1 or 2 words. The first word is the operator |
---|
| 49 | ; definition: "EQ" "NE" "GE" "GT" "LE" "LT" (default is NE). The |
---|
| 50 | ; second word define the testing value. If TESTOP contains only 1 |
---|
| 51 | ; word, then the test value is denifed by |
---|
| 52 | ; 1) MISSING_VALUE keyword |
---|
| 53 | ; 2) attribute missing_value or _fillvalue of the variable USEASMASK |
---|
| 54 | ; 3) !Values.f_nan (can be used only with NE and EQ operators) |
---|
| 55 | ; |
---|
[272] | 56 | ; @keyword |
---|
| 57 | ; _EXTRA to be able to call ncdf_getmask with _extra keyword |
---|
| 58 | ; |
---|
| 59 | ; @returns |
---|
| 60 | ; the land/sea mask 2D or 3D array or -1 in case of error or mask absence |
---|
| 61 | ; |
---|
| 62 | ; @examples |
---|
| 63 | ; |
---|
| 64 | ; IDL> mask = ncdf_getmask('HadISST1_1m_187001_200702_sst_reg1m.nc',useasmask = 'sst', missing_value = -1.00000e+30) |
---|
| 65 | ; |
---|
| 66 | ; IDL> mask = ncdf_getmask('meshmaskORCA2.nc', maskname = 'tmask') |
---|
| 67 | ; |
---|
[341] | 68 | ; IDL> mask = ncdf_getmask('t106.nc', useasmask = 'SLM', testop = 'le 0.5') |
---|
| 69 | ; |
---|
[272] | 70 | ; @history |
---|
| 71 | ; August 2007: Sebastien Masson (smasson\@lodyc.jussieu.fr) |
---|
| 72 | ; |
---|
| 73 | ; @version |
---|
| 74 | ; $Id$ |
---|
| 75 | ; |
---|
| 76 | ;- |
---|
[341] | 77 | FUNCTION ncdf_getmask, fileid, ADDSCL_BEFORE = addscl_before $ |
---|
| 78 | , MASKNAME = maskname, USEASMASK = useasmask $ |
---|
| 79 | , MISSING_VALUE = missing_value, INVMASK = invmask $ |
---|
| 80 | , TESTOP = testop, _EXTRA = ex |
---|
[272] | 81 | ; |
---|
| 82 | compile_opt idl2, strictarrsubs |
---|
| 83 | ; |
---|
| 84 | IF NOT (keyword_set(maskname) OR keyword_set(useasmask)) AND keyword_set(romsgrid) THEN maskname = 'mask_rho' |
---|
| 85 | IF NOT (keyword_set(maskname) OR keyword_set(useasmask)) THEN return, -1 |
---|
| 86 | ;---------------------------------------------------------- |
---|
| 87 | ; should we open the file? |
---|
| 88 | IF size(fileid, /type) EQ 7 THEN cdfid = ncdf_open(fileid) ELSE cdfid = fileid |
---|
| 89 | ; what is inside the file |
---|
| 90 | inq = ncdf_inquire(cdfid) |
---|
| 91 | ; name of the variables |
---|
| 92 | namevar = strarr(inq.nvars) |
---|
| 93 | for varid = 0, inq.nvars-1 do begin |
---|
| 94 | invar = ncdf_varinq(cdfid, varid) |
---|
| 95 | namevar[varid] = strlowcase(invar.name) |
---|
| 96 | ENDFOR |
---|
| 97 | ;---------------------------------------------------------- |
---|
| 98 | CASE 1 OF |
---|
| 99 | keyword_set(maskname):mskid = (where(namevar EQ strlowcase(maskname)))[0] |
---|
| 100 | keyword_set(useasmask):mskid = (where(namevar EQ strlowcase(useasmask)))[0] |
---|
| 101 | ENDCASE |
---|
| 102 | ; |
---|
| 103 | if mskid NE -1 THEN BEGIN |
---|
| 104 | mskinq = ncdf_varinq(cdfid, mskid) |
---|
| 105 | ; is the mask variable containing the record dimension? |
---|
| 106 | withrcd = (where(mskinq.dim EQ inq.recdim))[0] |
---|
| 107 | IF withrcd NE -1 THEN BEGIN |
---|
| 108 | ; in order to read only the first record |
---|
| 109 | ; we need to get the size of each dimension |
---|
| 110 | count = replicate(1L, mskinq.ndims) |
---|
| 111 | FOR d = 0, mskinq.ndims -1 DO BEGIN |
---|
| 112 | IF d NE withrcd THEN BEGIN |
---|
| 113 | ncdf_diminq, cdfid, mskinq.dim[d], name, size |
---|
| 114 | count[d] = size |
---|
| 115 | ENDIF |
---|
| 116 | ENDFOR |
---|
| 117 | ; read the variable for the first record |
---|
| 118 | ncdf_varget, cdfid, mskid, mask, count = count |
---|
| 119 | ENDIF ELSE ncdf_varget, cdfid, mskid, mask |
---|
[341] | 120 | |
---|
| 121 | ; get add_offset, scale factor and missing value attributes |
---|
[272] | 122 | ncdf_getatt, cdfid, mskid, add_offset = add, scale_factor = scl, missing_value = miss |
---|
[341] | 123 | ; do we apply add_offset and scale factor ? |
---|
[272] | 124 | IF keyword_set(addscl_before) THEN BEGIN |
---|
| 125 | IF scl NE 1 THEN mask = mask * scl |
---|
| 126 | IF add NE 0 THEN mask = mask + add |
---|
| 127 | ENDIF |
---|
| 128 | |
---|
| 129 | IF keyword_set(useasmask) THEN BEGIN |
---|
[341] | 130 | |
---|
| 131 | IF n_elements(missing_value) NE 0 THEN miss = missing_value |
---|
| 132 | IF size(miss, /type) EQ 7 THEN miss = !values.f_nan |
---|
| 133 | |
---|
| 134 | IF NOT keyword_set(testop) THEN testop = 'NE' |
---|
| 135 | tmp = strsplit(testop, ' ', /extract) |
---|
| 136 | op = strupcase(tmp[0]) |
---|
| 137 | IF op EQ 'EQ' THEN BEGIN |
---|
| 138 | op = 'NE' |
---|
| 139 | invmask = 1b - keyword_set(invmask) |
---|
| 140 | ENDIF |
---|
| 141 | IF n_elements(tmp) EQ 1 THEN testval = miss ELSE testval = float(tmp[1]) |
---|
| 142 | IF finite(testval) EQ 0 THEN BEGIN |
---|
| 143 | IF op NE 'NE' THEN mask = report('NaN test value can be used only with EQ or NE operator') ELSE mask = finite(mask) |
---|
| 144 | ENDIF ELSE BEGIN |
---|
| 145 | CASE op OF |
---|
| 146 | 'GE':mask = mask GE testval |
---|
| 147 | 'GT':mask = mask GT testval |
---|
| 148 | 'LE':mask = mask LE testval |
---|
| 149 | 'LT':mask = mask LT testval |
---|
| 150 | 'NE':BEGIN |
---|
[272] | 151 | ; we have to take care of the float accuracy |
---|
[341] | 152 | CASE 1 OF |
---|
| 153 | testval GE 1.e6:mask = mask LT (testval - 10) |
---|
| 154 | testval LE -1.e6:mask = mask GT (testval + 10) |
---|
| 155 | abs(testval) LE 1.e-6:mask = abs(mask) GT 1.e-6 |
---|
| 156 | ELSE:mask = mask NE testval |
---|
| 157 | ENDCASE |
---|
| 158 | END |
---|
[272] | 159 | ENDCASE |
---|
| 160 | ENDELSE |
---|
| 161 | |
---|
[361] | 162 | ENDIF |
---|
[272] | 163 | |
---|
[361] | 164 | IF mask[0] NE -1 THEN BEGIN |
---|
| 165 | mask = byte(round(mask)) |
---|
| 166 | if keyword_set(invmask) then mask = 1b - mask |
---|
[341] | 167 | ENDIF |
---|
[361] | 168 | |
---|
[272] | 169 | ENDIF ELSE mask = -1 |
---|
| 170 | |
---|
| 171 | IF size(fileid, /type) EQ 7 THEN ncdf_close, cdfid |
---|
| 172 | |
---|
| 173 | return, mask |
---|
| 174 | END |
---|