NCL Home> Application examples> Miscellaneous || Data files for some examples

Example pages containing: tips | resources | functions/procedures

Subsetting / extracting data based on lat/lon values

Subsetting or extracting your data based on a latitude/longitude region can be done in different ways:

latlon_subset_1.ncl: Data on a rectilinear grid is the easiest to subset, as you can use coordinate subscripting to extract the region of interest.

To find out if your data is on a rectilinear lat/lon grid, use printVarSummary. The output will look something like this:

Variable: ts
Type: float
Total Size: 221184 bytes
            55296 values
Number of Dimensions: 2
Dimensions and sizes:[lat | 192] x [lon | 288]
Coordinates: 
            lat: [ -90..  90]
            lon: [-180..178.75]
Number Of Attributes: 13
. . .
long_name : Surface Temperature
units : degK
. . .

Note the "Coordinates" section, which indicates this data has coordinate arrays called "lat" and "lon". To extract a lat/lon region that goes from 30 to 60 latitude and 0 to 50 longitude, use "{" and "}":

  ts_extract = ts({30:60},{0:50})  

The first frame of this example shows the full global plot of the data. The second frame extracts a region in Asia using coordinate subscripting. The third frame zooms in on this region and adds a lat/lon grid and markers to further show the region of interest. The gsn_coordinates procedure is used to add the lat/lon grid lines.

latlon_subset_2.ncl: Data on a curvilinear grid, which is data represented by 2D lat/lon arrays, cannot be extracted using "coordinate scripting" mentioned in the first example on this page.

To find out if your data is on a curvilinear lat/lon grid, first try using printVarSummary to print information about the variable. Check if there is an attribute called "coordinates" (not to be confused with the "Coordinates:" section):

Variable: temp
Type: float
Total Size: 15120 bytes
            3780 values
Number of Dimensions: 3
Dimensions and sizes:[lv_SPDY3 | 6] x [gridx_236 | 30] x [gridy_236 | 21]
Coordinates:
Number Of Attributes: 13
. . .
long_name : Temperature
units : degK
_FillValue : 1e+20
coordinates : gridlat_236 gridlon_236

"temp" doesn't have any information in the "Coordinates:" section, so this tells you right away this data is NOT on a rectilinear grid. However, it does have an attribute called "coordinates", which tells you that this variable is represented by latitude and longitude arrays called "gridlat_236" and "gridlon_236". You can look on the same file that you read "temp" from, to see if these two variables are on the file.

To extract a lat/lon region from curvilinear data, use the getind_latlon2d function. Assuming you have a 2D array "temp" with 2D lat/lon arrays that you've read off the file and named "lat2d", "lon2d":

  lat_pts  = (/30,60/)
  lon_pts  = (/ 0,50/)
  ij       = getind_latlon2d(lat2d,lon2d,lat_pts,lon_pts)
  temp_sub = temp(ij(0,0):ij(1,0),ij(0,1):ij(1,1))
The first frame of this example shows the plot of the whole data array, which is regional data that covers the United States. The second frame extracts a region over New Mexico and Colorado using getind_latlon2d. The third frame zooms in on this region and adds a lat/lon grid and markers to further show the region of interest. The gsn_coordinates procedure is used to add the lat/lon grid lines.
wrf_debug_4.ncl: WRF-ARW data is on a curvilinear grid. You could use the getind_latlon2d to extract a region of interest, but NCL has a function specific for extracting WRF data called wrf_user_ll_to_ij.

This particular example shows the difference between using the where function and the wrf_user_ll_to_ij function to subset a WRF lat/lon grid.