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

Last change on this file since 118 was 118, checked in by pinsard, 18 years ago

add $ in Calendar, Grid, Interpolation, Obsolete and Postscript *.pro files, add svn:keywords Id to all these files, some improvements in header

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