Giter Club home page Giter Club logo

wrf-python's Introduction

wrf-python

A collection of diagnostic and interpolation routines for use with output from the Weather Research and Forecasting (WRF-ARW) Model.

This package provides over 30 diagnostic calculations, several interpolation routines, and utilities to help with plotting via cartopy, basemap, or PyNGL. The functionality is similar to what is provided by the NCL WRF package.

Installation

conda install -c conda-forge wrf-python

Documentation

http://wrf-python.rtfd.org

Citation

If you use this software, please cite it as described at the WRF-Python - Citation page.


The National Center for Atmospheric Research is sponsored by the National Science Foundation. Any opinions, findings and conclusions or recommendations expressed in this material do not necessarily reflect the views of the National Science Foundation.

wrf-python's People

Contributors

andreas-h avatar bbonenfant avatar dependabot[bot] avatar dmey avatar erogluorhan avatar fedecutraro avatar hanschen avatar honnorat avatar irowebbn avatar kafitzgerald avatar kinow avatar kwadrat avatar letmaik avatar michaelavs avatar muchojp avatar pgamez avatar sfalmo avatar weinihou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wrf-python's Issues

uvmet10_wspd_wdir mixes up wind speed and direction

Using wrf.getvar(wrfin,'uvmet10_wspd_wdir), the returned DataArray has a dimension wpsd_wdir with values wspd and wdir. However, it seems that the wspd array contains the wind direction and vice versa.

Bug when reading wind from MFDataset

After loading an MFDataset with xarray,

ds = xr.open_mfdataset(filename)

I would like to load wind information. So I first create a list of the netCDF Dataset objects,

objs = [file_obj_.ds for file_obj_ in ds._file_obj.file_objs]

and then try to load using getvar:

wrf.getvar(objs, varname, timeidx=0)

This works just fine.

However, when I specify timeidx=None to read all time slices at once, I get a

/home/eb/software/wrf-python/1.0b3-intel-2016a-Python-3.5.1/lib/python3.5/site-packages/wrf_python-1.0b3-py3.5-linux-x86_64.egg/wrf/wind.py in _calc_wspd_wdir(u, v, two_d, units)
     97             (0, Ellipsis, slice(None), slice(None)))
     98 
---> 99     result[idxs0] = wspd[:]
    100     result[idxs1] = wdir[:]
    101 

ValueError: could not broadcast input array from shape (361,210,210) into shape (2,210,210)

I'm using version 1.0b3 of wrf-python.

Python 3.5 and scipy.io.netcdf coordinate issue

Although scipy.io.netcdf is not one of the recommended NetCDF readers for wrf-python, it should still work. A user reported an issue where a KeyError is being thrown because scipy.io.netcdf uses bytes strings for variable attributes, but then uses unicode strings for the variable names in the variables dictionary. The code that parses the coordinate information from the variable attributes needs to check for bytes strings and convert to utf-8 when this happens.

Updraft_Helicity Crashing with Dictionary

I am PhD student from the University of Balearic Islands (Spain) and I am using the module wrf-python to depict a wide range of wrf fields. I am performing EnKF data assimilation experiments, with the help of Louis Wicker (Oklahoma, NSSL), which requires an ensemble of simulations.

To use the diagnostic functions of the wrf-python (http://wrf-python.readthedocs.io/en/latest/diagnostics.html) to calculate for example, the mean sea level pressure field on the ensemble I am using the following sintax:

import wrf_dict_SYN_d02
slp = getvar(wrf_dict_SYN_d02.wrf_dict_d02, "slp", timeidx=ALL_TIMES)

where the wrf_dict_SYN_d02 is a dictionary where I defined the path of all ensemble members. When I am trying to compute slp, I do not have any problem and everything works correctly. However, when I am trying to compute the 'updraft helicity' I obtain the following error:

getvar(wrflist, "updraft_helicity", timeidx=ALL_TIMES)

TypeError                                 Traceback (most recent call last)
<ipython-input-24-aea088113e37> in <module>()
----> 1 kk=getvar(wrflist, "updraft_helicity", timeidx=ALL_TIMES)

/opt/anaconda/anaconda2/lib/python2.7/site-packages/wrf/routines.pyc in getvar(wrfin, varname, timeidx, method, squeeze, cache, meta, **kwargs)
    283 
    284     return _FUNC_MAP[actual_var](wrfin, timeidx, method, squeeze, cache, 
--> 285                                  meta, _key, **kwargs)
    286 

/opt/anaconda/anaconda2/lib/python2.7/site-packages/wrf/metadecorators.pyc in func_wrapper(wrapped, instance, args, kwargs)
    127         new_args[cache_argloc] = new_cache
    128 
--> 129         result = wrapped(*new_args)
    130 
    131         outname = ""

/opt/anaconda/anaconda2/lib/python2.7/site-packages/wrf/helicity.pyc in get_uh(wrfin, timeidx, method, squeeze, cache, meta, _key, bottom, top)
    177 
    178     ncvars = extract_vars(wrfin, timeidx, ("W", "PH", "PHB", "MAPFAC_M"),
--> 179                           method, squeeze, cache, meta=False, _key=None)
    180 
    181     wstag = ncvars["W"]

/opt/anaconda/anaconda2/lib/python2.7/site-packages/wrf/util.pyc in extract_vars(wrfin, timeidx, varnames, method, squeeze, cache, meta, _key)
   2178     return {var:_extract_var(wrfin, var, timeidx, None,
   2179                              method, squeeze, cache, meta, _key)
-> 2180             for var in varlist}
   2181 
   2182 

/opt/anaconda/anaconda2/lib/python2.7/site-packages/wrf/util.pyc in <dictcomp>((var,))
   2178     return {var:_extract_var(wrfin, var, timeidx, None,
   2179                              method, squeeze, cache, meta, _key)
-> 2180             for var in varlist}
   2181 
   2182 

/opt/anaconda/anaconda2/lib/python2.7/site-packages/wrf/util.pyc in _extract_var(wrfin, varname, timeidx, is_moving, method, squeeze, cache, meta, _key)
   2111         # Squeeze handled in this routine, so just return it
   2112         return combine_files(wrfin, varname, timeidx, is_moving, 
-> 2113                              method, squeeze, meta, _key)
   2114 
   2115     return result.squeeze() if squeeze else result

/opt/anaconda/anaconda2/lib/python2.7/site-packages/wrf/util.pyc in combine_files(wrfin, varname, timeidx, is_moving, method, squeeze, meta, _key)
   2010     # Dictionary is unique
   2011     if is_mapping(wrfseq):
-> 2012         outarr = _combine_dict(wrfseq, varname, timeidx, method, meta, _key)
   2013     elif method.lower() == "cat":
   2014         outarr = _cat_files(wrfseq, varname, timeidx, is_moving, 

/opt/anaconda/anaconda2/lib/python2.7/site-packages/wrf/util.pyc in _combine_dict(wrfdict, varname, timeidx, method, meta, _key)
    944                               timeidx, is_moving=is_moving, method=method,
    945                               squeeze=False, cache=None, meta=meta,
--> 946                               _key=_key[first_key])
    947 
    948     # Create the output data numpy array based on the first array

TypeError: 'NoneType' object has no attribute '__getitem__'

I do not know why the updraft helicity is giving this error and the slp, geopotential or wind fields do not.

I would like to know if you could help me to fix this problem or if you know any other python function that calculates the updraft helicity for an ensemble of members.

Thank you in advance!

UVMET does not correctly rotate winds to Earth coordinates for the rotated pole projection

For the rotated pole, the uvmet product does not perform any wind rotation. This behavior is correct for the standard lat/lon projection, but not when the pole is rotated. This is also broken in NCL and has been for years.

As a workaround, users should use the COSALPHA and SINALPHA variables and perform the rotation manually using:

u_earth = u * COSALPHA - v * SINALPHA
v_earth = v * COSALPHA + u * SINALPHA

AttributeError: 'function' object has no attribute 'get_max_dbz'

Hi,
I get the above Error for 'get_max_dbz' or 'get_ctt' (ctt module).
Is this a known bug?
Version 1.0.5

Traceback (most recent call last):
  File "dbz-test.py", line 8, in <module>
    a = dbz.get_max_dbz(ds)

Minimal working example

from netCDF4 import Dataset
from wrf import dbz, to_np, latlon_coords,get_basemap

ds = Dataset('wrfout_d02_2017-07-10_12:00:00')
print ds

a = dbz.get_max_dbz(ds)

lats, lons = latlon_coords(a)
bm = get_basemap(a)
x, y = bm(to_np(lons), to_np(lats))
bm.contourf(x, y, to_np(a))

Add column support for cape_2d

Cape_3d supports using a single vertical column, but cape_2d does not (same as NCL). However, in wrf-python, supporting columns for cape_2d should be possible.

plotting Lambert Conformal grids in the southern hemisphere with cartopy

Seems like there is something not quite right in the implementation of Lambert Conformal Grids in the Southern Hemisphere, the shape should be square, and it appears that some data gets cut off on the top of the map...verified this with PlateCaree projection...see below.

Here are the projection parameters for my domain over central South America:

print(slp.projection.cartopy)

<bound method WrfProj.cartopy of LambertConformal(stand_lon=-67.0, moad_cen_lat=-32.799991607666016, truelat1=-32.79999923706055, truelat2=-32.79999923706055, pole_lat=90.0, pole_lon=0.0)>

lambert conformal:
image

platecaree:
image

`getvar` doesn't work for geo files

I'm not sure if the getvar function is supposed to work with the geogrid output, but for me currently it doesn't: The geogrid output files contain 0000-00-00_00:00:00 as time, and getvar (obviously) cannot convert this.

Would it make sense to add a fix to getvar, so that in case the times cannot be converted, some magic happens?

PyNGL projection resources broken on Python 3.x

PyNGL for Python 3.x uses the native string type for the resources. Currently WRF-Python promotes Python 2.x strings to Unicode, then back to bytes to make the PyNGL resources. This breaks PyNGL on Python 3.x

Extract altitude of layer boundaries

Currently, the z diagnostic returns the "full model height". Is this the altitude of the layer midpoint or of the layer top? This should be clarified in the documentation.

On this note, in case z returns the layer midpoint, it would be useful to also have the layer interfaces available.

Having an issue plotting different variables from the diagnostic table

I am trying to use the example found here to plot temperature in my domain. The table of diagnostics says I can slide in "temp", but I am having an error and am not sure what it wants, much less a solution. (I just replaced "slp" in the sample code for "temp", just to test this whole thing out)
from netCDF4 import Dataset
import matplotlib.pyplot as plt from matplotlib.cm import get_cmap
``from mpl_toolkits.basemap import Basemap

``from wrf import to_np, getvar, smooth2d, get_basemap, latlon_coords

# Open the NetCDF file ncfile = Dataset("X:\WRF\AUG2022\Domain1\wrfout_d01_2017-08-20_12%3A30%3A00")``

# Get the sea level pressure
slp = getvar(ncfile, "temp")

# Smooth the sea level pressure since it tends to be noisy near the mountains
smooth_slp = smooth2d(slp, 3)

# Get the latitude and longitude points
lats, lons = latlon_coords(slp)

# Get the basemap object
bm = get_basemap(slp)

# Create a figure
fig = plt.figure(figsize=(12,9))

# Add geographic outlines
bm.drawcoastlines(linewidth=0.25)
bm.drawstates(linewidth=0.25)
bm.drawcountries(linewidth=0.25)

# Convert the lats and lons to x and y. Make sure you convert the lats and lons to
# numpy arrays via to_np, or basemap crashes with an undefined RuntimeError.
x, y = bm(to_np(lons), to_np(lats))

# Draw the contours and filled contours
bm.contour(x, y, to_np(smooth_slp), 10, colors="black")
bm.contourf(x, y, to_np(smooth_slp), 10, cmap=get_cmap("jet"))

# Add a color bar
plt.colorbar(shrink=.62)

plt.title("Sea Level Pressure (hPa)")

plt.show()

This is my error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-ba6c6362d2a8> in <module>()
34
35 # Draw the contours and filled contours
---> 36 bm.contour(x, y, to_np(smooth_slp), 10, colors="black")
37 bm.contourf(x, y, to_np(smooth_slp), 10, cmap=get_cmap("jet"))
38
C:\Users\Michael\Anaconda3\envs\wrf\lib\site-packages\mpl_toolkits\basemap_init_.pyc in with_transform(self, x, y, data, *args, **kwargs) 519 # convert lat/lon coords to map projection coords. 520 x, y = self(x,y) --> 521 return plotfunc(self,x,y,data,*args,**kwargs) 522 return with_transform 523
C:\Users\Michael\Anaconda3\envs\wrf\lib\site-packages\mpl_toolkits\basemap\__init__.pyc in contour(self, x, y, data, *args, **kwargs)
3566 mask = np.logical_or(ma.getmaskarray(data),xymask)
3567 data = ma.masked_array(data,mask=mask)
-> 3568 CS = ax.contour(x,y,data,*args,**kwargs)
3569 except:
3570 ax.hold(b)

``C:\Users\Michael\Anaconda3\envs\wrf\lib\site-packages\matplotlib\__init__.pyc in inner(ax, *args,`` ``**kwargs)``
``   1817                     warnings.warn(msg % (label_namer, func.__name__),``
``   1818                                   RuntimeWarning, stacklevel=2)``
``-> 1819             return func(ax, *args, **kwargs)``
``   1820         pre_doc = inner.__doc__``
``   1821         if pre_doc is None:``
``
``C:\Users\Michael\Anaconda3\envs\wrf\lib\site-packages\matplotlib\axes\_axes.pyc in contour(self,`` ``*args, **kwargs)``
``   5617             self.cla()``
``   5618         kwargs['filled'] = False``
``-> 5619         return mcontour.QuadContourSet(self, *args, **kwargs)``
``   5620     contour.__doc__ = mcontour.QuadContourSet.contour_doc``
``   5621 ``
``
``C:\Users\Michael\Anaconda3\envs\wrf\lib\site-packages\matplotlib\contour.pyc in __init__(self, ax,`` ``*args, **kwargs)``
``   1422         are described in QuadContourSet.contour_doc.``
``   1423         """
``-> 1424         ContourSet.__init__(self, ax, *args, **kwargs)``
``   1425 ``
``   1426     def _process_args(self, *args, **kwargs):``

``C:\Users\Michael\Anaconda3\envs\wrf\lib\site-packages\matplotlib\contour.pyc in __init__(self, ax,`` ``*args, **kwargs)``
``    861         self._transform = kwargs.get('transform', None)``
``    862 ``
``--> 863         self._process_args(*args, **kwargs)``
``    864         self._process_levels()``
``    865 ``

C:\Users\Michael\Anaconda3\envs\wrf\lib\site-packages\matplotlib\contour.pyc in _process_args(self, *args, **kwargs)
1443 self._corner_mask = mpl.rcParams['contour.corner_mask']
1444
-> 1445 x, y, z = self._contour_args(args, kwargs)
1446
1447 _mask = ma.getmask(z)
C:\Users\Michael\Anaconda3\envs\wrf\lib\site-packages\matplotlib\contour.pyc in _contour_args(self, args, kwargs) 1526 args = args[1:] 1527 elif Nargs <= 4: -> 1528 x, y, z = self._check_xyz(args[:3], kwargs) 1529 args = args[3:] 1530 else:``

``C:\Users\Michael\Anaconda3\envs\wrf\lib\site-packages\matplotlib\contour.pyc in _check_xyz(self,`` ``args, kwargs)``
``   1560 ``
``   1561         if z.ndim != 2:``
``-> 1562             raise TypeError("Input z must be a 2D array.")``
``   1563         else:``
``   1564             Ny, Nx = z.shape``
``
``TypeError: Input z must be a 2D array.```

Thank you so much in advance.
  

WrfProj does not have attributes ll_lat and ll_lon. vertcross fails because of this.

I am trying to use vertcross with a pivot point that is a lat, lon pair. This fails, and the bottom of the stack trace is at interputils.to_xy_coords. In interputils.to_xy_coords, the call of _ll_to_xy references WrfProj attributes ll_lat and ll_lon, but these are never defined anywhere. I tried manually adding ll_lat and ll_lon attributes to my projection object using the reference latitude and longitude in my WPS namelist, but this lead to the function returning an incorrect cross section (the latitude was all wrong and it didn't follow the 90° pivot angle I gave it, which is another issue). I don't understand enough about projections to know how this would be fixed.

getvar with uvmet10_wspd_wdir and ALL_TIMES gives broadcasting problem

`wrf.getvar(wrfin,uvmet10_wspd_wdir,timeidx=wrf.ALL_TIMES)' gives:
ValueError: could not broadcast input array from shape (35,599,599) into shape (2,599,599).

It should produce a 4d output: wspd/wdir, time, lat, lon (2,35,599,599).
uvmet10 works fine by the way, so the problem must be in the conversion to wspd/wdir.

TD, RH, CFRAC crash when xarray is not installed

Found when running continuous integration tests on Cheyenne without xarray installed. Crash message is below.

======================================================================
ERROR: test_cfrac (__main__.WRFVarsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "utests.py", line 73, in test
    my_vals = getvar(in_wrfnc, varname, timeidx=timeidx)
  File "/glade/u/home/ladwig/test/lib/python2.7/site-packages/wrf_python-1.0.1-py2.7-linux-x86_64.egg/wrf/routines.py", line 285, in getvar
    meta, _key, **kwargs)
  File "/glade/u/home/ladwig/test/lib/python2.7/site-packages/wrf_python-1.0.1-py2.7-linux-x86_64.egg/wrf/metadecorators.py", line 468, in func_wrapper
    return wrapped(*args, **kwargs)
  File "/glade/u/home/ladwig/test/lib/python2.7/site-packages/wrf_python-1.0.1-py2.7-linux-x86_64.egg/wrf/cloudfrac.py", line 86, in get_cloudfrac
    rh = _rh(qv, full_p, tk)
  File "/glade/u/home/ladwig/test/lib/python2.7/site-packages/wrf_python-1.0.1-py2.7-linux-x86_64.egg/wrf/decorators.py", line 522, in func_wrapper
    return wrapped(*args, **kwargs)
  File "/glade/u/home/ladwig/test/lib/python2.7/site-packages/wrf_python-1.0.1-py2.7-linux-x86_64.egg/wrf/decorators.py", line 189, in func_wrapper
    if isinstance(arg, DataArray):
NameError: global name 'DataArray' is not defined

======================================================================
ERROR: test_rh (__main__.WRFVarsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "utests.py", line 73, in test
    my_vals = getvar(in_wrfnc, varname, timeidx=timeidx)
  File "/glade/u/home/ladwig/test/lib/python2.7/site-packages/wrf_python-1.0.1-py2.7-linux-x86_64.egg/wrf/routines.py", line 285, in getvar
    meta, _key, **kwargs)
  File "/glade/u/home/ladwig/test/lib/python2.7/site-packages/wrf_python-1.0.1-py2.7-linux-x86_64.egg/wrf/metadecorators.py", line 87, in func_wrapper
    return wrapped(*args, **kwargs)
  File "/glade/u/home/ladwig/test/lib/python2.7/site-packages/wrf_python-1.0.1-py2.7-linux-x86_64.egg/wrf/rh.py", line 80, in get_rh
    rh = _rh(qvapor, full_p, tk)
  File "/glade/u/home/ladwig/test/lib/python2.7/site-packages/wrf_python-1.0.1-py2.7-linux-x86_64.egg/wrf/decorators.py", line 522, in func_wrapper
    return wrapped(*args, **kwargs)
  File "/glade/u/home/ladwig/test/lib/python2.7/site-packages/wrf_python-1.0.1-py2.7-linux-x86_64.egg/wrf/decorators.py", line 189, in func_wrapper
    if isinstance(arg, DataArray):
NameError: global name 'DataArray' is not defined

======================================================================
ERROR: test_td (__main__.WRFVarsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "utests.py", line 73, in test
    my_vals = getvar(in_wrfnc, varname, timeidx=timeidx)
  File "/glade/u/home/ladwig/test/lib/python2.7/site-packages/wrf_python-1.0.1-py2.7-linux-x86_64.egg/wrf/routines.py", line 285, in getvar
    meta, _key, **kwargs)
  File "/glade/u/home/ladwig/test/lib/python2.7/site-packages/wrf_python-1.0.1-py2.7-linux-x86_64.egg/wrf/metadecorators.py", line 87, in func_wrapper
    return wrapped(*args, **kwargs)
  File "/glade/u/home/ladwig/test/lib/python2.7/site-packages/wrf_python-1.0.1-py2.7-linux-x86_64.egg/wrf/decorators.py", line 45, in func_wrapper
    return do_conversion(wrapped(*args, **kwargs), unit_type,
  File "/glade/u/home/ladwig/test/lib/python2.7/site-packages/wrf_python-1.0.1-py2.7-linux-x86_64.egg/wrf/dewpoint.py", line 85, in get_dp
    td = _td(full_p, qvapor)
  File "/glade/u/home/ladwig/test/lib/python2.7/site-packages/wrf_python-1.0.1-py2.7-linux-x86_64.egg/wrf/decorators.py", line 522, in func_wrapper
    return wrapped(*args, **kwargs)
  File "/glade/u/home/ladwig/test/lib/python2.7/site-packages/wrf_python-1.0.1-py2.7-linux-x86_64.egg/wrf/decorators.py", line 189, in func_wrapper
    if isinstance(arg, DataArray):
NameError: global name 'DataArray' is not defined

----------------------------------------------------------------------

MercatorWithLatTS calculating same limits for x

For some WRF files, the cartopy.crs subclass (MercatorWithLatTS) calculates the same values for xlimit and ylimit, causing plotting to be blank. This is most likely due to the spherical globe. While cartopy has now added latitude_true_scale to Mercator, it will suffer from this same problem, so we're going to stick with our subclass. The fix is to use an epsilon when checking for xlimit equality.

extract_vars() and getvar() fail for WRF output variables DZS and ZS

Using either extract_vars() or getvar() to retrieve soil layer thicknesses (netCDF variables DZS and ZS) from a WRF output file fails with "KeyError: u'XLONG_Z'".

In [38]: foo = wrf.extract_vars(netCDF4.Dataset('/global/cscratch1/sd/twhilton/WRFv3.9_Sensitivity/FromScratch/WRFV3_urban/run/summen_sensitivity_urban/urban_d02_2009-06-02_12:00:00.nc'), wrf.ALL_TIMES, 'DZS')
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-38-5c5b74ce1fc9> in <module>()
----> 1 foo =     wrf.extract_vars(netCDF4.Dataset('/global/cscratch1/sd/twhilton/WRFv3.9_Sensitivity/FromScratch/WRFV3_urban/run/summen_sensitivity_urban/urban_d02_2009-06-02_12:00:00.nc'), wrf.ALL_TIMES, 'DZS')

/global/homes/t/twhilton/.conda/envs/cartopy_test_27/lib/python2.7/site-packages/wrf/util.pyc in extract_vars(wrfin, timeidx, varnames, method, squeeze, cache, meta, _key)    
   2178     return {var:_extract_var(wrfin, var, timeidx, None,
   2179                              method, squeeze, cache, meta, _key)
-> 2180             for var in varlist}
   2181
   2182

/global/homes/t/twhilton/.conda/envs/cartopy_test_27/lib/python2.7/site-packages/wrf/util.pyc in <dictcomp>((var,))
   2178     return {var:_extract_var(wrfin, var, timeidx, None,
   2179                              method, squeeze, cache, meta, _key)
-> 2180             for var in varlist}
   2181
   2182

/global/homes/t/twhilton/.conda/envs/cartopy_test_27/lib/python2.7/site-packages/wrf/util.pyc in _extract_var(wrfin, varname, timeidx, is_moving, method, squeeze, cache, meta, _key)
   2101                 is_moving = is_moving_domain(wrfin, varname, _key=_key)
   2102             result = _build_data_array(wrfin, varname, timeidx, is_moving,
-> 2103                                        multifile, _key)
   2104         else:
   2105             if not multitime:

    /global/homes/t/twhilton/.conda/envs/cartopy_test_27/lib/python2.7/site-packages/wrf/util.pyc in _build_data_array(wrfnc, varname, timeidx, is_moving_domain, is_multifile, _key)
   1212         lon_coord_vals = get_cached_item(_key, lon_coord_valkey)
   1213         if lon_coord_dims is None or lon_coord_vals is None:
-> 1214             lon_var = wrfnc.variables[lon_coord]
   1215             lon_coord_dims = lon_var.dimensions
   1216             lon_coord_vals = lon_var[:]

    KeyError: u'XLONG_Z'

The same command retrieves variable SMOIS successfully.

In [36]: foo = wrf.extract_vars(netCDF4.Dataset('/global/cscratch1/sd/twhilton/WRFv3.9_Sensitivity/FromScratch/WRFV3_urban/run/summen_sensitivity_urban/urban_d02_2009-06-02_12:00:00.nc'), wrf.ALL_TIMES, 'SMOIS')

In [37]: foo['SMOIS'].shape
Out[37]: (24, 4, 150, 114)

Memory leak appears in the function wrf.getvar

Today, i use the wrf-python to process my wrfout files, and loop to read variable and do some calculations, the code as follow is just a demo, for some reasons, i have to open same wrfout file many times, it doesn't matter for specifying the timeidx argument, but need a bigger sized wrfout file to reproduce this problem

from netCDF4 import Dataset
import wrf

for i in range(150):
    f = Dataset('wrfout_d01_2005-07-17_12_00_00.nc')
    p = wrf.getvar(f, 'pressure')
    f.close()

I use top to see the memory change, and found the used memory by my program continues to increase.

Finally, i found these memory leak can be linked to the Decorator in the wrf/pressure.py
@copy_and_set_metadata(copy_varname=either("P", "PRES"), name="pressure", description="pressure") for pressure variables.
If disable the xarray by using wrf.disable_xarray(), this memory leak question disappeared.

with following code, one can see the difference between enabled and disabled xarray

from netCDF4 import Dataset
import wrf
from multiprocessing import Pool

def do(jobid):
    if jobid == 1:
        wrf.disable_xarray()
    for i in range(150):
        f = Dataset('wrfout_d01_2005-07-17_12_00_00.nc')
        slp = wrf.getvar(f, 'slp')
        f.close()

pool = Pool(2)
pool.map(do, [0, 1])
pool.close()
pool.join()

memoryleakofwrf-python

I think the Decorator function copy_and_set_metadata in the wrf/metadecorators.py maybe need to revise, though i still do not know the reason for raising this question.

Special decorators don't handle missing arrays due to join operations

The standard left indexing decorator handles fully missing arrays as a result join operations when the number of time steps in the final file is less than the other files. This capability was never added to the special decorators for cape, cloudfrac, and uvmet. This was discovered while fixing #19.

Also related to #21 #22 .

Timeseries plots

Hello,

Could you provide a few simple examples to plot timeseries data (e.g., temperature) from a select point?

I am evaluating wrf-python package to make plots other than maps, so such an example would be useful to deal with 1D time-series data. Something similar to what NCL has here -- https://www.ncl.ucar.edu/Applications/wrfxy.shtml

Thanks.

support xarray.Dataset as input to get_cartopy and getvar

it would be great if the get_cartopy and getvar functions would accept a xarray.Dataset as var input. Obviously, this can only work properly if the Dataset was loaded with a standard xarray.open_dataset call and the metadata were not modified, but still, it would be very convenient.

cachetest.py failing

When the configuration parameters were moved to threadlocal storage, they were only being initialized in the main thread. The child threads aren't having their config options initialized, so this is causing errors for the unit test. I doubt anyone is using wrf-python inside of python threads, but for the stray web app developer, this could be an issue since web apps sometimes run in both threads and processes.

Cartopy is slower compared to Basemap

Hello,

I am using wrf-python to make some horizontal maps. Cartopy is advertised as the future mapping, yet it is significantly slow compared to Basemap when I use large arrays.

plt.contourf(to_np(lons), to_np(lats), to_np(t2), np.arange(294,306.1,0.2), transform=crs.PlateCarree(),
             cmap=plt.cm.jet)  

Here my t2 array (2-m temperature) has a shape of 600x800. Currently the following contouring operation is about 10x slower than basemap. Although, lowering contour intervals speeds-up the plotting a bit, I am really interested in making high-resolution plots. My experiments show that slowing is due to "transform=crs.PlateCarree()" so this is most likely stemming from Cartopy's plotting functionality.

Are you aware of a similar plotting behavior? I have examples that are plotting T2 both in Basemap and Cartopy. Let me know if you are interested in testing, I can also provide WRF files.

Thanks.

Move work arrays out of Fortran

Several routines (ctt, cape_2d, cape_3d) use work arrays that can consume quite a bit of stack memory, which can cause problems for some users (segfaults if environment not set up correctly, Windows crashes, etc). Let's move these to the Python/NCL side and have it manage the memory.

Binary wheels for Linux/macOS/Windows

Currently you only provide binary packages via conda. To support pip install wrf-python more generally I created a repository which builds those wheels using Travis CI/Appveyor: https://github.com/letmaik/wrf-python-wheels. It uses https://github.com/matthew-brett/multibuild and follows the concrete adaptation from https://github.com/MacPython/scipy-wheels which had similar requirements, e.g. building Fortran code using numpy distutils.

You can see the build logs for Linux and macOS here:
https://travis-ci.org/letmaik/wrf-python-wheels/builds/322376418
And for Windows including downloadable wheels (see artifacts tab in each job):
https://ci.appveyor.com/project/letmaik/wrf-python-wheels/build/1.0.5

Travis CI doesn't have free artifact storage, but if you guys like the general idea then maybe we can ask whether you can use the Rackspace container that scikit-learn uses as it says in the README of the multibuild repo:

# Contact Matthew Brett, or the
# scikit-learn team, for permission (and the API key) to upload to
# the Rackspace account used here, or use your own account.

The idea is that whenever you release a new version (or for dev builds), you would trigger a build in the wrf-python-wheels repository through a commit. This then builds and uploads wheels to some storage where you download the wheels, do some tests if you like, and then upload them to PyPI.

I'm happy to transfer the repo over to your GitHub org.

Rename the internal get_(product) routines and modules

Some of the module names that contain the 'get_(product)' internal methods have the same name as the raw computational routine that is added to the public API. Some users are getting errors while trying to use the module directly. While this type of usage is invalid, it shouldn't error out for people that like to live dangerously. Some of these modules include dbz, ctt, and rh. Refer to #32.

Make a Utility Similar to NCL's plotgrids_new.ncl

It would be nice to have a utility to view the domain nests, similar to plotgrids_new.ncl. This way wrf-python users would no longer need to install NCL just to view the domain (prior to running the model).

Improve xarray support

Current support for xarray is fairly primitive. Some features that would be nice are:

  • Add support for using xarray.Dataset directly instead of extracting the NetCDF object.
    • Being able to subset the Dataset's data prior to reading so the entire array isn't used.
    • Some of this may be better handled using input iterables classes as wrappers.
    • See also issue #6
  • Add multidimensional indexing support by using the latlon_to_xy routines.
  • Make isel, sel, and other indexing features work correctly.

This list will be updated as needed.

Add a function to check if lat/lon points are within the WRF domain

Had a user on ncl-talk try to use wrf_user_ll_to_ij to check if observations are within the WRF domain. The current ll_to_xy routines are simple lat/lon to x/y converters that don't care what the domain size is. A new function should be created to add a check to see if the points are outside of the domain bounds. Not sure how the return should be -- either an array of booleans, or sequences of values for in and out of domain. The boolean way seems to be the more common way of doing this in NCL and numpy, and will work with masking.

Lat/Lon to X/Y routines should be made to work with a DataArray argument

While working on #38, realized that there is no easy way to do lat/lon to x/y (and vice versa) with sliced DataArray variables. Currently, the WRF data file is used to extract the relevant information, and slices are not possible.

The DataArray variable needs to contain the lat/lon and projection information in order for this to work, so an error should be thrown if not (or fall back on the WRF data file). In interputils.py, to_xy_coords() needs modification, along with the _ll_to_xy() in interp.py.

The metadata handling for cross sections (and line interpolation) should also be updated to take advantage of this.

Issues for using getvar() with files contain multiple timeidx or without 'XTIME'

Hi all,
I got a problem when using wrf.getvar to read variables from netCDF4 objects for both single and multiple time cases.

===========================================================

D02 = nc.Dataset('wrfout_d02_2016-08-19_06:00:00')
slp_02 = wrf.getvar(D02, 'slp’)
# The file is readable via netCDF4

==================================================================

KeyError                                  Traceback (most recent call last)
<ipython-input-3-eb8ee2965f9c> in <module>()
---> 14 slp_02 = wrf.getvar(D02, 'slp')
......
/Users/yingkai/anaconda2/lib/python2.7/site-packages/wrf/util.pyc in _build_data_array(wrfnc, varname, timeidx, is_moving_domain, is_multifile, _key)
   1237 
   1238                 if time_coord_vals is None:
-> 1239                     time_coord_vals = wrfnc.variables[time_coord][:]
   1240 
   1241                     if not is_multifile:

KeyError: u'XTIME'

=======================================================

timeidx = range(31)
ua = wrf.getvar(nc_obj, 'ua', timeidx=timeidx)

=======================================================

<ipython-input-2-324fca9a34ef> in wrf_reader_2d(filename, wind_plev)
---> 65     ua = wrf.getvar(nc_obj, 'ua', timeidx=timeidx)
/anaconda2/lib/python2.7/site-packages/wrf/routines.pyc in getvar(wrfin, varname, timeidx, method, squeeze, cache, meta, **kwargs)
    283 
    284     return _FUNC_MAP[actual_var](wrfin, timeidx, method, squeeze, cache, 
--> 285                                  meta, _key, **kwargs)
    286 
/anaconda2/lib/python2.7/site-packages/wrf/metadecorators.pyc in func_wrapper(wrapped, instance, args, kwargs)
    260         copy_var = extract_vars(wrfin, timeidx, _copy_varname, 
    261                                 method, squeeze, cache,
--> 262                                 meta=True, _key=_key)[_copy_varname]
    263 
    264         # Make a copy so we don't modify a user supplied cache
/anaconda2/lib/python2.7/site-packages/wrf/util.pyc in extract_vars(wrfin, timeidx, varnames, method, squeeze, cache, meta, _key)
   2172     return {var:_extract_var(wrfin, var, timeidx, None,
   2173                              method, squeeze, cache, meta, _key)
-> 2174             for var in varlist}
   2175 
   2176 
/anaconda2/lib/python2.7/site-packages/wrf/util.pyc in <dictcomp>((var,))
   2172     return {var:_extract_var(wrfin, var, timeidx, None,
   2173                              method, squeeze, cache, meta, _key)
-> 2174             for var in varlist}
   2175 
   2176 
/anaconda2/lib/python2.7/site-packages/wrf/util.pyc in _extract_var(wrfin, varname, timeidx, is_moving, method, squeeze, cache, meta, _key)
   2095                 is_moving = is_moving_domain(wrfin, varname, _key=_key)
   2096             result = _build_data_array(wrfin, varname, timeidx, is_moving,
-> 2097                                        multifile, _key)
   2098         else:
   2099             if not multitime:
/anaconda2/lib/python2.7/site-packages/wrf/util.pyc in _build_data_array(wrfnc, varname, timeidx, is_moving_domain, is_multifile, _key)
   1270 
   1271         proj_params = get_proj_params(wrfnc)
-> 1272         proj = getproj(**proj_params)
   1273         attrs["projection"] = proj
   1274 
/anaconda2/lib/python2.7/site-packages/wrf/projection.pyc in getproj(**proj_params)
   1081         return LambertConformal(**proj_params)
   1082     elif proj_type == ProjectionTypes.POLAR_STEREOGRAPHIC:
-> 1083         return PolarStereographic(**proj_params)
   1084     elif proj_type == ProjectionTypes.MERCATOR:
   1085         return Mercator(**proj_params)
/anaconda2/lib/python2.7/site-packages/wrf/projection.pyc in __init__(self, **proj_params)
    676 
    677         """
--> 678         super(PolarStereographic, self).__init__(bottom_left, 
    679                         top_right, lats, lons, **proj_params)
    680         self._hemi = -90. if self.truelat1 < 0 else 90.

NameError: global name 'bottom_left' is not defined

==========================================================
Actually I was working ok last week, but I did a
conda update --all
for installing gdal and other packages. not sure if that raises the problem.

Thanks

Uniqueness issues for coordinate caching when running cells in jupyter notebook

When using jupyter notebook, if you re-run a cell and change the netcdf file, the same object id may be returned causing issues with the coordinate cache (usually crashes due to size mismatches when creating a DataArray). A smarter hashing is needed for single files and sequences of files.

As a workaround, the coordinate cache can be disabled by:

from wrf import set_cache_size
set_cache_size(0)

issue about mini-conda install wrf-python

Hello,
I am just using mini-conda to install the wrf-python package. While I tested if it can work, I used the code and got error like " File "./test.py", line 5, in
from netCDF4 import Dataset
File "/home1/03716/tg830159/.local/lib/python2.7/site-packages/netCDF4/init.py", line 3, in
from ._netCDF4 import *
ImportError: /home1/03716/tg830159/.local/lib/python2.7/site-packages/netCDF4/_netCDF4.so: undefined symbol: PyUnicodeUCS2_DecodeASCII"
While if I used a python version that I install before, no netcdf error but can not use the wrf-python package. Anyone can give me some idea?

"#!/usr/bin/env python

from future import print_function
from netCDF4 import Dataset
from wrf import getvar

diri = './'
ncfile = Dataset(diri + "wrfout_d01_2015-09-28_12:00:00")
#wrfout_d01_2015-09-28_12:00:00

Get the Sea Level Pressure

slp = getvar(ncfile, "slp")
print(slp)
"

Document wind direction

In the Table of Available Diagnostics, please indicate if all the wdir diagnostics mean the wind_to_direction or the wind_from_direction (speaking in CF standard name language).

Custom vertical dimension in vinterp

Hello,

It would be nice to allow wrf.vinterp() use a custom name for the vertical dimension it creates. Currently, it is called interp_level and cannot be changed.

This would allow the interpolation along an existing coordinate variable, or along different vertical axes.

Example:

from netCDF4 import Dataset
import wrf
nc = Dataset("test/ci_tests/ci_test_file.nc")
tk = wrf.getvar(ncfile, "tk")
levels = [925, 850, 750, 500]

tkp_def = wrf.vinterp(nc, field=tk, vert_coord="pressure", interp_levels=levels)
tkp_new = wrf.vinterp(nc, field=tk, vert_coord="pressure", interp_levels=levels, vdim_name="p_levels")
print("default:", tkp_def.dims)
print("named dim:", tkp_new.dims)

result:

default: ('interp_level', 'south_north', 'west_east')
named dim: ('p_levels', 'south_north', 'west_east')

interpline/vinterp: Show warning if latlon=True but field{2,3}d.coords is None

interpline and vinterp have an option latlon to return the interpolated latitude and longitude coordinates along the line. However, if the input field (field2d or field3d) doesn't contain information about the lat/lon coordinates, e.g. if it's a regular numpy array, the functions will silently fail to provide the interpolated lat/lon, which can be confusing.

It would be nice if the functions could alert the user when latlon=True but latlon_coordvars(field{2,3}d.coords) returns None. I don't know what the best way is to show this warning though, otherwise I would be willing to provide a Pull Request.

Specify x increments for vertcross function

It seems to me that currently the vertcross function's return value includes one x point for every WRF grid cell crossed by the line.

For some applications (i.e., integrate concentrations along the x axis) it would be very useful if one could not only specify levels to prescribe the points in z direction, but also specify the number of sampling points along the cross-section which vertcross result should contain.

Would you be interested in including this functionality in wrf-python, or should I rather do this as a post-processing step on my own?

Odd cloudfrac diagnostic output in regions with high terrain

I've noticed some odd behavior in the low cloudfrac diagnostic output in high altitude regions (see plots below). Effectively areas that should have no data values (where the low region intersects with the terrain) instead show odd striped output. I did a bit of digging into the source code and it looks like this is a case that might not be accounted for the in underlying Fortran code computing the diagnostic (wrf_cloud_fracf.f90).

Example low cloudfrac output in a problematic region:
cloudfraction_low
Areas shown in white are where the bottom model level has pressure values < 80000
pressurev2_bottomlayer

Read-only error in getvar(wrfin, 'rh')

Hi,

I've been using wrf-python for a while without any problems, however after the WRF V3.8.1 update, I'm getting the following error when specifically using getvar(wrfin, 'rh'):

import wrf
from scipy.io.netcdf import netcdf_file

WindFileName = '/home/user/WRF/wrfout_d04_2016-08-16_12:00:00'

WindFile = netcdf_file(WindFileName, 'r', mmap=True)

wrf.getvar(WindFile, 'rh')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-24-6174672b208b> in <module>()
----> 1 wrf.getvar(WindFile, 'rh')

~/anaconda3/lib/python3.6/site-packages/wrf/routines.py in getvar(wrfin, varname, timeidx, method, squeeze, cache, meta, **kwargs)
    288 
    289     return _FUNC_MAP[actual_var](wrfin, timeidx, method, squeeze, cache, 
--> 290                                  meta, _key, **kwargs)
    291 

~/anaconda3/lib/python3.6/site-packages/wrf/metadecorators.py in func_wrapper(wrapped, instance, args, kwargs)
    128         new_args[cache_argloc] = new_cache
    129 
--> 130         result = wrapped(*new_args)
    131 
    132         outname = ""

~/anaconda3/lib/python3.6/site-packages/wrf/g_rh.py in get_rh(wrfin, timeidx, method, squeeze, cache, meta, _key)
     76     full_t = t + Constants.T_BASE
     77     full_p = p + pb
---> 78     qvapor[qvapor < 0] = 0
     79     tk = _tk(full_p, full_t)
     80     rh = _rh(qvapor, full_p, tk)

ValueError: assignment destination is read-only

Is there a quick fix for this issue?

Cheers,
Trent.

No 'th' alias for 'theta'

Hi,
The documentation indicates that 'th' should be a valid alias for 'theta' like 'eth' is for 'theta_e' and 'omg' is for 'omega'. Unfortunately this isn't one of the aliases defined in routines.py, so it throws a ValueError unexpectedly. Could the documentation be brought in line with the code (or vice-versa)?

T_BASE=300

Hello!
I was wondering, why T_BASE=300 K?

May be it should depend on what is in the namelist.base_temp=290 (which is in my case) ?
Or I wrong?
Thank you!

Compatibility with numpy 1.13

It would be nice if wrf-python were compatible with numpy 1.13, which is currently not the case, at least on conda-forge.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.