0

I have relatively new to Python. I have a NetCDF file and I want to extract rainfall information for a particular coordinate. I have used the following code:

from netCDF4 import Dataset 
import numpy as np
import pandas as pd



## Reading in the netCDF file
data = Dataset(r'F:\IMD_rainfall_netCDF\2021.nc', 'r')

lat_grid1 = 23.5
long_grid1 = 86.5

## Displaying the names of the variables
print(data.variables.keys())

## accessing the variables
rain = data.variables['RAINFALL']
#print(rain)

lat = data.variables['LATITUDE']
#print(lat)

time = data.variables['TIME']
#print(time)

long = data.variables['LONGITUDE']
#print(long)

#print(rain)


## accessing data from the variables

rain_data = data.variables['RAINFALL'][:]
print(rain_data)

time_data = data.variables['TIME'][:]
#print(time_data)
 
long_data = data.variables['LONGITUDE'][:]
#print(long_data)

rain = data.variables['RAINFALL']
print(rain)


## Creating an empty data frame
starting_date = data.variables['TIME'].units[11:21]
ending_date = data.variables['TIME'].units[11:13] + '01-12-30'

date_range = pd.date_range(start = starting_date, end= ending_date )
df = pd.DataFrame(0, columns=['Rainfall'], index = date_range)

dt = np.arange(0,data.variables['TIME'].size)

for time_index in dt:
    df.iloc[time_index] = rain[time_index, lat_grid1, long_grid1]
    
    df.to_csv('precipitation_2021.csv', index=True, header=True)

But all the values are shown in double dash line(--). what does it mean? how can I show my rainfall data ... my output CSV also shows all the precipitation values as 0.. can anyone have the solution? please help me

Asish
  • 11
  • 1
  • Welcome to Stack Overflow. [Please don't post screenshots of text](https://meta.stackoverflow.com/a/285557/354577). They can't be searched or copied, or even consumed by users of adaptive technologies like screen readers. Instead, paste the code as text directly into your question. If you select it and click the `{}` button or Ctrl+K the code block will be indented by four spaces, which will cause it to be rendered as code. – ChrisGPT was on strike Apr 20 '22 at 20:34
  • If you are new to Python I recommend trying either xarray or my package nctoolkit. These will give you much more efficient ways to do these workflows. – Robert Wilson Apr 21 '22 at 07:06

0 Answers0