def extract_over_station(xls_in, xls_out, files, band=1, n=66, sort=True, verbose=False): """ it reads the gis file defined in the files then extract the data at coordinates defined in the xls_in and then write the data in the xls_out file Input: xls_in: the name of the input xls file containing the co-ordinates of the plot xls_out: the xls file in which the output will be written files: the data source file name in the gis format, these files must be in the tiff format band: band of the raster data to extract n: number of data fields in the input xls file sort: sort the files """ if type(files) is not list: raise TypeError('input files should be of list type') if sort: files.sort() xy = xlsread(xls_in).get_cells('B2:BO3', 'Sheet1') book_out = xlwt.Workbook() extracted_data = np.empty((len(files), n)) for gis_file, i in zip(files, range(len(files))): dataset = gdal.Open(gis_file, GA_ReadOnly) data = dataset.GetRasterBand(band).ReadAsArray() GT = dataset.GetGeoTransform() dataset = None x, y = utm2image(GT, xy.T) extracted_data[i, :] = data[y, x] if verbose: print('%s' % gis_file) sheet = book_out.add_sheet('Sheet1') sheet.write(0, 0, 'File \ Plot no.') for i in range(extracted_data.shape[1]): sheet.write(0, i + 1, i + 1) for gis_file, i in zip(files, range(len(files))): sheet.write(i + 1, 0, os.path.basename(gis_file).split('.')[0]) for i in range(extracted_data.shape[0]): for j in range(extracted_data.shape[1]): sheet.write(i + 1, j + 1, extracted_data[i, j]) book_out.save(xls_out)
def extract_over_station(xls_in, xls_out, files, band=1, n=66, sort=True, verbose=False): """ it reads the gis file defined in the files then extract the data at coordinates defined in the xls_in and then write the data in the xls_out file Input: xls_in: the name of the input xls file containing the co-ordinates of the plot xls_out: the xls file in which the output will be written files: the data source file name in the gis format, these files must be in the tiff format band: band of the raster data to extract n: number of data fields in the input xls file sort: sort the files """ if type(files) is not list: raise TypeError('input files should be of list type') if sort: files.sort() xy = xlsread(xls_in).get_cells('B2:BO3', 'Sheet1') book_out = xlwt.Workbook() extracted_data = np.empty((len(files),n)) for gis_file,i in zip(files,range(len(files))): dataset = gdal.Open(gis_file, GA_ReadOnly) data = dataset.GetRasterBand(band).ReadAsArray() GT = dataset.GetGeoTransform() dataset = None x,y = utm2image(GT,xy.T) extracted_data[i,:] = data[y,x] if verbose: print('%s'%gis_file) sheet = book_out.add_sheet('Sheet1') sheet.write(0,0,'File \ Plot no.') for i in range(extracted_data.shape[1]): sheet.write(0,i+1,i+1) for gis_file,i in zip(files,range(len(files))): sheet.write(i+1,0,os.path.basename(gis_file).split('.')[0]) for i in range(extracted_data.shape[0]): for j in range(extracted_data.shape[1]): sheet.write(i+1, j+1, extracted_data[i,j]) book_out.save(xls_out)
# -*- coding: utf-8 -*- """ Created on Fri Feb 03 18:12:26 2012 @author: K. Sreelash @website: www.ambhas.com @email: [email protected] """ # import required libraries from __future__ import division import numpy as np from ambhas.xls import xlsread #input infile_name = 'D:/svn/ambhas/examples/input_stics.xls' # read the parameters xls_file = xlsread(infile_name) # soil par argis = xls_file.get_cells('B2', 'soil_par') norgs = xls_file.get_cells('C2', 'soil_par') # plant par # tech par # climate
def corner_to_grid(xls_in, xls_out, in_sheet='Sheet1', res=5, rows=(2, 67)): """ it reads the xls file defined in the xls_in take the corner of the monitoring plots make a grid of res m resolution and save the output in xls_out this xls_out file can be used by ambhas.extract_gis_data.extract_gis function to extract the data over monitoring plots Note that there should be only and only 4 corner points defined for the plots Input: xls_in: name of xls input file xls_out: name of xls output file in_sheet: name of the sheet to be read from xls input file, default(Sheet1) res: resolution of the output grid in meters, default(5) rows: a tuple indicating the beginning and end of rows to read from input xls file, e.g. (2,67) """ xls_file = xlsread(xls_in) xy = xls_file.get_cells('B%i:I%i' % (rows[0], rows[1]), in_sheet) # writting output to excel file book = xlwt.Workbook() for i in xrange(xy.shape[0]): sheet = book.add_sheet(str(i + 1)) verts = xy[i, :].reshape(4, 2) min_x = verts.min(axis=0)[0] max_x = verts.max(axis=0)[0] min_y = verts.min(axis=0)[1] max_y = verts.max(axis=0)[1] min_x = min_x - np.mod(min_x, res) min_y = min_y - np.mod(min_y, res) max_x = max_x - np.mod(max_x, res) + res max_y = max_y - np.mod(max_y, res) + res x = np.arange(min_x, max_x, res) y = np.arange(min_y, max_y, res) X, Y = np.meshgrid(x, y) points = np.vstack([X.flatten(), Y.flatten()]).T #foo = nx.points_inside_poly(points, verts) foo = matplotlib.path.Path(verts).contains_points(points) sheet.write(0, 0, 'x') sheet.write(0, 1, 'y') for j in xrange(foo.sum()): sheet.write(j + 1, 0, points[foo, 0][j]) sheet.write(j + 1, 1, points[foo, 1][j]) print("%i/%i" % (i + 1, xy.shape[0])) book.save(xls_out)
import matplotlib.pyplot as plt from ambhas.xls import xlsread params = { 'axes.labelsize': 10, 'text.fontsize': 10, 'legend.fontsize': 10, 'xtick.labelsize': 10, 'ytick.labelsize': 10, 'text.usetex': False } plt.rcParams.update(params) in_file = 'maddur.xls' xls_file = xlsread(in_file) doy = xls_file.get_cells('B2:B366', 'forcing') rain = xls_file.get_cells('C2:C366', 'forcing') pet = xls_file.get_cells('D2:D366', 'forcing') plt.clf() plt.figure(figsize=(4, 3)) ax = plt.axes([0.15, 0.15, 0.7, 0.7]) plt.bar(doy, rain, color='m', edgecolor='m', label='Rainfall') plt.plot(pet, label='PET') plt.grid(True) plt.legend() plt.xlabel('DOY') plt.ylabel('mm') plt.savefig('../../ambhas-wiki/images/richards_forcing.png') plt.close()
if __name__ == "__main__": from scipy.interpolate import Rbf from random import sample import matplotlib.pyplot as plt ################## horizontal 2d grounwater model ############## # read the watershed in tiff format dataset = gdal.Open( '/home/tomer/south_gundal/geospatial_data/watershed_500.tif', GA_ReadOnly) watershed = dataset.GetRasterBand(1).ReadAsArray() watershed[-2, :] = 0 GT = dataset.GetGeoTransform() # read the initial groundwater level data from xls file fname = '/home/tomer/south_gundal/gw_level.xls' xls_file = xlsread(fname) sample_id = sample(xrange(23908), 500) gw_x = xls_file.get_cells('A1:A23908', 'dec2009')[sample_id].flatten() gw_y = xls_file.get_cells('B1:B23908', 'dec2009')[sample_id].flatten() gw_ini = xls_file.get_cells('E1:E23908', 'dec2009')[sample_id].flatten() # convert the point value of groundwater levels into a map having the same #extent and size as of watershed rbfi = Rbf(gw_x, gw_y, gw_ini, function='linear') xi = np.linspace(GT[0] + GT[1] / 2, GT[0] + GT[1] * (dataset.RasterXSize - 0.5), dataset.RasterXSize) yi = np.linspace(GT[3] + GT[5] / 2, GT[3] + GT[5] * (dataset.RasterYSize - 0.5), dataset.RasterYSize) XI, YI = np.meshgrid(xi, yi) hini = rbfi(XI, YI)
def corner_to_grid(xls_in, xls_out, in_sheet='Sheet1', res=5, rows=(2,67)): """ it reads the xls file defined in the xls_in take the corner of the monitoring plots make a grid of res m resolution and save the output in xls_out this xls_out file can be used by ambhas.extract_gis_data.extract_gis function to extract the data over monitoring plots Note that there should be only and only 4 corner points defined for the plots Input: xls_in: name of xls input file xls_out: name of xls output file in_sheet: name of the sheet to be read from xls input file, default(Sheet1) res: resolution of the output grid in meters, default(5) rows: a tuple indicating the beginning and end of rows to read from input xls file, e.g. (2,67) """ xls_file = xlsread(xls_in) xy = xls_file.get_cells('B%i:I%i'%(rows[0],rows[1]), in_sheet) # writting output to excel file book = xlwt.Workbook() for i in xrange(xy.shape[0]): sheet = book.add_sheet(str(i+1)) verts = xy[i,:].reshape(4,2) min_x = verts.min(axis=0)[0] max_x = verts.max(axis=0)[0] min_y = verts.min(axis=0)[1] max_y = verts.max(axis=0)[1] min_x = min_x - np.mod(min_x,res) min_y = min_y - np.mod(min_y,res) max_x = max_x - np.mod(max_x,res)+res max_y = max_y - np.mod(max_y,res)+res x = np.arange(min_x, max_x, res) y = np.arange(min_y, max_y, res) X,Y = np.meshgrid(x,y) points = np.vstack([X.flatten(), Y.flatten()]).T #foo = nx.points_inside_poly(points, verts) foo = matplotlib.path.Path(verts).contains_points(points) sheet.write(0,0,'x') sheet.write(0,1,'y') for j in xrange(foo.sum()): sheet.write(j+1,0,points[foo,0][j]) sheet.write(j+1,1,points[foo,1][j]) print("%i/%i"%(i+1,xy.shape[0])) book.save(xls_out)
this scripts plots the forcing for the richards' input file """ import matplotlib.pyplot as plt from ambhas.xls import xlsread params = {'axes.labelsize': 10, 'text.fontsize': 10, 'legend.fontsize': 10, 'xtick.labelsize': 10, 'ytick.labelsize': 10, 'text.usetex': False} plt.rcParams.update(params) in_file = 'maddur.xls' xls_file = xlsread(in_file) doy = xls_file.get_cells('B2:B366','forcing') rain = xls_file.get_cells('C2:C366','forcing') pet = xls_file.get_cells('D2:D366','forcing') plt.clf() plt.figure(figsize=(4, 3)) ax = plt.axes([0.15, 0.15, 0.7, 0.7]) plt.bar(doy,rain, color='m', edgecolor='m', label='Rainfall') plt.plot(pet, label='PET') plt.grid(True) plt.legend() plt.xlabel('DOY') plt.ylabel('mm') plt.savefig('../../ambhas-wiki/images/richards_forcing.png') plt.close()
self.hnew = hnew if __name__ == "__main__": from scipy.interpolate import Rbf from random import sample import matplotlib.pyplot as plt ################## horizontal 2d grounwater model ############## # read the watershed in tiff format dataset = gdal.Open('/home/tomer/south_gundal/geospatial_data/watershed_500.tif',GA_ReadOnly) watershed = dataset.GetRasterBand(1).ReadAsArray() watershed[-2,:] = 0 GT = dataset.GetGeoTransform() # read the initial groundwater level data from xls file fname = '/home/tomer/south_gundal/gw_level.xls' xls_file = xlsread(fname) sample_id = sample(xrange(23908), 500) gw_x = xls_file.get_cells('A1:A23908', 'dec2009')[sample_id].flatten() gw_y = xls_file.get_cells('B1:B23908', 'dec2009')[sample_id].flatten() gw_ini = xls_file.get_cells('E1:E23908', 'dec2009')[sample_id].flatten() # convert the point value of groundwater levels into a map having the same #extent and size as of watershed rbfi = Rbf(gw_x, gw_y, gw_ini, function='linear') xi = np.linspace(GT[0]+GT[1]/2, GT[0]+GT[1]*(dataset.RasterXSize-0.5), dataset.RasterXSize) yi = np.linspace(GT[3]+GT[5]/2, GT[3]+GT[5]*(dataset.RasterYSize-0.5), dataset.RasterYSize) XI, YI = np.meshgrid(xi,yi) hini = rbfi(XI, YI) #run the model