00001
00002 """
00003 Created on Wed Jan 25 13:30:46 2012
00004
00005 @author: Sat Kumar Tomer
00006 @website: www.ambhas.com
00007 @email: satkumartomer@gmail.com
00008
00009 this module reads the data from xls file
00010 perform the lumped grounwater level modelling
00011 then save the output as xls file and images
00012 """
00013
00014 import numpy as np
00015 import matplotlib.pyplot as plt
00016 import xlrd, xlwt
00017 from ambhas.gw import GW_1D
00018 import scikits.timeseries as ts
00019 import scikits.timeseries.lib.plotlib as tpl
00020
00021
00022 def gw_model_file(in_fname, out_fname, figure_dir=None):
00023 """"
00024 input:
00025 in_fname: name of input xls file
00026 out_fname: name of the output xls file
00027 figure_dir: name of the directory where to save the out figures
00028 """
00029
00030
00031 in_book = xlrd.open_workbook(in_fname)
00032 sheet_names = in_book.sheet_names()
00033 sheet_names.remove('legend')
00034
00035 out_book = xlwt.Workbook()
00036 for sheet_name in sheet_names:
00037
00038 sheet = in_book.sheet_by_name(sheet_name)
00039
00040 t = sheet.nrows-1
00041 year = np.empty(t, 'int')
00042 month = np.empty(t, 'int')
00043 rainfall = np.empty(t)
00044 pumping = np.empty(t)
00045 meas_gwl = np.empty(t)
00046 r = np.empty(t)
00047 for i in range(t):
00048 year[i] = sheet.cell_value(i+1,0)
00049 month[i] = sheet.cell_value(i+1,1)
00050 rainfall[i] = sheet.cell_value(i+1,2)
00051 pumping[i] = sheet.cell_value(i+1,3)
00052 meas_gwl[i] = sheet.cell_value(i+1,4)
00053 r[i] = sheet.cell_value(i+1,5)
00054 F = sheet.cell_value(1,6)
00055 G = sheet.cell_value(1,7)
00056 hmin = sheet.cell_value(1,8)
00057
00058
00059 gw_model = GW_1D(rainfall, pumping)
00060 gw_model.set_parameters(F, G, r, hmin)
00061
00062 hini = meas_gwl[0]
00063 gw_model.run_model(hini,t)
00064 sim_gwl = gw_model.h
00065 lam = gw_model.lam
00066 sy = gw_model.sy
00067 discharge = gw_model.discharge
00068
00069
00070 sheet = out_book.add_sheet(sheet_name)
00071 sheet.write(0,0,'year')
00072 sheet.write(0,1,'month')
00073 sheet.write(0,2,'rainfall')
00074 sheet.write(0,3,'pumping')
00075 sheet.write(0,4,'measured gwl')
00076 sheet.write(0,5,'simulated gwl')
00077 sheet.write(0,6,'recharge')
00078 sheet.write(0,7,'discharge')
00079 sheet.write(0,8,'lambda')
00080 sheet.write(0,9,'sy')
00081 for i in range(t):
00082 sheet.write(i+1,0,year[i])
00083 sheet.write(i+1,1,month[i])
00084 sheet.write(i+1,2,rainfall[i])
00085 sheet.write(i+1,3,pumping[i])
00086 sheet.write(i+1,4,meas_gwl[i])
00087 sheet.write(i+1,5,sim_gwl[i])
00088 sheet.write(i+1,6,rainfall[i]*r[i])
00089 sheet.write(i+1,7,discharge[i])
00090 sheet.write(1,8,lam)
00091 sheet.write(1,9,sy)
00092
00093 first_date = ts.Date(freq='M',year=year[0],month=month[1])
00094 gw_meas_series = ts.time_series(meas_gwl, start_date=first_date)
00095 gw_sim_series = ts.time_series(sim_gwl, start_date=first_date)
00096
00097 if figure_dir is not None:
00098
00099 fig = tpl.tsfigure()
00100 fsp = fig.add_tsplot(111)
00101 fsp.tsplot(gw_meas_series, 'r', lw=3, label='measured')
00102 fsp.tsplot(gw_sim_series, 'g', lw=3, label='simulated')
00103 fsp.legend(loc='best')
00104 plt.ylabel('Groundwater Level' )
00105 plt.savefig(figure_dir+'%s.png'%sheet_name)
00106 plt.close()
00107
00108 print('%s completed succesfully'%sheet_name)
00109
00110
00111 out_book.save(out_fname)