Loop to extract and specific lat and lon

151 views
Skip to first unread message

Alyce Sala Tenna

unread,
Oct 9, 2020, 8:24:03 AM10/9/20
to wrfpython-talk
Hello,

I am struggling with a loop to extract temperature at a specific lat and lon for every hours.

nl = wrf.extract_dim(f, 'Time')
len = nl-1

for i in range(0,len):
    temp_sub = temp[i,:,x_y[0],x_y[1]]

But it only returns the first level, and not all the hours in the files:

array([-273.15, -273.15, -273.15, -273.15, -273.15, -273.15, -273.15,

       -273.15, -273.15, -273.15, -273.15, -273.15, -273.15, -273.15,

       -273.15, -273.15, -273.15, -273.15, -273.15, -273.15, -273.15,

       -273.15, -273.15, -273.15, -273.15, -273.15, -273.15, -273.15,

       -273.15], dtype=float32)

Is there an easy solution for it? Ive been struggling for days!

Thanks

Alyce

Michaela Sizemore

unread,
Oct 9, 2020, 12:49:11 PM10/9/20
to wrfpython-talk, als...@gmail.com
Hi Alyce,
In my experience, if you are trying to use multiple time steps, you would have multiple wrfout files for each time step, however I don't think this is always the case. If this the case for you, this page may be of some assistance: https://www.reddit.com/r/learnpython/comments/57nn9v/python_loop_wrf_model_question/ . As that page says, you would want to first loop through to open/import each file of interest, then plot/print/manipulate the data from there. If you do not have multiple wrfout files, wrf-python has a function called 'extract_times' (https://wrf-python.readthedocs.io/en/latest/user_api/generated/wrf.extract_times.html) that may be able to help you. This function will return all of the time objects in the wrfout file which you may then use in a for loop to select the data "step" (date) and then use that step to feed into the for loop you have already been using. Give those two options a try and let me know if either works out for you!

Michaela

Alyce Sala Tenna

unread,
Oct 9, 2020, 2:03:27 PM10/9/20
to Michaela Sizemore, wrfpython-talk
Hi Michaela,

I only have one wrfout file, so I could do the second option you suggested, but the problem is I actually don’t know how to write the loop in the first instance as I’ve only just started using Python. 

If I use wrf.extract_times how would I then write a loop to extract temperature at 2m at the specific grid lat and long?

Thanks 
Alyce 

Michaela Sizemore

unread,
Oct 9, 2020, 3:03:06 PM10/9/20
to wrfpython-talk, als...@gmail.com, wrfpython-talk, Michaela Sizemore
Hi Alyce,
Would you mind attaching your wrfout file here? I haven't had much experience with extracting the time data of a single file, specifically one with multiple time steps, so I'd like to look at what is available in your file on my side and can further assist you from there. Overall, I believe it will be pretty similar to extracting the data from multiple wrfout files, but to be sure, I would like to experiment first before further offering advice. You can attach files using the paperclip symbol to the right of 'post message' below. Let me know if there are any difficulties with doing that (file size restrictions, file type restrictions, etc) and I will try to find a work around to that if it is the case.

Michaela 

Xin Zhang

unread,
Oct 9, 2020, 8:49:55 PM10/9/20
to wrfpython-talk, misi...@ucar.edu, als...@gmail.com, wrfpython-talk
Hi Alyce,

You can extract the variable at all times and use the x_y (calculated from lon_lat value) to get the grid from the 3D or 4D array.

Here's an example:

from netCDF4 import Dataset
from wrf import getvar, ll_to_xy, ALL_TIMES

ncfile = Dataset(filename)
var = getvar(ncfile, var_name, timeidx=ALL_TIMES)})
x_y = ll_to_xy(ncfile, lat, lon)
var_grid = var.sel(west_east=x_y[0], south_north=x_y[1])

Regards,
Xin
Message has been deleted

Alyce Sala Tenna

unread,
Oct 10, 2020, 2:53:40 AM10/10/20
to wrfpython-talk, xinzha...@gmail.com, misi...@ucar.edu, Alyce Sala Tenna, wrfpython-talk
Hi Xin,

Thank you so much! That makes so much sense now. I did not realise you had to specify west_east and north_south.

You have saved me lots of time and lots of stress!

Regards,
Alyce

Alyce Sala Tenna

unread,
Oct 11, 2020, 10:14:44 PM10/11/20
to wrfpython-talk, xinzha...@gmail.com, misi...@ucar.edu, Alyce Sala Tenna, wrfpython-talk
Hi Xi and Micheala,

I get 'nan' when trying to var_grid = var.sel(west_east=x_y[0], south_north=x_y[1]) for Temp at 2m and Temp 10m. 
I can however var.sel for temperature at 30m. Temp at 30m is interpolated from the same variable at temp interpolated at 10m.
Var.sel also works for wind speed and direction at 10m. 

Any thoughts? Is nothing in the actual data in the first instance?

Kind regards,
Alyce  
 

On Saturday, 10 October 2020 at 08:49:55 UTC+8 xinzha...@gmail.com wrote:

Xin Zhang

unread,
Oct 12, 2020, 4:57:56 AM10/12/20
to Alyce Sala Tenna, wrfpython-talk, misi...@ucar.edu
Hi Alyce,

You should check the original Temp2m variable in the wrfout file using ncview or python.

Here's the quick method of python:
Temp.isel(Time=0).plot()

If that's all 0 values, then you should check the setting in the namelist.input.
I remember Temp 2m is available for MYJ boundary option.
You can check it in the WRF USER GUIDE.

Regards,
Xin
Reply all
Reply to author
Forward
0 new messages