[英]How to remove latitude and longitude (they're constants) from variables in netcdf dataset using xarray?
I have a NetCDF data set I am trying to remove latitude and longitude data from my data variables so they can be indexed properly.我有一个 NetCDF 数据集我试图从我的数据变量中删除纬度和经度数据,以便它们可以被正确索引。 The shape of each data variable is always (x, 1, 1) with x being my number of data points and the 1's representing the static longitude and latitudes.
每个数据变量的形状始终为 (x, 1, 1),其中 x 是我的数据点数,1 代表静态经度和纬度。 I've tried
xarray.Dataset.squeeze
and xarray.Dataset.drop
and xarray.DataArray.drop_vars
targeting latitude and longitude.我试过
xarray.Dataset.squeeze
和xarray.Dataset.drop
和xarray.DataArray.drop_vars
定位纬度和经度。 I also tried it with a pandas data frame and the latitude and longitudes still stay glued to my variables and prevent indexing properly.我还用熊猫数据框尝试了它,纬度和经度仍然粘在我的变量上并阻止正确索引。 If you copy my code below you will see the variable
'wave_height'
shows a shape of (3411,1,1).如果您复制下面的代码,您将看到变量
'wave_height'
显示的形状为 (3411,1,1)。 I want a function that makes this shape just (3411,).我想要一个使这个形状只是 (3411,) 的函数。
url = 'https://dods.ndbc.noaa.gov/thredds/fileServer/data/stdmet/46077/46077h2021.nc'
reqSpectra = urllib.request.Request(url)
with urllib.request.urlopen(reqSpectra) as respS:
ds_s = xr.open_dataset(io.BytesIO(respS.read()))
wh = ds_s.variables['wave_height']
wh
I would use NumPy's squeeze to remove extra dimensions.我会使用 NumPy 的挤压来删除额外的维度。
import urllib
import xarray as xr
import numpy as np
import io
# --------------------------------------------------------------------------
url = 'https://dods.ndbc.noaa.gov/thredds/fileServer/data/stdmet/46077/46077h2021.nc'
reqSpectra = urllib.request.Request(url)
with urllib.request.urlopen(reqSpectra) as respS:
ds_s = xr.open_dataset(io.BytesIO(respS.read()))
# --------------------------------------------------------------------------
wh = ds_s.variables['wave_height'];
wh = np.squeeze(wh);
wh
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.