[ncl-talk] Dimension Error Issue

Dennis Shea shea at ucar.edu
Sun Aug 11 08:13:34 MDT 2019


Matlab <===> NCL

[1] You do not need a '*begin'*/'*end*' for an NCL  main script. They are
optional.
[2] I am not Matlab literate but I believe
     [a] Matlab uses column-major storage (like: Fortran, R, ...)
     [b] Matlab uses 1-based subscripting like Fortran, R
[3] NCL is like C/C++/Python
     [a] NCL uses row-major storage
     [b] NCL uses 0-based subscripts

Each language has syntax that is unique for certain purposes.
However, in general, there is no one-to-one  equivalence.


[snip]
else if (ustar.lt. 2.35) then  ; this is the loop where I'm having the issue
                                          ; not sure how to properly write
this in NCL
        p=p_b1
        z0(:,i)=p*(ust_array(:,i))   * ; array of size [9] * array of size
?4000?*
                                                * ; in NCL, arrays must
conform [same size/shape]*
*                                                 ; exception: a  scalar is
always conformant*
[snip]

The *ust_array* is one-dimensional and is of size *[9]*.
ust_array=((/ust^8, ust^7, ust^6, ust^5, ust^4, ust^3, ust^2, ust^1,
ust^0/))
n_ust_array = dimsizes(ust_array)
print("n_ust_array="+n_ust_array)  ; 9

===
I've attached a quick-and-dirty code for testing. Rather that 4000, I used
40 because there are many print statements to help you.

Play around a bit. This is a good way to learn a language.

Good Luck

On Sun, Aug 11, 2019 at 8:01 AM Sakib Ahmed via ncl-talk <ncl-talk at ucar.edu>
wrote:

> Hi Karin,
>
> Thank you for the suggestion. I tried the reassignment operator but it
> didn't work. I'm still getting the error message  "fatal:Dimension sizes on
> right hand side of assignment do not match dimension sizes of left hand
> side"
>
> I also tried to convert p_b1 & p_b2 from 1D to 2D just to be consistent
> with the other variables (z0 and ust_array) dimension but that didn't work
> either.
>
>  -Sakib
>
> On Sun, Aug 11, 2019 at 9:21 AM Karin Meier-Fleischer <
> meier-fleischer at dkrz.de> wrote:
>
>> Hi Sakib,
>>
>> try using the reassignment operator := for the lines defining p.
>>
>> p := p_b1
>> and
>> p := p_b2
>>
>> -Karin
>>
>> Am 11.08.2019 um 14:00 schrieb Sakib Ahmed via ncl-talk <
>> ncl-talk at ucar.edu>:
>>
>> Dear NCL Community,
>>
>> I'm stuck with a dimension size error issue. Following is what the code
>> looks like:
>>
>>
>> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
>>
>> begin
>>
>>  ustar=new(1,float)
>>
>>  z0=new((/1,4001/),float)
>>
>>  ust=fspan(0,4,4001)
>>
>>
>> ust_array=((/ust^8, ust^7, ust^6, ust^5, ust^4, ust^3, ust^2, ust^1,
>> ust^0/))
>>
>>
>> printVarSummary(ust_array)
>>
>> ;polynomial
>>
>> p_b1= (/-0.000098701949811,  0.001486209983407, -0.007584567778927,
>>  0.019487056157620, -0.029314498154105,  0.024309735869547,
>> -0.006997554677642,  0.001258400989803, -0.000043976208055/)
>>
>> p_b2=(/-0.002182648458354,  0.046387047659009, -0.428830523588356,
>>  2.251251262348664, -7.334368361013868, 15.163848944684784,
>> -19.388290305787166,13.970227275905133, -4.319572176336596/)
>>
>> g=9.806650
>>
>> do i=0,4000
>>     ustar=ust(i)
>>
>>     if (ustar.lt.0.3) then
>>         z0(:,i)=0.0185/g * ustar^2
>>
>>     else if (ustar.lt. 2.35) then                      ; this is the
>> loop where I'm having the issue; not sure how to properly write this in NCL
>>
>>         p=p_b1
>>         z0(:,i)=p*(ust_array(:,i))
>>
>>     else if (ustar.lt.3) then
>>
>>         p=p_b2
>>         z0(:,i)=p*ust_array(:,i)
>>
>>
>>     else
>>         z0(:,i)=0.001305
>>
>>     end if
>>     end if
>>     end if
>> end do
>> end
>>
>> fatal:Dimension sizes on right hand side of assignment do not match
>> dimension sizes of left hand side
>> fatal:["Execute.c":8635]:Execute: Error occurred at or near line 32
>>
>>
>> printVarSummary(ust_array)
>>
>> Variable: ust_array
>> Type: float
>> Total Size: 144036 bytes
>>             36009 values
>> Number of Dimensions: 2
>> Dimensions and sizes:   [9] x [4001]
>> Coordinates:
>>
>> printVarSummary(z0)
>>
>> Variable: z0
>> Type: float
>> Total Size: 16004 bytes
>>             4001 values
>> Number of Dimensions: 2
>> Dimensions and sizes:   [1] x [4001]
>> Coordinates:
>> Number Of Attributes: 1
>>   _FillValue :  9.96921e+36
>>
>>
>> I'm still new in NCL but I able to solve this same problem in Matlab and
>> putting this below as a reference. But I'm really interested to do this in
>> NCL.
>>
>> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Matlab
>> version;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
>>
>> z0=[]; ust_array=[];
>> ustar=[];
>>
>> ust=[0:001:4];
>>
>> ust_array=[ust.^8; ust.^7; ust.^6; ust.^5; ust.^4; ust.^3; ust.^2;
>> ust.^1; ust.^0;];
>>
>> % polynomial:
>> p_b1= [-0.000098701949811,  0.001486209983407, ...
>>     -0.007584567778927,  0.019487056157620, ...
>>     -0.029314498154105,  0.024309735869547, ...
>>     -0.006997554677642,  0.001258400989803, ...
>>     -0.000043976208055];
>> p_b2=[-0.002182648458354,  0.046387047659009, ...
>>     -0.428830523588356,  2.251251262348664, ...
>>     -7.334368361013868, 15.163848944684784, ...
>>     -19.388290305787166,13.970227275905133, ...
>>     -4.319572176336596];
>> g=9.806650;
>>
>>
>> for i=1:length(ust)
>>
>>     ustar=ust(i);
>>     if ustar < 0.3
>>         z0(i)=0.0185/g * ustar^2;
>>     elseif ustar < 2.35
>>         p=p_b1;
>>         z0(i)=p*ust_array(:,i);
>>     elseif ustar < 3
>>         p=p_b2;
>>         z0(i)=p*ust_array(:,i);
>>     else
>>         z0(i)=0.001305;
>>     end
>> end
>>
>> Thank you in advance for the help.
>> Sakib
>>
>> Sakib Ahmed
>> Connecticut College
>> Environmental Studies Major,
>> New London, CT 06320
>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> ncl-talk at ucar.edu
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>
>> _______________________________________________
> 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/20190811/1aadef58/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: SakebAmed.ncl
Type: application/octet-stream
Size: 1602 bytes
Desc: not available
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20190811/1aadef58/attachment.obj>


More information about the ncl-talk mailing list