[ncl-talk] convert non-regular 1d to regular 2d array
Rashed Mahmood
rashidcomsis at gmail.com
Fri Aug 16 00:32:24 MDT 2019
Hi Jiali,
Note, if you have multiple years or months that have 1,1,2,2,3, etc time
steps then you would need to subset for a period that does not repeat the
same time numbers. After that try the following, e.g.:
begin
t1 = (/1,1,2,2,2,3,3,3,3/)
d1 = (/22.,12,27,11,37,18,26,24,10/)
ntim = 3
ndat = 4
missing = -999.
d_new = new((/ntim,ndat/),float,missing)
do i=0,ntim-1
itim = i+1
t_ids := ind(t1.eq.itim)
d2 := d1(t_ids)
tt = dimsizes(t_ids)
do j=0,ndat-1
if(j.lt.tt)
d_new(i,j) = d2(j)
else
d_new(i,j) = missing
end if
end do
end do
print(d_new(0,:)+" ... "+d_new(1,:)+" ... "+d_new(2,:))
end
On Thu, Aug 15, 2019 at 9:23 PM Wang, Jiali via ncl-talk <ncl-talk at ucar.edu>
wrote:
> Dear NCL team,
>
>
>
> I have a 1D dataset like below: you can see that I have different number
> of data for each time step. Now I like to convert this 1d array to a
> regular 2d array by assigning -999 for those grid points where don’t have
> numbers.
>
>
>
> 1d array:
>
> =======================
>
> time
>
> data
>
> 1
>
> 22
>
> 1
>
> 12
>
> 2
>
> 27
>
> 2
>
> 11
>
> 2
>
> 37
>
> 3
>
> 18
>
> 3
>
> 26
>
> 3
>
> 24
>
> 3
>
> 10
>
> =======================
>
>
>
> 2d array: (x=4 data points for each time, y=3 time steps)
>
> =======================
>
> 22
>
> 12
>
> -999
>
> -999
>
> 27
>
> 11
>
> 37
>
> -999
>
> 18
>
> 26
>
> 24
>
> 10
>
> =======================
>
>
>
> I have been trying to use function ‘where’ but couldn’t get it working.
> Can you please advise?
>
>
>
> ID = new ((/3, 4/), “loat”)
>
>
>
> do i = 0, 2 ; time step
>
> ID (i,:) = where(data(:,0) .eq. (i+1), data(:,1), -999) ;; this won’t
> work because the dimensions on left and right are different (4 vs 9). But
> if I set a j=0 to 3 for ID (i, j), then I am not sure how to set it for
> data..
>
> end do
>
>
>
> Thank you!
>
> Jiali
> _______________________________________________
> 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/20190815/d5b7057e/attachment.html>
More information about the ncl-talk
mailing list