wrf.getvar for a specific lat/lon directly?

1,650 views
Skip to first unread message

Luis Carvalheiro

unread,
Jan 8, 2018, 8:30:48 AM1/8/18
to wrfpyth...@ucar.edu

Dear all,

 

thank you for you excellent job developing wrf-python.

I'm planning to switch my old post-processing routines, which envolve creating timeseries of several variables for specific points.

 

Thus, I'd like to ask for some guidance. Is there some way of using wrf.getvar passing the gridpoints (or lat/lon via wrf.ll_to_xy)directly? Or I just have to use wrf.get_var for the whole grid and then extract points?

 

Thank You very much in advance,

Regards

Luís

Bill Ladwig

unread,
Jan 8, 2018, 12:01:50 PM1/8/18
to Luis Carvalheiro, wrfpython-talk
Hi Luis,

Unfortunately, at this time you have to extract the whole grid first from getvar, and then extract the desired grid points.  In the future, we plan to provide better support for xarray, which does a better job of this kind of thing, but it isn't there yet.  However, for now if you want to do this kind of thing, you'll have to extract the arrays for the desired points and then use the raw computational routines to calculate the diagnostics. 

Hope this helps,

Bill


--
You received this message because you are subscribed to the Google Groups "wrfpython-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wrfpython-talk+unsubscribe@ucar.edu.
To post to this group, send email to wrfpyth...@ucar.edu.
Visit this group at https://groups.google.com/a/ucar.edu/group/wrfpython-talk/.

Luis Carvalheiro

unread,
Jan 8, 2018, 12:04:43 PM1/8/18
to Bill Ladwig, wrfpython-talk
Thanks, Bill. I'll proceed with the whole grid extraction for each needed variable and then extract my lat/lon values. I was just curious if there was, somehow, a quick way of accomplishing this whithout having to load the entire grid.

Thank You for the support!
Regards
luis

Saverio Tubito

unread,
Jan 9, 2018, 2:01:42 PM1/9/18
to wrfpyth...@ucar.edu
I have the same question, for example i need to extract some vertical variable for specified lat,long point. How i can do?

Regards
Saverio

Bill Ladwig

unread,
Jan 9, 2018, 4:32:25 PM1/9/18
to Saverio Tubito, wrfpython-talk
Hi Saverio,

First, use getvar to extract the diagnostic variable that you're interested in.  Then you can use the ll_to_xy routine to convert the latitude,longitude point(s) to x,y grid point(s).  If you set the 'as_int' argument to True, this will return the integer grid point(s) closest to the desired lat/lon point(s), which is usually good enough for most users.  Now just query the desired diagnostic variable at the x,y point(s) returned by ll_to_xy.  (If you need better accuracy, you need to do interpolation by either using a bilinear interpolation routine from numpy/scipy, or you can use the interp2dxy routine with a single point as the xy line.  I can talk you through this if you really need this accuracy.)

The documentation is here:


Give it a try and let me know if you have any questions.

Regards,

Bill


Saverio

--

Xin Zhang

unread,
Jan 10, 2018, 11:05:11 AM1/10/18
to Bill Ladwig, Saverio Tubito, wrfpython-talk
Hi Bill,

I'm trying to get the average value of grids (resolution is 3km) among one circle (D~12km).
My solution is interpolating WRF results to 0.005 deg and calculate the average of grids in the circle (setting D=0.1 deg and checking whether the circle contains grids).

Do you have simpler and more accurate method?

Thanks,
Xin

Bill Ladwig

unread,
Jan 10, 2018, 1:15:10 PM1/10/18
to Xin Zhang, Saverio Tubito, wrfpython-talk
Hi Xin,

If I'm understanding what you're trying to do, you have a circle with a 12 km diameter (~4 grid boxes) somewhere in your WRF domain and you want to take the average of all grid points contained within this circle? To be honest, this is starting to move beyond my realm of knowledge, but have you looked at Python's shapely library (http://shapely.readthedocs.io/en/stable/manual.html)? You can use wrf.ll_to_xy to get the circle center in grid space, then create a Point object with a radius to make your circle, and finally use that object to check if other Point objects (i.e. your WRF domain grid points) fall within it.  This would give you the grid points that fall in the circle, but I don't know how accurate it is going to be with partial grid box intersection, etc.  Once you have the grid points inside the circle, querying the data and taking the average is easy (assuming you don't need to interpolate the partial grid boxes intersected by the circle). Whether any of this is better than what you've already done, I'm not sure. Hopefully other's have suggestions as well.

Regards,

Bill

Saverio Tubito

unread,
Jan 13, 2018, 5:28:37 AM1/13/18
to wrfpython-talk, s.tub...@gmail.com
Hi Bill, thanks a lot, i have solved. Now i have problem with wind speed and direction at mine levels.
With the temperature and humidity i have wrote:

t_1d = wrf.getvar(ncfile, "tc",timeidx=t)[:,y,x]
t_z = to_np(interp1d(t_1d, ht_1d, levels))

With the wind_speed,wind_direction  in z dim,which variable i have to take?
Regard
To unsubscribe from this group and stop receiving emails from it, send an email to wrfpython-tal...@ucar.edu.

Bill Ladwig

unread,
Jan 16, 2018, 12:11:36 PM1/16/18
to Saverio Tubito, wrfpython-talk
Hi Saverio,

If you plan on comparing wind speed and wind direction to earth relative coordinates (e.g. North, East, South, West), then the product you want for getvar is "uvmet_wspd_wdir".  This is what you would want to do if you are trying to compare the model wind to an observation. If you want the wind speed and wind direction in grid coordinates (e.g. Left, Right, Bottom, Top), then you would use "wspd_wdir".  This is what you would want to do if you are plotting in the same map projection as the WRF grid.  I'm going to assume you're comparing against observations, so I'll use the "uvmet_wspd_wdir" product.

Following your example:

wspd_wdir_earth =  wrf.getvar(ncfile, "uvmet_wspd_wdir",timeidx=t)
 
wspd_1d = wspd_wdir_earth[0,:,y,x] # Leftmost index is 0 for wspd
wdir_1d = wspd_wdir_earth[1,:,y,x]  # Leftmost index is 1 for wdir
 
wspd_z = to_np(interp1d(wspd_1d, ht_1d, levels))
wdir_z = to_np(interp1d(wdir_1d, ht_1d, levels)) 

Hope this helps,

Bill 

To unsubscribe from this group and stop receiving emails from it, send an email to wrfpython-talk+unsubscribe@ucar.edu.
Reply all
Reply to author
Forward
0 new messages