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

Last change on this file since 157 was 157, checked in by navarro, 18 years ago

header improvements + xxx doc

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