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

Last change on this file since 236 was 236, checked in by pinsard, 17 years ago

replace some print by some report in some .pro #2

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