Re: accessing netcdf file

From: Andrew Mai <mai_at_nyahnyahspammersnyahnyah>
Date: Thu May 09 2013 - 03:28:37 MDT
On 05/09/2013 01:24 AM, Nikola Marjanovic wrote:

Is it possible to create an array of netCDF variables of the same dimension so they can easily be accessed by the same line of code?

For example I have a netCDF file: 
a=addfile(file,"r")

I want to access variables U, V and W in the netCDF file but don't want to write out a command for each variable:
U=a->U(i:j,k:l,p:q,r:s)

Can I create some sort of array?: X=(/"U","V","W"/) 

and access each variable in a loop:
do t=0,2
  U=X{t}(i:j,k:l,p:q,r:s)
  [....do something..]
  delete(U)
end do

Here is an old piece of code that I have kept over the years. I believe this approximates what you want to do.

a = addfile(flnm,"r")
vars = getfilevarnames(a)     ; gets a list of variables in flnm; could be replaced by a list like you have
do i = 0, dimsizes(vars)-1
    if (vars(i) .eq. "stream-heating")
	psi750 = a->$vars(i)$(0,{750},:,:)
    end if
end do

The key point seems to be that your "X{t}" needs to be "$X(t)$".

Andy

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Thu May 9 03:28:47 2013

This archive was generated by hypermail 2.1.8 : Wed May 15 2013 - 10:19:28 MDT