source: trunk/SRC/ReadWrite/ncdf_getaxis.pro @ 231

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

improvements/corrections of some *.pro headers

  • Property svn:keywords set to Id
File size: 9.1 KB
Line 
1;+
2; @file_comments
3; get the x/y dimension Id and x/y axes from a netcdf file
4;
5; @categories
6; Reading
7;
8; @param cdfid {in}{required}{type=scalar}
9; the id of the netcdf file
10;
11; @param dimidx {out}{type=scalar (long)}
12; id of the x dimension
13;
14; @param dimidy {out}{type=scalar (long)}
15; id of the y dimension
16;
17; @param xaxis {out}{type=1D or 2D array}
18; the x axis
19;
20; @param yaxis {out}{type=1D or 2D array}
21; the y axis
22;
23; @keyword ROMSGRID {out}{type=scalar: 0 or 1}
24; gives back if we are using a ROMS grid (1) or not (0)
25;
26; @keyword START1 {default=0}{type=scalar: 0 or 1}
27; Index the axis from 1 instead of 0 when using /xyindex
28;
29; @keyword XDIMNAME {default='longitude', 'lon', 'x', 'longitude*', 'lon*', 'x*', '*longitude*', '*lon*' or '*x*'}{type=scalar string}
30; A string giving the name of the x dimension
31;
32; @keyword YDIMNAME {default='latitude', 'lat', 'y', 'latitude*', 'lat*', 'y*', 'eta_*', '*latitude*', '*lat*', '*y*'}{type=scalar string}
33; A string giving the name of the y dimension
34;
35; @keyword XAXISNAME {default='x', 'longitude', 'nav_lon', 'lon', 'lon_rho' or 'NbLongitudes'}{type=scalar string}
36; A string giving the name of the variable in the file
37; that contains the [xyz]axis.
38;
39; @keyword YAXISNAME {default='y', 'latitude', 'nav_lat','lat', 'lat_rho' or 'NbLatitudes'}{type=scalar string}
40; A string giving the name of the variable in the file
41; that contains the [xyz]axis.
42;
43; @keyword XYINDEX {default=0}{type=scalar: 0 or 1}
44; To define the x/y axis with index instead of using
45; the values contained in X/YAXISNAME.
46; x/yaxis = keyword_set(start1) + findgen(jpi/jpj)
47;
48; @keyword XYINDEX {default=0}{type=scalar: 0 or 1}
49;
50; @keyword _EXTRA
51; Used to be able to call ncdf_getaxis with _extra
52;
53; @history
54; March 2007: Sebastien Masson (smasson\@locean-ipsl.upmc.fr)
55;
56;
57; @version
58; $Id$
59;-
60;
61PRO ncdf_getaxis, cdfid, dimidx, dimidy, xaxis, yaxis $
62                  , XAXISNAME = xaxisname, YAXISNAME = yaxisname $
63                  , XDIMNAME = xdimname, YDIMNAME = ydimname $
64                  , XYINDEX = xyindex, START1 = start1, ROMSGRID = romsgrid, _extra = ex
65;
66  compile_opt idl2, strictarrsubs
67;
68
69; what is inside the file
70  inside = ncdf_inquire(cdfid)
71;------------------------------------------------------------
72; name of all dimensions
73  namedim = strarr(inside.ndims)
74  for dimiq = 0, inside.ndims-1 do begin
75    ncdf_diminq, cdfid, dimiq, tmpname, value
76    namedim[dimiq] = strlowcase(tmpname)
77  ENDFOR
78;----------------------------------------------------------
79; name of the variables
80  namevar = strarr(inside.nvars)
81  for varid = 0, inside.nvars-1 do begin
82    invar = ncdf_varinq(cdfid, varid)
83    namevar[varid] = strlowcase(invar.name)
84  ENDFOR
85;----------------------------------------------------------
86; find the xaxis
87; try to get the variable that contains the xaxis
88  if keyword_set(xaxisname) then xaxisname = strlowcase(xaxisname) ELSE xaxisname = 'x'
89  xvarid = (where(namevar EQ xaxisname OR namevar EQ 'longitude' $
90                  OR namevar EQ 'nav_lon' OR namevar EQ 'lon' $
91                  OR namevar EQ 'lon_rho' OR namevar EQ 'nblongitudes'))[0]
92; no xaxis variable found, we will build a fake xaxis based on the size of the x dimension
93; -> we must find the x dimension
94  IF xvarid EQ -1 THEN BEGIN
95    dummy = report(['xaxis variable was not found within the default names:' $
96                    , '''longitude'', ''nav_lon'', ''lon'', ''lon_rho'', ''nblongitudes''' $
97                    , ' we use a fake xaxis based on x dimension size (or use XAXISNAME keyword)'], /simple)
98; try to get the dimension corresponding to x
99; roms file?
100    dimidx = where(namedim EQ 'xi_rho' OR namedim EQ 'xi_u' OR namedim EQ 'xi_v' OR namedim EQ 'xi_psi')
101    IF dimidx[0] EQ -1 THEN BEGIN
102; we are looking for a x dimension with a name matching one of the following regular expression:
103      if keyword_set(xdimname) then testname = strlowcase(xdimname) $
104      ELSE testname = ['longitude', 'lon', 'x', 'longitude*', 'lon*', 'x*', '*longitude*', '*lon*', '*x*']
105      cnt = -1
106      ii = 0
107      WHILE cnt NE 1 AND ii LT n_elements(testname) DO BEGIN
108        dimidx = where(strmatch(namedim, testname[ii]) EQ 1, cnt)
109        ii = ii+1
110      ENDWHILE
111      CASE cnt OF
112        0:begin
113          dummy = report(['none of the dimensions name matches one of the following regular expression:' $
114                          , '''longitude'', ''lon'', ''x'', ''longitude*'', ''lon*'', ''x*'', ''*longitude*'', ''*lon*'', ''*x*''' $
115                          , ' => we cannot find the x dimension, use XDIMNAME keyword'], /simple)
116          stop
117        END
118        1:dimidx = dimidx[0]
119        ELSE:begin
120          dummy = report(['several (and not one unique) dimensions name matches the following regular expression:' $
121                          , '''longitude'', ''lon'', ''x'', ''longitude*'', ''lon*'', ''x*'', ''*longitude*'', ''*lon*'', ''*x*''' $
122                          , ' => we cannot find the x dimension, use XDIMNAME keyword'], /simple)
123          stop
124        ENDELSE
125      ENDCASE
126    ENDIF
127    romsgrid = 0b
128  ENDIF ELSE BEGIN
129    romsgrid = strmid(namevar[xvarid], 0, 4) EQ 'lon_'
130    xinq = ncdf_varinq(cdfid, xvarid)
131    dimidx = xinq.dim[0]
132    IF xinq.ndims GE 2 THEN ncdf_diminq, cdfid, xinq.dim[1], blabla, jpjfromx
133  ENDELSE
134;
135  IF arg_present(xaxis) THEN BEGIN
136    ncdf_diminq, cdfid, dimidx, blabla, jpifromx
137; should we read or compute the xaxis?
138    IF keyword_set(xyindex) OR xvarid EQ -1 THEN BEGIN
139      xaxis = keyword_set(start1) + findgen(jpifromx)
140      xyindex = 1
141    ENDIF ELSE BEGIN
142; read the xaxis
143      ncdf_varget, cdfid, xvarid, xaxis
144; make sure of the shape of xaxis
145      IF n_elements(jpjfromx) NE 0 THEN xaxis = reform(xaxis, jpifromx, jpjfromx, /over)
146    ENDELSE
147  ENDIF
148
149;----------------------------------------------------------
150; find the yaxis
151; try to get the variable that contains the yaxis
152  if keyword_set(yaxisname) then yaxisname = strlowcase(yaxisname) ELSE yaxisname = 'y'
153  yvarid = (where(namevar EQ yaxisname OR namevar EQ 'latitude' $
154                  OR namevar EQ 'nav_lat' OR namevar EQ 'lat' $
155                  OR namevar EQ 'lat_rho' OR namevar EQ 'nblatitudes'))[0]
156  yvarid = yvarid[0]
157; no yaxis variable found, we will build a fake yaxis based on the size of the y dimension
158; -> we must find the y dimension
159  if yvarid EQ -1 then begin
160    dummy = report(['yaxis variable was not found within the default names:' $
161                    , '''latitude'', ''nav_lat'', ''lat'', ''lat_rho'', ''nblatitudes''' $
162                    , ' we use a fake yaxis based on y dimension size (or use YAXISNAME keyword)'], /simple)
163; try to get the dimension corresponding to y
164; roms file?
165    dimidy = where(namedim EQ 'eta_rho' OR namedim EQ 'eta_u' OR namedim EQ 'eta_v' OR namedim EQ 'eta_psi')
166    IF dimidy[0] EQ -1 THEN BEGIN
167; we are looking for a y dimension with a name matching one of the following regular expression:
168      if keyword_set(ydimname) then testname = strlowcase(ydimname) $
169      ELSE testname = ['latitude', 'lat', 'y', 'latitude*', 'lat*', 'y*', 'eta_*', '*latitude*', '*lat*', '*y*']
170      cnt = -1
171      ii = 0
172      WHILE cnt NE 1 AND ii LT n_elements(testname) DO BEGIN
173        dimidy = where(strmatch(namedim, testname[ii]) EQ 1, cnt)
174        ii = ii+1
175      ENDWHILE
176      CASE cnt OF
177        0:begin
178          dummy = report(['none of the dimensions name matches one of the following regular expression:' $
179                          , '''latitude'', ''lat'', ''y'', ''latitude*'', ''lat*'', ''y*'', ''eta_*'', ''*latitude*'', ''*lat*'', ''*y*''' $
180                          , ' => we cannot find the y dimension, use YDIMNAME keyword'], /simple)
181          stop
182        END
183        1:dimidy = dimidy[0]
184        ELSE:begin
185          dummy = report(['several (and not one unique) dimensions name matches the following regular expression:' $
186                          , '''latitude'', ''lat'', ''y'', ''latitude*'', ''lat*'', ''y*'', ''eta_*'', ''*latitude*'', ''*lat*'', ''*y*''' $
187                          , ' => we cannot find the y dimension, use YDIMNAME keyword'], /simple)
188          stop
189        ENDELSE
190      ENDCASE
191    ENDIF
192  ENDIF ELSE BEGIN
193    yinq = ncdf_varinq(cdfid, yvarid)
194    IF yinq.ndims GE 2 THEN BEGIN
195      ncdf_diminq, cdfid, yinq.dim[0], blabla, jpifromy
196      dimidy = yinq.dim[1]
197    ENDIF ELSE dimidy = yinq.dim[0]
198  ENDELSE
199;
200  IF arg_present(yaxis) THEN BEGIN
201    IF n_elements(jpifromy) NE 0 THEN BEGIN
202      IF jpifromy NE jpifromx THEN BEGIN
203        dummy = report('x/y axes do not have the same x dimension...')
204        stop
205      ENDIF
206    ENDIF
207    ncdf_diminq, cdfid, dimidy, blabla, jpjfromy
208    IF n_elements(jpjfromx) NE 0 THEN BEGIN
209      IF jpjfromy NE jpjfromx THEN BEGIN
210        dummy = report(' x/y axes do not have the same y dimension...')
211        stop
212      ENDIF
213    ENDIF
214; should we read or compute the xaxis?
215    IF keyword_set(xyindex) OR yvarid EQ -1 THEN BEGIN
216      yaxis = keyword_set(start1) + findgen(jpjfromy)
217    ENDIF ELSE BEGIN
218; read the yaxis
219      ncdf_varget, cdfid, yvarid, yaxis
220; make sure of the shape of xaxis
221      IF n_elements(jpifromy) NE 0 THEN yaxis = reform(yaxis, jpifromy, jpjfromy, /over)
222    ENDELSE
223  ENDIF
224
225  return
226END
Note: See TracBrowser for help on using the repository browser.