[ncl-talk] Fwd: appending columns of a 2d array to a list

David Brown dbrown at ucar.edu
Tue Aug 22 15:56:18 MDT 2017


Hi Dave,

Lists in NCL are still a bit of a work in progress. One thing to
realize is that when you add a variable to a list, the variable itself
is put on the list, not just a copy of it. However, there is the
concept of a "value-only" assignment that I think would work for you.
The value-only operator is the NCL parentheses: (/  /).

Here's a simple example that adds multiple elements of an array to a list:

x =  fspan(0,100,2000)
x1 = onedtond(x,(/20,100/))
alist = NewList("fifo")
do i = 0, 19
  ListAppend(alist,(/x1(i,:)/))     ; use value-only operator
end do


This creates 20 unnamed list items that are each 1D arrays of 100 elements.

I see you are using NCL 6.1.2. I would suggest you upgrade to the
latest version 6.4.0. This feature may not work reliably in earlier
versions.
 -dave



On Fri, Aug 18, 2017 at 12:14 PM, David Gill <gill at ucar.edu> wrote:
>
> Subject: appending columns of a 2d array to a list
> Folks,
> I am really new to NCL and am using NCL because that is what the original
> processing script uses.
>
> I am building a table that has columns that have the same type of data
> within the column. Each row of the table represents a different US county.
> The columns have information such as the state, the county name, latitude,
> longitude. So, within the column the data is the same type, and along a row
> the data type changes.I have no troubles manually constructing this table
> for a few.
>
> Where things are not working for me is that I want to add a few hundred more
> columns of data. I have this data inside a 2d array. I know this 2d data is
> OK, because I can print it and manipulate it. I am not able to column-wise
> append the data to the existing part of my list.
>
> The start of my list works fine (a combination of integers, strings, and
> floats):
>
>       ;**********************************************
>       ; Fill in all of the columns (location + lots of days), for each row
> (list of counties)
>       ;**********************************************
>       nf_each_county = new(dimsizes(states),integer)
>       nf_each_county = nf
>       alist1  = NewList("fifo")
>       ListAppend(alist1,nf_each_county)
>       ListAppend(alist1,states)
>       ListAppend(alist1,county)
>       ListAppend(alist1,lat0)
>       ListAppend(alist1,lon0)
>
> The problem is trying to get the columns of the 2d array appended to the
> list. I have tried everything I can think of.
>
> This does not work, jamming in a full 2d array.
>
>              ListAppend(alist1,x0(:,:))
>
> This does not work, trying to use each column via an index:
>
>       i = 0
>       do while(i.lt.nDays)
>          ListAppend(alist1,x0(i,:))
>           i=i+1
>       end do
>
> This does not work, trying to use a temporary 1d array (I put in the delete
> because of complaints that I was trying to overwrite):
>
>       i = 0
>       do while(i.lt.nDays)
>          xtemp = x0(i,:)
>          ListAppend(alist1,xtemp)
>          delete(xtemp)
>           i=i+1
>       end do
>
> Unfortunately, the only thing that I have found that does work is to append
> each column separately, where a new 1d array is created for each column.
> This works when no deleting is going on.
>
>       xtemp0 = x0(0,:)
>       ListAppend(alist1,xtemp0)
>       xtemp1 = x0(1,:)
>       ListAppend(alist1,xtemp1)
>       xtemp2= x0(2,:)
>       ListAppend(alist1,xtemp2)
>
> As a hack, I can do this, but for several hundred extra coluns, this
> requires writing a script to write NCL code. The code is now quite brittle
> and not able to be generalized because of this set of assignments.
>
>
> I also tried another method, using a list constructor.
>
> This does not work:
>
>        alist1  = [/nf_each_county, states, county, lat0, lon0,\
>               x0(:,:)/]
>
> As above, if I explicitly list out the columns (from the 2d array), then
> everything is OK:
>
>        alist1  = [/nf_each_county, states, county, lat0, lon0,\
>                x0(0,:),x0(1,:),x0(2,:)/]
>
> I would sure appreciate any advice - again, a real noob. Using version
> 6.1.2.
>
> Dave
>
>
>
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>


More information about the ncl-talk mailing list