In [1]:
%run basics
import csv
%matplotlib


Using matplotlib backend: Qt4Agg

In [2]:
# get the control file
cf = qcio.load_controlfile(path='../controlfiles')
# list of series to be read from CSV file
series_list = cf["Variables"].keys()
csv_list = []
for item in series_list:
    csv_list.append(cf["Variables"][item]["csv"]["name"])
# name of input CSV file
csv_name = qcio.get_infilenamefromcf(cf)

In [3]:
# get the header row
csv_file = open(csv_name, "rb")
datareader = csv.reader(csv_file)
headerrow = datareader.next()
csv_file.close()

In [4]:
# read the CSV file
data = numpy.genfromtxt(csv_name,delimiter=",",skip_header=1,names=headerrow,
                        usecols=csv_list,filling_values=-9999,dtype=None)

In [5]:
data["Fn_Con"][0:10]


Out[5]:
array([-26.91558791, -22.05371   , -19.16194   , -16.91185   ,
       -18.1424    , -16.98848   , -11.93495   , -14.55581   ,
       -13.40802   , -14.3668    ])

In [6]:
# get an instance of the data structure
ds = qcio.DataStructure()
# set some global attributes
ds.globalattributes['featureType'] = 'timeseries'
ds.globalattributes['csv_filename'] = csv_name
ds.globalattributes['nc_nrecs'] = len(data)
for gattr in cf['Global'].keys():
    ds.globalattributes[gattr] = cf['Global'][gattr]

In [ ]:
dt = [datetime.datetime.strptime(x,"%Y-%m-%d %H:%M:%S") for x in data["DateTime"]]
ds.series["DateTime"]["Data"] = dt
ds.series["DateTime"]["Flag"] = numpy.zeros(len(data),dtype=numpy.int32)
ds.series["DateTime"]["Attr"] = {"long_name":"Datetime in local timezone","units":"None" }
if "DateTime" in series_list: series_list.remove("DateTime")

In [9]:
# put the data into the data structure
nRecs = ds.globalattributes["nc_nrecs"]
for item in series_list:
    ds.series[item] = {}
    ds.series[item]["Data"] = numpy.array(data[cf["Variables"][item]["csv"]["name"]],dtype=numpy.float64)
    ds.series[item]["Flag"] = numpy.zeros(nRecs,dtype=numpy.int32)
    ds.series[item]["Attr"] = cf["Variables"][item]["Attr"]

In [ ]:


In [10]:
# get the datetime series
qcutils.get_datetimefromxldate(ds)
qcutils.round_datetime(ds,mode="nearest_second")
fixtimestepmethod = qcutils.get_keyvaluefromcf(cf,["Options"],"FixTimeStepMethod",default="round")
if qcutils.CheckTimeStep(ds): qcutils.FixTimeStep(ds,fixtimestepmethod=fixtimestepmethod)
qcutils.get_xldatefromdatetime(ds)
qcutils.get_ymdhmsfromdatetime(ds)
ds.globalattributes['start_date'] = str(ds.series['DateTime']['Data'][0])
ds.globalattributes['end_date'] = str(ds.series['DateTime']['Data'][-1])


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-10-c2dd3c173c14> in <module>()
      1 # get the datetime series
----> 2 qcutils.get_datetimefromxldate(ds)
      3 qcutils.round_datetime(ds,mode="nearest_second")
      4 fixtimestepmethod = qcutils.get_keyvaluefromcf(cf,["options"],"FixTimeStepMethod",default="round")
      5 if qcutils.CheckTimeStep(ds): qcutils.FixTimeStep(ds,fixtimestepmethod=fixtimestepmethod)

/home/peter/OzFlux/OzFluxQC/scripts/qcutils.pyc in get_datetimefromxldate(ds)
   1025     xldate = ds.series['xlDateTime']['Data']
   1026     nRecs = len(ds.series['xlDateTime']['Data'])
-> 1027     datemode = int(ds.globalattributes['xl_datemode'])
   1028     ds.series[unicode('DateTime')] = {}
   1029     ds.series['DateTime']['Data'] = [None]*nRecs

KeyError: 'xl_datemode'

In [ ]:
# write the data to an OzFlux netCDF file
outfilename = get_outfilenamefromcf(cf)
nc_file = nc_open_write(outfilename)
nc_write_series(nc_file,ds)