简体   繁体   English

如何通过特定变量值从 netcdf 文件中获取 select 数据?

[英]How to select data from netcdf file by specific variable value?

I am searching for an option to select data from a NetCDF file at a specific variable value.我正在从 NetCDF 文件中以特定变量值搜索 select 数据的选项。 The dataset contains time, lat, and lon coordinates and a range of variables.数据集包含时间、纬度和经度坐标以及一系列变量。 One of these variables is a mask with specific values for Land/ open ocean/ sea-ice /lake.其中一个变量是具有陆地/开阔海洋/海冰/湖泊的特定值的掩码。 Since the open ocean is represented by ds.mask = 1, I want to extract only sea surface temperature values which are located at the coordinates (time and space) where mask = 1. However, I do not want the sea surface temperature values at other coordinates to be set to NaN, but to keep only those coordinates and variable's values where ds.mask = 1. I know how to select and data with xarray.sel/isel, however, this works only with selecting by coordinates, not by variable values as I am trying it.由于公海由 ds.mask = 1 表示,我只想提取位于 mask = 1 的坐标(时间和空间)的海面温度值。但是,我不希望海面温度值位于其他坐标设置为 NaN,但只保留 ds.mask = 1 的那些坐标和变量值。我知道如何使用 xarray.sel/isel 来 select 和数据,但是,这只适用于通过坐标选择,而不是通过我正在尝试的变量值。 Any help would be very much appreciated.任何帮助将不胜感激。

lati = stormtrack_lat.values
loni = stormtrack_lon.values
timei = stormtrack_datetime.values
tmax = timei.max() + np.timedelta64(10,'D')
tmin = timei.min() - np.timedelta64(10,'D')
SSTskin_subfile = SSTskin_file.sel(time=slice(tmin, tmax))

#HERE I NEED HELP:
#extract data where mask = ocean (1) and use only these data points and keep these only!
SSTskin_subfile_masked = SSTskin_subfile.sel(SSTskin_subfile.mask == 1) #does not work yet (Thrown error: ValueError: the first argument to .isel must be a dictionary)

This is the NetCDF file's structure:这是 NetCDF 文件的结构:

文件结构

You can apply the ocean mask with.where:您可以使用.where 应用海洋面具:

SSTskin_subfile_masked = SSTskin_subfile.where(SSTskin_subfile.mask)

It is not possible to drop all masked points because the data are gridded.由于数据是网格化的,因此无法删除所有被屏蔽的点。 For example if you have just one defined value for a given latitude, you have to keep all the values along it.例如,如果给定纬度只有一个定义值,则必须保留所有值。 However you can drop the coordinates where all values are NaN with:但是,您可以使用以下命令删除所有值均为 NaN 的坐标:

SSTskin_subfile_masked.dropna(dim = ['lat', 'lon'], how = 'all')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM