source: trunk/SRC/Interpolation/file_interp.pro @ 305

Last change on this file since 305 was 305, checked in by smasson, 17 years ago

bugfix in file_interp, not cleaned in changeset:303

  • Property svn:keywords set to Id
File size: 18.6 KB
Line 
1;+
2;
3; @hidden
4;
5;-
6FUNCTION call_interp2d, data, inlon, inlat, inmask, outlon, outlat, INIRR = inirr, METHOD = method, SMOOTH = smooth, WEIG = weig, ADDR = addr, NAN_VALUE = NaN_value, _extra = ex
7;
8  compile_opt idl2, strictarrsubs
9;
10; for byte, short and long, convert to double before extrapolation and interpolation
11  intype = size(data, /type)
12  if intype LE 3 THEN data = double(temporary(data))
13;
14; take care of NaN values
15  mask = inmask *finite(data)
16;
17  IF keyword_set(NaN_value) THEN BEGIN
18    CASE 1 OF
19      nan_value GT 1.e6:mask = temporary(mask) * (data LT (nan_value-10.))
20      nan_value LT -1.e6:mask = temporary(mask) * (data GT (nan_value+10.))
21      abs(nan_value) LT 1.e-6:mask = temporary(mask) * (data GT 1.e-6)
22      ELSE:mask = temporary(mask) * (data NE nan_value)
23    ENDCASE
24  ENDIF
25; extrapolation
26    IF keyword_set(smooth) THEN data = extrapsmooth(temporary(data), mask, /x_periodic, _extra = ex) $
27    ELSE data = extrapolate(temporary(data), mask, /x_periodic, _extra = ex)
28; interpolation
29  IF NOT keyword_set(inirr) THEN BEGIN
30    data = fromreg(method, temporary(data), inlon, inlat, outlon, outlat, WEIG = weig, ADDR = addr, _extra = ex)
31  ENDIF ELSE BEGIN
32    data = fromirr(method, temporary(data), inlon, inlat, -1, outlon, outlat, -1, WEIG = weig, ADDR = addr)
33  ENDELSE
34
35  if intype LE 3 THEN data = round(temporary(data))
36
37  RETURN, data
38END
39;+
40;
41; @file_comments
42; interpolate a NetCDF file from a grid to another (both regular or not)
43;
44; @categories
45; Interpolation, NetCDF
46;
47; @param filein {in}{type=scalar string}
48; input file name (must exist)
49;
50; @param fileout {in}{type=scalar string}
51; output file name (will be overwritten if already exist)
52;
53; @param gridout {in}{type=scalar string}
54; output grid file name (must exist and must contain the
55; longitude and latitude axis as 1D or 2D arrays)
56;
57; @keyword GRIDIN {type=scalar string}{default=set to filein}
58; define the input grid file name. It must exist and must contain the
59; longitude and latitude axis as 1D or 2D arrays. Useful if
60; filein file doesn't contain longitude and latitude axis
61;
62; @keyword MASKIN {type=scalar string}{default=set to gridin}
63; define the input mask file name. It must exist. The mask will be
64; determined through <pro>ncdf_getmask</pro> according to the keywords
65; inmaskname, ininvmask, inuseasmask, inmissing_value, inaddscl_before
66; (see below)
67;
68; @keyword MASKOUT {type=scalar string}{default=set to gridout}
69; define the output mask file name. It must exist. The mask will be
70; determined through <pro>ncdf_getmask</pro> according to the keywords
71; outmaskname, outinvmask, outuseasmask, outmissing_value,
72; outaddscl_before (see bellow)
73;
74; @keyword KEEP {type=string array}{default=all variables}
75; array defining the name of the variables that must be kept in the
76; output file
77;
78; @keyword REMOVE {type=string array}{default=empty}
79; array defining the name of the variables that will be removed in the
80; output file
81;
82; @keyword METHOD {type=scalar string}{default='bilinear'}
83; interpolation method: can be only 'bilinear' (or 'imoms3' if the input grid
84; is a "regular" grid). A "regular/rectangular grid" is defined as a
85; grid for which each longitude lines have the same latitude and each
86; latitude columns have the same longitude.
87;
88; @keyword SMOOTH {type=scalar 0 or 1}{default=0}
89; activate to use <pro>extrapsmooth</pro> instead of
90; <pro>extrapolate</pro> when extrapolating input data over masked
91; points.
92;
93; @keyword SET_XDIMNAME {type=scalar string}{default=not used}
94; used to defined the name of x dimension in filein input file when
95; gridin keyword is used and when the x dimension name is not the same
96; in filein and gridin files. By default, we assume both file have the
97; same x dimension name.
98;
99; @keyword SET_YDIMNAME {type=scalar string}{default=not used}
100; same as set_xdimname but for y dimension
101;
102; @keyword SET_XAXISNAME {type=scalar string}{default=not used}
103; used to defined the name of the variable containing the x axis in
104; filein input file when gridin keyword is used and when its variable
105; containing the x axis name is not the same. By default, we assume
106; both file have the same x axis name. Not that if filein includes x
107; axis there is no point to use gridin
108;
109; @keyword SET_YAXISNAME {type=scalar string}{default=not used}
110; same as set_xaxisname but for y dimension
111;
112; @keyword INMASKNAME {type=scalar string}{default=not used}
113; A string giving the name of the variable in the file maskin that
114; contains the land/sea mask
115;
116; @keyword OUTMASKNAME {type=scalar string}{default=not used}
117; same as inmaskname but for output mask file maskout
118;
119; @keyword ININVMASK {default=0}{type=scalar: 0 or 1}
120; Inverse the land/sea mask of the input mask file maskin (that should
121; have 0/1 values for land/sea)
122;
123; @keyword OUTINVMASK {default=0}{type=scalar: 0 or 1}
124; same as ininvmask but for output mask file maskout
125;
126; @keyword INUSEASMASK {type=scalar string}
127; A string giving the name of the variable in the input mask file maskin
128; that will be used to build the land/sea mask. In this case the
129; mask is based on the first record (if record dimension
130; exists). The mask is build according to :
131;    1 the keyword missing_value if existing
132;    2 the attribute 'missing_value' if existing
133;    3 NaN values if existing
134;
135; @keyword OUTUSEASMASK {type=scalar string}
136; same as inuseasmask but for output mask file maskout
137;
138; @keyword INMISSING_VALUE {type=scalar}
139; To define (or redefine if the attribute is already existing) the
140; missing values used by INUSEASMASK keyword
141;
142; @keyword OUTMISSING_VALUE {type=scalar}
143; same as inmissing_value but for output mask file maskout
144;
145; @keyword INADDSCL_BEFORE {default=0}{type=scalar: 0 or 1}
146; put 1 to apply add_offset and scale factor on data before looking for
147; missing values when using INUSEASMASK keyword
148;
149; @keyword OUTADDSCL_BEFORE {default=0}{type=scalar: 0 or 1}
150; same as inaddscl_before but for output mask file maskout
151;
152; @keyword NAN_VALUE {type=scalar}{default=not used}
153; define the missing value in input data (missing values are treated
154; like masked values and will be filled with extrapolation before
155; interpolation).
156;
157; @keyword ADDR {type=2d array or variable name}
158; 1) at the first call of file_interp:
159;   This keyword can be set to a named variable (undefined or equal to
160;   0) into which the addresses used to perform the interpolation will
161;   be copied when the current routine exits.
162; 2) Next, once this keyword is set to a defined 2d array, it is used
163;   to bypass the computation of the weights and addresses used to
164;   perform the interpolation. In this case, interpolation is much
165;   faster
166;
167; @keyword WEIG {type=2d array or variable name}
168; (see ADDR)
169;
170; @keyword INXAXISNAME {default='x', 'longitude', 'nav_lon', 'lon', 'lon_rho' or 'NbLongitudes'}{type=scalar string}
171; A string giving the name of the variable containing the x axis in
172; the input grid file gridin
173;
174; @keyword INYAXISNAME {default='y', 'latitude', 'nav_lat','lat', 'lat_rho' or 'NbLatitudes'}{type=scalar string}
175; same as inxaxisname but for the y axis in the input grid file gridin
176;
177; @keyword OUTXAXISNAME {default='x', 'longitude', 'nav_lon', 'lon', 'lon_rho' or 'NbLongitudes'}{type=scalar string}
178; same as inxaxisname but for output grid file gridout
179;
180; @keyword OUTYAXISNAME {default='y', 'latitude', 'nav_lat','lat', 'lat_rho' or 'NbLatitudes'}{type=scalar string}
181; same as inyaxisname but for output grid file gridout
182;
183; @keyword  _EXTRA
184; to use <pro>extrapolate</pro>, <pro>extrapsmooth</pro> and <pro>fromreg</pro> keywords
185;
186; @uses
187; <pro>extrapsmooth</pro>, <pro>extrapolate</pro>, <pro>fromreg</pro> and <pro>fromirr</pro>
188;
189; @restrictions
190;
191; - perform only horizontal interpolations on scalar fields
192; - will fill all masked and missing values before interpolation
193; - attributes (like valid_min...) are not updated
194; - see restrictions of <pro>fromreg</pro> and <pro>fromirr</pro>
195;
196; @examples
197;
198; IDL> file_interp, filein, fileout, gridout, inxaxisname = 'lo', inyaxisname = 'la', keep = ['lo', 'la', 'cond_sed']
199;
200; IDL> file_interp, in, out, gdout, inuseasmask = 'sst', inmissing_value = -1.00000e+30, nan_value = -1000.00
201;
202; @history
203;  September 2007: Sebastien Masson (smasson\@locean-ipsl.upmc.fr)
204;
205; @version
206; $Id$
207;
208;-
209PRO file_interp, filein, fileout, gridout, GRIDIN = gridin, MASKIN = maskin, MASKOUT = maskout $
210                 , KEEP = keep, REMOVE = remove, METHOD = method, SMOOTH = smooth $
211                 , SET_XDIMNAME = set_xdimname, SET_YDIMNAME = set_ydimname $
212                 , SET_XAXISNAME = set_xaxisname, SET_YAXISNAME = set_yaxisname $
213                 , INMASKNAME = inmaskname, ININVMASK = ininvmask, INUSEASMASK = inuseasmask $
214                 , INMISSING_VALUE = inmissing_value, INADDSCL_BEFORE = inaddscl_before $
215                 , OUTMASKNAME = outmaskname, OUTINVMASK = outinvmask, OUTUSEASMASK = outuseasmask $
216                 , OUTMISSING_VALUE = outmissing_value, OUTADDSCL_BEFORE = outaddscl_before $
217                 , NAN_VALUE = NaN_value, WEIG = weig, ADDR = addr $
218                 , INXAXISNAME = inxaxisname, INYAXISNAME = inyaxisname $
219                 , OUTXAXISNAME = outxaxisname, OUTYAXISNAME = outyaxisname, _EXTRA = ex
220;
221  compile_opt idl2, strictarrsubs
222  revision = '$Id$'
223;
224  IF NOT keyword_set(method) THEN method = 'bilinear'
225;
226; input filenames checks...
227;
228  inid = ncdf_open(filein)
229  ininq = ncdf_inquire(inid)
230
231  outid = ncdf_create(fileout, /clobber)
232  ncdf_control, outid, /nofill
233
234  IF NOT keyword_set(gridin) THEN gridin = filein
235
236  IF NOT keyword_set(maskin) THEN maskin = gridin
237  IF NOT keyword_set(maskout) THEN maskout = gridout
238;
239; Copy global attributes
240;
241  FOR i = 0, ininq.ngatts-1 DO BEGIN
242    name = ncdf_attname(inid, i, /global)
243    dummy = ncdf_attcopy(inid, name, outid, /in_global, /out_global)
244  ENDFOR
245  ncdf_attput, outid, 'Created_by', revision, /GLOBAL
246;
247; x/y dim and x/yaxis informations
248;
249  ncdf_getaxis, gridin, indimidx, indimidy, inlon, inlat, xdimname = inxdimname, ydimname = inydimname $
250                , xaxisname = inxaxisname, yaxisname = inyaxisname
251  get_gridparams, inlon, inlat, jpiin, jpjin, 2
252  IF keyword_set(set_xdimname) THEN inxdimname = set_xdimname
253  IF keyword_set(set_ydimname) THEN inydimname = set_ydimname
254  IF keyword_set(set_xaxisname) THEN inxaxisname = set_xaxisname
255  IF keyword_set(set_yaxisname) THEN inyaxisname = set_yaxisname
256;
257  ncdf_getaxis, gridout, outdimidx, outdimidy, outlon, outlat, xaxisname = outxaxisname, yaxisname = outyaxisname
258  get_gridparams, outlon, outlat, jpiout, jpjout, 2
259;
260; masks
261;
262  inmask = ncdf_getmask(maskin, MASKNAME = inmaskname, INVMASK = ininvmask, USEASMASK = inuseasmask $
263                       , MISSING_VALUE = inmissing_value, ADDSCL_BEFORE = inaddscl_before)
264  inmasksz = size(inmask, /dimensions)
265  IF size(inmask, /n_dimensions) EQ 2 THEN inmasksz = [inmasksz, 0]
266  IF n_elements(inmaskname) EQ 0 THEN inmaskname = 'not defined' ; default definition
267  outmask = ncdf_getmask(maskout, MASKNAME = outmaskname, INVMASK = outinvmask, USEASMASK = outuseasmask $
268                        , MISSING_VALUE = outmissing_value, ADDSCL_BEFORE = outaddscl_before)
269;
270; irregular grids?
271;
272  CASE 0 OF
273    array_equal(inlon[*, 0], inlon[*, jpjin-1]):inirr = 1b
274    array_equal(inlat[0, *], inlat[jpiin-1, *]):inirr = 1b
275    array_equal(inlon, inlon[*, 0]#replicate(1, jpjin)):inirr = 1b
276    array_equal(inlat, replicate(1, jpiin)#(inlat[0, *])[*]):inirr = 1b
277    ELSE:inirr = 0b
278  ENDCASE
279  CASE 0 OF
280    array_equal(outlon[*, 0], outlon[*, jpjout-1]):outirr = 1b
281    array_equal(outlat[0, *], outlat[jpiout-1, *]):outirr = 1b
282    array_equal(outlon, outlon[*, 0]#replicate(1, jpjout)):outirr = 1b
283    array_equal(outlat, replicate(1, jpiout)#(outlat[0, *])[*]):outirr = 1b
284    ELSE:outirr = 0b
285  ENDCASE
286
287  IF inirr AND method NE 'bilinear' THEN stop
288;
289; Dimensions
290;
291  indimsz = lonarr(ininq.ndims)
292  outdimsz = lonarr(ininq.ndims)
293  outdimid = lonarr(ininq.ndims)
294  FOR i = 0, ininq.ndims-1 DO BEGIN
295    ncdf_diminq, inid, i, name, size
296    indimsz[i] = size
297    outdimsz[i] = size
298    CASE 1 OF
299      strlowcase(name) EQ strlowcase(inxdimname): BEGIN
300        outdimid[i] = ncdf_dimdef(outid, name, jpiout)
301        outdimsz[i] = jpiout
302        indimx = i
303        outdimx = outdimid[i]
304      END
305      strlowcase(name) EQ strlowcase(inydimname): BEGIN
306        outdimid[i] = ncdf_dimdef(outid, name, jpjout)
307        outdimsz[i] = jpjout
308        indimy = i
309        outdimy = outdimid[i]
310      END
311      i EQ ininq.recdim: outdimid[i] = ncdf_dimdef(outid, name, /UNLIMITED)
312      ELSE: outdimid[i] = ncdf_dimdef(outid, name, size)
313    ENDCASE
314  ENDFOR
315;
316; Variables
317;
318  outvarid = lonarr(ininq.nvars)
319  FOR i = 0, ininq.nvars-1 DO BEGIN
320    varinq = ncdf_varinq(inid, i)
321    okvar = 1
322    IF keyword_set(keep) THEN okvar = total(strlowcase(keep) EQ strlowcase(varinq.name)) EQ 1
323    IF keyword_set(remove) THEN okvar = total(strlowcase(remove) EQ strlowcase(varinq.name)) EQ 0
324    IF okvar THEN BEGIN
325      IF varinq.ndims EQ 0 THEN BEGIN ; scalar variable
326        outvarid[i] = ncdf_vardef(outid, varinq.name $
327                                  , BYTE = varinq.datatype EQ 'BYTE', CHAR = varinq.datatype EQ 'CHAR' $
328                                  , SHORT = varinq.datatype EQ 'INT', LONG = varinq.datatype EQ 'LONG' $
329                                  , FLOAT = varinq.datatype EQ 'FLOAT', DOUBLE = varinq.datatype EQ 'DOUBLE')
330      ENDIF ELSE BEGIN          ; array
331        CASE 1 OF
332          strlowcase(varinq.name) EQ strlowcase(inxaxisname):BEGIN ; xaxis
333            IF outirr THEN dimvar = [outdimx, outdimy] ELSE dimvar = [outdimx]
334          END
335          strlowcase(varinq.name) EQ strlowcase(inyaxisname):BEGIN ; yaxis
336            IF outirr THEN dimvar = [outdimx, outdimy] ELSE dimvar = [outdimy]
337          END
338          strlowcase(varinq.name) EQ strlowcase(inmaskname):BEGIN ; mask
339            IF outmask[0] NE -1 THEN dimvar = outdimid[varinq.dim] ELSE dimvar = -1
340          END
341          (total(varinq.dim EQ indimx) + total(varinq.dim EQ indimx)) EQ 1: dimvar = -1 ; strange variable...
342          ELSE: dimvar = outdimid[varinq.dim]
343        ENDCASE
344        IF dimvar[0] NE -1 THEN BEGIN
345          outvarid[i] = ncdf_vardef(outid, varinq.name, dimvar $
346                                    , BYTE = varinq.datatype EQ 'BYTE', CHAR = varinq.datatype EQ 'CHAR' $
347                                    , SHORT = varinq.datatype EQ 'INT', LONG = varinq.datatype EQ 'LONG' $
348                                    , FLOAT = varinq.datatype EQ 'FLOAT', DOUBLE = varinq.datatype EQ 'DOUBLE')
349        ENDIF ELSE outvarid[i] = - 1
350      ENDELSE
351; Variables attributes
352      IF outvarid[i] NE - 1 THEN BEGIN
353        FOR j = 0, varinq.natts-1 DO BEGIN
354          name = ncdf_attname(inid, i, j)
355          dummy = ncdf_attcopy(inid, i, name, outid, outvarid[i])
356        ENDFOR
357      ENDIF
358    ENDIF ELSE outvarid[i] = -1
359  ENDFOR
360;
361  ncdf_control, outid, /endef
362;
363  FOR i = 0, ininq.nvars-1 DO BEGIN
364    IF outvarid[i] NE -1 THEN BEGIN
365      varinq = ncdf_varinq(inid, i)
366      IF varinq.ndims GE 2 THEN BEGIN
367        interp = varinq.dim[0] EQ indimx AND varinq.dim[1] EQ indimy
368      ENDIF ELSE interp = 0b
369      CASE 1 OF
370        strlowcase(varinq.name) EQ strlowcase(inxaxisname):BEGIN ; x axis
371          IF outirr THEN ncdf_varput, outid, outvarid[i], outlon $
372          ELSE ncdf_varput, outid, outvarid[i], outlon[*, 0]
373        END
374        strlowcase(varinq.name) EQ strlowcase(inyaxisname):BEGIN ; y axis
375          IF outirr THEN ncdf_varput, outid, outvarid[i], outlat $
376          ELSE ncdf_varput, outid, outvarid[i], reform(outlat[0, *])
377        END
378        strlowcase(varinq.name) EQ strlowcase(inmaskname):BEGIN ; mask
379          ncdf_varput, outid, outvarid[i], outmask
380        END
381        ELSE:BEGIN
382          CASE varinq.ndims OF
383            0:BEGIN             ; salar
384              ncdf_varget, inid, i, data
385              ncdf_varput, outid, outvarid[i], temporary(data)
386            END
387            1:BEGIN             ; 1D
388              ncdf_varget, inid, i, data
389              ncdf_varput, outid, outvarid[i], temporary(data)
390            END
391            2:BEGIN             ; 2D
392              ncdf_varget, inid, i, data
393              IF interp THEN data = call_interp2d(temporary(data), inlon, inlat, inmask[*, *, 0], outlon, outlat $
394                                                  , INIRR = inirr, METHOD = method, SMOOTH = smooth $
395                                                  , WEIG = weig, ADDR = addr, NAN_VALUE = NaN_value, _extra = ex)
396              ncdf_varput, outid, outvarid[i], temporary(data)
397            END
398            3:BEGIN             ; 3D
399              FOR k = 0, indimsz[varinq.dim[2]]-1 DO BEGIN
400                incnt = [indimsz[varinq.dim[0: 1]], 1]
401                outcnt = [outdimsz[varinq.dim[0: 1]], 1]
402                off = [0, 0, k]
403                ncdf_varget, inid, i, data, offset = off, count = incnt
404                IF n_elements(inmasksz) GE 3 THEN BEGIN
405                  IF inmasksz[2] EQ indimsz[varinq.dim[2]] AND varinq.dim[2] NE ininq.recdim THEN tmpmsk = inmask[*, *, k] $
406                  ELSE tmpmsk = inmask[*, *, 0]
407                ENDIF ELSE tmpmsk = inmask[*, *, 0]
408                IF interp THEN data = call_interp2d(temporary(data), inlon, inlat, temporary(tmpmsk), outlon, outlat $
409                                                    , INIRR = inirr, METHOD = method, SMOOTH = smooth $
410                                                    , WEIG = weig, ADDR = addr, NAN_VALUE = NaN_value, _extra = ex)
411                ncdf_varput, outid, outvarid[i], temporary(data), offset = off, count = outcnt
412              ENDFOR
413            END
414            4:BEGIN             ; 4D
415              FOR t = 0, indimsz[varinq.dim[3]]-1 DO BEGIN
416                FOR k = 0, indimsz[varinq.dim[2]]-1 DO BEGIN
417                  incnt = [indimsz[varinq.dim[0: 1]], 1, 1]
418                  outcnt = [outdimsz[varinq.dim[0: 1]], 1, 1]
419                  off = [0, 0, k, t]
420                  ncdf_varget, inid, i, data, offset = off, count = incnt
421                  IF n_elements(inmasksz) GE 3 THEN BEGIN
422                    IF inmasksz[2] EQ indimsz[varinq.dim[2]] THEN tmpmsk = inmask[*, *, k] ELSE tmpmsk = inmask
423                  ENDIF ELSE tmpmsk = inmask[*, *, 0]
424                  IF interp THEN data = call_interp2d(temporary(data), inlon, inlat, temporary(tmpmsk), outlon, outlat $
425                                                      , INIRR = inirr, METHOD = method, SMOOTH = smooth $
426                                                      , WEIG = weig, ADDR = addr, NAN_VALUE = NaN_value, _extra = ex)
427                  ncdf_varput, outid, outvarid[i], temporary(data), offset = off, count = outcnt
428                ENDFOR
429              ENDFOR
430            END
431          ENDCASE
432        END
433      ENDCASE
434    ENDIF
435  ENDFOR
436
437  ncdf_close, inid
438  ncdf_close, outid
439
440  return
441END
Note: See TracBrowser for help on using the repository browser.