# [ncl-talk] Question regarding array indexing

Dave Allured - NOAA Affiliate dave.allured at noaa.gov
Tue Oct 17 15:10:15 MDT 2017

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]
>
> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20171017/e0a0f439/attachment.html>