简体   繁体   English

如何使用xarray从netcdf数据集中的变量中删除纬度和经度(它们是常量)?

[英]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.squeezexarray.Dataset.dropxarray.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.

相关问题 如何在 xarray 数据集中使用纬度和经度变量作为维度/坐标? - How to use latitude and longitude variables in a xarray dataset as dimensions/coordinates? 在xarray中使用经度和纬度到plot - Using longitude and latitude to plot in xarray 无法在 NetCDF 卫星文件 (Xarray) 中沿纬度和经度切片 - Unable to slice along latitude and longitude in NetCDF satellite file (Xarray) 从 xarray 数据集中的某些变量中删除维度 - Remove a dimension from some variables in an xarray Dataset Xarray:使用维度名称切片纬度经度 - Xarray: slicing latitude longitude using dimension name 如何在 NetCDF 上应用 xarray u_function 并将二维数组(多个新变量)返回到 DataSet - How to apply a xarray u_function over NetCDF and return a 2D-array (multiple new variables) to the DataSet 如何使用Python和xarray从变量满足netCDF数据集条件的位置提取坐标? - How to extract coordinates from where the variable meets a criterion for a netCDF dataset with Python and xarray? 如何从 Python 中的 netCDF 提取经度和纬度范围内的数据? - How can I extract data over a range of longitude and latitude from netCDF in Python? 使用 hvplot 从 xarray 数据集中绘制两个数据变量 - Plotting two data variables from an xarray dataset using hvplot xarray:如何将scipy函数应用于大型netcdf数据集 - xarray: How to apply a scipy function to a large netcdf dataset
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM