In [1]:
%run basics
import csv
%matplotlib
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]:
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])
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)