In [1]:
import datetime
import matplotlib.pyplot as plt
import netCDF4
import os
import pandas as pd
In [2]:
def read_netcdf(nc_full_name,variable_list=[]):
# open the netCDF file
nc_file = netCDF4.Dataset(nc_full_name,"r")
# creat the attribute dictionary
attr = {}
attr["global"] = {}
attr["variable"] = {}
# get the global attributes
gattrlist = nc_file.ncattrs()
if len(gattrlist)!=0:
for item in gattrlist:
attr["global"][item] = getattr(nc_file,item)
# get a list of Python datetimes
time = time = nc_file.variables["time"][:]
time_units = getattr(nc_file.variables["time"],"units")
dt = list(netCDF4.num2date(time,time_units))
# get a list of variables to read from the netCDF file
# was a variable list passed in as variable_list?
if len(variable_list)==0:
# if not, get the variable list from the netCDF file contents
variable_list = nc_file.variables.keys()
else:
# if so, add the QC flags to the list entered as an argument
flag_list = []
for item in variable_list: flag_list.append(item+"_QCFlag")
variable_list = variable_list+flag_list
# create the data dictionary
data = {}
# loop over the variables to be read
for item in variable_list:
# get the number of dimensions
# variables in OzFlux netCDF files can have 1 (time) or 3 dimensions (time,latitude,longitude)
ndims = len(nc_file.variables[item].shape)
if ndims==1:
data[item] = ncFile.variables[item][:]
elif ndims==3:
data[item] = nc_file.variables[item][:,0,0]
else:
raise Exception("unrecognised number of dimensions for variable"+str(item))
# get the variable attributes
vattrlist = nc_file.variables[item].ncattrs()
if len(vattrlist)!=0:
attr["variable"][item] = {}
for vattr in vattrlist:
attr["variable"][item][vattr] = getattr(nc_file.variables[item],vattr)
# close the netCDF file
nc_file.close()
# convert the dictionary to a Pandas data frame
df = pd.DataFrame(data,index=dt)
return df,attr
In [3]:
nc_full_name = "../../Sites/Whroo/Data/Processed/all/Whroo_2011_to_2014_L6.nc"
variable_list = ['Fsd','Ta','VPD','NEE_SOLO']
In [4]:
df,attr = read_netcdf(nc_full_name,variable_list=variable_list)
In [5]:
fig=plt.figure()
ax1 = plt.subplot(411)
ax1.plot(df['Fsd'])
ax2 = plt.subplot(412)
ax2.plot(df['Ta'])
ax3 = plt.subplot(413)
ax3.plot(df['VPD'])
ax4 = plt.subplot(414)
ax4.plot(df['NEE_SOLO'])
plt.show()
In [6]:
attr.keys()
Out[6]:
In [8]:
attr["variable"].keys()
Out[8]:
In [ ]: