In [1]:
import csv
import datetime
import dateutil.parser
import matplotlib.pyplot as plt
import netCDF4
import numpy
import os
%matplotlib
In [2]:
def get_data_from_netcdf(latitude,longitude,fire_date,window):
# dictionary for met data
met_data = {}
# initialise constants
latitude0 = -9.75
longitude0 = 109.5
lat_resolution = 0.75
lon_resolution = 0.75
nc_file_path = "/home/peter/hackfest/"
# get the year
year = fire_date.year
# get the latitude and longitude indices
lat_index = int(((latitude0-latitude)/lat_resolution)+0.5)
lon_index = int(((longitude-longitude0)/lon_resolution)+0.5)
# get the name of the netCDF file containing the data
nc_file_name = str(year)+"_"+str(lat_index)+"_"+str(lon_index)+".nc"
nc_full_name = nc_file_path+nc_file_name
print " File name is: ",nc_full_name
if not os.path.exists(nc_full_name):
print " File "+nc_full_name+" not found, skipping ..."
return met_data
# get the data
print " Opening file "+nc_file_name
nc_file = netCDF4.Dataset(nc_full_name,'r')
nc_time = nc_file.variables["time"][:]
nc_time_units = getattr(nc_file.variables["time"],"units")
dt_utc = netCDF4.num2date(nc_time,nc_time_units)
start_date = fire_date - datetime.timedelta(days=window["before"])
end_date = fire_date + datetime.timedelta(days=window["after"])
if start_date<datetime.datetime(year,1,1,0,0,0):
print " start_date before beginning of year, skipping ..."
return met_data
si = [n for n,dt in enumerate(dt_utc) if dt>start_date ][0]
ei = [n for n,dt in enumerate(dt_utc) if dt>end_date ][0]
met_data["DateTime"] = dt_utc[si:ei+1]
met_data["Fsd"] = nc_file.variables["Fsd"][si:ei+1]
met_data["Ta"] = nc_file.variables["Ta"][si:ei+1]
met_data["RH"] = nc_file.variables["RH"][si:ei+1]
met_data["Ws"] = nc_file.variables["Ws"][si:ei+1]
met_data["Wd"] = nc_file.variables["Wd"][si:ei+1]
met_data["Precip"] = nc_file.variables["Precip"][si:ei+1]
nc_file.close()
return met_data
In [3]:
# get the filename
file_path = "/home/peter/hackfest/"
year = 2015
csv_filename = file_path+"hotspot_"+str(year)+".csv"
erai_name = file_path+"ERAI_"+str(year)+".nc"
# sniff the file to see if we can find the dialect
csv_file = open(csv_filename,'rb')
dialect = csv.Sniffer().sniff(csv_file.readline(), [' ',',','\t'])
# rewind file
csv_file.seek(0)
csv_reader = csv.reader(csv_file,dialect)
# get the header line
header = csv_reader.next()
# close the file
csv_file.close()
# get a list of columns to be read
csv_list = ["load_dt","latitude","longitude"]
col_list = [header.index(item) for item in csv_list]
# read the csv file using numpy's genfromtxt
skip = 1
# define the missing values and the value with which to fill them
missing_values = {}
filling_values = {}
for item in col_list:
missing_values[item] = ["NA","N/A","NAN","#NAME?","#VALUE!","#DIV/0!","#REF!"]
filling_values[item] = float(-9999)
# read the CSV file
fire_data = numpy.genfromtxt(csv_filename,delimiter=dialect.delimiter,skip_header=skip,
names=header,usecols=col_list,missing_values=missing_values,
filling_values=filling_values,dtype=None)
In [2]:
plot_data = True
window = {"before":5,"after":0}
nrecs = len(fire_data["latitude"])
# test with first 5
# normal operation would use nrecs
nplots = 0
for i in range(nrecs):
latitude = fire_data["latitude"][i]
longitude = fire_data["longitude"][i]
fire_date = dateutil.parser.parse(fire_data["load_dt"][i])
met_data = get_data_from_netcdf(latitude,longitude,fire_date,window)
if "DateTime" not in met_data: continue
if plot_data and nplots<10:
nplots = nplots+1
fig = plt.figure()
plt.subplot(311)
plt.plot(met_data["DateTime"],met_data["Fsd"])
plt.subplot(312)
plt.plot(met_data["DateTime"],met_data["Ta"])
plt.subplot(313)
plt.plot(met_data["DateTime"],met_data["RH"])
plt.show()
In [ ]: