# [ncl-talk] Average values over a specified region

Karin Meier-Fleischer meier-fleischer at dkrz.de
Tue Jan 26 07:43:23 MST 2016

```Do you mean something like var({lat13-2:lat13+2},{lon80-2:lon80+2}) ?

Then the answer is no, the coordinate subscripting gets the value of the
closest grid cell and not the indices.

lat({13}) will use the latitude value which is the closest to 13
degrees, e.g. 12.12418.

Let us assume the following snippet of a script:

*lat13*  = closest_val(13,lat)       ;-- return index of closest
latitude to 13 deg
*lon80*  = closest_val(80,lon)       ;-- return index of closest
longitude to 80 deg

print("lat({13}) (closest value) = "+*lat({13})*+"   Index of
closest_val: "+lat13)
print("------------------")
print("lon({80})(closest value) = "+*lon({80})*+"   Index of
closest_val: "+lon80)
print("------------------")

lat_box = lat(lat13-2:lat13+2)     ;-- using indices to get the lats
around lat 13 deg.
lon_box = lon(lon80-2:lon80+2)     ;-- using indices to get the lons
around lon 80 deg.

print(""+lat_box)
print("------------------")
print(""+lon_box)
print("--------------------------------------")

lat_box2 = *lat({lat13-2:lat13+2})*;-- Don't do this: this would use
the index as latitude value !!
lon_box2 = *lon({lon80-2:lon80+2})*  ;-- Don't do this: this would
use the index as longitude value !!

print(""+lat_box2)
print(""+lon_box2)
print("------------------")

It will returns, e.g

(0)    lat({13})(closest value) = *12.12418712345578*   Index of
closest_val: *41*
(0)    ------------------
(0)    lon({80})(closest value) = *80.625*              Index of
closest_val: *139*
(0)    ------------------
(0) 15.85470386969488
(1) 13.98944571235667
(2) 12.12418712345578; <--- closest value
(3) 10.25892816800639
(4) 8.393668907692385
(0)    ------------------
(0) 76.875
(1) 78.75
(2) 80.625; <--- closest value
(3) 82.5
(4) 84.375
(0)    --------------------------------------
(0) *40.1029793042494*;-- Don't do this: this would use the index as
latitude value !!
(1) *41.96822026907537*;-- Don't do this: this would use the index as
latitude value !!
(0) *138.75*;-- Don't do this: this would use the index as longitude
value !!
(1) *140.625*;-- Don't do this: this would use the index as longitude
value !!
(0)    ------------------

Hope this helps for understanding coordinate subscripting.

Bye,
Karin

Am 26.01.16 um 15:13 schrieb Guido Cioni:
> Shouldn’t the coordinate subscripting do the same thing?
> Like say {lat13-2:lat13+2,  lon80-2:lon80+2}
>
> Guido Cioni
> http://guidocioni.altervista.org
>
>> On 26 Jan 2016, at 15:04, Karin Meier-Fleischer
>> <meier-fleischer at dkrz.de <mailto:meier-fleischer at dkrz.de>> wrote:
>>
>> Hi Krishna,
>>
>> you can use the function *closest_val* to retrieve an index of an value.
>>
>>   lat13  =  closest_val(13,lat)
>>   lon80  =  closest_val(80,lon)
>>
>>   print("Index: "+lat13+" -  "+lat(lat13))
>>   print("Index: "+lon80+" - "+lon(lon80))
>>
>> To plot 2 more grid boxes of lat/lon than you can use e.g.
>>
>>   plot = gsn_csm_contour_map(wks,
>> var(lat13-2:lat13+2,lon80-2:lon80+2),res)
>>
>>
>> Bye,
>> Karin
>>
>> Am 26.01.16 um 14:13 schrieb Krishna C:
>>> Hi
>>>
>>>
>>> Let us say i need a 2 by 2 box around 13 degree lat and 80 degree
>>> lon . How do i know the respective indices.
>>> Please correct if i am wrong.
>>>
>>> Regards
>>> -Krishna-
>>>
>>> On Tue, Jan 26, 2016 at 5:14 PM, Guido Cioni <guidocioni at gmail.com
>>> <mailto:guidocioni at gmail.com>> wrote:
>>>
>>>     Yes there is, but you should have a look at the documentation on
>>>     the page ;-)
>>>     The function that you need is dim_avg_n, then you just need to
>>>     restrict the latitude and longitude with brackets {43:45}.
>>>     Again, just go on ncl documentation and you'll definitely find a
>>>     way
>>>
>>>     Il 26 gen 2016 11:52 AM, "Krishna C"
>>>     <chandrakrishna.90 at gmail.com
>>>     <mailto:chandrakrishna.90 at gmail.com>> ha scritto:
>>>
>>>         Hi,
>>>
>>>
>>>         I am trying to extract precipitation values from WRF output,
>>>         over a small 2 by 2 lat  lon box and average it. Is there
>>>         any explicit way of doing it in ncl
>>>
>>>         With warm regards
>>>
>>>
>>>         -Krishna-
>>>
>>>         _______________________________________________
>>>         ncl-talk mailing list
>>>         ncl-talk at ucar.edu <mailto: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
>>
>> _______________________________________________
>> ncl-talk mailing list
>> ncl-talk at ucar.edu <mailto: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/20160126/e7c1963b/attachment.html
```