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

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

improvements/corrections of some *.pro headers + replace some message by some report

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