[ncl-talk] file with each line having varied length

Dennis Shea shea at ucar.edu
Thu May 18 12:06:02 MDT 2017


I am sure that NCL can do this. However, the description is rather obscure.

http://www.ncl.ucar.edu/Document/Functions/string.shtml

What have you tried?

----

     s   = addfile("foo.txt",-1,"string")
     ns = dimsizes(ns)                          ; number of lines

     delim = " "     ;    "\/:;=-"  ... whatever
     do n=0,ns-1
          nfld = *str_fields_count*(s(n), delim)  ; # fields in current line
          id    = *str_get_field*(s(n), 1, delim)    ; 1st field is the ID
       do nf=2,nfld
            fld = *str_get_field*(s(n), nf, delim)
       end do
     end do

Put print statements to follow this.

Good luck

On Thu, May 18, 2017 at 10:18 AM, Zhang, Xiao <xiao.zhang at pnnl.gov> wrote:

> Dennis and Dave,
>
>
>
> Thanks for your help! The question is more complicated than just reading
> in. I’ll try to explain more clearly.
>
>
>
> For example, each line first column refers to an ID (“ID”), and the
> following are other grid cells associated with this grid (“Vari_ID”). Now I
> have a mask file and need to select all associated grid cells (Vari_ID)
> belonging to the mask file and the output file should have same format as
> input which is ID first and then associate grid cells. As the file has more
> than 4000 rows, it would be almost impossible to check the column number in
> each line and then count over. Do you know a good way to solve the problem?
>
>
>
> Best,
>
> Xiao
>
>
>
> *From:* Dennis Shea [mailto:shea at ucar.edu]
> *Sent:* Thursday, May 18, 2017 7:44 AM
> *To:* Zhang, Xiao
> *Cc:* ncl-talk at ucar.edu
> *Subject:* Re: [ncl-talk] file with each line having varied length
>
>
>
> The question leaves out many details. What is it that you want?
>
>    x   = asciiread("foo.txt",-1,"integer")  ; x will be one-dimensional
>
>    nx = dimsizes(x)
>
>    print("nx="+nx)
>
>    print("---")
>
>    print(x)
>
>
>
> On Wed, May 17, 2017 at 11:25 PM, Zhang, Xiao <xiao.zhang at pnnl.gov> wrote:
>
> Hi I have a question in processing a file. Each line of this file has
> different length. For example, first line has 5 integers and second line
> has 9. And there are over 4000 lines. Is there a good way to deal with it?
> Thanks!
>
>
> _______________________________________________
> 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/20170518/8fc16669/attachment.html 


More information about the ncl-talk mailing list