[ncl-talk] help loop

Mary Haley haley at ucar.edu
Tue Jan 16 12:58:00 MST 2018


Vanúcia,

You didn't say exactly what the problem was, but write_matrix doesn't
append data. So, if you call it repeatedly with the same file, it's just
going to overwrite that file with the most recent data you give it.

In the "k" do loop, you are setting the output file name based on the
counter from the "i" do loop. This means that the file name is the same for
every iteration of k, and hence will get overwritten each time.

I suggest looking at write_table instead, which allows you to append data.

http://www.ncl.ucar.edu/Document/Functions/Built-in/write_table.shtml

There are some examples at:

http://www.ncl.ucar.edu/Applications/write_ascii.shtml

In particular, look at write_ascii_4.ncl.

--Mary


On Tue, Jan 16, 2018 at 6:52 AM, Vanúcia Schumacher via ncl-talk <
ncl-talk at ucar.edu> wrote:

> Hi NCLs
>
>
> I would like to create an array of 36 x 12 (time x months) for each lat
> and lon values from file (91 values), generating 91 separate files.
> I tried the script below, but I am not able to generate a loop for what I
> want to get, if anyone can give a help I appreciate.
>
>
>  lats  = asciiread("lat.txt",(/91,3/),"float")
>  lons = asciiread("lon..txt",(/91,3/),"float")
>  lat    = lats(:,1)
>  lon   = lons(:,2)
>
> nlat   = dimsizes(lat)
> nlon  = dimsizes(lon)
>
>
>  list = systemfunc("ls /cru.*.nc")  ; are 12 files separated by months
>
>
> var_matriz = new( (/36,dimsizes(list)/), float )
>       do j = 0,dimsizes(list)-1
>
> do i=0,nlat(0)-1
> do k=0,nlon(0)-1
>
>        a = addfile(list(j), "r")
>        var_matriz(:,j) = a->pr(:,i,k)
>
>     arq=True
>    arq at fout="ex."+i+".txt"
>    write_matrix( var_matriz, dimsizes(list)+"f10.2", arq )
>
> ------------------------------
> *De:* Vanúcia Schumacher <vanucia-schumacher at hotmail.com>
> *Enviado:* terça-feira, 16 de janeiro de 2018 11:11:09
> *Para:* ncl-talk at ucar.edu
> *Assunto:* help loop
>
>
> Hi NCLs
>
>
> I would like to create an array of 36 x 12 (time x months), but I can only
> generate one file at a time. Also calculate a file (36 x 12) for each lat
> and lon values from another file (91 values).
> I tried the script below, but I am not able to generate a loop for what I
> want to get, if anyone can give a help I appreciate.
>
>
>  lats  = asciiread("lat.txt",(/91,3/),"float")
>  lons = asciiread("lon..txt",(/91,3/),"float")
>  lat    = lats(:,1)
>  lon    = lons(:,2)
>
>
>  f = systemfunc("ls /cru.*.nc")  ; are 12 files separated by months
>
>  N = dimsizes( f )
>
>  do i = 0,N-1
> a = addfiles(f(i), "r")
> var = a[:]->pr(:,{lat},{lon})
>  asciiwrite ("prec.cru."+i+".txt", sprintf("%9.2f", var))
>  end do
>
>
>
>
> _______________________________________________
> 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/20180116/8892b111/attachment.html>


More information about the ncl-talk mailing list