I have written this code to write the NETCDF file utilizing lat long time variable (e.g. precip). I am reading all datasets from csv files. Therefore, I have made two csv files: (1) containing precipitation time series data (rows 11, columns 9) and (2) containing lat & Longitude (e.g. X, Y). When I am running the code, NETCDF file is generating but it is not writing in a proper manner. Dimensions, time and other attribute information is coming wrong. My code is given below as
import pandas as pd
import numpy as np
import netCDF4
stn_precip='stn_precip.csv'
orig_precip='precip_data.csv'
stations = pd.read_csv(stn_precip)
stncoords = stations.iloc[:,:]
orig = pd.read_csv(orig_precip)
lons = stncoords['X']
lats = stncoords['Y']
nstations = np.size(lons)
ncout = netCDF4.Dataset('precip_3.nc', 'w')
ncout.createDimension('station',nstations)
ncout.createDimension('time',orig.shape[0])
lons_out = lons.tolist()
lats_out = lats.tolist()
time_out = orig.index.tolist()
lats = ncout.createVariable('latitude',np.dtype('float32').char,('station',))
lons = ncout.createVariable('longitude',np.dtype('float32').char,('station',))
time = ncout.createVariable('time',np.dtype('float32').char,('time',))
precip = ncout.createVariable('precip',np.dtype('float32').char,('time', 'station'))
lats[:] = lats_out
lons[:] = lons_out
time[:] = time_out
precip[:] = orig
ncout.close()
The csv files contain data like this
After execution of the above code with the given data, I am getting this (seems wrong)
<xarray.Dataset>
Dimensions: (station: 9, time: 10)
Coordinates:
* time (time) float32 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
Dimensions without coordinates: station
Data variables:
latitude (station) float32 ...
longitude (station) float32 15.875 15.875 15.875 15.875 15.875 15.875 ...
precip (time, station) float32 ...
I want NETCDF output something like this
Dimensions: (lat: 1, lon: 9, time: 11)
Coordinates:
* time (time) datetime64[ns] 1901-01-01 1901-01-02 1901-01-03 ...
* lon (lon) float64 80.875..............................82.875
* lat (lat) float64 15.875..............................15.875
Data variables:
precip (time, lat, lon) float32 ...
Attributes:
CDI: Climate Data Interface version 1.9.5 (http://mpimet.mpg.de/...
Conventions: CF-1.6
history: Sun Dec 30 02:15:30 2018: cdo -f nc import_binary rf.ctl RF...
CDO: Climate Data Operators version 1.9.5 (http://mpimet.mpg.de/...


timeandstation; how do you expect thelatandlondimensions to automatically appear as both global dimensions, and dimensions of theprecipvariable?