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