[ncl-talk] Issue using reshape function
Mary Haley
haley at ucar.edu
Fri Jan 25 08:52:33 MST 2019
Hi Anne,
The short answer is that
t2m_test2(4,0,0) and t2m_reshape(4,0,0,0)
are not the same element. What are the same elements are:
t2m_test2(40,0,0) and t2m_reshape(4,0,0,0)
The simpler way to think about it is to ignore the rightmost two dimensions
of both arrays, since they are the same, and instead focus on the leftmost
dimension of t2m_test2 and the leftmost two dimensions of t2m_reshape.
Here's a column of the leftmost index of t2m_test2, and how it maps into
the leftmost 2 indexes of t2m_reshape:
t2m_test2 t2m_reshape
( 0) --> (0,0)
( 1) --> (0,1)
. . .
( 9) --> (0,9)
(10) --> (1,0) ; Note the shift here
(11) --> (1,1)
(12) --> (1,2)
. . .
(20) --> (2,0) ; Note the shift here
(21) --> (2,1)
. . .
(39) --> (3,9)
(40) --> (4,0) ; Again, the shift
. . .
Here's how to look at it if you fill in some dimensions on the right:
t2m_test2 t2m_reshape
( 0,0,0) --> (0,0,0,0)
( 1,0,0) --> (0,1,0,0)
. . .
( 9,0,0) --> (0,9,0,0)
(10,0,0) --> (1,0,0,0)
(11,0,0) --> (1,1,0,0)
(12,0,0) --> (1,2,0,0)
. . .
(20,0,0) --> (2,0,0,0)
(21,0,0) --> (2,1,0,0)
. . .
(39,0,0) --> (3,9,0,0)
(40,0,0) --> (4,0,0,0)
. . .
You can verify this with a simple program:
dims_3d = (/60,18,360/)
dims_4d = (/6,10,18,360/)
t2m_test2 = random_uniform(0.,10.,dims_3d)
t2m_reshape = reshape(t2m_test2,dims_4d)
print("t2m_test2d (3d) t2m_reshape (4d)")
print("( 0,0,0) " + t2m_test2( 0,0,0) + " --> (0,0,0,0) " +
t2m_reshape(0,0,0,0))
print("( 1,0,0) " + t2m_test2( 1,0,0) + " --> (0,1,0,0) " +
t2m_reshape(0,1,0,0))
print("( 4,0,0) " + t2m_test2( 4,0,0) + " --> (0,4,0,0) " +
t2m_reshape(0,4,0,0))
print("( 9,0,0) " + t2m_test2( 9,0,0) + " --> (0,9,0,0) " +
t2m_reshape(0,9,0,0))
print("(10,0,0) " + t2m_test2(10,0,0) + " --> (1,0,0,0) " +
t2m_reshape(1,0,0,0))
print("(11,0,0) " + t2m_test2(11,0,0) + " --> (1,1,0,0) " +
t2m_reshape(1,1,0,0))
print("(12,0,0) " + t2m_test2(12,0,0) + " --> (1,2,0,0) " +
t2m_reshape(1,2,0,0))
print("(20,0,0) " + t2m_test2(20,0,0) + " --> (2,0,0,0) " +
t2m_reshape(2,0,0,0))
print("(21,0,0) " + t2m_test2(21,0,0) + " --> (2,1,0,0) " +
t2m_reshape(2,1,0,0))
print("(39,0,0) " + t2m_test2(39,0,0) + " --> (3,9,0,0) " +
t2m_reshape(3,9,0,0))
print("(40,0,0) " + t2m_test2(40,0,0) + " --> (4,0,0,0) " +
t2m_reshape(4,0,0,0))
Output:
( 0,0,0) 3.23711 --> (0,0,0,0) 3.23711
( 1,0,0) 1.44937 --> (0,1,0,0) 1.44937
( 4,0,0) 1.50282 --> (0,4,0,0) 1.50282
( 9,0,0) 1.38478 --> (0,9,0,0) 1.38478
(10,0,0) 5.53886 --> (1,0,0,0) 5.53886
(11,0,0) 4.32108 --> (1,1,0,0) 4.32108
(12,0,0) 3.80684 --> (1,2,0,0) 3.80684
(20,0,0) 3.36136 --> (2,0,0,0) 3.36136
(21,0,0) 4.84756 --> (2,1,0,0) 4.84756
(39,0,0) 1.74234 --> (3,9,0,0) 1.74234
(40,0,0) 1.17584 --> (4,0,0,0) 1.17584
Hope this helps!
--Mary
On Thu, Jan 24, 2019 at 5:57 AM Anne <anne.seidenglanz at unive.it> wrote:
> Hello everyone,
>
> I am trying to use 'reshape' to convert a 60 x 180 x 360 matrix to a 6 x
> 10 x 18 x 360 matrix in the following way:
>
>
> dir_s
> ="/work/as10017/CESM/archive/Ensemble_means/yearly_means_h0/1993/"
> fil_s = systemfunc("ls "+dir_s+"/sps_199311_*_
> icef.cam.h0.6mth_grid.surface.nc")
> f_s = addfiles(fil_s,"r")
> t2m_test2 = f_s[:]->TREFHT
>
> printVarSummary(t2m_test2)
> t2m_reshape = reshape(t2m_test2,(/6,10,180,360/))
> copy_VarCoords(t2m_test2(0,:,:), t2m_reshape(0,0,:,:))
> printVarSummary(t2m_reshape)
>
>
> I am getting the 1st dimension of my 60 x 180 x 360 array by using a
> wildcard (*) and 'systemfunc' so that it concatenates (in a folder) 10
> files with each 6 x 180 x 360 dimensions (months x mlat x nlon). I then
> reshaped the 60 x 180 x 360 array so as to reflect the original file
> structure : 6 x 10 x 180 x 360 (months x member x mlat x nlon).
>
> This worked fine, however, if I check with
>
> print(t2m_test2(4,0,0))
> print(t2m_reshape(4,0,0,0))
>
> I do not get the same values (223 Kelvin vs. 221 Kelvin). Did I miss
> something here?
>
>
> thanks
> Anne
> _______________________________________________
> 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/20190125/c2504160/attachment.html>
More information about the ncl-talk
mailing list