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

Last change on this file since 325 was 325, checked in by pinsard, 16 years ago

modification of some headers (+some corrections) to prepare usage of the new idldoc

  • 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 position 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;-
120function ncdf_quickread_getdir, fullpath
121on_error,2
122compile_opt hidden
123; Retrieve the position at which the first '/' character occurs from
124; the end of the string.
125len = rstrpos(fullpath, '/')
126
127; Retrieve the filename.
128if (len EQ -1) then dir = "./" $
129else dir = strmid(fullpath, 0, len+1)
130
131; Return the file name.
132return, dir
133
134; End function.
135end
136
137;+
138; @file_comments
139; This routine ensures that the given name does not start with a number,
140; nor contain a dash.  IDL cannot accept a variable starting with a
141; number or containing a dash.  If the name starts with a number, an
142; underscore is prepended to the name, and if it contains a dash, the
143; dash is replaced with an underscore.
144;
145; @categories
146;
147;
148; @param VARNAME
149; The name of the variable to be read
150;
151;
152; @returns
153;
154;
155; @restrictions
156;
157;
158; @examples
159;
160;
161; @history
162;
163;
164; @version
165; $Id$
166;-
167function ncdf_quickread_validatename, varname
168on_error,2
169compile_opt hidden
170
171; Initialize the name.
172name = varname
173
174; If the name starts with a number, prepend it with an underscore.
175if (strpos(varname, '0') EQ 0) then name = strcompress("_"+varname)
176if (strpos(varname, '1') EQ 0) then name = strcompress("_"+varname)
177if (strpos(varname, '2') EQ 0) then name = strcompress("_"+varname)
178if (strpos(varname, '3') EQ 0) then name = strcompress("_"+varname)
179if (strpos(varname, '4') EQ 0) then name = strcompress("_"+varname)
180if (strpos(varname, '5') EQ 0) then name = strcompress("_"+varname)
181if (strpos(varname, '6') EQ 0) then name = strcompress("_"+varname)
182if (strpos(varname, '7') EQ 0) then name = strcompress("_"+varname)
183if (strpos(varname, '8') EQ 0) then name = strcompress("_"+varname)
184if (strpos(varname, '9') EQ 0) then name = strcompress("_"+varname)
185
186; If the name contains a dash replace it with an underscore.
187if (strpos(name, '-') NE -1) then begin
188   pieces = str_sep(name, '-')
189   n_pieces = n_elements(pieces)
190   name = pieces(0)
191   for i=1,n_pieces-1 do begin
192      name = strcompress(name+"_"+pieces(i))
193   endfor
194endif
195
196; Return the file name.
197return, name
198
199; End function.
200end
201;+
202; @file_comments
203; This procedure creates a script to read the data in a given netCDF
204; file into IDL.  The default output file is the name of the netCDF
205; file with idl replacing any existing suffix.  The default output is
206; variable data only.
207;
208; @categories
209;
210;
211; @param INFILE
212; full path to netCDF file of interest
213;
214; @keyword VERBOSE
215; Set this keyword to return an error message in case of an error.
216;
217; @keyword PREFIX
218; see changelog above.
219;
220; @keyword FIELDS
221;
222;
223; @keyword REFORM
224; see changelog above
225;
226; @returns
227; array of commands to run at top level
228;
229; @restrictions
230;
231;
232; @examples
233;
234;
235; @history
236;
237;
238; @version
239; $Id$
240;
241;-
242function ncdf_quickread_helper, infile, verbose=verbose,  $
243                                prefix=prefix, fields=fields, $
244                                reform=reform
245on_error,2
246compile_opt hidden
247;
248;
249
250; Ensure that the netCDF format is supported on the current platform.
251if not(ncdf_exists()) then message, $
252  "The Network Common Data Format is not supported on this platform."
253
254; Open the netcdf file for reading.
255ncid = NCDF_OPEN(strcompress(infile, /remove_all))
256if (ncid EQ -1) then message,$
257  "The file "+infile+" could not be opened, please check the path."
258
259; Retrieve general information about this netCDF file.
260ncidinfo = NCDF_INQUIRE(ncid)
261
262; command to write file header
263commands="__ncid = NCDF_OPEN('"+infile+"')"
264
265subset=0
266if n_elements(fields) ne 0 then begin
267  if (fields ne '') then begin
268      subset=1
269      subfields=strsplit(fields,/extract)
270  endif
271endif
272
273; Place the desired variables in local arrays.
274for i=0, ncidinfo.Nvars-1 do begin
275   vardata = NCDF_VARINQ(ncid, i)
276   if not subset then begin
277       wanted=1
278   endif else begin
279       match = where(subfields eq vardata.Name, nmatch)
280       wanted=(nmatch ne 0)
281   endelse
282   if wanted then begin
283       varname = ncdf_quickread_validatename(vardata.Name)
284       if keyword_set(prefix) then varname=prefix+varname
285       commands=$
286                [commands,"NCDF_VARGET, __ncid, "+strcompress(string(i))+", "+varname]
287       if keyword_set(reform) and vardata.ndims ge 2 $
288         then commands=[commands,varname+'=reform('+varname+')']
289       if (keyword_set(verbose)) then begin
290           for j=0, vardata.Natts-1 do begin
291               att = NCDF_ATTNAME(ncid, i, j)
292               attname = strcompress(varname+"_"+strcompress(att,/REMOVE_ALL))
293               commands=$
294                        [commands,"NCDF_ATTGET, __ncid, "+strcompress(string(i))+$
295                         ", '"+att+"', "+attname]
296               attinfo = ncdf_attinq(ncid, i, att)
297               if attinfo.datatype eq 'CHAR' then $
298                 commands=[commands,attname+" = STRING("+attname+")"]
299           endfor
300       endif
301   endif
302endfor
303
304if (keyword_set(verbose)) then begin
305  for i=0, ncidinfo.Ngatts-1 do begin
306     name = NCDF_ATTNAME(ncid, /GLOBAL, i)
307     attname = ncdf_quickread_validatename(name)
308     if keyword_set(prefix) then attname=prefix+attname
309     commands=$
310       [commands,"NCDF_ATTGET, __ncid, /GLOBAL, '"+name+"', "+attname]
311     attinfo = ncdf_attinq(ncid, /global, name)
312     if attinfo.datatype eq 'CHAR' then $
313       commands=[commands,attname+" = STRING("+attname+")"]
314  endfor
315endif
316
317ncdf_close,ncid
318commands=[commands,"NCDF_CLOSE, __ncid"]
319
320; Return commands to the caller.
321return,commands
322
323; End procedure.
324end
Note: See TracBrowser for help on using the repository browser.