NCL Home > Documentation > Functions > WRF, Lat/Lon functions

# wrf_user_ll_to_xy

Finds the nearest WRF-ARW model grid indexes (0-based) that are the closest to the requested longitude and latitude locations. Available in version 6.6.0 and later.

## Prototype

```load "\$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"      ; These two libraries are automatically
; No need for user to explicitly load.

function wrf_user_ll_to_xy (
file_handle  : file or list,
lon          : numeric,
lat          : numeric,
opt          : logical
)

return_val  :  float or double
```

## Arguments

file_handle

Reference to a single WRF-ARW NetCDF file opened with addfile or a list of WRF-ARW NetCDF files opened with addfiles.

lon
lat

Longitude and latitude arrays for which index locations into a WRF-ARW model domain are required.

opt

A logical scalar that can optionally contain attributes. See description below.

## Return value

An array that holds the index locations of a WRF model domain that are the closest to the requested lon, lat values.

If scalar lon, lat values are inputted, the return value will be a 1D array of length of 2, where the first (leftmost) element represents the X (lon) index and the second (rightmost) element represents the Y (lat) index.

If an array of lon, lat values are inputted, the return value will be a 2 x n array, where n is the length of the lon, lat arrays.

The return values will represent indexes in the WRF-ARW lon, lat model domain and will be 0-based. This is different from wrf_user_ll_to_ij (deprecated in NCL V6.6.0), in which the index values are 1-based.

## Description

This function is similar to wrf_user_ll_to_ij (which was deprecated as of NCL V6.6.0), except the indexes returned are 0-based.

This function makes use of map projection information stored in the global attributes of the WRF-ARW file (or files) to find the index locations which are the closest to the requested lat/lon locations. If the requested lat/lon locations are outside your model domain, the returned indexes locations will represent a point outside the domain.

The opt variable can contain the following attributes. Attributes are case-insensitive:

• returnInt - True, will return an integer value, False will return real (defaults to True)

• useTime - which time in the file should be used when extracting XLAT/XLONG arrays from input file. Only important to set if output is for a moving nest and all the output times are in a single wrfout file.

wrf_user_ll_to_xy is part of a library of functions and procedures in WRFUserARW.ncl written to help users plot ARW WRF model data.

See the full list of WRF functions.

## Examples

Example 1

A simple example showing how to use this function to extract a particular lat/lon region of interest for your data. The print statements in this example will also help ensure that you are using this function properly.

```  a = addfile("wrfout_d01_2000-01-24_12:00:00.nc","r")

minlat = 30
maxlat = 40
minlon = -80
maxlon = -70

opt = True
loc  = wrf_user_ll_to_xy(a,(/minlon,maxlon/),(/minlat,maxlat/),opt)

;---The requested and calculated values should be close
print("Requested min/max  xlon = " + minlon + "/" + maxlon)
print("Calculated min/max xlon = " + xlon(loc(1,0),loc(0,0)) + "/" + \
xlon(loc(1,1),loc(0,1)))

print("Requested  min/max xlat = " + minlat + "/" + maxlat)
print("Calculated min/max xlat = " + xlat(loc(1,0),loc(0,0)) + "/" + \
xlat(loc(1,1),loc(0,1)))

;---Just for fun, pick a variable and take a subdomain of it.
tc2 = wrf_user_getvar(a,"T2",0)   ; T2 in Kelvin
printMinMax(tc2,0)

tc2_sub = tc2(loc(1,0):loc(1,1),loc(0,0):loc(0,1))
tc2_sub@description = tc2@description + " (subdomain)"   ; just an example
printMinMax(tc2_sub,0)
```

Example 2

This example shows how using the output from wrf_user_ll_to_xy as input to wrf_user_xy_to_ll should give you close to the same values:

```  a    = addfile("wrfout_d01_2000-01-24_12:00:00.nc","r")
xlat = a->XLAT(0,:,:)
xlon = a->XLONG(0,:,:)

minlat =  30
maxlat =  40
minlon = -80
maxlon = -70

;---Get xy indexes, and then use these to get lat/lon values back again.
opt    = True
loc    = wrf_user_ll_to_xy(a,(/minlon,maxlon/),(/minlat,maxlat/),opt)
latlon = wrf_user_xy_to_ll(a,loc(0,:),loc(1,:),opt)

;---The min/max values printed should be close.
print("----")
print("Requested  min/max xlon = " + minlon + "/" + maxlon)
print("Actual     min/max xlon = " + xlon(loc(1,0),loc(0,0)) + "/" + \
xlon(loc(1,1),loc(0,1)))
print("Calculated min/max xlon = " + latlon(0,0) + "/" + latlon(0,1))

print("----")
print("Requested  min/max xlat = " + minlat + "/" + maxlat)
print("Actual     min/max xlat = " + xlat(loc(1,0),loc(0,0)) + "/" + \
xlat(loc(1,1),loc(0,1)))
print("Calculated min/max xlat = " + latlon(1,0) + "/" + latlon(1,1))
print("----")
```

Example 3

Have the "loc" values returned as floating point values, rather than integers:

```  a   = addfile("wrfout_d01_2000-01-24_12:00:00.nc","r")

res = True
res@returnInt = False                             ; return real values
loc  = wrf_user_ll_to_xy(a, -100.0, 40.0, res)
print("X/Y location is: " + loc)
```

Example 4

You can retrieve xy coordinates from a list of WRF ARW NetCDF files:

```  files = systemfunc("ls -1 wrfout_d01_2000*") + ".nc"

res = True
res@returnInt = False                             ; return real values
loc  = wrf_user_ll_to_xy(a, -100.0, 40.0, res)
print("X/Y locations are: " + loc)
```

You can see some other example scripts and their resultant images at:

http://www2.mmm.ucar.edu/wrf/OnLineTutorial/Graphics/NCL/

Example 5

To see a graphical example of using wrf_user_ll_to_xy, see example "wrf_debug_4.ncl" on the Debugging WRF grids page.
{"ft_lang_label":"__label__en","ft_lang_prob":0.64332724,"math_prob":0.9719881,"size":5525,"snap":"2021-21-2021-25","text_gpt3_token_len":1627,"char_repetition_ratio":0.1211737,"word_repetition_ratio":0.15757576,"special_character_ratio":0.30533937,"punctuation_ratio":0.1608456,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9910151,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-05-15T12:00:54Z\",\"WARC-Record-ID\":\"<urn:uuid:f1afa588-ae11-416c-829e-cb08304a7088>\",\"Content-Length\":\"22953\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c459a80a-9e06-4d92-bd07-5fdfad295eb2>\",\"WARC-Concurrent-To\":\"<urn:uuid:e86c3c1a-5b78-4444-b66e-1ff28a9f2f58>\",\"WARC-IP-Address\":\"128.117.225.48\",\"WARC-Target-URI\":\"http://ncl.ucar.edu/Document/Functions/WRF_arw/wrf_user_ll_to_xy.shtml\",\"WARC-Payload-Digest\":\"sha1:KNBZTDE3AU5X3DRQ5VUXI7FWZYJG4TZ7\",\"WARC-Block-Digest\":\"sha1:3GCG44EXMWNIBVCXTX3VKAVYYF6ZKBXE\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-21/CC-MAIN-2021-21_segments_1620243991801.49_warc_CC-MAIN-20210515100825-20210515130825-00369.warc.gz\"}"}