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

Last change on this file since 303 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
Line 
1;+
2;
3; @file_comments
4; Case 1: extract from a NetCDF file longitude and latitude arrays, their dimensions
5; and make sure it is 1D or 2D arrays
6;
7; Case 2: given longitude and latitude arrays, get their dimensions and make
8; sure they are 1D or 2D arrays
9;
10; @categories
11; Interpolation
12;
13; @examples
14;
15; Case 1:
16; IDL> get_gridparams, file name/id, lonname, latname, lon, lat, jpi, jpj, n_dimensions
17;
18; Case 2:
19; IDL> get_gridparams, lon, lat, jpi, jpj, n_dimensions
20;
21; @param in1 {in}{required}
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
25;
26; @param in2 {in}{required}
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
30;
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.
34;
35; @param in4 {out}
36; Case 1: returned longitudes array, with n_dimensions dimensions
37; Case 2: returned number of points in latitudinal direction
38;
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).
44;
45; @param in6 {out}
46; Case 1: returned number of points in longitudinal direction.
47;
48; @param in7 {out}
49; Case 1: returned number of points in latitudinal direction
50;
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).
55;
56; @keyword DOUBLE {default=0}{type=scalar: 0 or 1}
57; activate to force double precision for lon/lat arrays
58;
59; @examples
60;
61; 1) IDL> ncdf_get_gridparams, 'coordinates_ORCA_R05.nc', 'glamt', 'gphit' $
62;            , olon, olat, jpio, jpjo, 2
63;
64; 2) IDL> ncdf_get_gridparams, olon, olat, jpio, jpjo, 2
65;
66; @history
67;  November 2005: Sebastien Masson (smasson\@lodyc.jussieu.fr)
68;
69; @version
70; $Id$
71;
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;
78  compile_opt idl2, strictarrsubs
79;
80  CASE n_params() OF
81    8:BEGIN
82; get longitude and latitude
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
90      ncdf_varget, cdfido, in2, lon
91      ncdf_varget, cdfido, in3, lat
92      IF size(in1, /type) EQ 7 THEN ncdf_close, cdfido
93      n_dimensions = in8
94    END
95    5:BEGIN
96      lon = temporary(in1)
97      lat = temporary(in2)
98      n_dimensions = in5
99    END
100    ELSE:BEGIN
101      ras = report('Bad number of input parameters')
102      stop
103    end
104  ENDCASE
105;
106  sizelon = size(lon)
107  sizelat = size(lat)
108  CASE 1 OF
109;-------
110; lon and lat are 1D arrays
111;-------
112    sizelon[0] EQ 1 AND sizelat[0] EQ 1:BEGIN
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
117      CASE n_dimensions OF
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;-------
130    sizelon[0] EQ 2 AND sizelat[0] EQ 1:BEGIN
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
136      CASE n_dimensions OF
137        1:BEGIN
138          IF array_equal(lon, lon[*, 0] # replicate(1, jpj)) NE 1 THEN BEGIN
139            ras = report('Longitudes are not the same for all latitudes, impossible to extract a 1D array of the longitudes')
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;-------
151    sizelon[0] EQ 1 AND sizelat[0] EQ 2:BEGIN
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
157      CASE n_dimensions OF
158        1:BEGIN
159          IF array_equal(lat, replicate(1, jpi) # lat[0, *]) NE 1 THEN BEGIN
160            ras = report('Latitudes are not the same for all longitudes, impossible to extract a 1D array of the latitudes')
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;-------
172    sizelon[0] EQ 2 AND sizelat[0] EQ 2:BEGIN
173; get jpi and jpj
174      IF array_equal(sizelon[1:2], sizelat[1:2]) NE 1 THEN stop
175      jpi = sizelon[1]
176      jpj = sizelon[2]
177; make sure that lon and lat have the good number of dimensions
178      CASE n_dimensions OF
179        1:BEGIN
180          IF array_equal(lon, lon[*, 0] # replicate(1, jpj)) NE 1 THEN BEGIN
181            ras = report('Longitudes are not the same for all latitudes, impossible to extract a 1D array of the longitudes')
182            stop
183          ENDIF
184          lon = lon[*, 0]
185          IF array_equal(lat, replicate(1, jpi) # reform(lat[0, *])) NE 1 THEN BEGIN
186            ras = report('Latitudes are not the same for all longitudes, impossible to extract a 1D array of the latitudes')
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;-------
198    ELSE: BEGIN
199             ras = report('Longitudes and latitudes are not 1D and/or 2D arrays')
200             stop
201          END
202  ENDCASE
203;
204;-------
205; double keyword
206;-------
207  if keyword_set(double) then BEGIN
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.