def make_strahlerordermap(lddmap): ''' ''' cell = [] source_array = [cell] * lddmap.length strahler = ascraster.duplicategrid(lddmap) order_map = determine_order(lddmap) max_order = order_map.maximum() order_map.write_ascii_file('order_map.asc') # Loop over all cells to create source list: for o in range(0, max_order + 1): ordermask = ascraster.create_mask('order_map.asc', o, 'EQ', numtype=int) for icell in ordermask: if (lddmap.get_data(icell) != None): if (o == 0): strahler.set_data(icell, 6) icell_next = goto_nextcell(icell, lddmap.get_data(icell), lddmap.ncols) if (source_array[icell_next] == []): source_array[icell_next] = [icell] else: source_array[icell_next].append(icell) if (o != 0): dum = list() for source_icell in source_array[icell]: dum.append(strahler.get_data(source_icell)) if (len(dum) > 1): if (all(x == dum[0] for x in dum)): strahler.set_data(icell, dum[0] + 1) else: strahler.set_data(icell, max(dum)) else: strahler.set_data(icell, dum[0]) strahler.write_ascii_file('real_strahler.asc')
def do(mask_asc_fn, mask_id, dum_asc, logical="EQ", mask_type='np_grid'): dum_mask = ascraster.create_mask(mask_asc_fn, mask_id, logical=logical, numtype=int) mask = [] if mask_type == "rowcol": for i in dum_mask: mask.append(dum_asc.get_row_col_from_index(i)) elif mask_type == "index": for i in dum_mask: mask.append(i) elif mask_type == "latlon": for i in dum_mask: mask.append(dum_asc.get_coord_from_index(i)) elif mask_type == "np_grid": mask = np.zeros((dum_asc.nrows, dum_asc.ncols), dtype=bool) mask[:, :] = True for i in dum_mask: row, col = dum_asc.get_row_col_from_index(i) mask[row, col] = False return mask
else: print('Test 1 write ascii file passed.') my_sys.my_removefile("testout1.asc") # Test 2 write ascii file, grid with nodata values grid1 = ascraster.Asciigrid(ascii_file='testgrid8.asc', numtype=int) grid1.write_ascii_file("testout2.asc") msg = os.system("diff testgrid8.asc testout2.asc") if (msg != 0): print('Test 2 write ascii file is not a succes. Differences above.') else: print('Test 2 write ascii file passed.') my_sys.my_removefile("testout2.asc") # Test 3 write ascii file, grid with nodata values and mask mask = ascraster.create_mask('testgrid8.asc', 0, logical="GE", numtype=int) grid1 = ascraster.Asciigrid(ascii_file='testgrid8.asc', mask=mask, numtype=int) grid1.write_ascii_file("testout3.asc") msg = os.system("diff testgrid8.asc testout3.asc") if (msg != 0): print('Test 3 write ascii file is not a succes. Differences above.') else: print('Test 3 write ascii file passed.') my_sys.my_removefile("testout3.asc") # Test 4 write ascii file, grid with nodata values and mask with all cells have a value mask = ascraster.create_mask('testgrid8.asc', 0, logical="GE", numtype=int) #Add two cells with nodata to the mask mask.append(16) mask.append(17) grid1 = ascraster.Asciigrid(ascii_file='testgrid8.asc', mask=mask, numtype=int)
def run_aquaculture_model(args): ''' Run aquaculture 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_aquaculture.InputAgri(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 prods per province. Here no interpolation is done. So the exact year must be specified. production = general_class.read_general_file(params.fileproduction,sep=";",key=None,out_type="list") # Read N and P excretion. File must contain the proder Species;N_excretion;P_excretion N_excretion = general_class.read_general_file(params.fileN_excretion,sep=";",key="Species",out_type="dict") P_excretion = general_class.read_general_file(params.fileP_excretion,sep=";",key="Species",out_type="dict") # Make an excretion rate for this year. for key in N_excretion: years = [] vals = [] for name in N_excretion[key].get_attrib(): years.append(float(name)) vals.append(float(N_excretion[key].get_val(name))) N_excretion[key].add_item("N_excretion",interpolate.interpolate(params.year,years,vals,extrapol=1)) for key in P_excretion: years = [] vals = [] for name in P_excretion[key].get_attrib(): years.append(float(name)) vals.append(float(P_excretion[key].get_val(name))) P_excretion[key].add_item("P_excretion",interpolate.interpolate(params.year,years,vals,extrapol=1)) # Calculate the total N and P manure per province for each line in the production input file Nout = {} Pout = {} for item in range(len(production)): # Get the number of prods for the year specified. prod = production[item].get_val(str(params.year)) spec = production[item].get_val("Species") try: # Get the N and P excretion for this animal (kg per ton production) Nexcret = N_excretion[spec].get_val("N_excretion") Pexcret = P_excretion[spec].get_val("P_excretion") except KeyError: raise MyError("This animal " + spec + " has no excretion rate in file: " + params.fileP_excretion +\ " or in file " + params.fileN_excretion) # Multiply prods with excretion production[item].add_item("Nout",float(Nexcret)*float(prod)) production[item].add_item("Pout",float(Pexcret)*float(prod)) #BF=brackish fishponds, FF=freshwater fishponds, MF=marine water fishponds, BC=brackish cages, #FC=freshwater cages, MC=marine water cages, BP=brackish pens, #FP=freshwater pens, MP=marine water pens fresh_environments = ["BF", "FF", "BC","FC", "BP", "FP"] marine_environments = ["MF", "MC", "MP"] # Allocation of Nitrogen, fresh and brakish waters outgrid = ascraster.Asciigrid(ascii_file=params.fileiso,mask=mask) outgrid.add_values(outgrid.length*[0.0]) for environ in fresh_environments: grid = allocation_aquaculture.calculate(params,mask,production,environ=environ,substance="N") outgrid.add(grid) outgrid.write_ascii_file(params.fileNaqua) print "Total N of freshwater aquaculture in kg N: ",sum(outgrid.values) log.write_and_print(s.interval("Ready with allocation of N of freshwater aquaculture to grid cells.")) # Allocation of Phosphorus, fresh and brakish waters outgrid = ascraster.Asciigrid(ascii_file=params.fileiso,mask=mask) outgrid.add_values(outgrid.length*[0.0]) for environ in fresh_environments: grid = allocation_aquaculture.calculate(params,mask,production,environ=environ,substance="P") outgrid.add(grid) outgrid.write_ascii_file(params.filePaqua) print "Total P of freshwater aquaculture in kg P: ",sum(outgrid.values) log.write_and_print(s.interval("Ready with allocation of P of freshwater aquaculture to grid cells.")) # Allocation of Nitrogen, marine waters outgrid = ascraster.Asciigrid(ascii_file=params.fileprov_marine,mask=None) outgrid.add_values(outgrid.length*[0.0]) for environ in marine_environments: grid = allocation_aquaculture.calculate(params,mask,production,environ=environ,substance="N") outgrid.add(grid) outgrid.write_ascii_file(params.fileNmarine) print "Total N of marine aquaculture in kg N: ",sum(outgrid.values) log.write_and_print(s.interval("Ready with allocation of N of marine aquaculture to grid cells.")) # Allocation of Phosphorus, fresh and brakish waters outgrid = ascraster.Asciigrid(ascii_file=params.fileprov_marine,mask=None) outgrid.add_values(outgrid.length*[0.0]) for environ in marine_environments: grid = allocation_aquaculture.calculate(params,mask,production,environ=environ,substance="P") outgrid.add(grid) outgrid.write_ascii_file(params.filePmarine) print "Total P of marine aquaculture in kg P: ",sum(outgrid.values) log.write_and_print(s.interval("Ready with allocation of P of marine aquaculture to grid cells.")) fp = open(params.fileoutput_table,"w") lheader = True for item in range(len(production)): production[item].write(fp,sep=";",lheader=lheader,NoneValue="") lheader = False fp.close() log.write_and_print(s.total("Total run")) del log
def run_pointsrc_model(args): ''' Run point sources 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_pointsrc.InputPointsrc(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.")) # Make a list of all district codes that we want to calculate # Could be done by reading a grid, but now it is done by reading a input file with a complet set of isocodes. isocode = make_iso_list.make_iso_list(params,mask) # Read provence codes (isocodes) isogrid = ascraster.Asciigrid(ascii_file=params.fileiso,mask=mask,numtype=int) # Read total number of population PopNumTot,popgrid, = read_popnum.read_popnum(params,mask,isogrid,isocode) print_debug("PopNumTot",PopNumTot) # Calculate the coastal population. PopNum_coast,popgrid_coast = coast_population.calculate(params,mask,isogrid,isocode,popgrid) # Calculation of number of people without hte coastal population PopNum = {} for key in isocode: PopNum[key] = PopNumTot[key] - PopNum_coast[key] # Create regiondata class regiondata_dict = create_regiondata.create_regiondata(params,isocode) regiondata.put2regiondata(PopNumTot,regiondata_dict,"PopNum") regiondata.put2regiondata(PopNum_coast,regiondata_dict,"PopNum_coast") print_debug("PopNum_without_coast",PopNum) print_debug("PopNum_coast",PopNum_coast) # Determine the N and P emission per person. Nemiss,Pemiss = calculate_emission_per_person.calculate_emission_per_person(params,isocode,regiondata_dict,PopNumTot) log.write_and_print(s.interval("Ready with calculation of N and P emission per capita.")) # Calculation of N and P emissionto the coastal zone N_emiss_coast = {} P_emiss_coast = {} for key in isocode: N_emiss_coast[key] = PopNum_coast[key] * Nemiss[key] P_emiss_coast[key] = PopNum_coast[key] * Pemiss[key] regiondata.put2regiondata(N_emiss_coast,regiondata_dict,"N_emiss_coast") regiondata.put2regiondata(P_emiss_coast,regiondata_dict,"P_emiss_coast") print_debug("N_emiss_coast",N_emiss_coast) print_debug("P_emiss_coast",P_emiss_coast) # Read the percentage urban population. PopUrb = data_class.get_data(params.filePopUrb,year=params.year,sep=params.sep,isocode=isocode,method=1) print_debug("PopUrb",PopUrb) # Calculatation of urban and connected number of people PopUrbNum = {} for key in isocode: PopUrbNum[key] = PopNum[key] * (PopUrb[key]/100.0) regiondata.put2regiondata(PopUrbNum,regiondata_dict,"PopUrbNum") print_debug("PopUrbNum",PopUrbNum) # Calculatation of connected number of people # Read the percentage of the connected population. PopCon = data_class.get_data(params.filePopCon,year=params.year,sep=params.sep,isocode=isocode,method=1) print_debug("PopCon",PopCon) PopConNum = {} for key in isocode: PopConNum[key] = PopNum[key] * (PopCon[key]/100.0) regiondata.put2regiondata(PopConNum,regiondata_dict,"PopConNum") print_debug("PopConNum",PopConNum) # Get P emissions in kg P per year per inhabitant from laundry detergents and from dishwasher detergents. EmPldet = data_class.get_data(params.filePlaundry,year=params.year,sep=params.sep,isocode=isocode,method=1) EmPddet = data_class.get_data(params.filePdishwasher,year=params.year,sep=params.sep,isocode=isocode,method=1) # Add to global database regiondata.put2regiondata(EmPldet,regiondata_dict,"EmPldet") regiondata.put2regiondata(EmPddet,regiondata_dict,"EmPddet") print_debug("EmPldet",EmPldet) print_debug("EmPddet",EmPddet) # Calculation of industry emission to surface water Nemiss_industry,Pemiss_industry = industry_emission.calculate(params,isocode,regiondata_dict,PopNumTot,\ Nemiss,Pemiss,EmPldet,EmPddet,PopUrbNum,PopConNum) log.write_and_print(s.interval("Ready with calculation of N and P emission from industry.")) # Read the percentage of the primary, secondary and tertiary treatment. PrimTreatment = data_class.get_data(params.filePrimTreatment,year=params.year,sep=params.sep,isocode=isocode,method=0,weighing=0.0) print_debug("PrimTreatment",PrimTreatment) SecTreatment = data_class.get_data(params.fileSecTreatment,year=params.year,sep=params.sep,isocode=isocode,method=0,weighing=0.0) print_debug("SecTreatment",SecTreatment) TertTreatment = data_class.get_data(params.fileTertTreatment,year=params.year,sep=params.sep,isocode=isocode,method=0,weighing=0.0) print_debug("TertTreatment",TertTreatment) # Sum of the percentage treatment is smaller or equal to percentage population connected. # Determine the percentage of connected but not treated. NoTreatment = {} for key in isocode: total = PrimTreatment[key] + SecTreatment[key] + TertTreatment[key] if (total > PopCon[key] and total > 0.0): reduction = PopCon[key]/total print "Reduction of treatment for isocode country: " + str(key) + " with reduction: " + str(reduction) PrimTreatment[key] *= reduction SecTreatment[key] *= reduction TertTreatment[key] *= reduction NoTreatment[key] = 0.0 else: NoTreatment[key] = PopCon[key] - total print_debug("NoTreatment: sum equal to PopCon",NoTreatment) print_debug("PrimTreatment: sum equal to PopCon",PrimTreatment) print_debug("SecTreatment: sum equal to PopCon",SecTreatment) print_debug("TertTreatment: sum equal to PopCon",TertTreatment) # Determine the percentage of not-connected and not treated in urban areas. NotConnected = {} for key in isocode: if (PopUrb[key] > PopCon[key]): NotConnected[key] = PopUrb[key] - PopCon[key] else: NotConnected[key] = 0.0 print_debug("NotConnected",NotConnected) # Read the fraction removal of the primary, secondary and tertiary treatment for N and P. N_PrimRemoval = data_class.get_data(params.fileN_PrimRemoval,year=params.year,sep=params.sep,isocode=isocode,method=0,weighing=params.N_PrimRemoval_def) print_debug("N_PrimRemoval",N_PrimRemoval) N_SecRemoval = data_class.get_data(params.fileN_SecRemoval,year=params.year,sep=params.sep,isocode=isocode,method=0,weighing=params.N_SecRemoval_def) print_debug("N_SecRemoval",N_SecRemoval) N_TertRemoval = data_class.get_data(params.fileN_TertRemoval,year=params.year,sep=params.sep,isocode=isocode,method=0,weighing=params.N_TertRemoval_def) print_debug("N_TertRemoval",N_TertRemoval) P_PrimRemoval = data_class.get_data(params.fileP_PrimRemoval,year=params.year,sep=params.sep,isocode=isocode,method=0,weighing=params.P_PrimRemoval_def) print_debug("P_PrimRemoval",P_PrimRemoval) P_SecRemoval = data_class.get_data(params.fileP_SecRemoval,year=params.year,sep=params.sep,isocode=isocode,method=0,weighing=params.P_SecRemoval_def) print_debug("P_SecRemoval",P_SecRemoval) P_TertRemoval = data_class.get_data(params.fileP_TertRemoval,year=params.year,sep=params.sep,isocode=isocode,method=0,weighing=params.P_TertRemoval_def) print_debug("P_TertRemoval",P_TertRemoval) # Get the reuse fraction for agricultural, which comes from the unconnected people. agricultural_reuse = data_class.get_data(params.file_agri_reuse,year=params.year,sep=params.sep,isocode=isocode,method=1) # Calculate the percentage of each flow to NoConnectionRemoval (N and P) N_NoConnRemoval = {} P_NoConnRemoval = {} N_NoConnRemoval_agri = {} P_NoConnRemoval_agri = {} N_NoConnRemoval_other = {} P_NoConnRemoval_other = {} for key in isocode: N_NoConnRemoval[key] = 100.0 - (100.0 * (1.0 - agricultural_reuse[key]) * 0.5 * (1.0 - params.human_N_vol)) P_NoConnRemoval[key] = 100.0 - (100.0 * (1.0 - agricultural_reuse[key]) * 0.5) N_NoConnRemoval_agri[key] = 100.0 * agricultural_reuse[key] * (1.0 - params.human_N_vol) P_NoConnRemoval_agri[key] = 100.0 * agricultural_reuse[key] N_NoConnRemoval_other[key] = 100.0 * params.human_N_vol + \ 100.0 * (1.0 - params.human_N_vol)*(1.0 - agricultural_reuse[key])*0.5 P_NoConnRemoval_other[key] = 100.0 * (1.0 - agricultural_reuse[key]) * 0.5 print_debug("N_NoConnRemoval",N_NoConnRemoval) print_debug("P_NoConnRemoval",P_NoConnRemoval) print_debug("N_NoConnRemoval_agri",N_NoConnRemoval_agri) print_debug("P_NoConnRemoval_agri",P_NoConnRemoval_agri) print_debug("N_NoConnRemoval_other",N_NoConnRemoval_other) print_debug("P_NoConnRemoval_other",P_NoConnRemoval_other) regiondata.put2regiondata(N_NoConnRemoval,regiondata_dict,"N_NoConnRemoval") regiondata.put2regiondata(P_NoConnRemoval,regiondata_dict,"P_NoConnRemoval") # Calculate the total emission of the people Ntot = {} Ptot = {} NtotConnected = {} PtotConnected = {} NnotConnected = {} PnotConnected = {} Nsewerage_other = {} Psewerage_other = {} Npipe_loss = {} Ppipe_loss = {} # Remove the following when testing is ready Nhuman_connect = {} Nhuman_connect_with_pipe_loss = {} Phuman_connect = {} Phuman_connect_with_pipe_loss = {} Pdetergents1 = {} EmPldet_tot = {} EmPddet_tot = {} for key in isocode: Ntot[key] = PopNumTot[key] * Nemiss[key] Ptot[key] = PopNumTot[key] * Pemiss[key] + (PopNum[key]*PopCon[key]/100.0)*(EmPldet[key] + EmPddet[key]) NtotConnected[key] = Ntot[key] * (PopCon[key]/100.0) Nhuman_connect[key] = NtotConnected[key] PtotConnected[key] = (PopNum[key] * PopCon[key]/100.0)*(EmPldet[key] + EmPddet[key] + Pemiss[key]) Phuman_connect[key] = PtotConnected[key] Pdetergents1[key] = (PopNum[key] * PopCon[key]/100.0)*(EmPldet[key] + EmPddet[key]) EmPldet_tot[key] = (PopNum[key] * PopCon[key]/100.0) * EmPldet[key] EmPddet_tot[key] = (PopNum[key] * PopCon[key]/100.0) * EmPddet[key] # There is an extra loss of N and P due to leakage of pipes etc. Npipe_loss[key] = NtotConnected[key] * params.sewer_pipe_loss_fraction NtotConnected[key] *= (1.0 - params.sewer_pipe_loss_fraction) Nhuman_connect_with_pipe_loss[key] = NtotConnected[key] Ppipe_loss[key] = PtotConnected[key] * params.sewer_pipe_loss_fraction PtotConnected[key] *= (1.0 - params.sewer_pipe_loss_fraction) Phuman_connect_with_pipe_loss[key] = PtotConnected[key] # Add industry to the total connected load. NtotConnected[key] += Nemiss_industry[key] PtotConnected[key] += Pemiss_industry[key] NnotConnected[key] = Ntot[key] * NotConnected[key]/100.0 PnotConnected[key] = (PopNum[key] * Pemiss[key]) * (NotConnected[key]/100.0) print_debug("NotConnected",NotConnected) print_debug("PopCon",PopCon) print_debug("Ntot total population",Ntot) print_debug("Ptot total population",Ptot) print_debug("Nhuman_connect",Nhuman_connect) print_debug("Nhuman_connect_with_pipe_loss",Nhuman_connect_with_pipe_loss) print_debug("Phuman_connect",Phuman_connect) print_debug("Phuman_connect_with_pipe_loss",Phuman_connect_with_pipe_loss) print_debug("Pdetergents1",Pdetergents1) print_debug("Nemiss_industry",Nemiss_industry) print_debug("Pemiss_industry",Pemiss_industry) print_debug("NtotConnected included industry with pipe lost",NtotConnected) print_debug("PtotConnected included industry with pipe lost",PtotConnected) print_debug("NnotConnected",NnotConnected) print_debug("PnotConnected",PnotConnected) # Put all parameters in global database regiondata.put2regiondata(NtotConnected,regiondata_dict,"NtotConnected") regiondata.put2regiondata(NnotConnected,regiondata_dict,"NnotConnected") regiondata.put2regiondata(Ntot,regiondata_dict,"Ntot") regiondata.put2regiondata(PtotConnected,regiondata_dict,"PtotConnected") regiondata.put2regiondata(PnotConnected,regiondata_dict,"PnotConnected") regiondata.put2regiondata(Ptot,regiondata_dict,"Ptot") regiondata.put2regiondata(EmPddet_tot,regiondata_dict,"EmPddet_tot") regiondata.put2regiondata(EmPldet_tot,regiondata_dict,"EmPldet_tot") # Convert treatment faction of total people to fraction for the connected people. for key in isocode: if (PopCon[key] > 0.0): PrimTreatment[key] /= PopCon[key] SecTreatment[key] /= PopCon[key] TertTreatment[key] /= PopCon[key] NoTreatment[key] /= PopCon[key] else: PrimTreatment[key] = 0.0 SecTreatment[key] = 0.0 TertTreatment[key] = 0.0 NoTreatment[key] = 0.0 print_debug("PrimTreatment",PrimTreatment) print_debug("SecTreatment",SecTreatment) print_debug("TertTreatment",TertTreatment) print_debug("NoTreatment",NoTreatment) # Retention for the connected situation Nretention={} Pretention={} for key in isocode: Nretention[key] = N_PrimRemoval[key] * PrimTreatment[key] +\ N_SecRemoval[key] * SecTreatment[key] +\ N_TertRemoval[key] * TertTreatment[key] Pretention[key] = P_PrimRemoval[key] * PrimTreatment[key] +\ P_SecRemoval[key] * SecTreatment[key] +\ P_TertRemoval[key] * TertTreatment[key] print_debug("Nretention",Nretention) print_debug("Pretention",Pretention) regiondata.put2regiondata(Nretention,regiondata_dict,"Nretention") regiondata.put2regiondata(Pretention,regiondata_dict,"Pretention") NtotConnected_eff = {} PtotConnected_eff = {} NnotConnected_eff = {} PnotConnected_eff = {} Ntot_eff = {} Ptot_eff = {} NnotConnect_agri ={} NnotConnect_other ={} PnotConnect_agri ={} PnotConnect_other ={} Nhuman_connect_tosw = {} Nindustry_connect_tosw = {} Phuman_connect_tosw = {} Pindustry_connect_tosw = {} Nother_prim = {} Pother_prim = {} Nother_sec = {} Pother_sec = {} Nother_tert = {} Pother_tert = {} for key in isocode: Nsewerage_other[key] = NtotConnected[key] * (Nretention[key]/100.0) + Npipe_loss[key] Nother_prim[key] = NtotConnected[key] * (N_PrimRemoval[key] * PrimTreatment[key] /100.0) Nother_sec[key] = NtotConnected[key] * (N_SecRemoval[key] * SecTreatment[key] /100.0) Nother_tert[key] = NtotConnected[key] * (N_TertRemoval[key] * TertTreatment[key] /100.0) NtotConnected_eff[key] = NtotConnected[key] * (1.0 - (Nretention[key]/100.0)) Nhuman_connect_tosw[key] = Nhuman_connect_with_pipe_loss[key] * (1.0 - (Nretention[key]/100.0)) Nindustry_connect_tosw[key] = Nemiss_industry[key] * (1.0 - (Nretention[key]/100.0)) Pindustry_connect_tosw[key] = Pemiss_industry[key] * (1.0 - (Pretention[key]/100.0)) Psewerage_other[key] = PtotConnected[key] * (Pretention[key]/100.0) + Ppipe_loss[key] Pother_prim[key] = PtotConnected[key] * (P_PrimRemoval[key] * PrimTreatment[key] /100.0) Pother_sec[key] = PtotConnected[key] * (P_SecRemoval[key] * SecTreatment[key] /100.0) Pother_tert[key] = PtotConnected[key] * (P_TertRemoval[key] * TertTreatment[key] /100.0) PtotConnected_eff[key] = PtotConnected[key] * (1.0 - (Pretention[key]/100.0)) Phuman_connect_tosw[key] = Phuman_connect_with_pipe_loss[key] * (1.0 - (Pretention[key]/100.0)) # Not connected part. NnotConnected_eff[key] = NnotConnected[key] * (1.0 - (N_NoConnRemoval[key]/100.0)) PnotConnected_eff[key] = PnotConnected[key] * (1.0 - (P_NoConnRemoval[key]/100.0)) NnotConnect_agri[key] = NnotConnected[key] * (N_NoConnRemoval_agri[key]/100.0) PnotConnect_agri[key] = PnotConnected[key] * (P_NoConnRemoval_agri[key]/100.0) NnotConnect_other[key] = NnotConnected[key] * (N_NoConnRemoval_other[key]/100.0) PnotConnect_other[key] = PnotConnected[key] * (P_NoConnRemoval_other[key]/100.0) # Make a total to surface water. Ntot_eff[key] = NtotConnected_eff[key] + NnotConnected_eff[key] Ptot_eff[key] = PtotConnected_eff[key] + PnotConnected_eff[key] print_debug("NtotConnected_eff",NtotConnected_eff) print_debug("Nhuman_connect_tosw",Nhuman_connect_tosw) print_debug("Nindustry_connect_tosw",Nindustry_connect_tosw) print_debug("Nsewerage_other",Nsewerage_other) print_debug("PtotConnected_eff",PtotConnected_eff) print_debug("Phuman_connect_tosw",Phuman_connect_tosw) print_debug("Pindustry_connect_tosw",Pindustry_connect_tosw) print_debug("Psewerage_other",Psewerage_other) print_debug("NnotConnected_eff",NnotConnected_eff) print_debug("PnotConnected_eff",PnotConnected_eff) print_debug("NnotConnect_agri",NnotConnect_agri) print_debug("NnotConnect_other",NnotConnect_other) print_debug("PnotConnect_agri",PnotConnect_agri) print_debug("PnotConnect_other",PnotConnect_other) print_debug("Ntot_eff",Ntot_eff) print_debug("Ptot_eff",Ptot_eff) # Put all parameters in global database regiondata.put2regiondata(NtotConnected_eff,regiondata_dict,"NtotConnected_eff") regiondata.put2regiondata(NnotConnected_eff,regiondata_dict,"NnotConnected_eff") regiondata.put2regiondata(NnotConnect_agri,regiondata_dict,"NnotConnect_agri") regiondata.put2regiondata(NnotConnect_other,regiondata_dict,"NnotConnect_other") regiondata.put2regiondata(Nsewerage_other,regiondata_dict,"Nsewerage_other") regiondata.put2regiondata(Nother_prim,regiondata_dict,"Nother_prim") regiondata.put2regiondata(Nother_sec,regiondata_dict,"Nother_sec") regiondata.put2regiondata(Nother_tert,regiondata_dict,"Nother_tert") regiondata.put2regiondata(Ntot_eff,regiondata_dict,"Ntot_eff") regiondata.put2regiondata(PtotConnected_eff,regiondata_dict,"PtotConnected_eff") regiondata.put2regiondata(PnotConnected_eff,regiondata_dict,"PnotConnected_eff") regiondata.put2regiondata(PnotConnect_agri,regiondata_dict,"PnotConnect_agri") regiondata.put2regiondata(PnotConnect_other,regiondata_dict,"PnotConnect_other") regiondata.put2regiondata(Psewerage_other,regiondata_dict,"Psewerage_other") regiondata.put2regiondata(Pother_prim,regiondata_dict,"Pother_prim") regiondata.put2regiondata(Pother_sec,regiondata_dict,"Pother_sec") regiondata.put2regiondata(Pother_tert,regiondata_dict,"Pother_tert") regiondata.put2regiondata(Ptot_eff,regiondata_dict,"Ptot_eff") log.write_and_print(s.interval("Ready with calculation of N and P emission to surface water.")) # Make N and P human waste in kg. testN = {} testP = {} for key in isocode: pop = max(PopUrbNum[key],PopConNum[key]) emisN_rur = regiondata_dict[key].N_human_waste_other + Nemiss[key] emisP_rur = regiondata_dict[key].P_human_waste_other + Pemiss[key] regiondata_dict[key].N_human_waste_other *= pop regiondata_dict[key].P_human_waste_other *= pop testN[key] = regiondata_dict[key].N_human_waste_other testP[key] = regiondata_dict[key].P_human_waste_other regiondata_dict[key].Nemiss_pop_rural = emisN_rur * (PopNum[key] - pop) regiondata_dict[key].Pemiss_pop_rural = emisP_rur * (PopNum[key] - pop) print_debug("N_human_waste_other",testN) print_debug("P_human_waste_other",testP) del testN del testP # Read septic tank connection for the rural population Septic_connection = data_class.get_data(params.fileSeptic_rural,year=params.year,sep=params.sep,isocode=isocode,method=1) Nemiss_rural_sw = {} Pemiss_rural_sw = {} for key in isocode: regiondata_dict[key].Nemiss_rural_sw = regiondata_dict[key].Nemiss_pop_rural *(1.0-0.01*Septic_connection[key]) *\ (1.0 - params.Nreduction_septic_to_river) regiondata_dict[key].Pemiss_rural_sw = regiondata_dict[key].Pemiss_pop_rural *(1.0-0.01*Septic_connection[key]) *\ (1.0 - params.Preduction_septic_to_river) regiondata_dict[key].Nemiss_rural_grw = regiondata_dict[key].Nemiss_pop_rural *(0.01*Septic_connection[key]) *\ (1.0 - params.Nreduction_septic_to_grw) regiondata_dict[key].Pemiss_rural_grw = regiondata_dict[key].Pemiss_pop_rural *(0.01*Septic_connection[key]) *\ (1.0 - params.Preduction_septic_to_grw) regiondata_dict[key].Nemiss_rural_other = regiondata_dict[key].Nemiss_pop_rural *(1.0 - 0.01*Septic_connection[key]) *\ params.Nreduction_septic_to_river + regiondata_dict[key].Nemiss_pop_rural *\ (0.01 * Septic_connection[key]) *\ params.Nreduction_septic_to_grw regiondata_dict[key].Pemiss_rural_other = regiondata_dict[key].Pemiss_pop_rural *(1.0 - 0.01*Septic_connection[key]) *\ params.Preduction_septic_to_river + regiondata_dict[key].Pemiss_pop_rural *\ (0.01 * Septic_connection[key]) *\ params.Preduction_septic_to_grw # Add rural emissions to surface water effluent. regiondata_dict[key].Ntot_eff += regiondata_dict[key].Nemiss_rural_sw regiondata_dict[key].Ptot_eff += regiondata_dict[key].Pemiss_rural_sw Nemiss_rural_sw[key] = regiondata_dict[key].Nemiss_rural_sw Pemiss_rural_sw[key] = regiondata_dict[key].Pemiss_rural_sw # Add the agricultural load and the load to other sector for key in isocode: regiondata_dict[key].Ntotal_agri = regiondata_dict[key].NnotConnect_agri regiondata_dict[key].Ntotal_other = regiondata_dict[key].NnotConnect_other +\ regiondata_dict[key].N_human_waste_other +\ regiondata_dict[key].Nindustry_other +\ regiondata_dict[key].Nother_prim + regiondata_dict[key].Nother_sec +\ regiondata_dict[key].Nother_tert + Npipe_loss[key] +\ regiondata_dict[key].Nemiss_rural_grw +\ regiondata_dict[key].Nemiss_rural_other regiondata_dict[key].Nother_NH3 = NnotConnected[key] * params.human_N_vol regiondata_dict[key].Nother_soil = Npipe_loss[key] + \ regiondata_dict[key].NnotConnect_other - regiondata_dict[key].Nother_NH3 +\ regiondata_dict[key].Nemiss_rural_grw regiondata_dict[key].Ptotal_agri = regiondata_dict[key].PnotConnect_agri regiondata_dict[key].Ptotal_other = regiondata_dict[key].PnotConnect_other +\ regiondata_dict[key].P_human_waste_other +\ regiondata_dict[key].Pindustry_other +\ regiondata_dict[key].Pother_prim + regiondata_dict[key].Pother_sec +\ regiondata_dict[key].Pother_tert + Ppipe_loss[key] +\ regiondata_dict[key].Pemiss_rural_grw +\ regiondata_dict[key].Pemiss_rural_other regiondata_dict[key].Pother_soil = Ppipe_loss[key] +\ regiondata_dict[key].PnotConnect_other +\ regiondata_dict[key].Pemiss_rural_grw # Distribute the results on the grid. if (params.grid_output == 1): allocation_emission.allocation_emission(params,mask,isocode,isogrid,PopNumTot,popgrid,PopCon,PopUrb,\ NtotConnected_eff,PtotConnected_eff,NnotConnected_eff,\ PnotConnected_eff,NtotConnected,\ N_PrimRemoval,PrimTreatment,\ N_SecRemoval,SecTreatment,\ N_TertRemoval,TertTreatment,\ Nemiss_rural_sw,Pemiss_rural_sw, PopNum_coast,popgrid_coast) load_coast.calculate(params,mask,isocode,isogrid,popgrid_coast,Nemiss,Pemiss) log.write_and_print(s.interval("Ready with allocation of N and P emission to grid cells.")) # Aggregation of the tabular information on another id-grid than basinid's if (params.aggregationgrid != None): make_index_grid.make_index_grid(params,mask,regiondata_dict,isogrid) qq = os.path.basename(params.aggregationgrid) filename = os.path.join(params.outputdir,params.scenarioname +"_" + str(params.year) + "_" + qq + ".csv") mouth.aggregate(regiondata_dict,params.aggregationgrid,mask,\ filename,params.sep,keyname="isocode") # Aggregate to global level for key in regiondata_dict.keys(): try: regiondata_dict["Total"].sum(regiondata_dict[key]) except KeyError: regiondata_dict["Total"] = regiondata_dict[key].copy() # Write all region information to output file: fp = open(params.fileoutput_table,"w") regiondata_dict[isocode[0]].write_header(fp,sep=params.sep) write_dict.write_dict(fp_in=fp,filename=None,dic=regiondata_dict,headerkey="",sep=params.sep,lkey=0,ldebug=1,fatal=0) fp.close() log.write_and_print(s.total("Total run")) del log
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
def run_agri_model(args): ''' Run agricultural 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_agri.InputAgri(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 province codes (isocodes) isogrid = ascraster.Asciigrid(ascii_file=params.fileiso,mask=mask,numtype=int) # Make a list of isocodes for the provinces. isocode = list(set(isogrid.values)) if -9999 in isocode: isocode.remove(-9999) if 9999 in isocode: isocode.remove(9999) agri_area,Nfert,Pfert = allocation_agri.calculate(params,mask,isocode,isogrid) log.write_and_print(s.interval("Ready with allocation of N and P fertilizer to grid cells.")) Nmanure,Pmanure = allocation_manure.calculate(params,mask,isocode,isogrid,agri_area) log.write_and_print(s.interval("Ready with allocation of N and P manure to grid cells.")) # Write all region information to output file: #fp = open(params.fileoutput_table,"w") #regiondata_dict[isocode[0]].write_header(fp,sep=params.sep) #write_dict.write_dict(fp_in=fp,filename=None,dic=regiondata_dict,headerkey="",sep=params.sep,lkey=0,ldebug=1,fatal=0) #fp.close() log.write_and_print(s.total("Total run")) del log
def run_dgnm(args): ''' Run DGNM model main routine args: list of arguments passed trough command-line or other script ''' # Parse command-line arguments and set parameters for script # Startup logging and runtime start params, log, s = general_startup.general_startup(args) # Read mask of the input grids. # 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): # Global #mask = ascraster.create_mask(params.file_mask, 0,'GT',numtype=float) # General formulation in the ini file. mask = ascraster.create_mask(params.file_mask, params.maskid, params.mask_bool_operator, numtype=int) if (params.ldebug): print("MASK: ", params.file_mask + " has been read.") 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 species file, conservative variables, sources and processes file species, sources, proc, params_local = read_parameter.readfile( params.species_ini) # Add all params_local to params. for iname in params_local.names: setattr(params, iname, params_local.get_val(iname)) del params_local # Make an indexnumber in the species list with the name of the species. # For example: params.io2 = general_func.find_spec(species,"O2") make_index_species.make_index_species(params, species, proc) # Make the photosythesis for each species which needs it. # Calculate this for each day and for just 1 year. # Calculation is only done if no photosynthesis input file is provided. make_photo.make_photosyn(params, species) # Define characteristics of low Strahler orders if constant. if (not params.lstrahlergrids): #LV 28-02-2018 define_subgrid_streamorder.define_subgrid_streamorder_constant(params) # Delete all the locking files on the output directory file_locking.clean_locks(params) # Read river basin map basin = ascraster.Asciigrid(ascii_file=params.basin, mask=mask, numtype=int) if (params.ldebug): print(params.basin + " has been read.") # Create the information of the mask to an output file to be able to read the output files. fp_out = open(os.path.join(params.outputdir, "mask.pkl"), "wb") pickle.dump(basin, fp_out, -1) pickle.dump(params, fp_out, -1) fp_out.close() # Make a list of all the riverbasins that have to be calculated. list_riverid = list(set(basin.values)) list_riverid.sort() # Creation of pickle file with fixed initial concentrations (read in ini file). if (params.lspinup == -1): general_func.make_header( os.path.join(params.pkl_outputdir, "start_fixed_conc.pkl"), species, params.starttime) elif (params.lspinup == 0): # Check the startup file general_func.check_header(params.startup_file, species, params.starttime) elif (params.lspinup == 1): # Make empty output files where all the processes can add their information. create_outfiles.create_outfiles(params, species, proc, params.starttime) # Make all output files. year = params.starttime + params.outputtime timeperiod = -1 while (year < params.endtime): timeperiod += 1 # Make all restart files. lrestart = ((timeperiod + 1) % iround(params.restartnumber) == 0) create_outfiles.create_outfiles(params, species, proc, year, lrestart=lrestart) year += params.outputtime # Make output files for last time year = params.endtime create_outfiles.create_outfiles(params, species, proc, year) # Determine the number of cells per riverbasin, to avoid too small portions. riverid_num = {} for icell in range(basin.length): id = basin.get_data(icell) if (id != None): try: riverid_num[id] += 1 except KeyError: riverid_num[id] = 1 # Store the objects that are needed in the dgnm_river. # Write these objects to file, so dgnm_river can read this file. # Here the general data is accessible for all subprocesses. fp_out = open(os.path.join(params.outputdir, "general_objects.pkl"), "wb") pickle.dump(params, fp_out, -1) pickle.dump(species, fp_out, -1) pickle.dump(proc, fp_out, -1) pickle.dump(sources, fp_out, -1) fp_out.close() # Prepare for multitasking. if (params.llocal): # Use the local processing power and use the multiprocessing module of python. import multiprocessing as mp # Define a lock to regulate the access of shared objects over all the processes. lock = mp.Lock() # Set the number of cpu that will be used. number_of_cpu = min(mp.cpu_count(), params.ncpu) # Create list with jobs. jobs = [] # Start with submitting the jobs item = 0 jobnum = 1 while (item < len(list_riverid)): try: total_cells = riverid_num[list_riverid[item]] except KeyError: item += 1 continue numbers = [list_riverid[item]] while (total_cells < params.minimal_number_of_cells): item += 1 if (item > len(list_riverid) - 1): # Last riverbasin, so no more cells. break else: total_cells += riverid_num[list_riverid[item]] numbers.append(list_riverid[item]) if (params.llocal): # Define the process with riverid "num", return value p is process "handler" p = mp.Process(target=dgnm_river.calculate, args=(numbers, lock, params, species, proc, sources)) # Add the process handler to the jobs list. jobs.append(p) # Start the process general_func.start_process(p, jobs, number_of_cpu) else: # Distribute the work over the nodes of the EEJIT cluster with sbatch # Make command string to execute on the cluster cmd = make_cluster_cmd.make_cluster_cmd(params, jobnum, numbers, total_cells) # Submit the job. my_sys.my_system(cmd) time.sleep(2) item += 1 jobnum += 1 # Wait until all jobs are finished. if (params.llocal): for p in jobs: p.join() print("All processes are ready.") log.write_and_print(s.total("Total run")) del log
def post_processing(params, dformat): print('POST PROCESSING DATA IN ' + params.outputdir + ' AS ' + dformat) np.seterr(all='ignore') folder = os.path.join(params.outputdir, '..', "STATES", "subgrid") if dformat == 'NETCDF': alklist = sorted(directory.get_files_with_str(folder, 'conc*ALK*')) diclist = sorted(directory.get_files_with_str(folder, 'conc*DIC*')) Tlist = sorted( directory.get_files_with_str( os.path.join(params.outputdir, '..', "STREAM_ENV_CONDITIONS", "subgrid"), '*temperature*')) icell_list = ascraster.create_mask(params.file_mask, params.maskid, params.mask_bool_operator, numtype=int) dum_asc = ascraster.Asciigrid(ascii_file=params.file_mask) mask_2d = make_mask.do(params.file_mask, params.maskid, dum_asc, logical=params.mask_bool_operator, mask_type='np_grid') for order in range(len(alklist)): alk = Dataset(alklist[order], "a") dic = Dataset(diclist[order], "a") Tdat = Dataset(Tlist[order], "a") time_list = alk.variables['time'][:] pCO2 = Dataset( os.path.join(folder, 'pCO2_order' + str(order + 1) + '.nc'), "w") pH = Dataset( os.path.join(folder, 'pH_order' + str(order + 1) + '.nc'), "w") pCO2_grid = np.ma.array(np.zeros((dum_asc.nrows, dum_asc.ncols)), mask=mask_2d).unshare_mask() pH_grid = np.ma.array(np.zeros((dum_asc.nrows, dum_asc.ncols)), mask=mask_2d).unshare_mask() output_conversion.init_ncdata(folder, pCO2, 'pCO2', dum_asc) output_conversion.init_ncdata(folder, pH, 'pH', dum_asc) alk_matrix = alk['conc_ALK'] dic_matrix = dic['conc_DIC'] T_matrix = Tdat['temperature'] for itime in range(len(time_list)): for icell in icell_list: ilat, ilon = manip.calc_row_col_from_index( icell, dum_asc.ncols) conc_alk = alk_matrix[itime, ilat, ilon] conc_dic = dic_matrix[itime, ilat, ilon] T = T_matrix[itime, ilat, ilon] - 273 pCO2dat, pHdat = calc_carb_dat(conc_dic, conc_alk, T) pH_grid[ilat, ilon] = pHdat pCO2_grid[ilat, ilon] = pCO2dat manip.add_grid_time(pH, 'pH', pH_grid, time_list[itime]) manip.add_grid_time(pCO2, 'pCO2', pCO2_grid, time_list[itime]) alk.close() dic.close() pCO2.close() pH.close() for fn in directory.get_files_with_str(folder, "*order6*"): shutil.copyfile( fn, os.path.join(folder, '..', os.path.basename(fn).replace("_order6", "")))