我有一个netcdf文件,其中包含一个月的每日数据。在此文件中,存在irregular latitude and longitude
点数据。我想创建time[0]
该数据的绘图或任何时间,但是结果似乎不正确。如何显示该图nan-space
?
数据文件 https://www.dropbox.com/s/ll35zh4k5ws7nnh/day1.nc?dl=0
码
import xarray as xr
month_daily1 = xr.open_dataset('/Daily_Month/1/day1.nc')
month_daily1
<xarray.Dataset>
Dimensions: (Lat: 175, Lon: 200, time: 31)
Coordinates:
* time (time) datetime64[ns] 2018-01-01 ... 2018-01-31
* Lat (Lat) float64 29.92 29.93 29.94 ... 33.0 33.01 33.02
* Lon (Lon) float64 47.61 47.62 47.63 ... 50.5 50.51 50.52
Data variables:
Alt (time, Lat, Lon) float64 ...
Temperature (time, Lat, Lon) float64 ...
Relative Humidity (time, Lat, Lon) float64 ...
Wind speed (time, Lat, Lon) float64 ...
Wind direction (time, Lat, Lon) float64 ...
Short-wave irradiation (time, Lat, Lon) float64 ...
# convert kelvin to celsius
data_nonnull = month_daily1.dropna(dim ='time', how='all')
air = data_nonnull.Temperature - 273.15
air
<xarray.DataArray 'Temperature' (time: 31, Lat: 175, Lon: 200)>
array([[[nan, nan, ..., nan, nan],
[nan, nan, ..., nan, nan],
...,
[[nan, nan, ..., nan, nan],
[nan, nan, ..., nan, nan],
[[nan, nan, ..., nan, nan],
[nan, nan, ..., nan, nan],
Coordinates:
* time (time) datetime64[ns] 2018-01-01 2018-01-02 ... 2018-01-31
* Lat (Lat) float64 29.92 29.93 29.94 29.95 ... 32.99 33.0 33.01 33.02
* Lon (Lon) float64 47.61 47.62 47.63 47.64 ... 50.41 50.5 50.51 50.52
%matplotlib inline
import matplotlib.pyplot as plt
ax = plt.subplot(projection=ccrs.PlateCarree())
air2d = air.isel(time= 0)
air2d.plot.pcolormesh('Lon', 'Lat');
我对XArray不太满意,因此建议使用模块netCDF4解决方案:
#!/usr/bin/env ipython
import xarray as xr
import matplotlib as mpl
mpl.use('tkagg')
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
# =======================================================
from netCDF4 import Dataset
ncin=Dataset('day1.nc');
tempin=ncin.variables['Temperature'][0,:,:]- 273.15;
lonin=ncin.variables['Lon'][:];
latin=ncin.variables['Lat'][:];
ncin.close()
# -------------------------------------------------------
from scipy.interpolate import griddata
import numpy as np
kk=np.where(np.isnan(np.array(tempin).flatten())==False)
lonm,latm=np.meshgrid(lonin,latin);
tinterp=griddata((lonm.flatten()[kk],latm.flatten()[kk]),tempin.flatten()[kk],(lonm,latm));
ax = plt.subplot(121,projection=ccrs.PlateCarree())
ax.pcolormesh(lonin,latin,tempin);
ax = plt.subplot(122,projection=ccrs.PlateCarree())
ax.pcolormesh(lonin,latin,tinterp);
plt.show()
最终结果如下所示:左边是原始图像,右边是插值(nan掉落的数字)。
我可以提出一个答案,在这里我结合XArray和SciPy的的GridData因为interpolate_na
不工作很不错(看一部分,并与结果filled_a
,filled_b
)对我来说:
#!/usr/bin/env ipython
import xarray as xr
import matplotlib as mpl
mpl.use('tkagg')
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
# =======================================================
month_daily1 = xr.open_dataset('day1.nc')
# convert kelvin to celsius
data_nonnull = month_daily1.dropna(dim ='time', how='all')
air = data_nonnull.Temperature - 273.15
air2d = air.isel(time= 0)
# =======================================================
ax = plt.subplot(121,projection=ccrs.PlateCarree())
air2d.plot.pcolormesh('Lon', 'Lat');
ax = plt.subplot(122,projection=ccrs.PlateCarree())
filled_a=air2d.interpolate_na(dim='Lat');
filled_b=filled_a.interpolate_na(dim='Lon');
filled_c=filled_b.interpolate_na(dim='Lat');
filled_c.plot.pcolormesh('Lon', 'Lat');
plt.show()
# =======================================================
tempin=air2d.values[:];
lonin=air2d.Lon
latin=air2d.Lat
# -------------------------------------------------------
from scipy.interpolate import griddata
import numpy as np
kk=np.where(np.isnan(np.array(tempin).flatten())==False)
lonm,latm=np.meshgrid(lonin,latin);
tinterp=griddata((lonm.flatten()[kk],latm.flatten()[kk]),tempin.flatten()[kk],(lonm,latm));
ax = plt.subplot(121,projection=ccrs.PlateCarree())
ax.pcolormesh(lonin,latin,tempin);
ax = plt.subplot(122,projection=ccrs.PlateCarree())
ax.pcolormesh(lonin,latin,tinterp);
plt.show()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句