source: trunk/SRC/Interpolation/get_gridparams.pro @ 283

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

bugfix for interpolation from ORCA2 without mask

  • Property svn:keywords set to Id
File size: 6.6 KB
RevLine 
[59]1;+
2;
[118]3; @file_comments
[271]4; Case 1: extract from a NetCDF file longitude and latitude arrays, their dimensions
[136]5; and make sure it is 1D or 2D arrays
[59]6;
[271]7; Case 2: given longitude and latitude arrays, get their dimensions and make
[136]8; sure they are 1D or 2D arrays
[118]9;
[157]10; @categories
11; Interpolation
[59]12;
[118]13; @examples
14;
[271]15; Case 1:
16; IDL> get_gridparams, file name/id, lonname, latname, lon, lat, jpi, jpj, n_dimensions
[59]17;
[271]18; Case 2:
[121]19; IDL> get_gridparams, lon, lat, jpi, jpj, n_dimensions
[59]20;
[136]21; @param in1 {in}{required}
[271]22; Case 1: name or the id (returned by ncdf_open) of the netcdf file
23; Case 2: 1D or 2D arrays defining longitudes, will be forced to have
24;         n_dimensions dimensions when returned
[59]25;
[136]26; @param in2 {in}{required}
[271]27; Case 1: name of the variable containing the longitude in the NetCDF file
28; Case 2: 1D or 2D arrays defining latitudes, will be forced to have
29;         n_dimensions dimensions when returned
[136]30;
[271]31; @param in3
32; Case 1: name of the variable containing the latitude in the NetCDF file
33; Case 2: returned number of points in longitudinal direction.
[136]34;
35; @param in4 {out}
[271]36; Case 1: returned longitudes array, with n_dimensions dimensions
37; Case 2: returned number of points in latitudinal direction
[136]38;
[271]39; @param in5
40; Case 1: returned latitudes array, with n_dimensions dimensions
41; Case 2: input scalar (1 or 2) to specify if lon and lat should be returned
42;         as 1D or 2D arrays. Note that if n_dimensions = 1, the grid must be
43;         regular (longitude and latitudes can be described as 1D arrays).
[136]44;
45; @param in6 {out}
[271]46; Case 1: returned number of points in longitudinal direction.
[136]47;
48; @param in7 {out}
[271]49; Case 1: returned number of points in latitudinal direction
[136]50;
[271]51; @param in8 {in}
52; Case 1: input scalar (1 or 2) to specify if lon and lat should be returned
53;         as 1D or 2D arrays. Note that if n_dimensions = 1, the grid must be
54;         regular (longitude and latitudes can be described as 1D arrays).
[136]55;
[271]56; @keyword DOUBLE {default=0}{type=scalar: 0 or 1}
57; activate to force double precision for lon/lat arrays
[136]58;
[118]59; @examples
60;
61; 1) IDL> ncdf_get_gridparams, 'coordinates_ORCA_R05.nc', 'glamt', 'gphit' $
[59]62;            , olon, olat, jpio, jpjo, 2
63;
[118]64; 2) IDL> ncdf_get_gridparams, olon, olat, jpio, jpjo, 2
[59]65;
[101]66; @history
[118]67;  November 2005: Sebastien Masson (smasson\@lodyc.jussieu.fr)
68;
[231]69; @version
70; $Id$
[118]71;
[59]72;-
73;
74PRO get_gridparams, in1,   in2,   in3,     in4, in5, in6, in7, in8, DOUBLE = double
75;                  file, lonname, latname, lon, lat, jpi, jpj, n_dimensions
76;                   lon,   lat,   jpi,     jpj, n_dimensions
77;
[114]78  compile_opt idl2, strictarrsubs
79;
[59]80  CASE n_params() OF
81    8:BEGIN
82; get longitude and latitude
[271]83      IF size(in1, /type) EQ 7 THEN BEGIN
84        IF file_test(in1) EQ 0 THEN BEGIN
85          ras = report('file ' + in1 + ' does not exist')
86          stop
87        ENDIF
88        cdfido = ncdf_open(in1)
89      ENDIF ELSE cdfido = in1
[59]90      ncdf_varget, cdfido, in2, lon
91      ncdf_varget, cdfido, in3, lat
[271]92      IF size(in1, /type) EQ 7 THEN ncdf_close, cdfido
[59]93      n_dimensions = in8
94    END
[118]95    5:BEGIN
[59]96      lon = temporary(in1)
97      lat = temporary(in2)
98      n_dimensions = in5
99    END
[118]100    ELSE:BEGIN
[236]101      ras = report('Bad number of input parameters')
[59]102      stop
103    end
104  ENDCASE
105;
106  sizelon = size(lon)
107  sizelat = size(lat)
[118]108  CASE 1 OF
[59]109;-------
110; lon and lat are 1D arrays
111;-------
[118]112    sizelon[0] EQ 1 AND sizelat[0] EQ 1:BEGIN
[59]113; get jpi and jpj
114      jpi = sizelon[1]
115      jpj = sizelat[1]
116; make sure that lon and lat have the good number of dimensions
[118]117      CASE n_dimensions OF
[59]118        1:
119        2:BEGIN
120; make lon and lat 2D arrays
121          lon = temporary(lon) # replicate(1, jpj)
122          lat = replicate(1, jpi) # temporary(lat)
123        END
124        ELSE:stop
125      ENDCASE
126    END
127;-------
128; lon is 2D array and lat is 1D array
129;-------
[118]130    sizelon[0] EQ 2 AND sizelat[0] EQ 1:BEGIN
[59]131; get jpi and jpj
132      jpi = sizelon[1]
133      jpj = sizelon[2]
134      IF jpj NE n_elements(lat) THEN stop
135; make sure that lon and lat have the good number of dimensions
[118]136      CASE n_dimensions OF
137        1:BEGIN
138          IF array_equal(lon, lon[*, 0] # replicate(1, jpj)) NE 1 THEN BEGIN
[236]139            ras = report('Longitudes are not the same for all latitudes, impossible to extract a 1D array of the longitudes')
[59]140            stop
141          ENDIF
142          lon = lon[*, 0]
143        END
144        2:lat = replicate(1, jpi) # temporary(lat)
145        ELSE:stop
146      ENDCASE
147    END
148;-------
149; lon is 1D array and lat is 2D array
150;-------
[118]151    sizelon[0] EQ 1 AND sizelat[0] EQ 2:BEGIN
[59]152; get jpi and jpj
153      jpi = sizelat[1]
154      jpj = sizelat[2]
155      IF jpi NE n_elements(lon) THEN stop
156; make sure that lon and lat have the good number of dimensions
[118]157      CASE n_dimensions OF
158        1:BEGIN
159          IF array_equal(lat, replicate(1, jpi) # lat[0, *]) NE 1 THEN BEGIN
[236]160            ras = report('Latitudes are not the same for all longitudes, impossible to extract a 1D array of the latitudes')
[59]161            stop
162          ENDIF
163          lat = reform(lat[0, *])
164        END
165        2:lon = temporary(lon) # replicate(1, jpj)
166        ELSE:stop
167      ENDCASE
168    END
169;-------
170; lon and lat are 2D arrays
171;-------
[118]172    sizelon[0] EQ 2 AND sizelat[0] EQ 2:BEGIN
[59]173; get jpi and jpj
174      IF array_equal(sizelon[1:2], sizelat[1:2]) NE 1 THEN stop
175      jpi = sizelon[1]
[118]176      jpj = sizelon[2]
[59]177; make sure that lon and lat have the good number of dimensions
[118]178      CASE n_dimensions OF
179        1:BEGIN
180          IF array_equal(lon, lon[*, 0] # replicate(1, jpj)) NE 1 THEN BEGIN
[236]181            ras = report('Longitudes are not the same for all latitudes, impossible to extract a 1D array of the longitudes')
[59]182            stop
183          ENDIF
184          lon = lon[*, 0]
[118]185          IF array_equal(lat, replicate(1, jpi) # reform(lat[0, *])) NE 1 THEN BEGIN
[236]186            ras = report('Latitudes are not the same for all longitudes, impossible to extract a 1D array of the latitudes')
[59]187            stop
188          ENDIF
189        lat = reform(lat[0, *])
190        END
191        2:
192        ELSE:stop
193      ENDCASE
194    END
195;-------
196; lon and lat are not 1D and/or 2D arrays
197;-------
[236]198    ELSE: BEGIN
199             ras = report('Longitudes and latitudes are not 1D and/or 2D arrays')
200             stop
201          END
[59]202  ENDCASE
203;
204;-------
205; double keyword
206;-------
[118]207  if keyword_set(double) then BEGIN
[59]208    lon = double(temporary(lon))
209    lat = double(temporary(lat))
210  ENDIF
211;
212; give back the right outparameters.
213;
214
215  CASE n_params() OF
216    8:BEGIN
217      in4 = temporary(lon)
218      in5 = temporary(lat)
219      in6 = temporary(jpi)
220      in7 = temporary(jpj)
221    END
222    5:BEGIN
223      in1 = temporary(lon)
224      in2 = temporary(lat)
225      in3 = temporary(jpi)
226      in4 = temporary(jpj)
227    END
228  ENDCASE
229;
230
231return
232END
Note: See TracBrowser for help on using the repository browser.