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

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

change *.pro file properties (del eof-style, del executable, set keywords Id

  • Property svn:keywords set to Id
File size: 8.2 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
40function ncdf_quickread_getFile, fullpath, suffix=suffix
41on_error,2
42compile_opt hidden
43; func_description
44; This function returns the filename name from the full path.
45; Inputs:    fullpath - full directory+file path
46; Keyword:   /suffix:   include inptu suffix in output file name
47; Outputs:   file - filename
48; Example Call: file = ncdf_quickread_getfile(fullpath)
49
50; Retrieve the postion at which the first '/' character occurs from
51; the end of the string.
52dirlen = rstrpos(fullpath, '/')
53
54; Retrieve the full length of the original string.
55len = strlen(fullpath)
56
57; Retrieve the filename.
58fullfile = strmid(fullpath, dirlen+1, len)
59
60; Retrieve the position at which the first '.' character occurs from
61; the end of the string.
62len = -1
63if not(keyword_set(suffix)) then len = rstrpos(fullfile, '.')
64if (len EQ -1) then len = strlen(fullfile)
65
66; Retrieve the file.
67file = strmid(fullfile, 0, len)
68
69; Return the file name.
70return, file
71
72; End function.
73end
74
75
76function ncdf_quickread_getDir, fullpath
77on_error,2
78compile_opt hidden
79; func_description
80; This function returns the directory name from the full path.
81; Inputs:   fullpath - full directory+file path
82; Outputs:  dir - directory path
83; Example Call: dir = ncdf_quickread_getdir(fullpath)
84
85; Retrieve the postion at which the first '/' character occurs from
86; the end of the string.
87len = rstrpos(fullpath, '/')
88
89; Retrieve the filename.
90if (len EQ -1) then dir = "./" $
91else dir = strmid(fullpath, 0, len+1)
92
93; Return the file name.
94return, dir
95
96; End function.
97end
98
99
100function ncdf_quickread_validateName, varname
101on_error,2
102compile_opt hidden
103; func_description
104; This routine ensures that the given name does not start with a number,
105; nor contain a dash.  IDL cannot accept a variable starting with a
106; number or containing a dash.  If the name starts with a number, an
107; underscore is prepended to the name, and if it contains a dash, the
108; dash is replaced with an underscore. 
109
110; Initialize the name.
111name = varname
112
113; If the name starts with a number, prepend it with an underscore.
114if (strpos(varname, '0') EQ 0) then name = strcompress("_"+varname)
115if (strpos(varname, '1') EQ 0) then name = strcompress("_"+varname)
116if (strpos(varname, '2') EQ 0) then name = strcompress("_"+varname)
117if (strpos(varname, '3') EQ 0) then name = strcompress("_"+varname)
118if (strpos(varname, '4') EQ 0) then name = strcompress("_"+varname)
119if (strpos(varname, '5') EQ 0) then name = strcompress("_"+varname)
120if (strpos(varname, '6') EQ 0) then name = strcompress("_"+varname)
121if (strpos(varname, '7') EQ 0) then name = strcompress("_"+varname)
122if (strpos(varname, '8') EQ 0) then name = strcompress("_"+varname)
123if (strpos(varname, '9') EQ 0) then name = strcompress("_"+varname)
124
125; If the name contains a dash replace it with an underscore.
126if (strpos(name, '-') NE -1) then begin
127   pieces = str_sep(name, '-')
128   n_pieces = n_elements(pieces)
129   name = pieces(0)
130   for i=1,n_pieces-1 do begin
131      name = strcompress(name+"_"+pieces(i))
132   endfor
133endif
134
135; Return the file name.
136return, name
137
138; End function.
139end
140
141
142function ncdf_quickread_helper, infile, verbose=verbose,  $
143                                prefix=prefix, fields=fields, $
144                                reform=reform
145on_error,2
146compile_opt hidden
147;
148; This procedure creates a script to read the data in a given netCDF
149; file into IDL.  The default output file is the name of the netCDF
150; file with idl replacing any existing suffix.  The default output is
151; variable data only.
152; Inputs:           infile  - full path to netCDF file of interest
153; Optional Inputs:  verbose - includes extractions of all input file
154;                             attributes in idl script
155;                   prefix, reform - see changelog above.
156;
157; Return value:  array of commands to run at top level
158
159; Ensure that the netCDF format is supported on the current platform.
160if not(ncdf_exists()) then message, $
161  "The Network Common Data Format is not supported on this platform."
162
163; Open the netcdf file for reading.
164ncid = NCDF_OPEN(strcompress(infile, /remove_all))
165if (ncid EQ -1) then message,$
166  "The file "+infile+" could not be opened, please check the path."
167
168; Retrieve general information about this netCDF file.
169ncidinfo = NCDF_INQUIRE(ncid)
170
171; command to write file header
172commands="__ncid = NCDF_OPEN('"+infile+"')"
173
174subset=0
175if n_elements(fields) ne 0 then begin
176  if (fields ne '') then begin
177      subset=1
178      subfields=strsplit(fields,/extract)
179  endif
180endif
181
182; Place the desired variables in local arrays.
183for i=0, ncidinfo.Nvars-1 do begin
184   vardata = NCDF_VARINQ(ncid, i)
185   if not subset then begin
186       wanted=1
187   endif else begin
188       match = where(subfields eq vardata.Name, nmatch)
189       wanted=(nmatch ne 0)
190   endelse
191   if wanted then begin
192       varname = ncdf_quickread_validatename(vardata.Name)
193       if keyword_set(prefix) then varname=prefix+varname
194       commands=$
195                [commands,"NCDF_VARGET, __ncid, "+strcompress(string(i))+", "+varname]
196       if keyword_set(reform) and vardata.ndims ge 2 $
197         then commands=[commands,varname+'=reform('+varname+')']
198       if (keyword_set(verbose)) then begin
199           for j=0, vardata.Natts-1 do begin
200               att = NCDF_ATTNAME(ncid, i, j)
201               attname = strcompress(varname+"_"+strcompress(att,/REMOVE_ALL))
202               commands=$
203                        [commands,"NCDF_ATTGET, __ncid, "+strcompress(string(i))+$
204                         ", '"+att+"', "+attname]
205               attinfo = ncdf_attinq(ncid, i, att)
206               if attinfo.datatype eq 'CHAR' then $
207                 commands=[commands,attname+" = STRING("+attname+")"]
208           endfor
209       endif
210   endif
211endfor
212
213if (keyword_set(verbose)) then begin
214  for i=0, ncidinfo.Ngatts-1 do begin
215     name = NCDF_ATTNAME(ncid, /GLOBAL, i)
216     attname = ncdf_quickread_validatename(name)
217     if keyword_set(prefix) then attname=prefix+attname
218     commands=$
219       [commands,"NCDF_ATTGET, __ncid, /GLOBAL, '"+name+"', "+attname]
220     attinfo = ncdf_attinq(ncid, /global, name)
221     if attinfo.datatype eq 'CHAR' then $
222       commands=[commands,attname+" = STRING("+attname+")"]
223  endfor
224endif
225
226ncdf_close,ncid
227commands=[commands,"NCDF_CLOSE, __ncid"]
228
229; Return commands to the caller.
230return,commands
231
232; End procedure.
233end
Note: See TracBrowser for help on using the repository browser.