[ncl-talk] Question regarding array indexing

Dennis Shea shea at ucar.edu
Wed Oct 18 13:46:53 MDT 2017


See Example 2 & 3

On Tue, Oct 17, 2017 at 3:22 PM, Prashanth Bhalachandran <
prashanth.bhalachandran at gmail.com> wrote:

> Dave,
> This is a very elegant solution. My sincere appreciation.
>
> Prashanth
>
> On Oct 17, 2017, at 2:10 PM, Dave Allured - NOAA Affiliate <
> dave.allured at noaa.gov> wrote:
>
> Prashanth,
>
> I like using a modulo calculation and vector subscripting for cyclic
> indexing.  This reduces possible errors from hard coded indexing.
> Something like this:
>
>     theta = 20
>     lons = 10.0 * ispan (0,35,1)
>     offset = mod (lons + 360 - theta, 360)
>
>     iq1 = ind (offset .le. 90)
>     iq2 = ind (offset .ge. 90 .and. offset .le. 180)
>     iq3 = ind (offset .ge. 180 .and. offset .le. 270)
>     iq4 = ind (offset .ge. 270)
>
> Note that this can create overlapping index points in some cases.  For no
> overlap, change .le. to .lt. in 3 places.
>
> In the mod expression, it is important to add an extra 360 degrees, to
> prevent negative values in the first mod argument.  What this really does
> is convert the NCL mod function to a true mathematical modulo function for
> cyclic applications.  Understand the difference here:
>
>     https://en.wikipedia.org/wiki/Modulo_operation
>
> --Dave
>
>
> On Tue, Oct 17, 2017 at 1:19 PM, Prashanth Bhalachandran <
> prashanth.bhalachandran at gmail.com> wrote:
>
>> Hello all,
>> I have a relatively simple question on array indexing.
>>
>> Say, I have an array that is cyclic that contains values from 0 to 360 in
>> steps of 5 degrees (dimension [72]). And I am trying to divide into four
>> quadrants starting from a given theta. Say that the index of the given
>> theta is 13 (out of 73), I define my quadrants that are each 90 degrees as
>> follows :
>>
>> Array : theta [73]
>> Quadrant 1 : theta(13:31)
>> Quadrant 2: theta(31:49)
>> Quadrant 3: theta(49:67)
>> Quadrant 4: theta(67:13)
>>
>> Now, as you’ll notice, having an index of 67:13 doesn’t imply 67:72 and
>> 0:13, it simply implies 13:67 backwards. How do I index the array such that
>> it takes it in a cyclic fashion? For example (what I don’t want is), in a
>> simpler array of just five dimensions : Variable: a
>> Type: integer
>> Total Size: 20 bytes
>>             5 values
>> Number of Dimensions: 1
>> Dimensions and sizes: [5]
>> Coordinates:
>> (0) 1
>> (1) 2
>> (2) 3
>> (3) 4
>> (4) 5
>>
>> ncl 3> print(a(4:2))
>>
>>
>> Variable: a (subsection)
>> Type: integer
>> Total Size: 12 bytes
>>             3 values
>> Number of Dimensions: 1
>> Dimensions and sizes: [3]
>> Coordinates:
>> (0) 5
>> (1) 4
>> (2) 3
>>
>> What I would have liked it is for it to display 5 1 2 3.
>>
>> Please let me know how this is possible.
>>
>> Thank you all,
>> Prashanth
>>
>
>
> _______________________________________________
> 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/20171018/3a61be2e/attachment.html>


More information about the ncl-talk mailing list