I'm attempting to convert a netcdf (*.nc) (classic format) to CSV. This file is taken from the NOAA precipitation data set.
I found helpful code in this post; however, when I run it I get this exception:
Traceback (most recent call last): File "./test2.py", line 31, in precip_ts = pd.Series(precip, index=dtime) File "/usr/local/lib/python2.7/site-packages/pandas/core/series.py", line 275, in init raise_cast_failure=True) File "/usr/local/lib/python2.7/site-packages/pandas/core/series.py", line 4165, in _sanitize_array raise Exception('Data must be 1-dimensional') Exception: Data must be 1-dimensional
Here is the test2.py script (same as post referenced above):
#!/usr/local/bin/python2.7
import netCDF4
import pandas as pd
precip_nc_file = 'precip.V1.0.2006.nc'
nc = netCDF4.Dataset(precip_nc_file, mode='r')
nc.variables.keys()
lat = nc.variables['lat'][:]
lon = nc.variables['lon'][:]
time_var = nc.variables['time']
dtime = netCDF4.num2date(time_var[:],time_var.units)
precip = nc.variables['precip'][:]
# a pandas.Series designed for time series of a 2D lat,lon grid
precip_ts = pd.Series(precip, index=dtime)
precip_ts.to_csv('precip.csv',index=True, header=True)
It is failing at the Pandas Series call. Can you give me any guidance why the pandas is failing; I thought it is supposed to handle 2D data!
The end result I'm looking for is a CSV file with each line having lon, lat, datetime, precip value