Problem in extracting and saving multi-dimensional time series data from netcdf file to csv file

shalu.ashu50 at gmail.com shalu.ashu50 at gmail.com
Tue Apr 17 06:34:12 EDT 2018


Hi All,

I am using winpython spyder 3.6. I am trying to extract a variable with their time series values (daily from 1950 to 2004). The data structure is as follows:

<xarray.Dataset>
Dimensions:     (bnds: 2, lat: 90, lon: 144, time: 20075)
Coordinates:
  * lat         (lat) float64 -89.0 -87.0 -85.0 -83.0 -81.0 -79.0 -77.0 ...
  * lon         (lon) float64 1.25 3.75 6.25 8.75 11.25 13.75 16.25 18.75 ...
  * time        (time) datetime64[ns] 1950-01-01T12:00:00 ...
Dimensions without coordinates: bnds
Data variables:
    time_bnds   (time, bnds) datetime64[ns] ...
    lat_bnds    (time, lat, bnds) float64 ...
    lon_bnds    (time, lon, bnds) float64 ...
    clt         (time, lat, lon) float32 ...

Now I am extracting "clt" variable values based on my area of interest using lat/long boxes 

(latbounds = [ -13.0 , 31.0 ]# 22 grid numbers
lonbounds = [ 89.75 , 151.25 ]#26 grid numbers

My code is here:

import netCDF4
import xarray as xr
import numpy as np
import csv
import pandas as pd
from pylab import *
import datetime

# NetCDF4-Python can read a remote OPeNDAP dataset or a local NetCDF file:
nc = netCDF4.Dataset('clt_day_GFDL-CM3_historical_r1i1p1_19500101-20041231.nc.nc')
nc.variables.keys()


lat = nc.variables['lat'][:]
lon = nc.variables['lon'][:]
time_var = nc.variables['time']
dtime = netCDF4.num2date(time_var[:],time_var.units)

lat_bnds, lon_bnds = [-13.0 , 31.0], [89.75 , 151.25]
# determine what longitude convention is being used [-180,180], [0,360]
print (lon.min(),lon.max())
print (lat.min(),lat.max())

# latitude lower and upper index
latli = np.argmin( np.abs( lat - lat_bnds[0] ) )
latui = np.argmin( np.abs( lat - lat_bnds[1] ) ) 


# longitude lower and upper index
lonli = np.argmin( np.abs( lon - lon_bnds[0] ) )
lonui = np.argmin( np.abs( lon - lon_bnds[1] ) )  
print(lat)

clt_subset = nc.variables['clt'][:,latli:latui , lonli:lonui]

upto here I am able to extract the data but I am not able to save these values in csv file. I am also able to save values for one location but when I am going with multi-dimentional extracted values so it is giving an error

when i am executing this:

hs = clt_subset[istart:istop,latli:latui , lonli:lonui]
tim = dtime[istart:istop]
print(tim)
# Create Pandas time series object
ts = pd.Series(hs,index=tim,name=clt_subset)

Error: - 

ts = pd.Series(hs,index=tim,name=clt_subset)
Traceback (most recent call last):

  File "<ipython-input-141-de5651093e24>", line 1, in <module>
    ts = pd.Series(hs,index=tim,name=clt_subset)

  File "C:\python3\WinPython\python-3.6.5.amd64\lib\site-packages\pandas\core\series.py", line 264, in __init__
    raise_cast_failure=True)

  File "C:\python3\WinPython\python-3.6.5.amd64\lib\site-packages\pandas\core\series.py", line 3275, in _sanitize_array
    raise Exception('Data must be 1-dimensional')

Exception: Data must be 1-dimensional 

Suggestions would be appreciated. Thanks
Vishu



More information about the Python-list mailing list