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