[ncl-talk] Fwd: Adding additional dimensions to an existing variable

Dennis Shea shea at ucar.edu
Wed Jun 10 10:43:26 MDT 2015

```I would suggest that you do Alan has written. It is very efficient and no
separate array need be created.

In 'netCDF land', you will rarely find anything like what you specified.
'time' is 99.99% of the time ... one dimensional.

Please read the following:

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

============
Further. there is an issue with your: x(year,month,day,lat,lon) approach

Unless the calendar is a 360-day  .... The 'day' dimension can be 28,29,30
31 ... You must have it as a fixed 31.
The original 'time' has no 'space holders'. Hence, you can *not* directly
'reshape' the array via NCL functions.

A brute force approach might be (untested) ...
dimx   = dimsizes(x)
ntim    = dimx(0)
nlat     = dimx(1)
mlon   = dimx(2)

tinfo    = cd_calendar(time, 0)       ; read about this    (ntim,6)  =>
yyyy,mm,dd,hh,mn,sc
NDAY = 31                                    ; max size
nmos  = 12
yrStrt  = toint( tinfo(0,0) )
yrLast = toint( tinfo( ntim-1,0) )
nyrs    = yrLast-yrStrt+1    ; (last year) - (first year) +1
xnew  = new( (/nyrs,nmos,NDAY,nlat,mlon/), typeof(x),
getVarFillValue(x))

then fill xnew via a do loop

do nt=0,ntim-1
nyr   = toint( tinfo(nt,0) ) - yrStrt
nmo = toint( tinfo(nt,1) ) - 1         ; NCL indexing starts at 0
ndy  = toint( tinfo(nt,2) ) - 1
xnew(nyr,nmo,ndy,:,:) =  (/ x(nt,:,:) /)   ; read about (/.../)
... no meta data transfer
end do

This is cumbersome. The suggested approach is more efficient and 'elegant'

Good luck

On Wed, Jun 10, 2015 at 10:08 AM, Alan Brammer <abrammer at albany.edu> wrote:

> The easiest way to do this is using the time functions and indexing.
>
> ymdhms = cd_calendar(x, 0) ;
>
> dec_ind = ind(ymdhms(:,1) .eq. 12) ;; find locations where month .eq. 12.
>
> x_dec = x(dec_ind,:,:)
>
> Adding another 2 dimensions, doesn't seem necessary. You can stack the
> ind() function to be as specific or as broad as you like very easily. Will
> be more flexible than multiple time dimension.
>
>
> ​Alan. ​
>
>
>
> On 10 June 2015 at 11:58, Zachary Suriano <zsuriano at udel.edu> wrote:
>
>>
>> Ultimately I would like to pull out data from each month individually
>> with a statement such as: dec=x( : , 12 , : , : , : ). With leap days
>> present within the dataset, I cannot figure out a way to achieve this
>> without having year, month, day being their own dimensions.
>>
>
>
>
> _______________________________________________
> 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/20150610/87f2c527/attachment.html
```

More information about the ncl-talk mailing list