0

I am learning on how to plot various variables from WRF output netcdf file. My requirement is to extract variables for a certain lat/lon (8.4875° N, 76.9525° E) in order to plot SkewT profiles using the matplotlib package.

I found a similar question on this SO page netcdf4 extract for subset of lat lon. However, its location is a set of boundaries.

Vaibhav Mule
  • 5,016
  • 4
  • 35
  • 52
sundar_ima
  • 3,604
  • 8
  • 33
  • 52
  • See answer two at: http://stackoverflow.com/questions/33789379/netcdf-and-python-finding-the-closest-lon-lat-index-given-actual-lon-lat-values/33793437#33793437 – Eric Bridger Dec 21 '15 at 03:00
  • Thanks for your comment. When I executed your code it returned `1632` and `43` for lat `8.4` and lon `76.95`. This is no way close to the actual Lat & Lon I would expect. – sundar_ima Dec 21 '15 at 16:03
  • 76.95 E is -76.95 in decimal degrees. I added code to show how to test the results at the answer above. – Eric Bridger Dec 21 '15 at 18:36
  • Well I have got the index error `IndexError: index 1632 is out of bounds for axis 0 with size 73`. Does it mean that the lat and lon is not available in the file? – sundar_ima Dec 21 '15 at 19:10
  • I am not at all familiar with the WRF NetCDF file format. A quick looks suggests that the lat,lon columns might be named south_north and west_east. Do you have the url where you got the WRF file. – Eric Bridger Dec 23 '15 at 17:06
  • The file I use is a quite big one and cant be uploaded. You can use the sample file from here http://www.unidata.ucar.edu/software/netcdf/examples/wrfout_v2_Lambert.nc – sundar_ima Dec 23 '15 at 18:05

2 Answers2

1

Check out xray. You'll have to do some work to make the SkewT chart but accessing and summarizing the netCDF Dataset and variables will be pretty easy. Just a few examples below.

import xray

ds = xray.open_dataset('your_wrf_file.nc')

ds_point = ds.sel(lon=76.9525, lat=8.4875)

ds_point['Temperature'].plot()  # plot profile at point assuming Temperature had dimensions of (level, lat, lon)

df = ds_point.to_dataframe()  # export dataset to Pandas.DataFrame

temp_array = ds_point['Temperature'].values  # access the underlying numpy array of the "Temperature" variable
jhamman
  • 5,867
  • 19
  • 39
  • Your code gives me a key error. I have changed the key to `XLAT` and `XLONG` which is equivalent of `lon` and `lat`. But still same issue. – sundar_ima Dec 21 '15 at 16:07
  • You didn't provide a sample file so this was just to give you a rough idea of how this could be done. – jhamman Dec 21 '15 at 20:26
0

What I usually do is extract the nearest gridpoint to the column using CDO from the command line before using ncl,python,R etc for plotting:

cdo remapnn,lon=76.9525/lat=8.4875 wrf_file.nc pnt_file.nc
ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86