1 ;----------------------------------------------------------------------
3 ; add lat, lon to output
6 ; This example reads an ASCII file that is formatted a specific way, and
7 ; writes out the results to a netCDF file.
9 ; The first line in the ASCII file must be a header, with each field
10 ; separated by a single character delimiter (like a ","). The rest of
11 ; the file must be such that each row contains all fields, each
12 ; separated by the designated delimiter.
14 ; The fields can be integer, float, double, character, or string.
15 ; String fields cannot be written to a netCDF file. They have to
16 ; be read in as character arrays and written out that way.
17 ;----------------------------------------------------------------------
19 ;----------------------------------------------------------------------
20 ; This function returns the index locations of the given delimiter
21 ; in a row or several rows of strings.
22 ;----------------------------------------------------------------------
23 function delim_indices(strings,nfields,delimiter)
24 local cstrings, cdelim
26 nrows = dimsizes(strings)
28 ; Handle special case if we only have one string. Make sure it
29 ; is put into a 2D array.
32 cstrings = new((/1,strlen(strings)+1/),character)
35 cstrings = stringtochar(strings) ; Convert to characters.
36 cdelim = stringtochar(delimiter) ; Convert delimiter to character.
38 ; Som error checking here. Make sure delimiter is one character.
42 if(rank.ne.1.or.(rank.eq.1.and.nc.ne.2)) then
43 print("delim_indices: fatal: the delimiter you've selected")
44 print("must be a single character. Can't continue.")
49 ; Create array to hold indices of delimiter locations, and then loop
50 ; through each row and find all the delimiters. Make sure each row has
51 ; the correct number of delimiters.
54 cindices = new((/nrows,ndelims/),integer)
56 ii = ind(cstrings(i,:).eq.cdelim(0))
58 ; Make sure there were delimiters on this row. If not, we just quit.
59 ; This could probably be modified to do this more gracefully.
61 if(any(ismissing(ii))) then
62 print("delim_indices: fatal: I didn't find any delimiters")
63 print("('" + delimiter + "') on row " + i + ". Can't continue.")
66 if(dimsizes(ii).ne.ndelims) then
67 print("delim_indices: fatal: I expected to find " + ndelims)
68 print("delimiters on row " + i + ". Instead, I found " + dimsizes(ii) + ".")
69 print("Can't continue.")
75 delete(ii) ; For next time through loop
81 ;----------------------------------------------------------------------
82 ; This function reads in a particular field from a string array,
83 ; given the field number to read (fields start at #1 and go to #nfield),
84 ; and the indices of the delimiters.
86 ; It returns either an integer, float, double, character, or a string,
87 ; depending on the input flag "return_type".
88 ;----------------------------------------------------------------------
89 function read_field(strings,ifield,indices,return_type)
90 local nstring, cstrings, nf, tmp_str
92 nrows = dimsizes(strings)
94 ; Handle special case if we only have one string. Make sure it
95 ; is put into a 2D array.
98 cstrings = new((/1,strlen(strings)+1/),character)
101 cstrings = stringtochar(strings)
102 nf = dimsizes(indices(0,:))+1 ; indices is nrows x (nfields-1)
105 ; Error checking. Make sure user has entered a valid field.
107 if(ifield.le.0.or.ifield.gt.nf) then
108 print("read_field: fatal: you've selected a field that is")
109 print("out-of-range of the number of fields that you have (" + nf + ").")
114 ; Set up array to return. For string, int, float, or double arrays,
115 ; we don't have to do anything special. For character arrays,
118 if(return_type.ne."character") then
119 return_array = new(nrows,return_type)
122 ; We don't know what the biggest character array is at this point, so
123 ; make it bigger than necessary, and then resize later as necessary.
125 tmp_return_array = new((/nrows,dimsizes(cstrings(0,:))/),"character")
127 max_len = 0 ; Use to keep track of max lengths of strings.
132 ; Special case of first field in row.
136 iend = indices(i,ifield-1)-1
139 ; Special case of first field in row.
141 if(ifield.eq.nf) then
142 ibeg = indices(i,ifield-2)+1
143 iend = dimsizes(cstrings(i,:))-1
145 ; Any field between first and last field.
148 ibeg = indices(i,ifield-2)+1
149 iend = indices(i,ifield-1)-1
153 ; Here's the code that pulls off the correct string, and converts it
154 ; to float if desired.
156 if(return_type.eq."integer") then
157 return_array(i) = stringtointeger(chartostring(cstrings(i,ibeg:iend)))
159 if(return_type.eq."float") then
160 return_array(i) = stringtofloat(chartostring(cstrings(i,ibeg:iend)))
162 if(return_type.eq."double") then
163 return_array(i) = stringtodouble(chartostring(cstrings(i,ibeg:iend)))
165 if(return_type.eq."string") then
166 return_array(i) = chartostring(cstrings(i,ibeg:iend))
168 if(return_type.eq."character") then
169 if( (iend-ibeg+1) .gt. max_len) then
170 max_len = iend-ibeg+1
172 tmp_return_array(i,0:iend-ibeg) = cstrings(i,ibeg:iend)
176 if(return_type.eq."character") then
177 return_array = new((/nrows,max_len/),"character")
178 return_array = tmp_return_array(:,0:max_len-1)
185 ;----------------------------------------------------------------------
186 ; This function reads in string fields only to get the maximum string
188 ;----------------------------------------------------------------------
189 function get_maxlen(strings,ifield,indices)
190 local nstring, cstrings, nf, tmp_str
192 nrows = dimsizes(strings)
194 ; Handle special case if we only have one string. Make sure it
195 ; is put into a 2D array.
198 cstrings = new((/1,strlen(strings)+1/),character)
201 cstrings = stringtochar(strings)
202 nf = dimsizes(indices(0,:))+1 ; indices is nrows x (nfields-1)
205 ; Error checking. Make sure user has entered a valid field.
207 if(ifield.le.0.or.ifield.gt.nf) then
208 print("read_field: fatal: you've selected a field that is")
209 print("out-of-range of the number of fields that you have (" + nf + ").")
213 ; We don't know what the biggest character array is at this point, so
214 ; make it bigger than necessary, and then resize later as necessary.
216 tmp_return_array = new((/nrows,dimsizes(cstrings(0,:))/),"character")
218 max_len = 0 ; Use to keep track of max lengths of strings.
222 ; Special case of first field in row.
226 iend = indices(i,ifield-1)-1
229 ; Special case of first field in row.
231 if(ifield.eq.nf) then
232 ibeg = indices(i,ifield-2)+1
233 iend = dimsizes(cstrings(i,:))-1
235 ; Any field between first and last field.
238 ibeg = indices(i,ifield-2)+1
239 iend = indices(i,ifield-1)-1
242 if( (iend-ibeg+1) .gt. max_len) then
243 max_len = iend-ibeg+1
250 ;----------------------------------------------------------------------
252 ;----------------------------------------------------------------------
255 ; Set up defaults here. We are hard-coding the field types here.
256 ; You can set up this script to try to determine the field types
257 ; automatically, but this is a bit tedious. Maybe later.
261 lon = -72.1715 + 360.
263 nfields = 30 ; # of fields
264 delimiter = "," ; field delimiter
266 filename = station+year+"_L4_m.txt" ; ASCII" file to read.
267 cdf_file = station+year+"_L4_m.nc" ; netCDF file to write.
269 ; In this case, fields #2-#2 are integers,
270 ; and the rest of the fields are floats.
272 var_types = new(nfields,string)
273 var_strlens = new(nfields,integer) ; var to hold strlens, just in case.
275 var_types = "float" ; Most are floats.
276 var_types(0:1) = "integer"
278 if(isfilepresent(cdf_file))
279 print("Warning: '" + cdf_file + "' exists. Will remove it.")
280 system("/bin/rm " + cdf_file)
283 ; Read in data as strings. This will create a string array that has the
284 ; same number of strings as there are rows in the file. We will then need
285 ; to parse each string later.
287 read_data = asciiread(filename,-1,"string")
288 header = read_data(0) ; Header. Use for variable names.
289 data = read_data(1:) ; Get rid of first line which is a header.
290 nrows = dimsizes(data) ; Number of rows.
292 ; Read in locations of delimiters in each string row.
294 hindices = delim_indices(header,nfields,delimiter) ; header row
295 dindices = delim_indices(data,nfields,delimiter) ; rest of file
300 ; Read in the field names which will become variable names on
303 var_names = new(nfields,string)
306 var_names(i) = read_field(header,i+1,hindices,"string")
309 ; Write out this netCDF file efficiently so it will be faster.
310 ; Try to predefine everything before you write to it.
312 f = addfile(cdf_file,"c")
313 setfileoption(f,"DefineMode",True) ; Enter predefine phase.
315 ; Write global attributes to file. It's okay to do this before
316 ; predefining the file's variables. We are still in "define" mode.
319 fAtt@description = "Data read in from " + filename + " ASCII file."
320 fAtt@creation_date = systemfunc ("date")
321 fileattdef( f, fAtt )
323 ; Write dimension names to file. If there are no character variables,
324 ; then there's only one dimension name ("nvalues").
329 dim_names = (/ "year", "month" /)
330 dim_sizes = (/ nyear , nmonth /)
331 dimUnlim = (/ True , False /)
333 filedimdef( f, dim_names, dim_sizes, dimUnlim )
334 filedimdef( f, "lat", 1, False )
335 filedimdef( f, "lon", 1, False )
337 ; Define each variable on the file.
339 filevardef( f, "year", "integer", "year" )
340 filevardef( f, "lat" , "float" , "lat" )
341 filevardef( f, "lon" , "float" , "lon" )
343 ; Don't deal with variable Month (i=0).
346 filevardef(f, var_names(i), var_types(i), dim_names)
349 ; Loop through each field, read the values for that field, print
350 ; information about the variable, and then write it to the netCDF
354 ifield = i+1 ; Fields start at #1, not #0.
356 ; Note: you can't write strings to a netCDF file, so these have
357 ; to be written out as character arrays.
359 tmp_data = new((/1,nmonth/),var_types(i))
361 tmp_data(0,:) = read_field(data,ifield,dindices,var_types(i))
363 ; tmp_data(0,:) = out_data(:)
365 ; Print some info about the variable.
368 print("Writing variable '" + var_names(i) + "' (field #" + ifield + ").")
369 print("Type is " + var_types(i) + ".")
370 print("min/max = " + min(tmp_data) + "/" + max(tmp_data))
372 if(any(ismissing(tmp_data))) then
373 print("This variable does contain missing values.")
375 print("This variable doesn't contain missing values.")
378 f->$var_names(i)$ = tmp_data ; Write to netCDF file.
380 delete(tmp_data) ; Delete for next round.