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


Using matplotlib backend: Qt4Agg

In [43]:
# 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)
# get the header row
csv_file = open(csv_name, "rb")
datareader = csv.reader(csv_file)
headerrow = datareader.next()
csv_file.close()
# read the CSV file
data = numpy.genfromtxt(csv_name,delimiter=",",skip_header=1,names=headerrow,
                        usecols=csv_list,filling_values=-9999,dtype=None)
# 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]
label = cf["Variables"]["DateTime"]["csv"]["name"]
dt = [datetime.datetime.strptime(x,"%Y-%m-%d %H:%M:%S") for x in data[label]]
ds.series["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")
# 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"]
# get the datetime series
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])
# write the data to an OzFlux netCDF file
outfilename = qcio.get_outfilenamefromcf(cf)
nc_file = qcio.nc_open_write(outfilename)
qcio.nc_write_series(nc_file,ds)

In [ ]: