Re: How to get the exact data type from a known file? "new "function problem

From: Mary Haley <haley_at_nyahnyahspammersnyahnyah>
Date: Fri, 10 Jul 2009 08:17:16 -0600 (MDT)

On Thu, 9 Jul 2009, Yan Bao wrote:

> Hi, Mary Thanks for help. Also for this script (see attachment), I have
> another problem. I want to do looping to calculate all the variables I need
> in this directory, but the system cannot recognize the variables when read
> them. I got the error message like :file (a) isn't defined or variable vv
> is not a variable in the file". It sounds like I made another mistake for
> giving the correct type
> for variable (for define "vv" I have tried stringtocharacter or chartostring
> function, see the commenced place, all the way I have tried), any
> suggestions ?
>
> Thanks,
> Yan

Yan,

In order to do something like:

     v = a->vv

you must have a variable called "vv" on the file pointed to by "a".

To see what variables you have in this file, add:

    print(a)

after the "addfile" call. This will list all the variables plus a
bunch of other information about the file (global attributes,
dimension names, attributes, etc).

You can also just list the variable names and nothing else:

    varnames = getfilevarnames(a)
    print(varnames)

Look at this list closely to see what the variable names are, and then
use the appropriate one in place of "vv".

--Mary

>
>
> 2009/7/8 Mary Haley <haley_at_ucar.edu>
>
>>
>> On Wed, 8 Jul 2009, Yan Bao wrote:
>>
>> Hi,Every one
>>> I'd like to creat a file namelist in my directory, but I don't know what
>>> the type of the file name namelist, I have tried 'character','string',
>>> ' byte',
>>> all of them don't work. I got the error message like:
>>> fatal:Assignment type mismatch, right hand side can't be coerced to type
>>> of
>>> left hand side
>>> fatal:Execute: Error occurred at or near line 26 in file ght.time.ncl
>>>
>>> I want to use the type attribute from file_list1, I think they are the
>>> same,
>>> how can I do that?
>>> Thanks.
>>>
>>
>> Yan,
>>
>> You want to use "string":
>>
>> However, you don't need to do "new" on "file_list3", and
>> you don't want it inside the do loop.
>>
>> Use NCL's array syntax to create your array of strings in one call:
>>
>> file_list3 = "ght_A6."+year+"010100-"+year+"123123.nc"
>>
>> "year" is an array of integers, so "file_list3" will be equal to:
>>
>> "ght_A6.1984010100-1984123123.nc"
>> "ght_A6.1985010100-1985123123.nc"
>> "ght_A6.1986010100-1986123123.nc"
>> ...
>> "ght_A6.2069010100-2069123123.nc"
>>
>> Finally, you have:
>>
>> year = new(nfiles,integer)
>> year
>> =(/1984,1985,1986,1987,1988,1989,1990,1991,1992,1993,1994,2060,2061,2062,2063,2064,2065,2066,2067,2068,2069/)
>>
>> Again, it is not necessary to do a "new" first, so remove the "year
>> new(nfiles,interger)" line.
>>
>> --Mary
>>
>>
>> Script:
>>>
>>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
>>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
>>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
>>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"
>>> begin
>>> file_home ="./"
>>> file_list1 = systemfunc("cd " + file_home+ " ; ls ta_A6.*nc ;")
>>> print(file_list1)
>>> file_list2 = systemfunc("cd " + file_home+ " ; ls hus_A6.*nc ;")
>>> print(file_list2)
>>> nfiles = dimsizes(file_list1)
>>> year = new(nfiles,integer)
>>> year
>>>
>>> =(/1984,1985,1986,1987,1988,1989,1990,1991,1992,1993,1994,2060,2061,2062,2063,2064,2065,2066,2067,2068,2069/)
>>>
>>> file_list3 =new(nfiles,byte)
>>> nf = nfiles-1
>>> do fil = 0, nf ; files numbers !from 1985
>>> a =addfile(file_home+file_list1(fil),"r")
>>> b =addfile(file_home+file_list2(fil),"r")
>>> file_list3(fil) ="ght_A6."+year+"010100-"+year+"123123.nc"
>>> print(file_list3(fil))
>>> end do
>>> end
>>>
>>>
>
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Fri Jul 10 2009 - 08:17:16 MDT

This archive was generated by hypermail 2.2.0 : Mon Jul 13 2009 - 20:56:19 MDT