0

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.
"This is the data shown by python" "This is the data shown by cdo" on dec 31, "index exceeds dimension bounds" - ERROR MESSAGE Need some help in this regard.Thanks Also attaching some more metadata information which i think might help "Metadata info"

#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')
ti7
  • 16,375
  • 6
  • 40
  • 68
  • 1
    We will need to see the code and have access to the file to help - usually best to upload a minimal reproducible example... – ClimateUnboxed Jun 23 '22 at 13:46
  • At the very least your code. See [ask] – Michael Delgado Jun 23 '22 at 15:08
  • picture of the code uploaded file is too large to be attched 8..8GB – Ahmad Bilal Jun 23 '22 at 15:23
  • Please avoid posting pictures of code as it is impossible for people to copy and inspect it. Copy and paste your code into stackoverflow and then indent it. Also. please show what line of code is causing the problem and remove anything that is irrelevant to your actual problem. – Robert Wilson Jun 23 '22 at 15:24
  • df.loc [d_range[t_index]]['t2'] = temp[t_index, min_index_lat, min_index_lon] This line is creating issue – Ahmad Bilal Jun 23 '22 at 15:48
  • This doesn't tell us what is causing the problem. When does it fail? Have you printed the t_index? – Robert Wilson Jun 23 '22 at 17:47
  • let me explain a little. This code creates two columns, first column of dates (INDEX) and second colums of corresponding temperature vales (t2). When the code runs it calculates t2 value at my given lat long in csv and prints in t2 column. As it reaches 366th row of leap year code fails. Error msg "INDEX EXCEEDS DIMENSION BOUNDS" when i check window for variable t2 it shows (365,1800,3600). This 365 should be 366. thats why index exceeds. When i check the same thing in cdo it gives 366 timesteps. But in python its showing 365. Same file different metadata values. I am confused. – Ahmad Bilal Jun 23 '22 at 18:17
  • Please explain these things in the question. You should not expect people to walk through code debugging with you in the comments – Robert Wilson Jun 23 '22 at 18:28

0 Answers0