python - how to check whether some given date exists in netcdf file

678 views Asked by At

I read my timesteps from a netcdf file:

timesteps   = ncfile.variables['time']

Now I would like to know whether some given date (say, June 23th) exists in that list. If it does, I want to omit it. The netcdf file contains arbitrary dates.

I have no clue how to do this in python. I'm new to python and my programming experience in python is best described as in https://xkcd.com/1513/ yet I have no time to follow a course, so any help would be greatly appreciated.

Thanks!

1

There are 1 answers

2
Bernhard On BEST ANSWER

Ok, this is not good style but it might get you what you want. Assuming your times are strings and you are confident that plain string comparison would work for you, you could do something like this:

timesteps   = ncfile.variables['time']
dates_to_skip = set(['June 23th', 'May 28th', 'April 1st'])
filtered_timesteps = filter(lambda t: t not in dates_to_skip, timesteps)

A better way of doing this is to parse your timesteps times (whether string or number) and turn into a datetime.date objects and compare against datetime.date objects you create in code.