如何使用Python从NETCDF选择变量的时间范围

发布于 2025-02-09 14:00:35 字数 1394 浏览 1 评论 0原文

我每天都有一个包含单个LAT和LON的点数据。 Vairable(var.shape)的形状是('time','z'),这里z是海洋中的深度。我想在指定的日期范围内(2019-01-01至2020-12-31)和深度范围(100至1500)选择和绘制可变性。我知道如何选择深度范围,但时间不能做同样的时间。我如何选择一定范围的时间?

这里没有工作

import warnings
warnings.filterwarnings('ignore')

import os
import numpy as np
import xarray as xr
import matplotlib.pyplot as plt
import matplotlib as mpl

path_in = "./"
file_ds = xr.open_dataset(path_in + 'ocean.nc')

lon = file_ds.variables['lon'][:]
lat = file_ds.variables['lat'][:]
li = file_ds.variables['depth'][0,:,0,0] 
time = file_ds.variables['time'][:]
var = file_ds.variables['var'][:,:,0,0]

index = np.intersect1d(np.where(li<-100),np.where(li>=-1500))
tindex = np.intersect1d(np.where(time<(['2020-01-01'],dtype='datetime64[ns]'),np.where(time>=['2020-12-31'],dtype='datetime64[ns]'))

var_100_1500 = var[tindex,index] 

fig, ax = plt.subplots(figsize=(15, 9))
fillprof = ax.contourf(time[tindex],li[index],var_100_1500.T,levels=40)

clb=plt.colorbar(fillprof, orientation="vertical", pad=0.02)
clb.ax.tick_params(labelsize=18) 

我尝试了以下代码,但是时间的索引在

  tindex = np.intersect1d(np.where(time<(['2020-01-01'],dtype='datetime64[ns]'),np.where(time>=['2020-12-31'],dtype='datetime64[ns]'))
                                                               ^
SyntaxError: invalid syntax

I have a daily netcdf data containing a point data of single lat and lon. The shape of vairable (var.shape) is ('time', 'z'), here z is depths in ocean. I want to select and plot a varaible within a specified date range (2019-01-01 to 2020-12-31) and also range of depths (100 to 1500). I know how to select the depth range, but couldn't do the same for time. How can I choose a range of time?

I tried the following code, but the indexing of time didn't work here

import warnings
warnings.filterwarnings('ignore')

import os
import numpy as np
import xarray as xr
import matplotlib.pyplot as plt
import matplotlib as mpl

path_in = "./"
file_ds = xr.open_dataset(path_in + 'ocean.nc')

lon = file_ds.variables['lon'][:]
lat = file_ds.variables['lat'][:]
li = file_ds.variables['depth'][0,:,0,0] 
time = file_ds.variables['time'][:]
var = file_ds.variables['var'][:,:,0,0]

index = np.intersect1d(np.where(li<-100),np.where(li>=-1500))
tindex = np.intersect1d(np.where(time<(['2020-01-01'],dtype='datetime64[ns]'),np.where(time>=['2020-12-31'],dtype='datetime64[ns]'))

var_100_1500 = var[tindex,index] 

fig, ax = plt.subplots(figsize=(15, 9))
fillprof = ax.contourf(time[tindex],li[index],var_100_1500.T,levels=40)

clb=plt.colorbar(fillprof, orientation="vertical", pad=0.02)
clb.ax.tick_params(labelsize=18) 

The error while ploting is as below

  tindex = np.intersect1d(np.where(time<(['2020-01-01'],dtype='datetime64[ns]'),np.where(time>=['2020-12-31'],dtype='datetime64[ns]'))
                                                               ^
SyntaxError: invalid syntax

Can anyone give me a solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清晨说晚安 2025-02-16 14:00:35

您在tindex分配行的末尾缺少一个“)”。

看起来应该这样:

tindex = np.intersect1d(np.where(time<(['2020-01-01'], dtype='datetime64[ns]'), np.where(time>=['2020-12-31'], dtype='datetime64[ns]')))

Your are missing a ")" at the end of the tindex assignment line.

It should look like this:

tindex = np.intersect1d(np.where(time<(['2020-01-01'], dtype='datetime64[ns]'), np.where(time>=['2020-12-31'], dtype='datetime64[ns]')))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文