[gradsusr] GRADS txt file, substr, looping
Jeff Duda
jeffduda319 at gmail.com
Wed Sep 14 16:23:38 EDT 2016
James and Luciano,
The tough thing about the string functions in Grads is they only parse by
spaces. So if you have a csv or other delimited file with no spaces, it
suddenly takes a lot more coding work to parse lines of text. It appears
this is the case for Luciano's work.
As long as you have a constant delimiter and a pattern to follow, this can
still be done, but it will take some clever scripting.
The basic essence of reading in text data from a large file is to use a
loop and the status messages. From the documentation on the read()
function, you'll get two lines in return. The first line is an integer
status message indicating things like "ok", "error", "EOF", and the like.
The second line contains the actual data. So the template of your code will
be something like this
status = 0
while (status = 0)
raw = read(.....file......)
status = sublin(raw,1)
data = sublin(raw,2)
*....parse text data...
endwhile
If you need to store values in arrays, you can add an optional count
variable in the while loop.
As for the parsing, I've done this before, and it requires a few nested
loops. But basically, within the loop above, you'll have something like
...
...
f = 1
row = 1
check = substr(data,f,1)
while (check ~= '')
if (check = [delimiter])
row = row + 1
endif
if (row = [target_row_for_data])
*read to next delimiter and count the number of characters you had to
read to get there
actual_data = substr(data,f-(n+1),n)
endif
f = f + 1
check = substr(data,f,1)
endwhile
...
...
where above it is assumed you know which entry in each line you are trying
to read from and where n represents the character width of the target
column of data. In your example, Luciano, it appears you are trying to read
different columns depending on the row. In that case, you will have to
create a separate array indicating the column for each particular line, and
you'll definitely want to use the loop counter in that case. How you define
that will have to be up to you since I don't know what you're doing.
Hope this helps. Good luck.
Jeff Duda
On Wed, Sep 14, 2016 at 2:52 PM, James T. Potemra <jimp at hawaii.edu> wrote:
> Luciano,
>
>
> I think you need to first subset by word (using subwrd; defined by
> spaces); in this case,
>
>
> var1 = subwrd(line1,3)
>
> var = substr(var1,28,4)
>
>
> Jim
>
> On 9/14/16 7:47 AM, Luciano Ritter wrote:
>
> Hi
>
> I am trying to read a data file in .txt and extract the information that I
> need.
> I read the file trough the function 'read', but the problem is that i
> can't extract the information.
> The format of the data is:
> line1 = 83844;01/01/2000;1200;57.6;23.6;22.8;93;0;0;
> line2 = 83844;01/01/2000;1800;;23.6;22.6;91;18;6.6;
> line3 = 83844;02/01/2000;0000;;22;21.3;93;18;4.6;
> line4 = 83844;02/01/2000;1200;51.4;23.2;21.7;87;18;3.3;
>
> I need to extract only 23,6 ; 23,6 ; 22 ; 23,2 of the first, second, third
> and fourth lines. I was trying with the intrinsic function, for example to
> the first line:
>
> var = substr(line1,28,4)
>
> var = 23,6
>
> If it was only those 4 lines, i could do manually but they are 10200
> lines. So, I think i need some loop with 'while' variating the substr
> function, but i don't know how to do this.
>
> Someone can help me?
>
> Thanks a lot.
>
>
>
> _______________________________________________
> gradsusr mailing listgradsusr at gradsusr.orghttp://gradsusr.org/mailman/listinfo/gradsusr
>
>
>
> _______________________________________________
> gradsusr mailing list
> gradsusr at gradsusr.org
> http://gradsusr.org/mailman/listinfo/gradsusr
>
>
--
Jeff Duda
Post-doctoral research associate
University of Oklahoma School of Meteorology
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://gradsusr.org/pipermail/gradsusr/attachments/20160914/0c3cdcca/attachment.html
More information about the gradsusr
mailing list