Re: Order of attributes

From: Wei Huang <huangwei_at_nyahnyahspammersnyahnyah>
Date: Tue Jun 19 2012 - 11:04:28 MDT

Daniel,

In NCL, list (for dynamically allocate memory) is used for lots of things, like attributes.

When adding attributes, a list is created, and all new attributes is added at the head of the list.
When accessing the attributes, it is always search starting from head.

So, when print the attributes, it always in the reversed order of adding.

Apply above logical to a variable copying:
New variable is created, and so a list attributes is created as well.
But the attributes was copied from a list accessing from head,
and the new list is increased by adding to the head.
At the end, new variables attributes is in the reverse order of source variable.

This is how NCL variable attributes works.

(It is easier to code, and may save some memory and may be more efficient,
while the computer is not very powerful yet.)

For this reason, NCL never assumed any order of attributes, as attributes are
mostly accessed by name (like a dictionary).

In newly developed code, we are moving to use arrays to store informations
like variable names, attributes, etc. to preserve the order of properties added/created.
But for historical reason, we have NOT done anything to change lists to
arrays yet. (Use list can preserve the orders as well, but that need little extra code,
and memory).

Hope this help,

Wei

huangwei@ucar.edu
VETS/CISL
National Center for Atmospheric Research
P.O. Box 3000 (1850 Table Mesa Dr.)
Boulder, CO 80307-3000 USA
(303) 497-8924

On Jun 19, 2012, at 8:13 AM, Mary Haley wrote:

> Hi Daniel,
>
> I will try to answer this, but Dave or Wei may want to jump in here.
>
> I believe the ordering of the attributes is the reverse of how they are added. This is sort of like "pushing them on the stack", and hence the first one you add is on the bottom of the list because the others get "pushed" on top of it.
>
> Note: this reverse ordering of attributes does not seem to apply when you write attributes to a file. I think Wei or Dave will definitely need to explain that. :-)
>
> I think for now, it's safe to assume the attributes will always be in the reverse order that you add them, although in general, it's not safe to assume any specific ordering.
>
> Is there a reason you need them to be in a specific order? Could you use "sqsort" to sort the attributes first?
>
> --Mary
>
> On Jun 19, 2012, at 3:59 AM, <Daniel.Leuenberger@meteoswiss.ch> <Daniel.Leuenberger@meteoswiss.ch> wrote:
>
>> Dear all,
>>
>> I’m using NCL 6.0.0 and came across an unexpected behavior in the ordering of attributes of a logical variable:
>>
>> res_def = True
>> res_def@a = 10
>> res_def@b = 5
>> res_def@c = 8
>>
>> print(res_def)
>>
>> res=res_def
>>
>> print(res)
>>
>> gives the following:
>>
>> Variable: res_def
>> Type: logical
>> Total Size: 4 bytes
>> 1 values
>> Number of Dimensions: 1
>> Dimensions and sizes: [1]
>> Coordinates:
>> Number Of Attributes: 3
>> c : 8
>> b : 5
>> a : 10
>> (0) True
>>
>>
>> Variable: res
>> Type: logical
>> Total Size: 4 bytes
>> 1 values
>> Number of Dimensions: 1
>> Dimensions and sizes: [1]
>> Coordinates:
>> Number Of Attributes: 3
>> a : 10
>> b : 5
>> c : 8
>> (0) True
>>
>> 1) Is there a systematic ordering of the attributes of a logical variable?
>> 2) Why is the ordering reversed after the definition?
>> 3) Why is the ordering reversed when copied to another variable?
>>
>> In my application the order of the attributes matter, therefore I need to be sure on how they are sorted.
>>
>> Thanks a lot for your clarification!
>>
>> Best regards
>> Daniel
>>
>>
>> ________________________________________
>>
>> Daniel Leuenberger
>> Numerical Prediction
>>
>> Federal Department of Home Affairs FDHA
>> Federal Office of Meteorology and Climatology MeteoSwiss
>>
>> Kraehbuehlstrasse 58, P. O. Box 514, CH-8044 Zurich
>> Phone +41 44 256 97 10
>> Fax +41 44 256 92 78
>> daniel.leuenberger@meteoswiss.ch
>> www.meteoswiss.admin.ch
>>
>> _______________________________________________
>> 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

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Tue Jun 19 11:04:47 2012

This archive was generated by hypermail 2.1.8 : Mon Jun 25 2012 - 09:57:23 MDT