Re: Lost long_names / units

From: David Brown <dbrown_at_nyahnyahspammersnyahnyah>
Date: Wed, 6 May 2009 16:38:53 -0600

Hi Mike,
Just to add a bit to what Dennis says about how to get an exact copy
of the existing dimensions, whether or not they are used by any
variable:

There are two complications, one is that if you have any scalar
variables in the file then you will get a 'pseudo' dimension in NCL
called "ncl_scalar" that does not go into the file but shows up as a
dimension name when you do 'getvardims'. If it exists in the list it
is always element 0, so that helps some. The other complication is if
one of the dimensions is 'unlimited' then you have to figure out
which it is. So here is a piece of code that will copy the dimensions
from any file (where 'a' is the input name and 'out' is the output
file name):

dims = getvardims(a)
dsizes = getfiledimsizes(a)
unlim = new(dimsizes(dims),logical)
unlim = False
if (dims(0) .eq. "ncl_scalar") then
    is = 1
else
    is = 0
end if
do i = 0,dimsizes(dims)-1
      if (isunlimited(a,dims(i))) then
           unlim(i) = True
      end if
end do
filedimdef(out, dims(is:),dsizes(is:),unlim(is:))

  -dave

On May 6, 2009, at 4:22 PM, Dennis Shea wrote:

> Mike, .... I know that Dave is looking but
> you stated in an earlier emails: "
>
> 2 things go wrong. Some units are lost (e.g. time_mgr_rst_type)
> and some long_names are lost (AGDD_PERIOD). Also,
> levsoi and string_length, 2 unused attributes, are lost.
> ========
>
>
> I ran the appended script and both "time_mgr_rst_type"
> and "AGDD_PERIOD" have the *same meta data*.
> I used ncdump -h rather than ncl_filedump to remove
> any doubt that it is related to NCL. I am not sure why you
> would be experiencing the issues you describe.
>
> As to why the dimension names/values 'levsoi' and 'string_length'
> are not written to the file, the answer is simple:
>
> out->$names(i)$ = v ; write variable values and meta data
>
> This writes out meta data associated with each *variable*.
> Since 'levsoi' and 'string_length' are *not* associated with any
> of the *variables* being written, they will not appear on the
> output netcdf file. The user must do this.
>
> Note that NCL places variables in a stack. I think it is last-in/
> first-out
> so if you want the exact order then uncomment the lines below.
>
>
> ==================== NCL SCRIPT ====================
> in =addfile("b35.012mons1.clm2.r.2193-01-01-00000.nc","r")
> names = getfilevarnames(in)
> numb = dimsizes(names)
>
> dimNames = getvardims(in) ; "in" if a *file reference* AND
> dimSizes = getfiledimsizes(in) ; it is also a *variable*
> dimUnlim = new( dimsizes(dimNames), "logical", "No_FillValue")
> dimUnlim = False
>
> system("/bin/rm -f newfile.nc") ; remove any pre-existing file
> out=addfile("newfile.nc","c")
>
> ;names = names(::-1) ; NOT needed but if exact
> order ;dimSizes = dimSizes(::-1) ; desired.
> ;dimNames = dimNames(::-1)
> do i=0,numb-1 v = in->$names(i)$
> out->$names(i)$ = v ; write variable values and meta data
> delete(v)
> end do
>
>
>
> Michael Notaro wrote:
>> Dave,
>>
>> Even with this other script, the dimensions levsoi & string_length
>> are
>> lost and some variables lose their units and long_names. Something
>> seems wrong.
>>
>> Mike
>>
>>
>> begin
>> system ("/bin/rm b35.012mons1.clm2.r.
>> 2193-01-01-00000.new.nc") ; remove if exist
>> out = addfile("b35.012mons1.clm2.r.2193-01-01-00000.new.nc","c")
>> in = addfile("b35.012mons1.clm2.r.2193-01-01-00000.nc", "r")
>> vname = getfilevarnames(in)
>>
>> do i=0,dimsizes(vname)-1
>> dims = getfilevardims(in,vname(i))
>> nrank = dimsizes(dims)
>> Vold = in->$vname(i)$ ; read each variable to memory
>> Vnew = Vold
>> copy_VarMeta(Vold,Vnew)
>> out->$vname(i)$ = Vnew
>> delete(Vnew)
>> delete(Vold)
>> delete(dims)
>> delete(nrank)
>> end do
>>
>> end
>>
>>
>>
>> On May 6, 2009, at 3:53 PM, David Brown wrote:
>>
>>
>>> Hi Mike,
>>> This doesn't sound like the correct documented behavior. Can you
>>> supply a test case NetCDF file that will demonstrate the problem
>>> using the sample code below?
>>> Thanks,
>>> -dave
>>>
>>>
>>> On May 6, 2009, at 2:15 PM, Michael Notaro wrote:
>>>
>>>
>>>> If I open a file, retrieve all the variables, and write them
>>>> to a new file, some of the variables lose their long_names and
>>>> units
>>>> in the process, mainly integer variables. Please see below.
>>>> Any idea how to maintain all the variables' info, including
>>>> long_name, dimensions, units, etc?
>>>>
>>>> Mike
>>>>
>>>> a=addfile("originalfile.nc","r")
>>>> .....
>>>> ......
>>>> out=addfile("newfile.nc","c")
>>>> do i=0,numb-1
>>>> v = a->$names(i)$
>>>> out->$names(i)$ = v
>>>> delete(v)
>>>> end do
>>>>
>>>> _______________________________________________
>>>> ncl-talk mailing list
>>>> List instructions, subscriber options, unsubscribe:
>>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>
>
>
> --
> ======================================================
> Dennis J. Shea tel: 303-497-1361 |
> P.O. Box 3000 fax: 303-497-1333 |
> Climate Analysis Section |
> Climate & Global Dynamics Div. |
> National Center for Atmospheric Research |
> Boulder, CO 80307 |
> USA email: shea 'at' ucar.edu |
> ======================================================
>

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Wed May 06 2009 - 16:38:53 MDT

This archive was generated by hypermail 2.2.0 : Wed May 06 2009 - 16:48:59 MDT