[ncl-talk] Loop creating different variable names

Dennis Shea shea at ucar.edu
Tue Mar 27 16:38:12 MDT 2018


NCL variables are based upon the netCDF variable data structure. Generally,
NCL variables adhere to netCDF rules regarding attributes and coordinate
variables.

By netCDF rule, an attribute can only be assigned to a **pre-existing**
variable. You have:

*Ts* = True     ; variable Ts
ens=(/"001","011","012"/)
e  = 0

Later, you use *Ts_*  as a variable name. There is no pre-existing variable
*Ts_* . Hence,

*Ts_*@$ens(e)$ = fc->TS

will fail with an error message saying the *Ts_* does not exist.
This has nothing to do with the attribute assignment per se.

===

You can do the following:

*Ts*_ = True     ; variable Ts_

Since *Ts_* exists

*Ts_*@$ens(e)$ = fc->TS

However, *by netCDF rules* which NCL follows

[1] A variable's attribute can not have its own attributes.  'Attribute
cascade' (ie: attributes of attributes) is not allowed.
[2] A variable's attribute can not have coordinate variables associated
with it.
[3] netCDF allows  only one-dimensional arrays for attributes.

Presumably, the 'TS" variable being imported has named dimensions,
coordinates, attributes and is multidimensional.

You hit the 'jack-pot'! The simple import statement you used has violated
multiple netCDF (hence, NCL) rules. :-)    :-)    :-)

Likely, NCL did not know what to do! My speculation, is that you would get
a variable *T*s_ size [1] of type logical with a *one-dimensional* array of
attributes with no attribute name!!

====
For a simple illustration:


  X =True ; create variable

  foo = "FOO"        ; string
  X@$foo$ = (/2, -5, 3/)

  foo2= "FOO2"
  X at foo2 = (/ (/10,20/), (/30,40/) /)    ; 2x2 array

  printVarSummary(X)

====
Variable: X
Type: logical
Total Size: 4 bytes
            1 values
Number of Dimensions: 1
Dimensions and sizes:    [1]
Coordinates:
Number Of Attributes: 2
  foo2 :    ( 10, 20, 30, 40 )      ; note one-dimensional
  FOO :    ( 2, -5, 3 )
=====

OK, What to do?

I would suggest:

https://www.ncl.ucar.edu/Document/Functions/Built-in/addfiles.shtml

[ncl_join | 5] x [time | ??] x [lev | ?] x [lat | ?] x [lon | ?]

You can change the 0-th name

  TS!0 = "ensemble"

[ensemble | 5] x [time | ??] x [lev | ?] x [lat | ?] x [lon | ?]

This allows for explicit acces to each or all ensemble members.

===

The key is accessing ensemble members in different directories.

https://www.ncl.ucar.edu/Document/Functions/Built-in/systemfunc.shtml

Think about it.

Good Luck



On Tue, Mar 27, 2018 at 9:59 AM, Anne <anne.seidenglanz at unive.it> wrote:

> Hi Kevin,
>
> I just wanted to be a little clearer on what I meant in my last email; I
> created a loop in which I loop over 3 ensemble members, and using the
> approach to define the single members as attributes of 'Ts' (last
> conversation):
> [Note here 'year' and 'Year' are defined elsewhere in the script]
>
> *ens=(/"001","011","012"/)*
> *Ts = True*
>
>
> *do e=0,dimsizes(ens)-1*
>
> *  ctrl_dir =
> "/work/as10017/SPS_hindcast_1993_2016/Ini_11_cam/daily_surface/sps_"+year+"11_"+ens(e)+"/"*
> *  ctrl_fil =
> "CMCC_sps_201111_"+ens(e)+".atm.h3."+year+"11-"+Year+"04.reg.nc
> <http://04.reg.nc>"*
> *  path=ctrl_dir+ctrl_fil*
> *  fc=addfile(ctrl_dir+ctrl_fil,"r")*
> *  Ts_@$ens(e)$ = fc->TS*
> *  delete(fc)*
> * end do*
>
> However, NCL doesn't seem to recognize the @ syntax, because it gives me
> the following error message:
>
> *fatal:Variable (Ts_) is undefined, can not assign attribute (001)*
>
> So somehow 001, 011, 012 are not being recognized. Is it because they are
> numbers instead if strings?
>
> Any help appreciated!
> thanks,
> Anne
>
>
> On Tue, Mar 27, 2018 at 4:49 AM, Anne <anne.seidenglanz at unive.it> wrote:
>
>> Hi Kevin,
>>
>> thanks for your advice, I have implemented what you wrote above just for
>> another case this time (ensemble members instead of months), however, when
>> doing
>>
>> printVarSummary(Ts at 001)
>>
>> NCL complains about the @ sign, and it doesn't proceed. Do you have any
>> idea why?
>>
>> Thanks,
>> Anne
>>
>> On Tue, Feb 27, 2018 at 4:42 PM, Kevin Hallock <hallock at ucar.edu> wrote:
>>
>>> Hi Anne,
>>>
>>> Unfortunately, NCL’s “$var$” notation is only valid when used to
>>> reference a variable on a file (“file->$vars(n)$”) or attributes/dimensions
>>> on a variable (“var@$atts(n)$”) and cannot be used as part of a
>>> variable’s name.
>>>
>>> I think the idea you found about treating months as attributes of
>>> another variable might be the easiest way to proceed:
>>> month = (/"nov", "dec", "jan”/)
>>> var_nsidc = True
>>> do m=0,dimsizes(month)-1
>>>   var_nsidc@$month(m)$ = var_dtrend_nsidc(0::12,:,:)
>>> end do
>>>
>>> print(var_nsidc at nov)
>>> print(var_nsidc at dec)
>>>
>>> I hope this helps,
>>> Kevin
>>>
>>> On Feb 27, 2018, at 4:18 AM, Anne <anne.seidenglanz at unive.it> wrote:
>>>
>>> Hello,
>>>
>>> I am trying to loop through a number of months (like *month = (/"nov",
>>> "dec", "jan"/)*) to compute some climatologies (I know there are
>>> functions but I need to do it manually here). Because I am reading in and
>>> plotting 4 different data sets I am looking for way to loop over the
>>> months, *in order to become part of the new variable name*, so as to
>>> make the script shorter and better readable.
>>> My "$" syntax doesn't seem to work here (see below). There was an ncl
>>> talk a few years ago saying that one needs to treat the months as
>>> attributes of another variable in order to be able to use the "$" syntax,
>>> is that still the case?
>>>
>>> month = (/"nov", "dec", "jan"/)
>>>
>>>  do m=0,dimsizes(month)-1
>>>    var_$month(m)$_nsidc = var_dtrend_nsidc(0::12,:,:)
>>>  end do
>>>
>>> *I want to end up with:*
>>> var_nov_nsidc
>>> var_dec_nsidc
>>> var_jan_nsidc
>>>
>>>
>>> What am I doing wrong here?
>>>
>>> Any help appreciated,
>>> thanks
>>> Anne
>>>
>>>
>>>
>>>
>>>
>>>
>>> Nota automatica aggiunta dal sistema di posta.
>>> _______________________________________________
>>> ncl-talk mailing list
>>> ncl-talk at ucar.edu
>>> List instructions, subscriber options, unsubscribe:
>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>
>>>
>>>
>>
>
> Nota automatica aggiunta dal sistema di posta.
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20180327/a2c0a9cb/attachment.html>


More information about the ncl-talk mailing list