source: trunk/SRC/ReadWrite/idl-NetCDF/ncdf_quickread/ncdf_quickread_helper.pro @ 163

Last change on this file since 163 was 163, checked in by navarro, 18 years ago

header improvements : type of parameters and keywords, default values, spell checking + idldoc assistant (IDL online_help)

  • Property svn:keywords set to Id
File size: 8.6 KB
Line 
1;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2;
3; ncdf_quickread_helper.pro - This file contains IDL functions to read netCDF
4;                             data files into IDL variables.
5;
6; Adapted from CDF2IDL.pro
7;
8;  This file contains the following functions and procedures:
9;     functions:
10;        ncdf_quickread_getfile - strips the directory and optionally) any
11;                                 suffixes from the path+file
12;        ncdf_quickread_getdir - returns the directory from the full path+file
13;        ncdf_quickread_validatename - validates the name that will be used
14;                                      as a netCDF variable
15;     procedures:
16;        ncdf_quickread_helper1 - construct commands which when executed at
17;                                 the top level will read netCDF variables
18;                                 into IDL
19;
20;  History:
21;  Date       Name          Action
22;  ---------  ------------  ----------------------------------------------
23;  06 Jun 97  S. Rupert     Created.
24;  09 Jun 97  S. Rupert     Fully tested.
25;  10 Jun 97  S. Rupert     Modified keyword usage.
26;  03 Feb 98  S. Rupert     Added additional error checking, and warning to
27;                           output script.
28;  17 Feb 98  S. Rupert     Corrected validation routine to handle instance
29;                           of name strating with a number and containing a
30;                           dash.
31;  05 Jul 00  A.M.Iwi       Added keyword PREFIX= on CDF2IDL.  Supplied string
32;                           gets prepended to all variable names.
33;  19 Jun 01  A.M.Iwi       Added keyword REFORM on CDF2IDL.  REFORM function
34;                           is used to remove dimensions of size 1.
35;  02 Oct 03  A.M.Iwi       Change into helper routine for ncdf_quickread
36;  11 Aug 04  A.M.Iwi       Add "fields" option to read only certain fields.
37;                           Also, only stringify attributes of type CHAR.
38;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39;+
40; @file_comments
41; This function returns the filename name from the full path.
42;
43; @categories
44;
45;
46; @param FULLPATH
47; full directory+file path
48;
49; @keyword SUFFIX
50; include input suffix in output file name
51;
52; @returns
53; file - filename
54;
55; @restrictions
56;
57;
58; @examples
59; Call: file = ncdf_quickread_getfile(fullpath)
60;
61; @history
62;
63;
64; @version
65; $Id$
66;-
67function ncdf_quickread_getfile, fullpath, suffix=suffix
68on_error,2
69compile_opt hidden
70; Retrieve the postion at which the first '/' character occurs from
71; the end of the string.
72dirlen = rstrpos(fullpath, '/')
73
74; Retrieve the full length of the original string.
75len = strlen(fullpath)
76
77; Retrieve the filename.
78fullfile = strmid(fullpath, dirlen+1, len)
79
80; Retrieve the position at which the first '.' character occurs from
81; the end of the string.
82len = -1
83if not(keyword_set(suffix)) then len = rstrpos(fullfile, '.')
84if (len EQ -1) then len = strlen(fullfile)
85
86; Retrieve the file.
87file = strmid(fullfile, 0, len)
88
89; Return the file name.
90return, file
91
92; End function.
93end
94
95;+
96; @file_comments
97; This function returns the directory name from the full path.
98;
99; @categories
100;
101;
102; @param FULLPATH
103; full directory+file path
104;
105; @returns
106; directory path
107;
108; @restrictions
109;
110;
111; @examples
112; Call: dir = ncdf_quickread_getdir(fullpath)
113;
114; @history
115;
116;
117; @version
118; $Id$
119;-
120
121function ncdf_quickread_getdir, fullpath
122on_error,2
123compile_opt hidden
124; Retrieve the postion at which the first '/' character occurs from
125; the end of the string.
126len = rstrpos(fullpath, '/')
127
128; Retrieve the filename.
129if (len EQ -1) then dir = "./" $
130else dir = strmid(fullpath, 0, len+1)
131
132; Return the file name.
133return, dir
134
135; End function.
136end
137
138;+
139; @file_comments
140; This routine ensures that the given name does not start with a number,
141; nor contain a dash.  IDL cannot accept a variable starting with a
142; number or containing a dash.  If the name starts with a number, an
143; underscore is prepended to the name, and if it contains a dash, the
144; dash is replaced with an underscore. 
145;
146; @categories
147;
148;
149; @param VARNAME
150; The name of the variable to be read
151;
152;
153; @returns
154;
155;
156; @restrictions
157;
158;
159; @examples
160;
161;
162; @history
163;
164;
165; @version
166; $Id$
167;-
168
169function ncdf_quickread_validatename, varname
170on_error,2
171compile_opt hidden
172
173; Initialize the name.
174name = varname
175
176; If the name starts with a number, prepend it with an underscore.
177if (strpos(varname, '0') EQ 0) then name = strcompress("_"+varname)
178if (strpos(varname, '1') EQ 0) then name = strcompress("_"+varname)
179if (strpos(varname, '2') EQ 0) then name = strcompress("_"+varname)
180if (strpos(varname, '3') EQ 0) then name = strcompress("_"+varname)
181if (strpos(varname, '4') EQ 0) then name = strcompress("_"+varname)
182if (strpos(varname, '5') EQ 0) then name = strcompress("_"+varname)
183if (strpos(varname, '6') EQ 0) then name = strcompress("_"+varname)
184if (strpos(varname, '7') EQ 0) then name = strcompress("_"+varname)
185if (strpos(varname, '8') EQ 0) then name = strcompress("_"+varname)
186if (strpos(varname, '9') EQ 0) then name = strcompress("_"+varname)
187
188; If the name contains a dash replace it with an underscore.
189if (strpos(name, '-') NE -1) then begin
190   pieces = str_sep(name, '-')
191   n_pieces = n_elements(pieces)
192   name = pieces(0)
193   for i=1,n_pieces-1 do begin
194      name = strcompress(name+"_"+pieces(i))
195   endfor
196endif
197
198; Return the file name.
199return, name
200
201; End function.
202end
203;+
204; @file_comments
205; This procedure creates a script to read the data in a given netCDF
206; file into IDL.  The default output file is the name of the netCDF
207; file with idl replacing any existing suffix.  The default output is
208; variable data only.
209;
210; @categories
211;
212;
213; @param INFILE
214; full path to netCDF file of interest
215;
216; @keyword VERBOSE 
217; Set this keyword to return an error message in case of an error.
218;
219; @keyword PREFIX
220; see changelog above.
221;
222; @keyword FIELDS
223;
224;
225; @keyword REFORM
226; see changelog above.
227;
228; @returns
229; array of commands to run at top level
230;
231; @restrictions
232;
233;
234; @examples
235;
236;
237; @history
238;
239;
240; @version
241; $Id$
242;
243;-
244function ncdf_quickread_helper, infile, verbose=verbose,  $
245                                prefix=prefix, fields=fields, $
246                                reform=reform
247on_error,2
248compile_opt hidden
249;
250;
251
252; Ensure that the netCDF format is supported on the current platform.
253if not(ncdf_exists()) then message, $
254  "The Network Common Data Format is not supported on this platform."
255
256; Open the netcdf file for reading.
257ncid = NCDF_OPEN(strcompress(infile, /remove_all))
258if (ncid EQ -1) then message,$
259  "The file "+infile+" could not be opened, please check the path."
260
261; Retrieve general information about this netCDF file.
262ncidinfo = NCDF_INQUIRE(ncid)
263
264; command to write file header
265commands="__ncid = NCDF_OPEN('"+infile+"')"
266
267subset=0
268if n_elements(fields) ne 0 then begin
269  if (fields ne '') then begin
270      subset=1
271      subfields=strsplit(fields,/extract)
272  endif
273endif
274
275; Place the desired variables in local arrays.
276for i=0, ncidinfo.Nvars-1 do begin
277   vardata = NCDF_VARINQ(ncid, i)
278   if not subset then begin
279       wanted=1
280   endif else begin
281       match = where(subfields eq vardata.Name, nmatch)
282       wanted=(nmatch ne 0)
283   endelse
284   if wanted then begin
285       varname = ncdf_quickread_validatename(vardata.Name)
286       if keyword_set(prefix) then varname=prefix+varname
287       commands=$
288                [commands,"NCDF_VARGET, __ncid, "+strcompress(string(i))+", "+varname]
289       if keyword_set(reform) and vardata.ndims ge 2 $
290         then commands=[commands,varname+'=reform('+varname+')']
291       if (keyword_set(verbose)) then begin
292           for j=0, vardata.Natts-1 do begin
293               att = NCDF_ATTNAME(ncid, i, j)
294               attname = strcompress(varname+"_"+strcompress(att,/REMOVE_ALL))
295               commands=$
296                        [commands,"NCDF_ATTGET, __ncid, "+strcompress(string(i))+$
297                         ", '"+att+"', "+attname]
298               attinfo = ncdf_attinq(ncid, i, att)
299               if attinfo.datatype eq 'CHAR' then $
300                 commands=[commands,attname+" = STRING("+attname+")"]
301           endfor
302       endif
303   endif
304endfor
305
306if (keyword_set(verbose)) then begin
307  for i=0, ncidinfo.Ngatts-1 do begin
308     name = NCDF_ATTNAME(ncid, /GLOBAL, i)
309     attname = ncdf_quickread_validatename(name)
310     if keyword_set(prefix) then attname=prefix+attname
311     commands=$
312       [commands,"NCDF_ATTGET, __ncid, /GLOBAL, '"+name+"', "+attname]
313     attinfo = ncdf_attinq(ncid, /global, name)
314     if attinfo.datatype eq 'CHAR' then $
315       commands=[commands,attname+" = STRING("+attname+")"]
316  endfor
317endif
318
319ncdf_close,ncid
320commands=[commands,"NCDF_CLOSE, __ncid"]
321
322; Return commands to the caller.
323return,commands
324
325; End procedure.
326end
Note: See TracBrowser for help on using the repository browser.