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?