How to convert csv to netcdf please help me python users

kotichowdary28 at gmail.com kotichowdary28 at gmail.com
Fri May 29 11:10:04 EDT 2020


Hi all

I hope all are  doing well

please help me how to convert CSV to NetCDF. Im trying but its not working


#!/usr/bin/env ipython
import pandas as pd
import numpy as np
import netCDF4
import pandas as pd
import xarray as xr


stn_precip='ts_sept.csv'
orig_precip='ts_sept.csv'
stations = pd.read_csv(stn_precip)

stncoords = stations.iloc[:]
orig = pd.read_csv(orig_precip)
lats = stncoords['latitude']
lons = stncoords['longitude']
nstations = np.size(lons)
ncout = netCDF4.Dataset('Precip_1910-2018_homomod.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()

 



More information about the Python-list mailing list