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

Last change on this file since 114 was 114, checked in by smasson, 18 years ago

new compilation options (compile_opt idl2, strictarrsubs) in each routine

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