Modifications to scoring and graphics production for the final version of code for the C-LAMP paper in GCB.
1 ;----------------------------------------------------------------------
2 ; change Month to yyyymm
4 ; add lat, lon to output
7 ; This example reads an ASCII file that is formatted a specific way, and
8 ; writes out the results to a netCDF file.
10 ; The first line in the ASCII file must be a header, with each field
11 ; separated by a single character delimiter (like a ","). The rest of
12 ; the file must be such that each row contains all fields, each
13 ; separated by the designated delimiter.
15 ; The fields can be integer, float, double, character, or string.
16 ; String fields cannot be written to a netCDF file. They have to
17 ; be read in as character arrays and written out that way.
18 ;----------------------------------------------------------------------
20 ;----------------------------------------------------------------------
21 ; This function returns the index locations of the given delimiter
22 ; in a row or several rows of strings.
23 ;----------------------------------------------------------------------
24 function delim_indices(strings,nfields,delimiter)
25 local cstrings, cdelim
27 nrows = dimsizes(strings)
29 ; Handle special case if we only have one string. Make sure it
30 ; is put into a 2D array.
33 cstrings = new((/1,strlen(strings)+1/),character)
36 cstrings = stringtochar(strings) ; Convert to characters.
37 cdelim = stringtochar(delimiter) ; Convert delimiter to character.
39 ; Som error checking here. Make sure delimiter is one character.
43 if(rank.ne.1.or.(rank.eq.1.and.nc.ne.2)) then
44 print("delim_indices: fatal: the delimiter you've selected")
45 print("must be a single character. Can't continue.")
50 ; Create array to hold indices of delimiter locations, and then loop
51 ; through each row and find all the delimiters. Make sure each row has
52 ; the correct number of delimiters.
55 cindices = new((/nrows,ndelims/),integer)
57 ii = ind(cstrings(i,:).eq.cdelim(0))
59 ; Make sure there were delimiters on this row. If not, we just quit.
60 ; This could probably be modified to do this more gracefully.
62 if(any(ismissing(ii))) then
63 print("delim_indices: fatal: I didn't find any delimiters")
64 print("('" + delimiter + "') on row " + i + ". Can't continue.")
67 if(dimsizes(ii).ne.ndelims) then
68 print("delim_indices: fatal: I expected to find " + ndelims)
69 print("delimiters on row " + i + ". Instead, I found " + dimsizes(ii) + ".")
70 print("Can't continue.")
76 delete(ii) ; For next time through loop
82 ;----------------------------------------------------------------------
83 ; This function reads in a particular field from a string array,
84 ; given the field number to read (fields start at #1 and go to #nfield),
85 ; and the indices of the delimiters.
87 ; It returns either an integer, float, double, character, or a string,
88 ; depending on the input flag "return_type".
89 ;----------------------------------------------------------------------
90 function read_field(strings,ifield,indices,return_type)
91 local nstring, cstrings, nf, tmp_str
93 nrows = dimsizes(strings)
95 ; Handle special case if we only have one string. Make sure it
96 ; is put into a 2D array.
99 cstrings = new((/1,strlen(strings)+1/),character)
102 cstrings = stringtochar(strings)
103 nf = dimsizes(indices(0,:))+1 ; indices is nrows x (nfields-1)
106 ; Error checking. Make sure user has entered a valid field.
108 if(ifield.le.0.or.ifield.gt.nf) then
109 print("read_field: fatal: you've selected a field that is")
110 print("out-of-range of the number of fields that you have (" + nf + ").")
115 ; Set up array to return. For string, int, float, or double arrays,
116 ; we don't have to do anything special. For character arrays,
119 if(return_type.ne."character") then
120 return_array = new(nrows,return_type)
123 ; We don't know what the biggest character array is at this point, so
124 ; make it bigger than necessary, and then resize later as necessary.
126 tmp_return_array = new((/nrows,dimsizes(cstrings(0,:))/),"character")
128 max_len = 0 ; Use to keep track of max lengths of strings.
133 ; Special case of first field in row.
137 iend = indices(i,ifield-1)-1
140 ; Special case of first field in row.
142 if(ifield.eq.nf) then
143 ibeg = indices(i,ifield-2)+1
144 iend = dimsizes(cstrings(i,:))-1
146 ; Any field between first and last field.
149 ibeg = indices(i,ifield-2)+1
150 iend = indices(i,ifield-1)-1
154 ; Here's the code that pulls off the correct string, and converts it
155 ; to float if desired.
157 if(return_type.eq."integer") then
158 return_array(i) = stringtointeger(chartostring(cstrings(i,ibeg:iend)))
160 if(return_type.eq."float") then
161 return_array(i) = stringtofloat(chartostring(cstrings(i,ibeg:iend)))
163 if(return_type.eq."double") then
164 return_array(i) = stringtodouble(chartostring(cstrings(i,ibeg:iend)))
166 if(return_type.eq."string") then
167 return_array(i) = chartostring(cstrings(i,ibeg:iend))
169 if(return_type.eq."character") then
170 if( (iend-ibeg+1) .gt. max_len) then
171 max_len = iend-ibeg+1
173 tmp_return_array(i,0:iend-ibeg) = cstrings(i,ibeg:iend)
177 if(return_type.eq."character") then
178 return_array = new((/nrows,max_len/),"character")
179 return_array = tmp_return_array(:,0:max_len-1)
186 ;----------------------------------------------------------------------
187 ; This function reads in string fields only to get the maximum string
189 ;----------------------------------------------------------------------
190 function get_maxlen(strings,ifield,indices)
191 local nstring, cstrings, nf, tmp_str
193 nrows = dimsizes(strings)
195 ; Handle special case if we only have one string. Make sure it
196 ; is put into a 2D array.
199 cstrings = new((/1,strlen(strings)+1/),character)
202 cstrings = stringtochar(strings)
203 nf = dimsizes(indices(0,:))+1 ; indices is nrows x (nfields-1)
206 ; Error checking. Make sure user has entered a valid field.
208 if(ifield.le.0.or.ifield.gt.nf) then
209 print("read_field: fatal: you've selected a field that is")
210 print("out-of-range of the number of fields that you have (" + nf + ").")
214 ; We don't know what the biggest character array is at this point, so
215 ; make it bigger than necessary, and then resize later as necessary.
217 tmp_return_array = new((/nrows,dimsizes(cstrings(0,:))/),"character")
219 max_len = 0 ; Use to keep track of max lengths of strings.
223 ; Special case of first field in row.
227 iend = indices(i,ifield-1)-1
230 ; Special case of first field in row.
232 if(ifield.eq.nf) then
233 ibeg = indices(i,ifield-2)+1
234 iend = dimsizes(cstrings(i,:))-1
236 ; Any field between first and last field.
239 ibeg = indices(i,ifield-2)+1
240 iend = indices(i,ifield-1)-1
243 if( (iend-ibeg+1) .gt. max_len) then
244 max_len = iend-ibeg+1
251 ;----------------------------------------------------------------------
253 ;----------------------------------------------------------------------
256 ;###############################################################
257 ; Set up defaults. We are hard-coding here..
262 lon = -72.1715 + 360.
264 nfields = 30 ; # of fields
265 delimiter = "," ; field delimiter
267 filename = station+year+"_L4_m.txt" ; ASCII" file to read.
268 cdf_file = station+year+"_L4_m.nc" ; netCDF file to write.
270 if(isfilepresent(cdf_file))
271 print("Warning: '" + cdf_file + "' exists. Will remove it.")
272 system("/bin/rm " + cdf_file)
275 ; In this case, fields #1-#2 are integers,
276 ; and the rest of the fields are floats.
278 var_types = new(nfields,string)
279 var_types = "float" ; Most are floats.
280 var_types(0:1) = "integer"
282 ;#####################################################################
284 ; Read in data as strings. This will create a string array that has the
285 ; same number of strings as there are rows in the file. We will then need
286 ; to parse each string later.
288 read_data = asciiread(filename,-1,"string")
290 header = read_data(0) ; Header. Use for variable names.
291 data = read_data(1:) ; Get rid of first line which is a header.
292 nmonth = dimsizes(data) ; Number of rows == number of month.
294 ; Read in locations of delimiters in each string row.
296 hindices = delim_indices(header,nfields,delimiter) ; header row
297 dindices = delim_indices(data,nfields,delimiter) ; rest of file
299 ; Read in the field names which will become variable names on
302 var_names = new(nfields,string)
305 var_names(i) = read_field(header,i+1,hindices,"string")
308 ;-------------------------------------------------------------------
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.
325 dim_names = (/ "year", "month" /)
326 dim_sizes = (/ -1 , nmonth /)
327 dimUnlim = (/ True , False /)
328 filedimdef( f, dim_names, dim_sizes, dimUnlim )
330 filedimdef( f, "lat", 1, False )
331 filedimdef( f, "lon", 1, False )
333 ; Define each variable on the file.
335 filevardef( f, "year", "integer", "year" )
336 filevardef( f, "lat" , "float" , "lat" )
337 filevardef( f, "lon" , "float" , "lon" )
340 filevardef(f, var_names(i), var_types(i), dim_names)
342 ;-----------------------------------------------------------------
344 ; Loop through each field, read the values for that field, print
345 ; information about the variable, and then write it to the netCDF file.
348 ifield = i+1 ; Fields start at #1, not #0.
350 tmp_data = new((/1,nmonth/),var_types(i))
353 tmp_data@_FillValue = -999
355 tmp_data@_FillValue = -9999.00
358 tmp_data(0,:) = read_field(data,ifield,dindices,var_types(i))
360 ; change Month to yyyymm
363 tmp_data(0,:) = tmp_data(0,:) + year*100
366 ; Print some info about the variable.
369 ; print("Writing variable '" + var_names(i) + "' (field #" + ifield + ").")
370 ; print("Type is " + var_types(i) + ".")
371 ; print("min/max = " + min(tmp_data) + "/" + max(tmp_data))
373 ; if(any(ismissing(tmp_data))) then
374 ; print("This variable does contain missing values.")
376 ; print("This variable doesn't contain missing values.")
379 ; write variable to file
381 f->$var_names(i)$ = tmp_data ; Write to netCDF file.
383 delete(tmp_data) ; Delete for next round.
386 ; write variable to file