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

Last change on this file since 443 was 443, checked in by smasson, 13 years ago

file_interp: mask output data if outmask defined

  • Property svn:keywords set to Id
File size: 25.1 KB
Line 
1;+
2;
3; @hidden
4;
5;-
6FUNCTION boxmean_interp, data, divx, divy
7 ;
8  compile_opt idl2, strictarrsubs
9;
10  sz = size(data, /dimensions )
11  jpiout = sz[0] / divx
12  jpjout = sz[1] / divy
13;
14  data = reform(data, divx, jpiout, divy, jpjout, /overwrite)
15  data = total(temporary(data), 1)   ; ave along divx
16  data = total(temporary(data), 2)   ; ave along divy
17  data = temporary(data) / float(divx*divy)
18
19  RETURN, data
20END
21;+
22;
23; @hidden
24;
25;-
26FUNCTION call_interp2d, data, inlon, inlat, inmask, outlon, outlat $
27                        , INIRR = inirr, METHOD = method, SMOOTH = smooth $
28                        , WEIG = weig, ADDR = addr, MISSING_VALUE = missing_value $
29                        , DIVX = divx, DIVY = divy, OUTMASK = outmask $
30                        , GETHAN = gethan, LETHAN = lethan, _EXTRA = ex
31;
32  compile_opt idl2, strictarrsubs
33;
34; for byte, short and long, convert to double before extrapolation and interpolation
35  intype = size(data, /type)
36  if intype LE 3 THEN data = double(temporary(data))
37;
38; take care of NaN values
39  nanmask = finite(data)
40  totnanmask = total(nanmask)
41  IF totnanmask EQ 0 THEN return, !values.f_nan
42  IF totnanmask NE n_elements(nanmask) THEN BEGIN
43    data[where(nanmask EQ 0b)] = 1.e20 ; put large value to be sure they are removed during the interpolation
44    IF inmask[0] NE -1 THEN mask = temporary(nanmask) * inmask ELSE mask = temporary(nanmask)
45  ENDIF ELSE mask = inmask
46; take care of missing values
47  tpmiss = size(missing_value, /type)
48  IF tpmiss NE 0 AND tpmiss NE 7 THEN BEGIN
49    CASE 1 OF
50      missing_value GT 1.e6:missmask = data LT (missing_value - 10.)
51      missing_value LT -1.e6:missmask = data GT (missing_value + 10.)
52      abs(missing_value) LT 1.e-6:missmask = abs(data) GT 1.e-6
53      ELSE:missmask = data NE missing_value
54    ENDCASE
55    IF total(missmask) EQ 0 THEN return, missing_value
56    IF mask[0] NE -1 THEN mask = temporary(missmask) * mask ELSE mask = temporary(missmask)
57  ENDIF
58
59; extrapolation
60  IF keyword_set(smooth) THEN data = extrapsmooth(temporary(data), mask, /x_periodic, _extra = ex) $
61  ELSE data = extrapolate(temporary(data), mask, /x_periodic, _extra = ex)
62; interpolation
63  IF method EQ 'boxmean' THEN BEGIN
64    data = boxmean_interp(temporary(data), divx, divy)
65  ENDIF ELSE BEGIN
66    IF NOT keyword_set(inirr) THEN BEGIN
67      data = fromreg(method, temporary(data), inlon, inlat, outlon, outlat, WEIG = weig, ADDR = addr, _extra = ex)
68    ENDIF ELSE BEGIN
69      data = fromirr(method, temporary(data), inlon, inlat, -1, outlon, outlat, -1, WEIG = weig, ADDR = addr)
70    ENDELSE
71  ENDELSE
72
73  IF n_elements(gethan) EQ 1 THEN data = gethan > temporary(data)
74  IF n_elements(lethan) EQ 1 THEN data = temporary(data) < lethan
75
76  if intype LE 3 THEN data = round(temporary(data))
77  IF outmask[0] NE -1 THEN data = temporary(data) * outmask
78
79  RETURN, data
80END
81;+
82;
83; @file_comments
84; interpolate a NetCDF file from a grid to another (both regular or not)
85;
86; @categories
87; Interpolation, NetCDF
88;
89; @param filein {in}{type=scalar string}
90; input file name (must exist)
91;
92; @param fileout {in}{type=scalar string}
93; output file name (will be overwritten if already exist)
94;
95; @param gridout {in}{type=scalar string or 2 element vector if boxmean method}
96; if boxmean method:
97;  2 elements vector defining the size of the box used to compute the
98;  mean value. It must divide the original grid size.
99; else:
100;  output grid file name (must exist and must contain the
101;  longitude and latitude axis as 1D or 2D arrays)
102;
103; @keyword GRIDIN {type=scalar string}{default=set to filein}
104; define the input grid file name. It must exist and must contain the
105; longitude and latitude axis as 1D or 2D arrays. Useful if
106; filein file doesn't contain longitude and latitude axis
107;
108; @keyword MASKIN {type=scalar string}{default=set to gridin}
109; define the input mask file name. It must exist. The mask will be
110; determined through <pro>ncdf_getmask</pro> according to the keywords
111; inmaskname, ininvmask, inuseasmask, inmissing_value, inaddscl_before
112; (see below)
113;
114; @keyword MASKOUT {type=scalar string}{default=set to gridout}
115; define the output mask file name. It must exist. The mask will be
116; determined through <pro>ncdf_getmask</pro> according to the keywords
117; outmaskname, outinvmask, outuseasmask, outmissing_value,
118; outaddscl_before (see below).
119;
120; @keyword KEEP {type=string array}{default=all variables}
121; array defining the name of the variables that must be kept in the
122; output file
123;
124; @keyword REMOVE {type=string array}{default=empty}
125; array defining the name of the variables that will be removed in the
126; output file
127;
128; @keyword METHOD {type=scalar string}{default='bilinear'}
129; interpolation method: can be only 'bilinear', 'boxmean' (or 'imoms3' if the input grid
130; is a "regular" grid). A "regular/rectangular grid" is defined as a
131; grid for which each longitude lines have the same latitude and each
132; latitude columns have the same longitude.
133; boxmean interpolation is simply the mean value over a box of nx by ny
134; points (see gridout definition).
135;
136; @keyword SMOOTH {type=scalar 0 or 1}{default=0}
137; activate to use <pro>extrapsmooth</pro> instead of
138; <pro>extrapolate</pro> when extrapolating input data over masked
139; points.
140;
141; @keyword SET_XDIMNAME {type=scalar string}{default=not used}
142; used to defined the name of x dimension in filein input file when
143; gridin keyword is used and when the x dimension name is not the same
144; in filein and gridin files. By default, we assume both file have the
145; same x dimension name.
146;
147; @keyword SET_YDIMNAME {type=scalar string}{default=not used}
148; same as set_xdimname but for y dimension
149;
150; @keyword SET_XAXISNAME {type=scalar string}{default=not used}
151; used to defined the name of the variable containing the x axis in
152; filein input file when gridin keyword is used and when its variable
153; containing the x axis name is not the same. By default, we assume
154; both file have the same x axis name. Not that if filein includes x
155; axis there is no point to use gridin
156;
157; @keyword SET_YAXISNAME {type=scalar string}{default=not used}
158; same as set_xaxisname but for y dimension
159;
160; @keyword INMASKNAME {type=scalar string}{default=not used}
161; A string giving the name of the variable in the file maskin that
162; contains the land/sea mask
163;
164; @keyword OUTMASKNAME {type=scalar string}{default=not used}
165; same as inmaskname but for output mask file maskout
166;
167; @keyword ININVMASK {default=0}{type=scalar: 0 or 1}
168; Inverse the land/sea mask of the input mask file maskin (that should
169; have 0/1 values for land/sea)
170;
171; @keyword OUTINVMASK {default=0}{type=scalar: 0 or 1}
172; same as ininvmask but for output mask file maskout
173;
174; @keyword INUSEASMASK {type=scalar string}
175; A string giving the name of the variable in the input mask file
176; that will be used to build the input land/sea mask. In this case the
177; mask is based on the first record (if record dimension
178; exists). The input mask is build according to operator defined by INTESTOP
179; keyword (default NE) and the testing values defined as
180;   1) the second word of TESTOP if existing
181;   2) MISSING_VALUE keyword
182;   3) attribute missing_value or _fillvalue of the variable USEASMASK
183;   4) !Values.f_nan (can be used only with NE and EQ operators)
184;
185; @keyword OUTUSEASMASK {type=scalar string}
186; same as inuseasmask but for output mask file maskout
187;
188; @keyword INMISSING_VALUE {type=scalar}
189; To define (or redefine if the attribute is already existing) the
190; missing values used with INUSEASMASK keyword to build the input mask.
191; Note that this value is not used if INTESTOP keyword is given and
192; contains 2 words.
193; Note: do not mismatch with MISSING_VALUE used to detect missing
194; values at reach record.   
195;
196; @keyword OUTMISSING_VALUE {type=scalar}
197; same as inmissing_value but for output mask file maskout
198;
199; @keyword INTESTOP {default='NE'} {type=scalar string, for example 'GT 0.5'}
200; a string describing the type of test that will be done to define the
201; input mask. The test is performed on the variable specified by INUSEASMASK
202; keyword.
203; INTESTOP can contain 1 or 2 words. The first word is the operator
204; definition: "EQ" "NE" "GE" "GT" "LE" "LT" (default is NE). The
205; second word define the testing value. If INTESTOP contains only 1
206; word, then the test value is denifed by
207;   1) INMISSING_VALUE keyword
208;   2) attribute missing_value or _fillvalue of the variable INUSEASMASK
209;   3) !Values.f_nan (can be used only with NE and EQ operators)
210;
211; @keyword OUTTESTOP {default='NE'} {type=scalar string, for example 'GT 0.5'}
212; same as INTESTOP but for output mask file maskout
213;
214; @keyword INADDSCL_BEFORE {default=0}{type=scalar: 0 or 1}
215; put 1 to apply add_offset and scale factor on data before looking for
216; missing values when using INUSEASMASK keyword
217;
218; @keyword OUTADDSCL_BEFORE {default=0}{type=scalar: 0 or 1}
219; same as inaddscl_before but for output mask file maskout
220;
221; @keyword MISSING_VALUE {type=scalar}{default=defined by attribute missing_value or _fillvalue}
222; (Re)define the missing value in input data (missing values are treated
223; like masked values and will be filled with extrapolation before
224; interpolation).
225; Note: do not mismatch with (IN/OUT)MISSING_VALUE which are missing value
226; used (in association with (IN/OUT)USEASMASK) to built the mask (that
227; does not change from one record to another).
228; Note: this value will be apply to all interpolated variables
229;
230; @keyword ADDR {type=2d array or variable name}
231; 1) at the first call of file_interp:
232;   This keyword can be set to a named variable (undefined or equal to
233;   0) into which the addresses used to perform the interpolation will
234;   be copied when the current routine exits.
235; 2) Next, once this keyword is set to a defined 2d array, it is used
236;   to bypass the computation of the weights and addresses used to
237;   perform the interpolation. In this case, interpolation is much
238;   faster
239;
240; @keyword WEIG {type=2d array or variable name}
241; (see ADDR)
242;
243; @keyword INXAXISNAME {default='x', 'longitude', 'nav_lon', 'lon', 'lon_rho' or 'NbLongitudes'}{type=scalar string}
244; A string giving the name of the variable containing the x axis in
245; the input grid file gridin
246;
247; @keyword INYAXISNAME {default='y', 'latitude', 'nav_lat','lat', 'lat_rho' or 'NbLatitudes'}{type=scalar string}
248; same as inxaxisname but for the y axis in the input grid file gridin
249;
250; @keyword OUTXAXISNAME {default='x', 'longitude', 'nav_lon', 'lon', 'lon_rho' or 'NbLongitudes'}{type=scalar string}
251; same as inxaxisname but for output grid file gridout
252;
253; @keyword OUTYAXISNAME {default='y', 'latitude', 'nav_lat','lat', 'lat_rho' or 'NbLatitudes'}{type=scalar string}
254; same as inyaxisname but for output grid file gridout
255;
256; @keyword GETHAN
257; to force interpolated data to be always > value defined by gethan (for example gethan = 0.)
258;
259; @keyword LETHAN
260; to force interpolated data to be always < value defined by lethan (for example lethan = 0.)
261;
262; @keyword  _EXTRA
263; to use <pro>extrapolate</pro>, <pro>extrapsmooth</pro> and <pro>fromreg</pro> keywords
264;
265; @uses
266; <pro>extrapsmooth</pro>, <pro>extrapolate</pro>, <pro>fromreg</pro> and <pro>fromirr</pro>
267;
268; @restrictions
269;
270; - perform only horizontal interpolations on scalar fields
271; - all masked and missing values are filled before interpolation
272;   -> output data are not masked and have values everywhere.
273; - attributes (like valid_min...) are not updated
274; - see restrictions of <pro>fromreg</pro> and <pro>fromirr</pro>
275; - output mask is not used but, if the input file contains the mask
276;   in a variable (defined by inmaskname), this variable will contain
277;   the output mask in the ouput file
278;
279; @examples
280;
281;   IDL> file_interp, filein, fileout, gridout, inxaxisname = 'lo', inyaxisname = 'la', keep = ['lo', 'la', 'cond_sed']
282;   IDL> file_interp, in, out, gdout, inuseasmask = 'sst', inmissing_value = -1.00000e+30, missing_value = -1000.00
283;   IDL> file_interp,'sst_reg025.nc', 'sst_reg1.nc',[4,4], method = 'boxmean'
284;
285; @history
286;  September 2007: Sebastien Masson (smasson\@locean-ipsl.upmc.fr)
287;
288; @version
289; $Id$
290;
291;-
292PRO file_interp, filein, fileout, gridout, GRIDIN = gridin, MASKIN = maskin, MASKOUT = maskout $
293                 , KEEP = keep, REMOVE = remove, METHOD = method, SMOOTH = smooth $
294                 , SET_XDIMNAME = set_xdimname, SET_YDIMNAME = set_ydimname $
295                 , SET_XAXISNAME = set_xaxisname, SET_YAXISNAME = set_yaxisname $
296                 , INMASKNAME = inmaskname, ININVMASK = ininvmask $
297                 , INUSEASMASK = inuseasmask, INMISSING_VALUE = inmissing_value $
298                 , INADDSCL_BEFORE = inaddscl_before, INTESTOP = intestop $
299                 , OUTMASKNAME = outmaskname, OUTINVMASK = outinvmask $
300                 , OUTUSEASMASK = outuseasmask, OUTMISSING_VALUE = outmissing_value $
301                 , OUTADDSCL_BEFORE = outaddscl_before, OUTTESTOP = outtestop $
302                 , MISSING_VALUE = MISSING_VALUE, WEIG = weig, ADDR = addr $
303                 , INXAXISNAME = inxaxisname, INYAXISNAME = inyaxisname $
304                 , OUTXAXISNAME = outxaxisname, OUTYAXISNAME = outyaxisname $
305                 , GETHAN = gethan, LETHAN = lethan $
306                 , _EXTRA = ex
307;
308  compile_opt idl2, strictarrsubs
309  revision = '$Id$'
310;
311  IF NOT keyword_set(method) THEN method = 'bilinear'
312;
313; input filenames checks...
314;
315  inid = ncdf_open(filein)
316  ininq = ncdf_inquire(inid)
317
318  outid = ncdf_create(fileout, /clobber)
319  ncdf_control, outid, /nofill
320
321  IF NOT keyword_set(gridin) THEN gridin = filein
322
323  IF NOT keyword_set(maskin) THEN maskin = gridin
324  IF NOT keyword_set(maskout) THEN maskout = gridout
325;
326; Copy global attributes
327;
328  FOR i = 0, ininq.ngatts-1 DO BEGIN
329    name = ncdf_attname(inid, i, /global)
330    dummy = ncdf_attcopy(inid, name, outid, /in_global, /out_global)
331  ENDFOR
332  ncdf_attput, outid, 'Created_by', revision, /GLOBAL
333;
334; x/y dim and x/yaxis informations
335;
336  ncdf_getaxis, gridin, indimidx, indimidy, inlon, inlat, xdimname = inxdimname, ydimname = inydimname $
337                , xaxisname = inxaxisname, yaxisname = inyaxisname
338  get_gridparams, inlon, inlat, jpiin, jpjin, 2
339  IF keyword_set(set_xdimname) THEN inxdimname = set_xdimname
340  IF keyword_set(set_ydimname) THEN inydimname = set_ydimname
341  IF keyword_set(set_xaxisname) THEN inxaxisname = set_xaxisname
342  IF keyword_set(set_yaxisname) THEN inyaxisname = set_yaxisname
343;
344  IF method EQ 'boxmean' THEN BEGIN
345    IF n_elements(gridout) NE 2 THEN stop
346    divx = round(gridout[0])
347    divy = round(gridout[1])
348    IF jpiin MOD divx NE 0 THEN BEGIN
349      print, 'in boxmean method, the x size ('+strtrim(divx, 1)+') of the box used to average the data must devide the size of the x dimension ('+strtrim(jpiin, 1)+')'
350      return
351    ENDIF
352    IF jpjin MOD divy NE 0 THEN BEGIN
353      print, 'in boxmean method, the y size ('+strtrim(divy, 1)+') of the box used to average the data must devide the size of the y dimension ('+strtrim(jpjin, 1)+')'
354      return
355    ENDIF
356    jpiout = jpiin / divx
357    jpjout = jpjin / divy
358    outlon = inlon   &   outlon = boxmean_interp(outlon, divx, divy)
359    outlat = inlat   &   outlat = boxmean_interp(outlat, divx, divy)
360    IF jpiout EQ 1 OR jpjout EQ 1 THEN BEGIN   
361      outlon = reform(outlon, jpiout, jpjout, /overwrite)
362      outlat = reform(outlat, jpiout, jpjout, /overwrite)
363    ENDIF
364  ENDIF ELSE BEGIN
365    ncdf_getaxis, gridout, outdimidx, outdimidy, outlon, outlat, xaxisname = outxaxisname, yaxisname = outyaxisname
366    get_gridparams, outlon, outlat, jpiout, jpjout, 2
367  ENDELSE
368;
369; masks
370;
371  inmask = ncdf_getmask(maskin, MASKNAME = inmaskname, INVMASK = ininvmask, USEASMASK = inuseasmask $
372                       , MISSING_VALUE = inmissing_value, ADDSCL_BEFORE = inaddscl_before, TESTOP = intestop)
373  inmasksz = size(inmask, /dimensions)
374  IF size(inmask, /n_dimensions) EQ 2 THEN inmasksz = [inmasksz, 0]
375  IF n_elements(inmaskname) EQ 0 THEN inmaskname = 'not defined' ; default definition
376  IF method EQ 'boxmean' THEN BEGIN
377    outmask = inmask
378    IF inmask[0] NE -1 THEN outmask = boxmean_interp(outmask, divx, divy)
379  ENDIF ELSE BEGIN
380    outmask = ncdf_getmask(maskout, MASKNAME = outmaskname, INVMASK = outinvmask, USEASMASK = outuseasmask $
381                           , MISSING_VALUE = outmissing_value, ADDSCL_BEFORE = outaddscl_before, TESTOP = outtestop)
382  ENDELSE
383;
384; irregular grids?
385;
386  CASE 0 OF
387    array_equal(inlon[*, 0], inlon[*, jpjin-1]):inirr = 1b
388    array_equal(inlat[0, *], inlat[jpiin-1, *]):inirr = 1b
389    array_equal(inlon, inlon[*, 0]#replicate(1, jpjin)):inirr = 1b
390    array_equal(inlat, replicate(1, jpiin)#(inlat[0, *])[*]):inirr = 1b
391    ELSE:inirr = 0b
392  ENDCASE
393  CASE 0 OF
394    array_equal(outlon[*, 0], outlon[*, jpjout-1]):outirr = 1b
395    array_equal(outlat[0, *], outlat[jpiout-1, *]):outirr = 1b
396    array_equal(outlon, outlon[*, 0]#replicate(1, jpjout)):outirr = 1b
397    array_equal(outlat, replicate(1, jpiout)#(outlat[0, *])[*]):outirr = 1b
398    ELSE:outirr = 0b
399  ENDCASE
400
401  IF inirr AND method EQ 'imoms3' THEN stop
402;
403; Dimensions
404;
405  indimsz = lonarr(ininq.ndims)
406  outdimsz = lonarr(ininq.ndims)
407  outdimid = lonarr(ininq.ndims)
408  FOR i = 0, ininq.ndims-1 DO BEGIN
409    ncdf_diminq, inid, i, name, size
410    indimsz[i] = size
411    outdimsz[i] = size
412    CASE 1 OF
413      strlowcase(name) EQ strlowcase(inxdimname): BEGIN
414        outdimid[i] = ncdf_dimdef(outid, name, jpiout)
415        outdimsz[i] = jpiout
416        indimx = i
417        outdimx = outdimid[i]
418      END
419      strlowcase(name) EQ strlowcase(inydimname): BEGIN
420        outdimid[i] = ncdf_dimdef(outid, name, jpjout)
421        outdimsz[i] = jpjout
422        indimy = i
423        outdimy = outdimid[i]
424      END
425      i EQ ininq.recdim: outdimid[i] = ncdf_dimdef(outid, name, /UNLIMITED)
426      ELSE: outdimid[i] = ncdf_dimdef(outid, name, size)
427    ENDCASE
428  ENDFOR
429;
430; Variables
431;
432  outvarid = lonarr(ininq.nvars)
433  FOR i = 0, ininq.nvars-1 DO BEGIN
434    varinq = ncdf_varinq(inid, i)
435    okvar = 1
436    IF keyword_set(keep) THEN okvar = total(strlowcase(keep) EQ strlowcase(varinq.name)) EQ 1
437    IF keyword_set(remove) THEN okvar = total(strlowcase(remove) EQ strlowcase(varinq.name)) EQ 0
438    IF okvar THEN BEGIN
439      IF varinq.ndims EQ 0 THEN BEGIN ; scalar variable
440        outvarid[i] = ncdf_vardef(outid, varinq.name $
441;                                  , BYTE = varinq.datatype EQ 'BYTE', CHAR = varinq.datatype EQ 'CHAR' $
442                                  , CHAR = varinq.datatype EQ 'CHAR' $
443                                  , SHORT = varinq.datatype EQ 'INT' OR varinq.datatype EQ 'SHORT' OR varinq.datatype EQ 'BYTE' $
444                                  , LONG = varinq.datatype EQ 'LONG' $
445                                  , FLOAT = varinq.datatype EQ 'FLOAT', DOUBLE = varinq.datatype EQ 'DOUBLE')
446      ENDIF ELSE BEGIN          ; array
447        CASE 1 OF
448          strlowcase(varinq.name) EQ strlowcase(inxaxisname):BEGIN ; xaxis
449            IF outirr THEN dimvar = [outdimx, outdimy] ELSE dimvar = [outdimx]
450          END
451          strlowcase(varinq.name) EQ strlowcase(inyaxisname):BEGIN ; yaxis
452            IF outirr THEN dimvar = [outdimx, outdimy] ELSE dimvar = [outdimy]
453          END
454          strlowcase(varinq.name) EQ strlowcase(inmaskname):BEGIN ; mask
455            IF outmask[0] NE -1 THEN dimvar = outdimid[varinq.dim] ELSE dimvar = -1
456          END
457          (total(varinq.dim EQ indimx) + total(varinq.dim EQ indimx)) EQ 1: dimvar = -1 ; strange variable...
458          ELSE: dimvar = outdimid[varinq.dim]
459        ENDCASE
460        IF dimvar[0] NE -1 THEN BEGIN
461          outvarid[i] = ncdf_vardef(outid, varinq.name, dimvar $
462;                                    , BYTE = varinq.datatype EQ 'BYTE', CHAR = varinq.datatype EQ 'CHAR' $
463                                    , CHAR = varinq.datatype EQ 'CHAR' $
464                                    , SHORT = varinq.datatype EQ 'INT' OR varinq.datatype EQ 'SHORT' OR varinq.datatype EQ 'BYTE' $
465                                    , LONG = varinq.datatype EQ 'LONG' $
466                                    , FLOAT = varinq.datatype EQ 'FLOAT', DOUBLE = varinq.datatype EQ 'DOUBLE')
467        ENDIF ELSE outvarid[i] = - 1
468      ENDELSE
469; Variables attributes
470      IF outvarid[i] NE - 1 THEN BEGIN
471        FOR j = 0, varinq.natts-1 DO BEGIN
472          name = ncdf_attname(inid, i, j)
473          dummy = ncdf_attcopy(inid, i, name, outid, outvarid[i])
474        ENDFOR
475      ENDIF
476    ENDIF ELSE outvarid[i] = -1
477  ENDFOR
478;
479  ncdf_control, outid, /endef
480;
481  FOR i = 0, ininq.nvars-1 DO BEGIN
482    IF outvarid[i] NE -1 THEN BEGIN
483      varinq = ncdf_varinq(inid, i)
484      IF varinq.ndims GE 2 THEN BEGIN
485        interp = varinq.dim[0] EQ indimx AND varinq.dim[1] EQ indimy
486      ENDIF ELSE interp = 0b
487      CASE 1 OF
488        strlowcase(varinq.name) EQ strlowcase(inxaxisname):BEGIN ; x axis
489          IF outirr THEN ncdf_varput, outid, outvarid[i], outlon $
490          ELSE ncdf_varput, outid, outvarid[i], outlon[*, 0]
491        END
492        strlowcase(varinq.name) EQ strlowcase(inyaxisname):BEGIN ; y axis
493          IF outirr THEN ncdf_varput, outid, outvarid[i], outlat $
494          ELSE ncdf_varput, outid, outvarid[i], reform(outlat[0, *])
495        END
496        strlowcase(varinq.name) EQ strlowcase(inmaskname):BEGIN ; mask
497          ncdf_varput, outid, outvarid[i], outmask
498        END
499        ELSE:BEGIN
500          IF n_elements(missing_value) NE 0 THEN var_missing_value = MISSING_VALUE $
501          ELSE ncdf_getatt, inid, i, MISSING_VALUE = var_missing_value
502          CASE varinq.ndims OF
503            0:BEGIN             ; salar
504              ncdf_varget, inid, i, data
505              ncdf_varput, outid, outvarid[i], temporary(data)
506            END
507            1:BEGIN             ; 1D
508              ncdf_varget, inid, i, data
509              ncdf_varput, outid, outvarid[i], temporary(data)
510            END
511            2:BEGIN             ; 2D
512              ncdf_varget, inid, i, data
513              IF interp THEN data = call_interp2d(temporary(data), inlon, inlat, inmask[*, *, 0], outlon, outlat $
514                                                  , INIRR = inirr, METHOD = method, SMOOTH = smooth $
515                                                  , WEIG = weig, ADDR = addr, MISSING_VALUE = var_missing_value $
516                                                  , DIVX = divx, DIVY = divy, OUTMASK = outmask $
517                                                  , GETHAN = gethan, LETHAN = lethan, _extra = ex)
518              IF interp AND n_elements(data) EQ 1 THEN data = replicate(data, jpiout, jpjout)
519              ncdf_varput, outid, outvarid[i], temporary(data)
520            END
521            3:BEGIN             ; 3D
522              FOR k = 0, indimsz[varinq.dim[2]]-1 DO BEGIN
523                IF k MOD 100 EQ 0 THEN print, k
524                incnt = [indimsz[varinq.dim[0: 1]], 1]
525                outcnt = [outdimsz[varinq.dim[0: 1]], 1]
526                off = [0, 0, k]
527                ncdf_varget, inid, i, data, offset = off, count = incnt
528                IF n_elements(inmasksz) GE 3 THEN BEGIN
529                  IF inmasksz[2] EQ indimsz[varinq.dim[2]] AND varinq.dim[2] NE ininq.recdim THEN tmpmsk = inmask[*, *, k] $
530                  ELSE tmpmsk = inmask[*, *, 0]
531                ENDIF ELSE tmpmsk = inmask[*, *, 0]
532                IF interp THEN data = call_interp2d(temporary(data), inlon, inlat, temporary(tmpmsk), outlon, outlat $
533                                                    , INIRR = inirr, METHOD = method, SMOOTH = smooth $
534                                                    , WEIG = weig, ADDR = addr, MISSING_VALUE = var_missing_value $
535                                                    , DIVX = divx, DIVY = divy, OUTMASK = outmask $
536                                                    , GETHAN = gethan, LETHAN = lethan, _extra = ex)
537                IF interp AND n_elements(data) EQ 1 THEN data = replicate(data, jpiout, jpjout)
538                ncdf_varput, outid, outvarid[i], temporary(data), offset = off, count = outcnt
539              ENDFOR
540            END
541            4:BEGIN             ; 4D
542              FOR t = 0, indimsz[varinq.dim[3]]-1 DO BEGIN
543                IF t MOD 100 EQ 0 THEN print, t
544                FOR k = 0, indimsz[varinq.dim[2]]-1 DO BEGIN
545                  incnt = [indimsz[varinq.dim[0: 1]], 1, 1]
546                  outcnt = [outdimsz[varinq.dim[0: 1]], 1, 1]
547                  off = [0, 0, k, t]
548                  ncdf_varget, inid, i, data, offset = off, count = incnt
549                  IF n_elements(inmasksz) GE 3 THEN BEGIN
550                    IF inmasksz[2] EQ indimsz[varinq.dim[2]] THEN tmpmsk = inmask[*, *, k] ELSE tmpmsk = inmask
551                  ENDIF ELSE tmpmsk = inmask[*, *, 0]
552                  IF interp THEN data = call_interp2d(temporary(data), inlon, inlat, temporary(tmpmsk), outlon, outlat $
553                                                      , INIRR = inirr, METHOD = method, SMOOTH = smooth $
554                                                      , WEIG = weig, ADDR = addr, MISSING_VALUE = var_missing_value $
555                                                      , DIVX = divx, DIVY = divy, OUTMASK = outmask $
556                                                      , GETHAN = gethan, LETHAN = lethan, _extra = ex)
557                  IF interp AND n_elements(data) EQ 1 THEN data = replicate(data, jpiout, jpjout)
558                  ncdf_varput, outid, outvarid[i], temporary(data), offset = off, count = outcnt
559                ENDFOR
560              ENDFOR
561            END
562          ENDCASE
563        END
564      ENDCASE
565    ENDIF
566  ENDFOR
567
568  ncdf_close, inid
569  ncdf_close, outid
570
571  return
572END
Note: See TracBrowser for help on using the repository browser.