[ncl-talk] Help: how to just read out one line from a big ascii file?
Guido Cioni
guidocioni at gmail.com
Tue Feb 28 04:56:39 MST 2017
Hey Xiaofeng,
First, I would suggest you to remove all the rows that are not supposed to be read by NCL (i.e. the header) beforehand, otherwise you'll get into the trouble. If you tell NCL to expect 2 columns of integers when there is 1 of string...you imagine what could result of this :) You can skip the header somehow, but to debug the code it's easier to have fewer complications.
Second, I'm pretty sure that the asciiread procedure (which, I believe, you're using) does not work well with strings AND numeric values on the same row. If you read from the documentation (http://www.ncl.ucar.edu/Document/Functions/Built-in/asciiread.shtml <http://www.ncl.ucar.edu/Document/Functions/Built-in/asciiread.shtml>) "For string data, each line of the file is read as a string"
So the line begins as a string it would read it as a string. In order to read strings and values you have to read everything as string and then perform a conversion, as explained here (http://www.ncl.ucar.edu/Applications/read_ascii.shtml#pw <http://www.ncl.ucar.edu/Applications/read_ascii.shtml#pw>, see Example for p <http://www.ncl.ucar.edu/Applications/Data/asc/istasyontablosu_son.txt>w.dat file, which is really similar to what you have).
So I would modify your ascii file as follows (you can compute totals afterwards, that's what NCL is meant for):
China 1,321 19.84%
India 1,132 16.96%
United States 304 4.56%
Indonesia 232 3.47%
Brazil 187 2.80%
Pakistan 163 2.44%
Bangladesh 159 2.38%
Nigeria 148 2.22%
Russia 142 2.13%
Japan 128 1.92%
Mexico 107 1.60%
Philippines 89 1.33%
Vietnam 84 1.31%
Germany 82 1.23%
Egypt 81 1.13%
and read it as (NOT TESTED):
fname = "asc4.txt"
data = asciiread <http://www.ncl.ucar.edu/Document/Functions/Built-in/asciiread.shtml>(fname,-1,"string")
population = stringtofloat <http://www.ncl.ucar.edu/Document/Functions/Built-in/stringtofloat.shtml>(str_get_field <http://www.ncl.ucar.edu/Document/Functions/Built-in/str_get_field.shtml>(data, 2," "))
precentage = stringtofloat <http://www.ncl.ucar.edu/Document/Functions/Built-in/stringtofloat.shtml>(str_get_field <http://www.ncl.ucar.edu/Document/Functions/Built-in/str_get_field.shtml>(data, 3," "))
country = str_get_field <http://www.ncl.ucar.edu/Document/Functions/Built-in/str_get_field.shtml>(data, 1," ")
Carefully read the page http://www.ncl.ucar.edu/Applications/read_ascii.shtml <http://www.ncl.ucar.edu/Applications/read_ascii.shtml>, there are a lot of examples.
Cheers
Guido Cioni
http://guidocioni.altervista <http://guidocioni.altervista/>.org
> On 28 Feb 2017, at 12:29, Xiaofeng Li <Xiaofeng.Li at newcastle.ac.uk> wrote:
>
> <asc4.txt>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20170228/0d331fd1/attachment.html
More information about the ncl-talk
mailing list