How do I make this code faster #2821
-
I am trying to analyze some climate data. Below is the code pwg_eu10 = xr.open_mfdataset('*/*PGW_EU10*.nc')
pwg_ev10= xr.open_mfdataset('*/*PGW_EV10*.nc') this is the main code that I want to run and for some reason it is incrediblity slow. Just to calculate the wind speed takes about 2 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
I'm working with @DOptimusPrime on this. The issue is a very slow |
Beta Was this translation helpful? Give feedback.
-
I did some testing locally with some synthetic data I made of similar size (though for only two time chunks of 2208): import metpy.calc as mpcalc
from metpy.units import units
import numpy as np
import xarray as xr
ntime = 2208
nlat = 1015
nlon = 1359
# Create one big array and then tell xarray to turn into Dask chunks
u = v = xr.DataArray(np.full((2 * ntime, nlat, nlon), 1., dtype=np.float32),
coords={'time': np.arange(2 * ntime), 'lat': np.linspace(-90, 90, nlat), 'lon': np.linspace(0, 360, nlon)},
attrs={'units': 'm/s'}).chunk({'time': 2208})
# Calculate, .compute() tells Dask to actually do the computation
s = mpcalc.wind_speed(u, v).compute() The above code, which computes windspeed on the entire grid (not just one lat/lon point), takes ~100 seconds on my MacBook laptop with 64GB of memory (~40s of that time is spent just generating the xarray source data). What that means is that your problem is I/O-bound, meaning of that two hours for your full dataset, the time is mostly spent loading the data from disk into memory. As far as making it quicker:
eu10 = pwg_eu10.EU10[:, [lat1, lat2, lat3], [lon1, lon2, lon3]]
ev10 = pwg_ev10.EV10[:, [lat1, lat2, lat3], [lon1, lon2, lon3]] I'd expect this to still take 2 hours, but at least it doesn't scale to 12.
You might want to try asking on the Pangeo Discourse or xarray GitHub discussions since this isn't really a MetPy-specific question (the metpy calculation in question is a glorified wrapper around |
Beta Was this translation helpful? Give feedback.
I did some testing locally with some synthetic data I made of similar size (though for only two time chunks of 2208):