0

I have netcdf data which contains daily for the year 2020 with specific humidity as the variable, and an excel file that contains the date of all the cyclones that formed over the North Indian Ocean. Mostly, when we want to extract data within a particular time range we use to do slice using xarray, but in this particular situation, I am facing the problem that I need to extract data only for those specific cyclone days which are in the excel file and create a composite. If anyone can help me by showing how it can be done it will be much appreciated. I am attaching a link to the netcdf dataset and the excel file containing the cyclone days.netcdf cyclonedays

Debashis Paul
  • 67
  • 1
  • 10

1 Answers1

2

This is fairly straightforward. You can convert the string dates to pandas datetime objects, and then select data from the DataArray directly using that.

import xarray as xr
import pandas as pd

ds = xr.load_dataset('data.nc')
times = pd.read_excel('time.xlsx')

times['Date(DD/MM/YYYY)'] = pd.to_datetime(times['Date(DD/MM/YYYY)'], infer_datetime_format=True)

ds['shum'][ds.time.isin(times.values)]

In this case, the output has no data because your dates in 2020 while the netcdf file only has dates from 2019.

  • Oh yes you are right. We just need to get the time steps and then select the similar time steps in the NC data. Thanks a lot for the help. – Debashis Paul Feb 12 '22 at 11:04