[ncl-talk] CSV file script

Grace Choi gracesh27 at gmail.com
Tue Oct 24 05:42:18 MDT 2017


Thank you so much for your explanations. It makes so much sense now.

Sincerely,
Grace

On Mon, Oct 23, 2017 at 6:45 PM, Kevin Hallock <hallock at ucar.edu> wrote:

> Hi Grace,
>
> It looks like you’re trying to run the example script csv_2.ncl
> <http://www.ncl.ucar.edu/Applications/Scripts/csv_2.ncl> from the NCL
> website. Here’s a link <http://ncl.ucar.edu/Applications/Data/> to all of
> the sample data files we host; specifically, you may want to download
> example2.csv <http://ncl.ucar.edu/Applications/Data/asc/example2.csv> if
> you don’t already have it.
>
> A quick issue I’d like to mention: this example seems to be using “group”
> as a variable name, but recent versions of NCL treat “group” as a keyword
> (meaning it cannot be used as a variable). I will try to update this
> example on our website in the near future. For now, please use “grp”
> instead of “group” as needed.
>
> In this example, a CSV file “example2.csv” is read into NCL using
> asciiread(). Then, the function str_get_field() is used repeatedly to
> store the 1st, 5th, and 9th values of each row into the variables “name”,
> “grp”, and “size”, respectively. If you were to add a “print(name)” line
> after “name = str_get_field(lines,1,delim)”, you should see output like
> this:
> > print(name)
>
> Variable: name
> Type: string
> Total Size: 56 bytes
>             7 values
> Number of Dimensions: 1
> Dimensions and sizes:   [7]
> Coordinates:
> (0)     psmith01
> (1)     smehta
> (2)     mrsjohns
> (3)     psmith02
> (4)     scohen
> (5)     swright
> (6)     amarkov
>
> Similarly, printing the other variables, you would see:
> > print(grp)
>
> Variable: grp
> Type: integer
> Total Size: 28 bytes
>             7 values
> Number of Dimensions: 1
> Dimensions and sizes:   [7]
> Coordinates:
> (0)     1
> (1)     1
> (2)     -1
> (3)     0
> (4)     2
> (5)     1
> (6)     3
>
> and
> > print(size)
>
> Variable: size
> Type: float
> Total Size: 28 bytes
>             7 values
> Number of Dimensions: 1
> Dimensions and sizes:   [7]
> Coordinates:
> (0)      1
> (1)     2.1
> (2)      2
> (3)     10
> (4)      1
> (5)      1
> (6)      1
>
> NCL is able to concatenate strings with other data types, which is what we
> see in the "print("Name is'" + name + "', group is " + grp + ", size is "
> + size)” statement. What is actually happening under the hood is
> something like this:
>            / psmith01 \               / 1 \              /  1  \
>           // smehta   \\             // 1 \\            // 2.1 \\
>          /// mrsjohns \\\           ///-1 \\\          ///  2  \\\
> "Name is -   psmith02  - , group is     0    , size is     10     "
>          \\\ scohen   ///           \\\ 2 ///          \\\  1  ///
>           \\ swright  //             \\ 1 //            \\  1  //
>            \ amarkov  /               \ 3 /              \  1  /
>
> or in NCL:
> print("Name is'" + name + "', group is " + grp + ", size is " + size)
> (0)     Name is'psmith01', group is 1, size is 1
> (1)     Name is'smehta', group is 1, size is 2.1
> (2)     Name is'mrsjohns', group is -1, size is 2
> (3)     Name is'psmith02', group is 0, size is 10
> (4)     Name is'scohen', group is 2, size is 1
> (5)     Name is'swright', group is 1, size is 1
> (6)     Name is'amarkov', group is 3, size is 1
>
>
> I hope this helps,
> Kevin
>
> On Oct 23, 2017, at 4:14 PM, Grace Choi <gracesh27 at gmail.com> 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
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20171024/7cd3ea91/attachment.html>


More information about the ncl-talk mailing list