[ncl-talk] Conform a multi-dimensional array to another multi-dimensional array

Dave Allured - NOAA Affiliate dave.allured at noaa.gov
Mon Apr 24 07:54:08 MDT 2023


Giorgio, here is a compact version of Dennis's method.  This preserves
attributes, coordinates, and the fill value.  Assumptions are that dim 1
increases by 1, dim 2 is the same, and the new row is added in the last
position.  Originally you said "easy", so here you go.  Untested:

nrows = dimsizes (a, 0)
c = new (dimsizes (a), typeof (b), getFillValue (b))
c(0:nrows-2,:) = b(:,:)


On Mon, Apr 24, 2023 at 6:09 AM Giorgio Graffino via ncl-talk <
ncl-talk at mailman.ucar.edu> wrote:

> Hi Dennis,
>
> Thanks for the tip, but I prefer to not create the variable beforehand. I
> used the brute-force approch on the table_attach_rows function to read a
> one-dimensional array and append it to a two-dimensional array in a
> specific index. Probably it won't work with patterns becasue I removed the
> metadata part.
>
> undef("table_attach_singlerow")
> function table_attach_singlerow (t1[*][*], t2[*], nind:integer)
> local dim_t1, dim_t2, ncol1, ncol2, nrow1,    \
>      dimNames_t1, dimNames_t2, dimNames, n
>
> ; ******************************************************************
> ; Attaches/appends additional 1-D row.
> ; Adapted from the function table_attach_rows
> ; (
> https://github.com/NCAR/ncl/blob/develop/ni/src/examples/gsun/contributed.ncl
> ).
> ; ******************************************************************
>
> begin
>                                   ; get array shape/sizes
>  dim_t1  = dimsizes(t1)
>  dim_t2  = dimsizes(t2)
>
>  ncol1   = dim_t1(1)
>  ncol2   = dim_t2(0)
>
> ;  if (ncol1.ne.ncol2) then
> ;      print ("table_attach_rows: tables must have same number of columns")
> ;      print ("                    ncol1="+ncol1)
>
> ;      print ("                    ncol2="+ncol2)
>
> ;      exit
> ;  end if
>
>  if (typeof(t1).ne.typeof(t2)) then
>      print ("table_attach_rows: arrays must be of the same type")
>      print ("                    typeof(t1)="+typeof(t1))
>      print ("                    typeof(t2)="+typeof(t2))
>      exit
>  end if
>                                   ; allocate space for new array
>  nrow1       = dim_t1(0)
>
>                                   ; chk _FillValue stuff
>  if (isatt(t1,"_FillValue") ) then
>      tNew = new ( (/nrow1+1, ncol1/), typeof(t1), t1 at _FillValue)
>  else if (isatt(t2,"_FillValue") ) then
>           tNew = new ( (/nrow1+1, ncol1/), typeof(t2), t2 at _FillValue)
>       else
>           tNew = new ( (/nrow1+1, ncol1/), typeof(t2), "No_FillValue")
>       end if
>  end if
>
>                                 ; insert values
>  tNew(0:nind-1,:)  = (/ t1(0:nind-1,:) /)
>  tNew(nind    ,:)    = (/ t2 /)
>  tNew(nind+1:,:)   = (/ t1(nind:,:) /)
>  return (tNew)
> end
>
> Cheers,
>
> Giorgio
>
>
> ------ Messaggio Originale ------
> Da: shea at ucar.edu
> A: g.graffino at tim.it Cc: ncl-talk at ucar.edu
> Inviato: venerdì 21 aprile 2023 19:35
> Oggetto: Re: [ncl-talk] Conform a multi-dimensional array to another
> multi-dimensional array
> Maybe this is a start:
>
> =================
>
> a(12,100),  b(11,100)     ? meta data
>
> =================
>   dima = dimsizes(a)
>   dimb = dimsizes(b)
>
>   na0   = dima(0)
>   nb0   = dimb(0)
>
>   c       = new(dima, typeof(a))
>   c(0:nb0-1) = (/  b /)    ;   (/ ... /) means no meta data
>
> ; meta data
>
>   copy_VatAtts(a,c)
>
> ; If necessary, coordinate info (named dimensions and coordinate values)
> will hav e to be added
>
>
> On Fri, Apr 21, 2023 at 6:21 AM Giorgio Graffino via ncl-talk <
> ncl-talk at mailman.ucar.edu> wrote:
>
>> Dear NCL folks,
>>
>> I have two multi-dimensional arrays: array a is 12x100 and array b is
>> 11x100. I want to conform them to make their dimensions the same. I'm sure
>> there is an easy way to do it, but so far I failed all attempts.
>>
>> For now, I mainly tried to add a row of missing values to array b to get
>> an array c with dimensions 12x100 (the same of array a). Here is a list of
>> the things I tried with their error message.
>>
>> c = conform(a,b,1)                ; fatal:conform: The array to be
>> conformed must have the same number of dimensions as indicated by the
>> length of the last argument
>>
>> c = (/b,new(dimsizes(b(0,:)),typeof(b),b at _FillValue)/)          ;
>> fatal:_NclBuildArray: each element of a literal array must have the same
>> number of dimensions
>>
>> c = array_append_record(b,new(dimsizes(b(0,:)),typeof(b),b at _FillValue),0)
>>                  ; array_append_record: ranks not equal: rank_x1=2
>>  rank_x2=1
>>
>> c = table_attach_rows(b,new(dimsizes(b(0,:)),typeof(b),b at _FillValue),0)
>>             ; fatal:Number of dimensions in parameter (1) of
>> (table_attach_rows) is (1), (2) dimensions were expected
>>
>> Please let me know what else I can try or what I've done wrong.
>>
>> Thanks,
>>
>> Giorgio
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20230424/e51612f9/attachment.htm>


More information about the ncl-talk mailing list