[ncl-talk] CSV file script

Gus Correa gus at ldeo.columbia.edu
Mon Oct 23 17:42:34 MDT 2017


Hi Grace

The example script just reads the CSV file and prints its contents,
adding some fixed strings that clarify what each field in the CSV file 
means.

The print function takes a string as argument.
In this case the string is made of several parts,
which are concatenated into a single string with the "+" sign operator.

Some parts are fixed strings, such as "Name is'", and "', group is ".
Other parts are the actual values of the variables/data/information
you want, such as 'name' and 'group'.

The CSV file lines are read as strings,
in the code line:
lines = asciiread(filename,-1,"string")

Then each line string
is split into the parts that have the data/information
you need by the function str_get_field:
https://www.ncl.ucar.edu/Document/Functions/Built-in/str_get_field.shtml

However, note that originally group was an integer number, hence it has
to be converted to a string, by the tointeger function,
which is done in the line:
group = tointeger(str_get_field(lines,5,delim):
https://www.ncl.ucar.edu/Document/Functions/Built-in/tointeger.shtml

A similar operation is applied to the floating point number size,
with the function tofloat:
https://www.ncl.ucar.edu/Document/Functions/Built-in/tofloat.shtml

This way all the ingredients needed to form the string argument of the
"print" function are ready to be concatenated as a single string.

I hope this helps,
Gus Correa


On 10/23/2017 06:14 PM, Grace Choi wrote:
> Hi everyone,
> 
> So I am having a trouble understanding a part of a script for reading 
> CSV file.
> 
> I have been having a hard time getting this script to work, because I am 
> not sure how to correctly write the highlighted part of the script.
> 
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
> 
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
> 
> 
> begin
> 
> filename = "example2.csv"
> 
> 
> lines = asciiread(filename,-1,"string")
> 
> 
> delim = ","
> 
> 
> name = str_get_field(lines,1,delim)
> 
> group = tointeger(str_get_field(lines,5,delim)
> 
> size = tofloat(str_get_field(lines,9,delim)
> 
> 
> print("Name is'" + name + "', group is " + group + ", size is " + size)
> 
> end
> 
> 
> I am not sure what print("Name is'" + name + "', group is " + group + ", 
> size is " + size)indicates... What is the difference between "Name is"' 
> + name. With an actual value put in, what should this script actually 
> look like?
> 
> Thanks so much for your help.
> 
> Sincerely,
> Grace
> 
> 
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
> 



More information about the ncl-talk mailing list