I am trying to convert a netCDF file to csv. I have succeeded to do that. However, as illustrated in the attached image, i have two problems in the exported csv. dataframe. First, i need to split datetime into two separate columns (Only Date, Only time). Second, when i run the code i get the same values for the 3 different variables (par,temp, and evap). Here is the code:
import netCDF4
from netCDF4 import Dataset
from netCDF4 import num2date
import pandas as pd
import numpy as np
import os
# Reading netCDF file
data = Dataset(r'G:\trial\Jan.nc', 'r')
# Defining parameters and dimensions
lat = data.variables['latitude'][:]
long = data.variables['longitude'][:]
time = data.variables['time']
times = num2date(time[:], time.units)
par = data.variables['par'][:]
temp = data.variables['t2m'][:]
evap = data.variables['e'][:]
# Importing netCDF data from a single point
idku_lat = 31.249464
idku_long = 30.210806
lat_diff = (lat - idku_lat)**2
lon_diff = (long - idku_long)**2
min_lat = lat_diff.argmin()
min_long = lon_diff.argmin()
# Creating a pandas DataFrame and import values from the netCDF file
df = pd.DataFrame(0, columns = ['par','temp','evap'], index = times)
dt = np.arange(0, data.variables['time'].size)
for time_index in dt:
df.iloc[time_index]= par[time_index,min_lat,min_long]
# Exporting the file to csv.
df.to_csv('my_data.csv')