i am working on climate data. I have extracted data on my desired lat long using ERA5 data and code works perfectly. Now i am working with another dataset but its not working. Code is written in a way that it is not disturbed by leap-years. But in this dataset, the code stops working when there 366th day of the year by an error "index exceeds dimension bound". Variables data on python show that temp(temperature is the variable in my data which I access using temp=data.variables['t2']) has a window of [365,1800,3600] which should be 366 in case of a leap year. when i check the file in cdo there are complete 366 timesteps. Other than leap years code runs fine but in leap year it flops.
Need some help in this regard.Thanks
Also attaching some more metadata information which i think might help
#importing libraries
from netCDF4 import Dataset
import numpy as np
import glob
import pandas as pd
all_years = []
#Reading Netcdf file
for file in glob.glob('*.nc'):
print (file)
data = Dataset(file,'r')
time = data.variables['time']
year = time.units[11:15]
temp = data.variables['t2']
all_years.append(year)
year_start = min(all_years)
year_end = max(all_years)
#creating a list for the data/time column
date_range = pd.date_range(start = str(year_start) + '-01-01',
end = str(year_end) + '-12-31',
freq = 'D')
#Creating a data frame having an index column and 't2' column
df = pd.DataFrame(0.0, columns = ['t2'], index = date_range)
locations = pd.read_csv('Locations.csv')
for index, row in locations.iterrows():
#getting data from my csv file
locations = row['Name']
loc_lat = row['Latitude']
loc_long = row['Longitude']
all_years.sort()
for yr in all_years:
data = Dataset(year+'.nc', 'r')
#storing lat lon data into variables
lati = data.variables['lat'][:]
long = data.variables['lon'][:]
#squared diff of lat and lon
sq_diff_lat = (lati - loc_lat)**2
sq_diff_lon = (long - loc_long)**2
#identify the index of minimum value of lat and lon
min_index_lat = sq_diff_lat.argmin()
min_index_lon = sq_diff_lon.argmin()
temp = data.variables['t2']
start = str(yr) + '-01-01'
end = str(yr) + '-12-31'
d_range = pd.date_range(start = start,
end = end,
freq = 'D')
for t_index in np.arange(0, len(d_range)):
df.loc[d_range[t_index]]['t2'] = temp[t_index,
min_index_lat, min_index_lon]
df.to_csv(locations + '.csv')