Re: Need help : dimension problems

From: Dennis Shea <shea_at_nyahnyahspammersnyahnyah>
Date: Thu Feb 06 2014 - 12:14:15 MST

The code I sent misapplied the sequence of operations.
Also, it had Y = A+cos(B+C) ==> I *think* you want A*cos(B+C)

Please note: even though not correct, script did give you the idea
of how to approach the problem. You learn by trying things.
Use 'print' or 'printVarSummary' to see the effects of operations.

For example, the B4 below

   B4 = conform_dims(dim4, B, (/0,1,2/)) ; [*][*][*] ==> [*][*][*][*]

is the same as

   b4 = C4 ; illustration of B4 creatioin
   do ml=0,mlon-1
      b4(:,:,:,ml) = (/ B /) ; at each longitude copy the 3D array B
   end do
   print(sprintf("%9.4f", B4)+" "+sprintf("%9.4f", b4))

    --------------------------------------------------------

undef("YABC")
function YABC(A[*][*][*], B[*][*][*], C[*])
local dimA, dimB, ntim, klev, nlat, mlon, dim4, A4, B4, C4, BC4, Y
begin
    dimA = dimsizes(A)
    dimB = dimsizes(B)
    if (all(dimA.ne.dimB)) then
        print("ABC: dimension sizes of A and B are not the same.")
        print("dimA="+dimA+" dimB="+dimB)
        exit
    end if

    ntim = dimA(0) ; clarity
    klev = dimA(1)
    nlat = dimA(2)
    mlon = dimsizes(C) ; = dimC

    dim4 = (/ntim, klev, nlat, mlon /) ; 0,1,2,3 <== dim numbers

    C4 = conform_dims(dim4, C, 3 ) ; [*] ==> [*][*][*][*]
    B4 = conform_dims(dim4, B, (/0,1,2/)) ; [*][*][*] ==> [*][*][*][*]
    A4 = conform_dims(dim4, A, (/0,1,2/))
    BC4 = 0.01745329*(B4+C4) ; degrees to radians

    Y = A4*cos(BC4)
                                             ; meta data
   ;Y@long_name = "Y=A*cos(B+C)"
   ;Y@units = A@units

   ;copy_VarCoords(A,Y(:,:,:,0))
   ;copy_VarCoords(C,Y(0,0,0,:))

    return(Y)
end

;--------------------------------------------------------
;====================== MAIN ===========================
;--------------------------------------------------------

    ntim = 2
    klev = 3
    nlat = 4
    mlon = 5

    A = random_normal(0,1,(/ntim,klev,nlat/)) ; [*][*][*]
    print("A: min="+min(A)+" max="+max(A))

    B = random_uniform(-180,180, (/ntim,klev,nlat/)) ; [*][*][*]
    C = random_uniform(-180,180, mlon) ; [*]

    Y = YABC(A,B,C) ; [*][*][*][*]
    print(Y)

[SNIP]

>
> On Thu, Feb 6, 2014 at 3:59 PM, Dennis Shea <shea@ucar.edu> wrote:
>
>> This is a bit confusing (to me anyway).
>>
>> Arrays B and C must conform to allow (B+C).
>>
>> B and C must then have units of radians for 'cos(B+C)'
>>
>> See attached
>>
>>
>>
>> On 2/5/14, 12:20 PM, Xi Chang wrote:
>>
>>> Hallo NCL,
>>>
>>> I have a question regarding dimension problem.
>>> I want to solve this equation *Y= A cos (B+C) *in NCL,
>>>
>>> where:
>>> A: is two dimensional field (time, lat,lev)
>>> B: is two dimensional field (time, lat,lev)
>>> C: is an array for longitude, let say expands from ispan (-180,180,2)
>>>
>>> How could I solve this problem in order to produce Y with dimension
>>> *(time, lev, lat, lon)*
>>>
>>> Thank you
>>> Chang
>>>
>>>
>>>
>>> _______________________________________________
>>> ncl-talk mailing list
>>> List instructions, subscriber options, unsubscribe:
>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>
>>>
>
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Thu Feb 6 12:14:24 2014

This archive was generated by hypermail 2.1.8 : Fri Feb 07 2014 - 16:39:11 MST