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

Last change on this file since 231 was 231, checked in by pinsard, 17 years ago

improvements/corrections of some *.pro headers

  • Property svn:keywords set to Id
File size: 6.2 KB
Line 
1;+
2;
3; @file_comments
4; 1) extract from a NetCDF file the longitude, latitude, and their dimensions
5; and make sure it is 1D or 2D arrays
6;
7; or
8; 2) given longitude and latitude arrays get their dimensions and make
9; sure they are 1D or 2D arrays
10;
11; @categories
12; Interpolation
13;
14; @examples
15;
16; 1)
17; IDL> get_gridparams, file, lonname, latname, lon, lat, jpi, jpj, n_dimensions
18;
19; or
20;
21; 2)
22; IDL> get_gridparams, lon, lat, jpi, jpj, n_dimensions
23;
24; 1)
25; @param in1 {in}{required}
26; Case 1: the name of the netcdf file
27; Case 2: 1d or 2D arrays defining longitudes and latitudes.
28; Out: the variable that will contain the longitudes
29;
30; @param in2 {in}{required}
31; Case 1: the name of the variable that contains the longitude in the NetCDF file
32; Case 2: 1d or 2D arrays defining longitudes and latitudes.
33;         Note that these arrays are also outputs and can therefore be modified.
34; Out: the variable that will contain the latitudes
35;
36; @param in3 {in}{required}
37; Case 1: the name of the variable that contains the latitude in the NetCDF file
38; Case 2: the number of points in the longitudinal direction.
39;
40; @param in4 {out}
41; Case 1: the number of points in the longitudinal direction
42; Case 2: the number of points in the latitudinal direction
43;
44; @param in5 {out}
45; Case 1: the number of points in the latitudinal direction
46; Case 2: 1 or 2 to specify if lon and lat should be 1D (jpi or jpj)
47; arrays or 2D arrays (jpi,jpj). Note that of  n_dimensions = 1, then the
48; grid must be regular (each longitudes must be the same for all latitudes
49; and each latitudes should be the same for all longitudes).
50;
51; @param in6 {out}
52; the variable that will contain the longitudes
53;
54; @param in7 {out}
55; the variable that will contain the latitudes
56;
57; @param in8 {out}
58; 1 or 2 to specify if lon and lat should be 1D (jpi or jpj)
59;
60; @keyword DOUBLE
61; use double precision to perform the computation
62;
63; @examples
64;
65; 1) IDL> ncdf_get_gridparams, 'coordinates_ORCA_R05.nc', 'glamt', 'gphit' $
66;            , olon, olat, jpio, jpjo, 2
67;
68; 2) 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;-
77;
78PRO get_gridparams, in1,   in2,   in3,     in4, in5, in6, in7, in8, DOUBLE = double
79;                  file, lonname, latname, lon, lat, jpi, jpj, n_dimensions
80;                   lon,   lat,   jpi,     jpj, n_dimensions
81;
82;
83  compile_opt idl2, strictarrsubs
84;
85  CASE n_params() OF
86    8:BEGIN
87; get longitude and latitude
88      IF file_test(in1) EQ 0 THEN BEGIN
89        print, 'file ' + in1 + ' does not exist'
90        stop
91      ENDIF
92      cdfido = ncdf_open(in1)
93      ncdf_varget, cdfido, in2, lon
94      ncdf_varget, cdfido, in3, lat
95      ncdf_close, cdfido
96      n_dimensions = in8
97    END
98    5:BEGIN
99      lon = temporary(in1)
100      lat = temporary(in2)
101      n_dimensions = in5
102    END
103    ELSE:BEGIN
104      print, 'Bad nimber of input parameters'
105      stop
106    end
107  ENDCASE
108;
109  sizelon = size(lon)
110  sizelat = size(lat)
111  CASE 1 OF
112;-------
113; lon and lat are 1D arrays
114;-------
115    sizelon[0] EQ 1 AND sizelat[0] EQ 1:BEGIN
116; get jpi and jpj
117      jpi = sizelon[1]
118      jpj = sizelat[1]
119; make sure that lon and lat have the good number of dimensions
120      CASE n_dimensions OF
121        1:
122        2:BEGIN
123; make lon and lat 2D arrays
124          lon = temporary(lon) # replicate(1, jpj)
125          lat = replicate(1, jpi) # temporary(lat)
126        END
127        ELSE:stop
128      ENDCASE
129    END
130;-------
131; lon is 2D array and lat is 1D array
132;-------
133    sizelon[0] EQ 2 AND sizelat[0] EQ 1:BEGIN
134; get jpi and jpj
135      jpi = sizelon[1]
136      jpj = sizelon[2]
137      IF jpj NE n_elements(lat) THEN stop
138; make sure that lon and lat have the good number of dimensions
139      CASE n_dimensions OF
140        1:BEGIN
141          IF array_equal(lon, lon[*, 0] # replicate(1, jpj)) NE 1 THEN BEGIN
142            print, 'Longitudes are not the same for all latitudes, impossible to extract a 1D array of the longitudes'
143            stop
144          ENDIF
145          lon = lon[*, 0]
146        END
147        2:lat = replicate(1, jpi) # temporary(lat)
148        ELSE:stop
149      ENDCASE
150    END
151;-------
152; lon is 1D array and lat is 2D array
153;-------
154    sizelon[0] EQ 1 AND sizelat[0] EQ 2:BEGIN
155; get jpi and jpj
156      jpi = sizelat[1]
157      jpj = sizelat[2]
158      IF jpi NE n_elements(lon) THEN stop
159; make sure that lon and lat have the good number of dimensions
160      CASE n_dimensions OF
161        1:BEGIN
162          IF array_equal(lat, replicate(1, jpi) # lat[0, *]) NE 1 THEN BEGIN
163            print, 'Latitudes are not the same for all longitudes, impossible to extract a 1D array of the latitudes'
164            stop
165          ENDIF
166          lat = reform(lat[0, *])
167        END
168        2:lon = temporary(lon) # replicate(1, jpj)
169        ELSE:stop
170      ENDCASE
171    END
172;-------
173; lon and lat are 2D arrays
174;-------
175    sizelon[0] EQ 2 AND sizelat[0] EQ 2:BEGIN
176; get jpi and jpj
177      IF array_equal(sizelon[1:2], sizelat[1:2]) NE 1 THEN stop
178      jpi = sizelon[1]
179      jpj = sizelon[2]
180; make sure that lon and lat have the good number of dimensions
181      CASE n_dimensions OF
182        1:BEGIN
183          IF array_equal(lon, lon[*, 0] # replicate(1, jpj)) NE 1 THEN BEGIN
184            print, 'Longitudes are not the same for all latitudes, impossible to extract a 1D array of the longitudes'
185            stop
186          ENDIF
187          lon = lon[*, 0]
188          IF array_equal(lat, replicate(1, jpi) # reform(lat[0, *])) NE 1 THEN BEGIN
189            print, 'Latitudes are not the same for all longitudes, impossible to extract a 1D array of the latitudes'
190            stop
191          ENDIF
192        lat = reform(lat[0, *])
193        END
194        2:
195        ELSE:stop
196      ENDCASE
197    END
198;-------
199; lon and lat are not 1D and/or 2D arrays
200;-------
201    ELSE:stop
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.