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

Last change on this file since 378 was 372, checked in by pinsard, 16 years ago

improvements of headers (alignments)

  • 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)
62;
63;   IDL> ncdf_get_gridparams, 'coordinates_ORCA_R05.nc', 'glamt', 'gphit' $
64;            , olon, olat, jpio, jpjo, 2
65;
66; 2)
67;
68:   IDL> ncdf_get_gridparams, olon, olat, jpio, jpjo, 2
69;
70; @history
71; November 2005: Sebastien Masson (smasson\@lodyc.jussieu.fr)
72;
73; @version
74; $Id$
75;
76;-
77PRO get_gridparams, in1, in2, in3, in4, in5, in6, in7, in8 $
78                  , DOUBLE=double
79;
80;                  file, lonname, latname, lon, lat, jpi, jpj, n_dimensions
81;                  lon,      lat,     jpi, jpj, n_dimensions
82;
83  compile_opt idl2, strictarrsubs
84;
85  CASE n_params() OF
86    8:BEGIN
87; get longitude and latitude
88      IF size(in1, /type) EQ 7 THEN BEGIN
89        IF file_test(in1) EQ 0 THEN BEGIN
90          ras = report('file ' + in1 + ' does not exist')
91          stop
92        ENDIF
93        cdfido = ncdf_open(in1)
94      ENDIF ELSE cdfido = in1
95      ncdf_varget, cdfido, in2, lon
96      ncdf_varget, cdfido, in3, lat
97      IF size(in1, /type) EQ 7 THEN ncdf_close, cdfido
98      n_dimensions = in8
99    END
100    5:BEGIN
101      lon = temporary(in1)
102      lat = temporary(in2)
103      n_dimensions = in5
104    END
105    ELSE:BEGIN
106      ras = report('Bad number of input parameters')
107      stop
108    end
109  ENDCASE
110;
111  sizelon = size(lon)
112  sizelat = size(lat)
113  CASE 1 OF
114;-------
115; lon and lat are 1D arrays
116;-------
117    sizelon[0] EQ 1 AND sizelat[0] EQ 1:BEGIN
118; get jpi and jpj
119      jpi = sizelon[1]
120      jpj = sizelat[1]
121; make sure that lon and lat have the good number of dimensions
122      CASE n_dimensions OF
123        1:
124        2:BEGIN
125; make lon and lat 2D arrays
126          lon = temporary(lon) # replicate(1, jpj)
127          lat = replicate(1, jpi) # temporary(lat)
128        END
129        ELSE:stop
130      ENDCASE
131    END
132;-------
133; lon is 2D array and lat is 1D array
134;-------
135    sizelon[0] EQ 2 AND sizelat[0] EQ 1:BEGIN
136; get jpi and jpj
137      jpi = sizelon[1]
138      jpj = sizelon[2]
139      IF jpj NE n_elements(lat) THEN stop
140; make sure that lon and lat have the good number of dimensions
141      CASE n_dimensions OF
142        1:BEGIN
143          IF array_equal(lon, lon[*, 0] # replicate(1, jpj)) NE 1 THEN BEGIN
144            ras = report('Longitudes are not the same for all latitudes, impossible to extract a 1D array of the longitudes')
145            stop
146          ENDIF
147          lon = lon[*, 0]
148        END
149        2:lat = replicate(1, jpi) # temporary(lat)
150        ELSE:stop
151      ENDCASE
152    END
153;-------
154; lon is 1D array and lat is 2D array
155;-------
156    sizelon[0] EQ 1 AND sizelat[0] EQ 2:BEGIN
157; get jpi and jpj
158      jpi = sizelat[1]
159      jpj = sizelat[2]
160      IF jpi NE n_elements(lon) THEN stop
161; make sure that lon and lat have the good number of dimensions
162      CASE n_dimensions OF
163        1:BEGIN
164          IF array_equal(lat, replicate(1, jpi) # lat[0, *]) NE 1 THEN BEGIN
165            ras = report('Latitudes are not the same for all longitudes, impossible to extract a 1D array of the latitudes')
166            stop
167          ENDIF
168          lat = reform(lat[0, *])
169        END
170        2:lon = temporary(lon) # replicate(1, jpj)
171        ELSE:stop
172      ENDCASE
173    END
174;-------
175; lon and lat are 2D arrays
176;-------
177    sizelon[0] EQ 2 AND sizelat[0] EQ 2:BEGIN
178; get jpi and jpj
179      IF array_equal(sizelon[1:2], sizelat[1:2]) NE 1 THEN stop
180      jpi = sizelon[1]
181      jpj = sizelon[2]
182; make sure that lon and lat have the good number of dimensions
183      CASE n_dimensions OF
184        1:BEGIN
185          IF array_equal(lon, lon[*, 0] # replicate(1, jpj)) NE 1 THEN BEGIN
186            ras = report('Longitudes are not the same for all latitudes, impossible to extract a 1D array of the longitudes')
187            stop
188          ENDIF
189          lon = lon[*, 0]
190          IF array_equal(lat, replicate(1, jpi) # reform(lat[0, *])) NE 1 THEN BEGIN
191            ras = report('Latitudes are not the same for all longitudes, impossible to extract a 1D array of the latitudes')
192            stop
193          ENDIF
194        lat = reform(lat[0, *])
195        END
196        2:
197        ELSE:stop
198      ENDCASE
199    END
200;-------
201; lon and lat are not 1D and/or 2D arrays
202;-------
203    ELSE: BEGIN
204             ras = report('Longitudes and latitudes are not 1D and/or 2D arrays')
205             stop
206          END
207  ENDCASE
208;
209;-------
210; double keyword
211;-------
212  if keyword_set(double) then BEGIN
213    lon = double(temporary(lon))
214    lat = double(temporary(lat))
215  ENDIF
216;
217; give back the right outparameters.
218;
219
220  CASE n_params() OF
221    8:BEGIN
222      in4 = temporary(lon)
223      in5 = temporary(lat)
224      in6 = temporary(jpi)
225      in7 = temporary(jpj)
226    END
227    5:BEGIN
228      in1 = temporary(lon)
229      in2 = temporary(lat)
230      in3 = temporary(jpi)
231      in4 = temporary(jpj)
232    END
233  ENDCASE
234;
235
236return
237END
Note: See TracBrowser for help on using the repository browser.