[ncl-talk] Help: how to just read out one line from a big ascii file?
Alan Brammer
abrammer at albany.edu
Wed Mar 1 15:17:42 MST 2017
Similar to Mary's response I've had success using grep when targeting lines
with specific data.
e.g. India in your example
datastring = systemfunc("grep India asc4.txt")
This works well for ascii files with 10k+ lines in them, grep is much
faster at finding a specific line(s) than parsing all the data in NCL.
~Alan
On Wed, Mar 1, 2017 at 2:36 PM, Mary Haley <haley at ucar.edu> wrote:
> Unfortunately you can't use NCL functions to just read parts of an ASCII
> file.
>
> However, you could use the UNIX "cut" command to select out the
> subsections you want. See this page for some examples:
>
> http://www.ncl.ucar.edu/Applications/read_ascii.shtml
>
> Use your browser search to look for the word "cut". You will find several
> references.
>
> Good luck,
>
> --Mary
>
>
> On Tue, Feb 28, 2017 at 4:58 AM, Guido Cioni <guidocioni at gmail.com> wrote:
>
>> Just to be correct about this, but someone could step in if I'm saying
>> stupid things, you HAVE to load the file into the memory (asciifile) and
>> then subset with data(5:10), for example.
>> If you don't want to load the entire file just cut it before reading it
>> into NCL.
>>
>> Guido Cioni
>> http://guidocioni.altervista.org
>>
>> On 28 Feb 2017, at 12:56, Guido Cioni <guidocioni at gmail.com> wrote:
>>
>> 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) "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/Appli
>> cations/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/A
>> pplications/read_ascii.shtml, there are a lot of examples.
>>
>> Cheers
>>
>>
>> Guido Cioni
>> http://guidocioni.altervista.org
>>
>> On 28 Feb 2017, at 12:29, Xiaofeng Li <Xiaofeng.Li at newcastle.ac.uk>
>> wrote:
>>
>> <asc4.txt>
>>
>>
>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> ncl-talk at ucar.edu
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>
>>
>
> _______________________________________________
> 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/20170301/a8c4b569/attachment.html
More information about the ncl-talk
mailing list