def calculate_emission_per_person(params,isocode,regiondata_dict,PopNum):
    '''
    Calculate the N and P emission per person with a GDP formula of van Drecht or 
    use historical N and P emissions (based on FAO protein consumption). In the latter case, the van Drecht formula 
    is used only in case of a future year.
    In case of FAO protein consumption the lost of N and P is also calculated.
    '''
    # Initialisation of the output parameters.
    Nemiss = {}
    Pemiss = {}

    # Other declarations
    Nemiss_PopNum = {}
    Pemiss_PopNum = {}
    N_human_waste_other = {}
    P_human_waste_other = {}

    # Read protein consumption per person.
    protein = data_class.get_data(params.fileprotein,year=params.year,sep=params.sep,isocode=isocode,method=0,weighing=0.0)
    print_debug("Protein",protein)

    # Convert protein into N and P
    for key in isocode:
        Nemiss[key] = protein[key] * g_per_d_to_kg_per_yr * params.N_fraction_protein
        Pemiss[key] = params.fPN * Nemiss[key]
    print_debug("Nemiss",Nemiss)
    print_debug("Pemiss",Pemiss)         

    # Read from file the sheet with the wasting at retail and the sheet for household level losses
    frac_retail_loss = data_class.get_data(params.file_retail_loss,year=params.year,sep=params.sep,isocode=isocode,method=1)
    frac_household_loss = data_class.get_data(params.file_household_loss,year=params.year,sep=params.sep,isocode=isocode,method=1)
    print_debug("frac_retail_loss",frac_retail_loss)
    print_debug("frac_household_loss",frac_household_loss) 

    # Calculate the actual amount of N and P consumed.
    for key in isocode:
        Nval = Nemiss[key]
        Pval = Pemiss[key]
        Nemiss_PopNum[key] = Nval * PopNum[key]
        Pemiss_PopNum[key] = Pval * PopNum[key]
        Nemiss[key] *= (1.0 - frac_retail_loss[key]) * (1.0 - frac_household_loss[key]) * (1.0 - params.fraction_human_skin_loss)
        Pemiss[key] *= (1.0 - frac_retail_loss[key]) * (1.0 - frac_household_loss[key]) * (1.0 - params.fraction_human_skin_loss)
        N_human_waste_other[key] = Nval - Nemiss[key]
        P_human_waste_other[key] = Pval - Pemiss[key]

    # Add to global database
    regiondata.put2regiondata(Nemiss_PopNum,regiondata_dict,"Nemiss_PopNum")
    regiondata.put2regiondata(Pemiss_PopNum,regiondata_dict,"Pemiss_PopNum")
    regiondata.put2regiondata(N_human_waste_other,regiondata_dict,"N_human_waste_other")
    regiondata.put2regiondata(P_human_waste_other,regiondata_dict,"P_human_waste_other")

    # Print debug information to screen.
    print_debug("Nemiss of humans per person",Nemiss)
    print_debug("Pemiss of humans per person",Pemiss)
    print_debug("Nemiss of humans",Nemiss_PopNum)
    print_debug("Pemiss of humans",Pemiss_PopNum)          
    print_debug("N_human_waste per person",N_human_waste_other)
    print_debug("P_human_waste per person",P_human_waste_other)

    return Nemiss,Pemiss
def calculate(params,isocode,regiondata_dict,PopNum,Nemiss,Pemiss,EmPldet,EmPddet,PopUrbNum,PopConNum):
    '''
    Calculate the N and P emission from industry
    '''

    # Multiply urban emission with factor_industry to obtain output industry [kg/yr]
    # Assumption is that only urban areas have industry and 
    # industry depends on urban emission only and also detergents for P
    factor_industry = data_class.get_data(params.filefactor_industry,year=params.year,sep=params.sep,isocode=isocode,method=1)
    Nemiss_industry_tot = {}
    Pemiss_industry_tot = {}
    Nemiss_industry = {}
    Pemiss_industry = {}
    Nindustry_other = {}
    Pindustry_other = {}
    for key in isocode:
        pop = max(PopUrbNum[key],PopConNum[key])
        Nemiss_industry_tot[key] = Nemiss[key] * factor_industry[key] * pop
        Pemiss_industry_tot[key] = ((Pemiss[key] * pop) + (EmPldet[key] + EmPddet[key])*PopNum[key]) * \
                               factor_industry[key] 
        Nemiss_industry[key] = Nemiss_industry_tot[key] *\
                               (1.0-params.industry_loss_fraction_N)
        Pemiss_industry[key] = Pemiss_industry_tot[key] * (1.0-params.industry_loss_fraction_P)
        Nindustry_other[key] = Nemiss_industry_tot[key] * params.industry_loss_fraction_N
        Pindustry_other[key] = Pemiss_industry_tot[key] * params.industry_loss_fraction_P

    regiondata.put2regiondata(Nemiss_industry_tot,regiondata_dict,"Nemiss_industry")
    regiondata.put2regiondata(Pemiss_industry_tot,regiondata_dict,"Pemiss_industry")
    regiondata.put2regiondata(Nindustry_other,regiondata_dict,"Nindustry_other")
    regiondata.put2regiondata(Pindustry_other,regiondata_dict,"Pindustry_other")

    print_debug("Total Nemiss of industry",Nemiss_industry_tot)
    print_debug("Total Pemiss of industry",Pemiss_industry_tot)
    print_debug("Nemiss of industry",Nemiss_industry)
    print_debug("Pemiss of industry",Pemiss_industry)
    print_debug("factor_industry",factor_industry)
    print_debug("Nindustry_other",Nindustry_other)
    print_debug("Pindustry_other",Pindustry_other)

    return Nemiss_industry,Pemiss_industry
def calculate(params,mask,isocode,isogrid):
    '''
    Allocation of fertilizer on the basis of agricultural landarea grid. The new map of agricultural land in km2 is returned as well
    as the N and P fertilizer.
    '''

    # Read region codes (regcodes)
    reggrid = ascraster.Asciigrid(ascii_file=params.fileregion,mask=mask,numtype=int)  

    # Make a list of isocodes for the provinces.
    regcode = list(set(reggrid.values))      

    # Read P fertilizer per region, missing regions are set to zero.
    Pfert_reg = data_class.get_data(params.filePfertuse,year=params.year,sep=params.sep,isocode=regcode,method=0,weighing=0.0)    
    print_debug("Pfert_reg",Pfert_reg)

    # Read N fertilizer per region, missing regions are set to zero.
    Nfert_reg = data_class.get_data(params.fileNfertuse,year=params.year,sep=params.sep,isocode=regcode,method=0,weighing=0.0)    
    print_debug("Nfert_reg",Nfert_reg)

    # Read agricultural land per province, missing provinces are set to zero.
    agri_area_table = data_class.get_data(params.filefertarea,year=params.year,sep=params.sep,isocode=isocode,method=0,weighing=0.0)    
    print_debug("agri_area_table",agri_area_table)

    # Read the basic map with the amount of agricultural land in km2 per grid cell
    agriland = ascraster.Asciigrid(ascii_file=params.fileagrilandarea,mask=mask,numtype=float)  

    # Read the basic map with the amount of land in km2 per grid cell
    arealand = ascraster.Asciigrid(ascii_file=params.landarea,mask=mask,numtype=float)  

    # Create two output grids with zeros and fill these with the ranking method.
    agri_area = ascraster.duplicategrid(agriland)
    agri_area.add_values(agri_area.length * [0.0])    
    Pfert = ascraster.duplicategrid(agri_area)
    Nfert = ascraster.duplicategrid(agri_area)

    # Make a dictionairy of pointers for all the isocodes in the grid.
    pointer_dict = {}
    for icell in xrange(isogrid.length):
        iso = isogrid.get_data(icell)
        if (iso != None):
            iso = int(iso)
            try:
                pointer_dict[iso].append(icell)
            except KeyError:
                pointer_dict[iso] = [icell]

    for key in isocode:
        # Select agricultural land of the key country
        # First step is to allocate the agricultural area for each province.
        weight_key = []
        qmax_key = []
        try:
            pointer1 = pointer_dict[key]
        except KeyError:
            pointer1 = []
        sumqmax = 0.0
        sumweight = 0.0
        for icell in pointer1:
            area = agriland.get_data(icell,0.0)
            maxarea = arealand.get_data(icell,0.0)
            # Fill weighing data and max data
            qmax_key.append(maxarea)
            weight_key.append(area)
            sumqmax += qmax_key[-1]
            sumweight += weight_key[-1]

        if (sumweight < 0.0001):
            # Make uniform weighing to all cells.
            for icell in range(len(weight_key)):
                weight_key[icell] =  1
                sumweight += weight_key[icell]

        # Take the table information on agricultural area.
        area_demand = agri_area_table[key]
        if (area_demand > sumqmax):
            raise MyError("It is not possible to allocate " + str(area_demand) + " km2 in provincenumber " + str(key),\
                          "Maximum area that is possible to allocate in this province is: "+str(sumqmax))
        # Do the allocation
        area_new = allocweighing.allocweighing(area_demand,sumweight,weight_key,qmax_key)

        if (abs(sum(area_new) - agri_area_table[key]) > 0.001*agri_area_table[key]):
            print "***** There is not enough allocated for agri_area for region "+str(key)+". Difference: " + str(agri_area_table[key]-sum(area_new))
            print "***** Needed for agri_area for region "+str(key)+" " + str(agri_area_table[key])

        # Fill gridded result
        for item in xrange(len(area_new)):
            agri_area.set_data(pointer1[item],area_new[item])

    # Now the fertilizer is allocated to the gridcells, based on agricultural land area. This means that
    # each grid cell will have another load  but the same application rate (kg N/ha)
    pointer_dict = {}
    for icell in xrange(reggrid.length):
        iso = reggrid.get_data(icell)
        if (iso != None):
            iso = int(iso)
            try:
                pointer_dict[iso].append(icell)
            except KeyError:
                pointer_dict[iso] = [icell]

    for key in regcode:
        weight_key = []
        qmax_key = []
        try:
            pointer1 = pointer_dict[key]
        except KeyError:
            pointer1 = []
        sumqmax = 0.0
        sumweight = 0.0
        for icell in pointer1:
            # Note the new map of the previous step is used here.
            area = agri_area.get_data(icell,0.0)
            maxarea = 10000000.
            # Fill weighing data and max data
            qmax_key.append(maxarea)
            weight_key.append(area)
            sumqmax += qmax_key[-1]
            sumweight += weight_key[-1]

        if (sumweight < 0.0001):
            # Make uniform weighing to all cells.
            for icell in range(len(weight_key)):
                weight_key[icell] =  1
                sumweight += weight_key[icell]

        # Grid these country connected population data with help of the population density map
        fert_demand = Nfert_reg[key]
        fert_new = allocweighing.allocweighing(fert_demand,sumweight,weight_key,qmax_key)

        if (abs(sum(fert_new) - Nfert_reg[key]) > 0.001*Nfert_reg[key]):
            print "***** There is not enough allocated for Nfert_reg for region "+str(key)+". Difference: " + str(Nfert_reg[key]-sum(fert_new))
            print "***** Needed for Nfert_reg for region "+str(key)+" " + str(Nfert_reg[key])

        # Fill gridded result
        for item in xrange(len(fert_new)):
            Nfert.set_data(pointer1[item],fert_new[item])

    for key in regcode:
        weight_key = []
        qmax_key = []
        try:
            pointer1 = pointer_dict[key]
        except KeyError:
            pointer1 = []
        sumqmax = 0.0
        sumweight = 0.0
        for icell in pointer1:
            # Note the new map of the previous step is used here.
            area = agri_area.get_data(icell,0.0)
            maxarea = 10000000.
            # Fill weighing data and max data
            qmax_key.append(maxarea)
            weight_key.append(area)
            sumqmax += qmax_key[-1]
            sumweight += weight_key[-1]

        if (sumweight < 0.0001):
            # Make uniform weighing to all cells.
            for icell in range(len(weight_key)):
                weight_key[icell] =  1
                sumweight += weight_key[icell]

        # Grid these country connected population data with help of the population density map
        fert_demand = Pfert_reg[key]
        # Change fertilizer from P2O5 to P
        fert_demand *= 62.0/142.0
        fert_new = allocweighing.allocweighing(fert_demand,sumweight,weight_key,qmax_key)

        if (abs(sum(fert_new) - Pfert_reg[key]* 62.0/142.0) > 0.001*Pfert_reg[key]* 62.0/142.0):
            print "***** There is not enough allocated for Pfert_reg for region "+str(key)+". Difference: " + str(Pfert_reg[key]* (62.0/142.0)-sum(fert_new))
            print "***** Needed for Pfert_reg for region "+str(key)+" " + str(Pfert_reg[key])

        # Fill gridded result
        for item in xrange(len(fert_new)):
            Pfert.set_data(pointer1[item],fert_new[item])

    # Write to output file
    agri_area.write_ascii_file(params.fileagri_area)
    Nfert.write_ascii_file(params.fileNfert)
    Pfert.write_ascii_file(params.filePfert)

    total = sum(agri_area.values)
    print "Total agricultural area in km2: ",total
    print "Total N fertilzer in kg N: ",sum(Nfert.values)
    print "Total P fertilizer in kg P: ",sum(Pfert.values)
    print "Total N fertilzer in kg N/ha: ",sum(Nfert.values)/(100*total)
    print "Total P fertilizer in kg P/ha: ",sum(Pfert.values)/(100*total)

    return agri_area,Nfert,Pfert
Beispiel #4
0
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
Beispiel #5
0
def read_popnum(params,mask,isogrid,isocode):
    '''
    Read population numbers from grid or from table.
    If it is given on a table then a weighing grid is needed to allocate the population on the grid.
    Table and grid is returned.
    '''
 
    # Scale the population of timescen so that the
    # total population of the region is correct.
    PopNum = data_class.get_data(params.filePopNum,year=params.year,sep=params.sep)
        
    # PopNum is given in thousand people. So we have to multiply by 1000
    for key in PopNum:
        PopNum[key] *= 1000

    # Add isocodes when they are not in list isocode
    for key in PopNum:
        try:
            qq=isocode.index(key)
        except ValueError:
            # Isocode found which is not taken into account
            print "ISOCODE " + str(key) +" is not used for number of people: " + str(PopNum[key]) + " people are missed." 
            PopNum[key] = 0.0

    # Add popnum for missing isocodes. Population is zero
    for key in isocode:
        try:
            qq=PopNum[key]
        except KeyError:
            # New iso code found.
            PopNum[key] = 0.0

    print "Number of people read : " + str(sum(PopNum.itervalues())) + " is found." 
    print_debug("PopNum in read_popnum",PopNum)
            
    # Read population number grid which is used to scale the table population numbers information
    popgrid_scale = ascraster.Asciigrid(ascii_file=params.filePopNumgrid,mask=mask,numtype=float)

    # Read land area
    landarea = ascraster.Asciigrid(ascii_file=params.landarea,mask=mask,numtype=float)

    # Make new population number grid zero
    popgrid = ascraster.duplicategrid(popgrid_scale)
    popgrid.add_values(popgrid.length * [0.0])

    # Make a dictionary of pointers for all the isocodes in the grid.
    pointer_dict = {}
    for icell in xrange(isogrid.length):
        iso = isogrid.get_data(icell)
        if (iso != None):
            iso = int(iso)
            try:
                pointer_dict[iso].append(icell)
            except KeyError:
                pointer_dict[iso] = [icell]

    for key in isocode:
        if (PopNum[key] <= 0.):
            print "WEIGHING: table population is ZERO in region: " + str(key)
            continue
        # Select population of the key country
        weight_key = []
        qmax_key = []
        try:
            pointer1 = pointer_dict[key]
        except KeyError:
            print "Key: " + str(key) + " is not found in the isogrid."
            print "Table information of population numbers is changed for region: " + str(int(key)) +\
                  " from " + str(PopNum[key]) + " to 0.0"
            continue
        sumqmax = 0.0
        sumweight = 0.0
        for icell in pointer1:
            pop = popgrid_scale.get_data(icell,0.0)
            # Fill weighing data and max data
            qmax_key.append(params.max_popdens * landarea.get_data(icell,0.0))
            weight_key.append(pop)
            sumqmax += qmax_key[-1]
            sumweight += weight_key[-1]
   
        # Warn for possible errors
        if (sumweight <= 0.):
            print "WEIGHING: grid population is ZERO in region: " + str(key)
            if (len(weight_key) > 0):
                # There are cells on the grid for this region. So make a uniform distribution
                weight_key = len(weight_key) * [1.0]
                sumweight = sum(weight_key)

        if (sumqmax < PopNum[key]):
            print "WEIGHING: PopNum ",PopNum[key], " is bounded by sumqmax",sumqmax, " in region: " + str(key)

        # Grid these country population data with help of the popgrid_scale map
        PopNum_reg = PopNum[key]
        popnum_new = allocweighing.allocweighing(PopNum_reg,sumweight,weight_key,qmax_key)      
            
        if (key == debug_code):
            sum1 = 0.0
            for item in xrange(len(popnum_new)):       
                sum1 += popnum_new[item]
            print "New population number: ",sum1

        # Fill gridded result
        sum1 = 0.0
        for item in xrange(len(popnum_new)):
            popgrid.set_data(pointer1[item],popnum_new[item])
            sum1 += popnum_new[item]

        # Check whether the table information has landed in the popgrid
        if (math.fabs(sum1 - PopNum[key]) > 0.01):
            print "Table information of population numbers is changed for region: " + str(int(key)) +\
                  " from " + str(PopNum[key]) + " to " + str(sum1)

    print "Total number of people: " + str(sum(PopNum.itervalues())) + " is found." 
 
    return PopNum,popgrid