def calculate_discharge(params, mask, pnet): ''' Calculate the accumulated water discharge ''' ldd = ascraster.Asciigrid(ascii_file=params.ldd, mask=mask, numtype=int) if params.ldebug: print(params.ldd + " has been read.") landarea = ascraster.Asciigrid(ascii_file=params.landarea, mask=mask, numtype=float) if params.ldebug: print(params.landarea + " has been read.") water = ascraster.duplicategrid(landarea) # Calculate accumulated water flux of each grid cell to the mouth of the river basin. Make discharge in km3 # Convert pnet [mm/yr] to km3 by multiplying with landarea and 10e-6 (conversion from mm to km) for icell in range(landarea.length): wt = pnet.get_data(icell, 0.0) * landarea.get_data(icell, 0.0) * 1.0e-6 water.set_data(icell, wt) discharge = accuflux.accuflux(ldd, water, negative_flux_possible=1) # Write discharge to output file: discharge.write_ascii_file(os.path.join(params.outputdir, "discharge.asc")) return discharge
def calculate_discharge(params,mask,pnet): ''' Calculate the accumulated water discharge ''' ldd = ascraster.Asciigrid(ascii_file=params.ldd,mask=mask,numtype=int) if params.ldebug: print params.ldd + " has been read." landarea = ascraster.Asciigrid(ascii_file=params.landarea,mask=mask,numtype=float) if params.ldebug: print params.landarea + " has been read." water = ascraster.duplicategrid(landarea) # Calculate accumulated water flux of each grid cell to the mouth of the river basin. Make discharge in km3 # Convert pnet [mm/yr] to km3 by multiplying with landarea and 10e-6 (conversion from mm to km) for icell in range(landarea.length): wt = pnet.get_data(icell,0.0) * landarea.get_data(icell,0.0) * 1.0e-6 water.set_data(icell,wt) discharge = accuflux.accuflux(ldd,water,negative_flux_possible=1) # Write discharge to output file: discharge.write_ascii_file(os.path.join(params.outputdir,"discharge.asc")) return discharge
def run_watermodel(args): ''' Run N model main routine @param listargs: list of arguments passed trough command-line or other script Must look like sys.argv so make sure is starts with scriptname. ''' # Parse command-line arguments and set parameters for script try: param = cmd_options_water.InputWater(args) params = param.options params_var = param.options_var except SystemExit: raise MyError("Error has occured in the reading of the commandline options.") # Start timer and logging s = my_sys.SimpleTimer() log = my_logging.Log(params.outputdir,"%s_%i.log" % (params.scenarioname,params.year)) print "Log will be written to %s" % log.logFile # If no arguments are provided do a run with defaults in params if len(args) == 0: log.write_and_print("No arguments provided: starting default run...") # time start of run log.write_and_print("Starting run....") log.write("# Parameters used:",print_time=False,lcomment=False) for option in str(params_var).split(", "): log.write("--%s = %s" % (option.split(": ")[0].strip("{").strip("'"), option.split(": ")[1].strip("}").strip("'")), print_time=False,lcomment=False) log.write("All parameters used:") for option in str(params).split(", "): log.write("# %s = %s" % (option.split(": ")[0].strip("{").strip("'"), option.split(": ")[1].strip("}").strip("'")), print_time=False,lcomment=False) log.write("# End of all parameters used.",print_time=False,lcomment=False) # Check whether there are command-line arguments which are not used if (len(param.args) > 0): txt = "The following command line arguments will not be used:" log.write_and_print(txt + str(param.args)) # Write svn information of input and scripts to log file. log.write("******************************************************",print_time=False,lcomment=True) log.write("Version information:",print_time=False,lcomment=True) log.write("Version information main script:",print_time=False,lcomment=True) log.write("Revision $LastChangedDate: 2013-09-25 13:37:10 +0200 (Tue, 25 Sep 2013)",print_time=False,lcomment=True) log.write("Date $LastChangedRevision: 344 $",print_time=False,lcomment=True) #message = get_versioninfo.get_versioninfo(params.inputdir,params.outputdir) #message.extend(get_versioninfo.get_versioninfo("tools",params.outputdir)) #for item in range(len(message)): # log.write(str(message[item]),print_time=False,lcomment=True) log.write("******************************************************",print_time=False,lcomment=True) # Read mask of the input grids. We take the iso grid as mask for this. # The mask takes care to do the calculations on an efficient way. Only # the grid cells which are in the mask are calculated and stored. if (params.lmask): mask = ascraster.create_mask(params.file_mask, 0.0,'GT',numtype=float) log.write_and_print(s.interval("Reading mask")) else: mask = None log.write_and_print(s.interval("No mask is used for this simulation.")) # Read original flow path direction map. Determination of the flow path is only possible in masked mode! # flowdir is expected on the fixed input directory. # if (params.lcreate_ldd_basinid == 1): # flowdir = ascraster.Asciigrid(ascii_file=os.path.join(params.fixinputdir,params.file_flowdir),mask=mask,numtype=int) # Change the flow dir numbers to PCraster format and create ldd and basinid map. # ldd = change_flowdir.change_flowdir(flowdir,mask,params.outputdir) # log.write_and_print(s.interval("Ldd conversion ready.")) # raise MyError0("Ready with creation of ldd and basinid") ldd = ascraster.Asciigrid(ascii_file=params.file_ldd,mask=mask,numtype=int) # Make dummy map with nodata in it. dummy_grid = ascraster.duplicategrid(ldd) # Make all values nodata dummy_grid.add_values(dummy_grid.length*[dummy_grid.nodata_value]) # Read landarea in km2 landarea_grid = ascraster.Asciigrid(ascii_file=params.landarea,mask=mask,numtype=float) # Convert landarea from m2 to km2 #landarea_grid.multiply(1.0e-6) # Calculate net precipitation in mm/yr #pnet = netprecip.calculate(params.inputdir,mask,params.outputdir) # Calculate accumulated water flux of each grid cell to the mouth of the river basin. Make discharge in km3 # Convert pnet [mm/yr] to km3 by multiplying with landarea (m2) and 10e-6 (conversion from mm to km) #water = ascraster.duplicategrid(landarea_grid) #for icell in range(landarea_grid.length): # wt = pnet.get_data(icell,0.0) * landarea_grid.get_data(icell,0.0) * 1.0e-6 # # Add water use of the population (and conversion from liters per day to km3 per year) # water.set_data(icell,wt) #discharge = accuflux.accuflux(ldd,water,negative_flux_possible=1) # Write discharge to output file: #discharge.write_ascii_file(params.filedischarge) #log.write_and_print(s.interval("Ready with calculating discharge.")) # Get the N emission from population. This are yearly loads. So divide with timespan. N_emiss_point = ascraster.Asciigrid(ascii_file=params.file_gemnsw,mask=mask,numtype=float) P_emiss_point = ascraster.Asciigrid(ascii_file=params.file_gempsw,mask=mask,numtype=float) N_emiss_point.divide(float(params.timespan)) P_emiss_point.divide(float(params.timespan)) # Retention on point sources N_emiss_point.multiply(1.0 - float(params.N_retention_point)) P_emiss_point.multiply(1.0 - float(params.P_retention_point)) # Get the fertilizer emission from agriculture N_emiss_agri_fert = ascraster.Asciigrid(ascii_file=params.fileNfert,mask=mask,numtype=float) P_emiss_agri_fert = ascraster.Asciigrid(ascii_file=params.filePfert,mask=mask,numtype=float) N_emiss_agri_fert.divide(float(params.timespan)) P_emiss_agri_fert.divide(float(params.timespan)) # Retention on agricultural sources N_emiss_agri_fert.multiply(1.0 - float(params.N_retention_agri)) P_emiss_agri_fert.multiply(1.0 - float(params.P_retention_agri)) # Get the manure emission from agriculture (livestock) N_emiss_agri_man = ascraster.Asciigrid(ascii_file=params.fileNmanure,mask=mask,numtype=float) P_emiss_agri_man = ascraster.Asciigrid(ascii_file=params.filePmanure,mask=mask,numtype=float) N_emiss_agri_man.divide(float(params.timespan)) P_emiss_agri_man.divide(float(params.timespan)) # Retention on agricultural sources N_emiss_agri_man.multiply(1.0 - float(params.N_retention_agri)) P_emiss_agri_man.multiply(1.0 - float(params.P_retention_agri)) # Get the emission from aquaculture N_emiss_aqua = ascraster.Asciigrid(ascii_file=params.fileNaqua,mask=mask,numtype=float) P_emiss_aqua = ascraster.Asciigrid(ascii_file=params.filePaqua,mask=mask,numtype=float) # Retention on aquaculture sources N_emiss_aqua.multiply(1.0 - float(params.N_retention_aqua)) P_emiss_aqua.multiply(1.0 - float(params.P_retention_aqua)) # Route all to the streams to the mouth of the river. accu_area = accuflux.accuflux(ldd,landarea_grid,negative_flux_possible=0) accu_N_emiss_point = accuflux.accuflux(ldd,N_emiss_point,negative_flux_possible=0) accu_P_emiss_point = accuflux.accuflux(ldd,P_emiss_point,negative_flux_possible=0) accu_N_emiss_agri_fert = accuflux.accuflux(ldd,N_emiss_agri_fert,negative_flux_possible=0) accu_P_emiss_agri_fert = accuflux.accuflux(ldd,P_emiss_agri_fert,negative_flux_possible=0) accu_N_emiss_agri_man = accuflux.accuflux(ldd,N_emiss_agri_man,negative_flux_possible=0) accu_P_emiss_agri_man = accuflux.accuflux(ldd,P_emiss_agri_man,negative_flux_possible=0) accu_N_emiss_aqua = accuflux.accuflux(ldd,N_emiss_aqua,negative_flux_possible=0) accu_P_emiss_aqua = accuflux.accuflux(ldd,P_emiss_aqua,negative_flux_possible=0) # Add all sources into one file. accu_N_emiss = ascraster.duplicategrid(accu_N_emiss_point) accu_N_emiss.add(accu_N_emiss_agri_fert) accu_N_emiss.add(accu_N_emiss_agri_man) accu_N_emiss.add(accu_N_emiss_aqua) accu_P_emiss = ascraster.duplicategrid(accu_P_emiss_point) accu_P_emiss.add(accu_P_emiss_agri_fert) accu_P_emiss.add(accu_P_emiss_agri_man) accu_P_emiss.add(accu_P_emiss_aqua) # Create retention grid for N and P. #Nret_grid = ascraster.duplicategrid(dummy_grid) #Nret_grid.add_values(Nret_grid.length*[params.N_retention]) #Pret_grid = ascraster.duplicategrid(dummy_grid) #Pret_grid.add_values(Nret_grid.length*[params.P_retention]) # Let the nitrogen flow through the basins. #accu_N_emiss = accuflux.accuflux(ldd,N_emiss,retentionmap=Nret_grid,negative_flux_possible=0) #accu_P_emiss = accuflux.accuflux(ldd,P_emiss,retentionmap=Pret_grid,negative_flux_possible=0) #accu_N_emiss = accuflux.accuflux(ldd,N_emiss,negative_flux_possible=0) #accu_P_emiss = accuflux.accuflux(ldd,P_emiss,negative_flux_possible=0) # Calculate the concentrations in the stream #N_conc = ascraster.duplicategrid(accu_N_emiss) #P_conc = ascraster.duplicategrid(accu_P_emiss) # Divide accu_N_emiss by discharge #N_conc.divide(discharge,default_nodata_value = 0.0) #P_conc.divide(discharge,default_nodata_value = 0.0) # Convert unit from kg/km3 to mgN/l #N_conc.multiply(1.0e-6) #P_conc.multiply(1.0e-6) # Write Nload and concentration to output file: accu_N_emiss.write_ascii_file(params.file_nload) #N_conc.write_ascii_file(params.file_nconc) accu_P_emiss.write_ascii_file(params.file_pload) #P_conc.write_ascii_file(params.file_pconc) log.write_and_print(s.interval("Ready with calculating of N and P load and concentration in grid cells.")) # Read basinid basinid = ascraster.Asciigrid(ascii_file=params.file_basinid,mask=mask,numtype=int) # Make a dictionary with the index of each river mouth mouth_dict = {} for icell in range(basinid.length): id = int(basinid.get_data(icell,-1)) if (id > 0): ldd_cell = int(ldd.get_data(icell,-1)) if (ldd_cell == 5): # I found a river mouth mouth_dict[id] = icell # Put accumulated information into a dictionary output_dict ={} for key in mouth_dict: output_dict[key] = general_class.General() output_dict[key].add_item("basinid",key) output_dict[key].add_item("Area",accu_area.get_data(mouth_dict[key])) output_dict[key].add_item("Nload_point",accu_N_emiss_point.get_data(mouth_dict[key])) output_dict[key].add_item("Nload_fert",accu_N_emiss_agri_fert.get_data(mouth_dict[key])) output_dict[key].add_item("Nload_manure",accu_N_emiss_agri_man.get_data(mouth_dict[key])) output_dict[key].add_item("Nload_aqua",accu_N_emiss_aqua.get_data(mouth_dict[key])) output_dict[key].add_item("Nload_total",accu_N_emiss.get_data(mouth_dict[key])) #output_dict[key].add_item("discharge",discharge.get_data(mouth_dict[key])) #output_dict[key].add_item("Nconc",N_conc.get_data(mouth_dict[key])) output_dict[key].add_item("Pload_point",accu_P_emiss_point.get_data(mouth_dict[key])) output_dict[key].add_item("Pload_fert",accu_P_emiss_agri_fert.get_data(mouth_dict[key])) output_dict[key].add_item("Pload_manure",accu_P_emiss_agri_man.get_data(mouth_dict[key])) output_dict[key].add_item("Pload_aqua",accu_P_emiss_aqua.get_data(mouth_dict[key])) output_dict[key].add_item("Pload_total",accu_P_emiss.get_data(mouth_dict[key])) #output_dict[key].add_item("Pconc",P_conc.get_data(mouth_dict[key])) # Make a list of keys and sort this for output purpose outputlist = [] for key in mouth_dict: outputlist.append(key) outputlist.sort() fp = open(params.fileoutput_table,"w") lheader = True for key in outputlist: output_dict[key].write(fp,lheader=lheader,sep=";") lheader = False fp.close() log.write_and_print(s.total("Total run")) del log