Scheduled Downtime
On Friday 21 April 2023 @ 5pm MT, this website will be down for maintenance and expected to return online the morning of 24 April 2023 at the latest

cannot use the wrf.getvar()

srinaldi

New member
Hello everyone.

I've recently switched to a Macbook Pro M4 and I've installed all the libraries I need for WRF post-processing, with the wrf-python installed from source code. I want to use a script for plotting I was using on my previous laptop (running with Ubuntu distro), but now I get errors when using the wrf.getvar() function (see below).
Have you ever had this issue, or do you know how to solve it?

Thank you very much!!

from netCDF4 import Dataset
from wrf import getvar

file = "/WRF/test_control_sim/wrfout_d02_2018-09-26_22:00:00"
ds = Dataset(file)
lat = getvar(ds, "XLAT")
File src/netCDF4/_netCDF4.pyx:2695, in netCDF4._netCDF4.Dataset.__reduce__()
NotImplementedError: Dataset is not picklable
 
Last edited:
Please try the scripts below:
from netCDF4 import Dataset
from wrf import getvar

fname = "/WRF/test_control_sim/wrfout_d02_2018-09-26_22:00:00"
ds = Dataset(fname)
lat = getvar(ds, "XLAT").values

Let me know if you still cannot make it work.
 
Please try the scripts below:
from netCDF4 import Dataset
from wrf import getvar

fname = "/WRF/test_control_sim/wrfout_d02_2018-09-26_22:00:00"
ds = Dataset(fname)
lat = getvar(ds, "XLAT").values

Let me know if you still cannot make it work.
Hi Ming Chen,

thank you for your help. Unfortunately, adding the .values doesn't help. I paste you the complete error message, hope this can help:

---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
Cell In[1], line 6
4 fname = "/WRF/test_control_sim/wrfout_d02_2018-09-26_22:00:00"
5 ds = Dataset(fname)
----> 6 lat = getvar(ds, "XLAT").values

File /opt/homebrew/Caskroom/miniconda/base/envs/wrf_env/lib/python3.11/site-packages/wrf/routines.py:339, in getvar(wrfin, varname, timeidx, method, squeeze, cache, meta, **kwargs)
231 def getvar(wrfin, varname, timeidx=0,
232 method="cat", squeeze=True, cache=None, meta=True,
233 **kwargs):
235 """Returns basic diagnostics from the WRF ARW model output.
236
237 A table of all available diagnostics is below.
(...) 336
337 """
--> 339 _key = get_id(wrfin)
341 wrfin = get_iterable(wrfin)
343 if is_standard_wrf_var(wrfin, varname) and varname != "Times":

File /opt/homebrew/Caskroom/miniconda/base/envs/wrf_env/lib/python3.11/site-packages/wrf/util.py:3102, in get_id(obj, prefix)
3100 if not is_mapping(obj):
3101 _obj = get_iterable(obj)
-> 3102 _next = next(iter(_obj))
3103 return get_id(_next, prefix + str(id(obj)))
3105 # For each key in the mapping, recursively call get_id until
3106 # until a non-mapping is found

File /opt/homebrew/Caskroom/miniconda/base/envs/wrf_env/lib/python3.11/site-packages/wrf/util.py:363, in IterWrapper.__iter__(self)
361 return gen_copy
362 else:
--> 363 obj_copy = copy(self._wrapped)
364 return obj_copy.__iter__()

File /opt/homebrew/Caskroom/miniconda/base/envs/wrf_env/lib/python3.11/copy.py:92, in copy(x)
90 reductor = getattr(x, "__reduce_ex__", None)
91 if reductor is not None:
---> 92 rv = reductor(4)
93 else:
94 reductor = getattr(x, "__reduce__", None)

File src/netCDF4/_netCDF4.pyx:2695, in netCDF4._netCDF4.Dataset.__reduce__()

NotImplementedError: Dataset is not picklable


Thank you again.
 
Sorry sorry that I messed up the variable name. Please try:

from netCDF4 import Dataset

import numpy as np

from wrf import getvar

fname = "/WRF/test_control_sim/wrfout_d02_2018-09-26_22:00:00"

ds = Dataset(fname)

lat = getvar(ds,"lat")

print(lat.shape)
 
Sorry sorry that I messed up the variable name. Please try:

from netCDF4 import Dataset

import numpy as np

from wrf import getvar

fname = "/WRF/test_control_sim/wrfout_d02_2018-09-26_22:00:00"

ds = Dataset(fname)

lat = getvar(ds,"lat")

print(lat.shape)

No worries!

I tried this and it gives me again the error NotImplementedError: Dataset is not picklable.

I also tried opening the file using xarray:

import xarray as xr

fname = "/WRF/test_control_sim/wrfout_d02_2018-09-26_22:00:00"
ds = xr.open_dataset(fname)
lat = ds['XLAT']
print(lat.shape)

> (1, 99, 99)
 
Thanks for the update. I am glad it works for you !

The function getvar from wrf python works fine on my side. I am perplexed why it doesn't work for you. Please keep me updated if you figure out the reason. Thanks in advance.
 
Last edited:
Hi, good news!
I fixed the error, the problem was in the version of netcdf package (check this) so I had to downgraded it to v1.7.2.

I've attached here some notes on how I installed wrf-python on macOS, in case anyone get stuck in that too.

Thank you very much, and have a great day :)
 

Attachments

  • Installing wrf-python on macOS from source code.pdf
    117.3 KB · Views: 1
Hi, good news!
I fixed the error, the problem was in the version of netcdf package (check this) so I had to downgraded it to v1.7.2.

I've attached here some notes on how I installed wrf-python on macOS, in case anyone get stuck in that too.

Thank you very much, and have a great day :)
another option is conda using this simple yaml file
 

Attachments

  • wrf-python-stable.zip
    667 bytes · Views: 0
Top