def table_mult_scalar(self,
                          label,
                          filename,
                          key="isocode",
                          sep=";",
                          minimum=None,
                          maximum=None):
        '''
        Make a new table file for a sensitivity analyse. Filename is changed to output directory.
        '''
        if (self.label_present(label)):
            print(label + ' is found.')
            # Store old filename
            filename_old = filename
            filename = os.path.join(self.options.outputdir,
                                    os.path.basename(filename_old))
            # Read input file
            data_dict = general_class.read_general_file(filename_old,\
                                                sep=sep,out_type="list")

            # Get multiplier
            mult_factor = float(getattr(self.options, label))

            # Multiply every element of the file
            for item in range(len(data_dict)):
                for name in data_dict[item].names:
                    if (name == key):
                        continue
                    try:
                        val = float(data_dict[item].get_val(name))
                        #print data_dict[item].get_val("isocode"),name,val,
                        val = self._check_range(mult_factor * val, minimum,
                                                maximum)
                        #print val
                        data_dict[item].set_val(name, val)
                    except ValueError as TypeError:
                        pass

            # Write all info to file
            fp = open(filename, "w")
            # Write header to file
            fp.write(sep.join(data_dict[0].names) + "\n")

            # Write data block to file.
            lastname = data_dict[0].names[-1]
            for item in range(len(data_dict)):
                for name in data_dict[item].names[:-1]:
                    fp.write(str(data_dict[item].get_val(name)) + sep)
                fp.write(str(data_dict[item].get_val(lastname)) + "\n")
            fp.close()

        else:
            print(label + ' is NOT found.')

        # Return new file name
        return filename
    if (os.path.isdir(idir)):
        ldir = False
        # Check whether directory name is a year
        try:
            qq = int(idir)
            ldir = True
        except ValueError:
            pass

        if (ldir):
            print "Read year: ", idir
            # Directory is a output directory which must be read.
            filename = os.path.join(idir,scenarioname +"_" + idir + "_" +  aggregation_grid + ".csv")
            if (os.path.isfile(filename)):
                # Read input of file
                total[idir] = general_class.read_general_file(filename,sep=";",out_type="list")
            else:
                 raise MyError("Filename: " + filename + " does not exist.")
            
# Sort all the years
keylist=[]
for key in total:
    keylist.append(key)
keylist.sort()

# Take the startyear of all the years
year_start = keylist[0]
# Take a name from the database
names_list = total[year_start][0].names

# Make a empty general class
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 calculate(params, mask, isocode, isogrid, agri_area):
    """
    Allocation of manure on the basis of agricultural landarea grid. The N and P manure map is returned.
    """

    # Read heads per province. Here no interpolation is done. So the exact year must be specified.
    animals = general_class.read_general_file(params.fileanimal_heads, sep=";", key=None, out_type="list")

    # Read N and P excretion. File must contain the header Species;N_excretion;P_excretion
    excretion = general_class.read_general_file(params.fileanimal_excretion, sep=";", key="Species", out_type="dict")

    # Calculate the total N and P manure per province for each line in the animals input file
    Nmanure = {}
    Pmanure = {}
    for item in range(len(animals)):
        # Get the number of heads for the year specified.
        head = animals[item].get_val(str(params.year))
        spec = animals[item].get_val("Species")
        try:
            # Get the N and P excretion for this animal
            Nexcret = excretion[spec].get_val("N_excretion")
            Pexcret = excretion[spec].get_val("P_excretion")
        except KeyError:
            raise MyError("This animal " + spec + " has no excretion rate in file: " + params.fileanimal_excretion)
        # Multiply heads with excretion
        animals[item].add_item("Nmanure", float(Nexcret) * float(head))
        animals[item].add_item("Pmanure", float(Pexcret) * float(head))

        # Calculate total manure per province
        iso = int(animals[item].get_val("ISO-code"))
        try:
            Nmanure[iso] += animals[item].get_val("Nmanure")
            Pmanure[iso] += animals[item].get_val("Pmanure")
        except KeyError:
            Nmanure[iso] = animals[item].get_val("Nmanure")
            Pmanure[iso] = animals[item].get_val("Pmanure")

    # Create two output grids with zeros and fill these with the weighing method.
    Nmanure_grid = ascraster.duplicategrid(agri_area)
    Nmanure_grid.add_values(Nmanure_grid.length * [0.0])
    Pmanure_grid = ascraster.duplicategrid(Nmanure_grid)

    # 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 Nmanure
        weight_key = []
        qmax_key = []
        try:
            pointer1 = pointer_dict[key]
        except KeyError:
            pointer1 = []
        sumqmax = 0.0
        sumweight = 0.0
        for icell in pointer1:
            area = agri_area.get_data(icell, 0.0)
            maxarea = 10000000000
            # 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.
        man_demand = Nmanure[key]
        # Do the allocation
        man_new = allocweighing.allocweighing(man_demand, sumweight, weight_key, qmax_key)

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

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

    for key in isocode:
        # Select agricultural land of the key country
        # Second step is to allocate the Pmanure
        weight_key = []
        qmax_key = []
        try:
            pointer1 = pointer_dict[key]
        except KeyError:
            pointer1 = []
        sumqmax = 0.0
        sumweight = 0.0
        for icell in pointer1:
            area = agri_area.get_data(icell, 0.0)
            maxarea = 10000000000
            # 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.
        man_demand = Pmanure[key]
        # Do the allocation
        man_new = allocweighing.allocweighing(man_demand, sumweight, weight_key, qmax_key)

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

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

    # Write to output file
    Nmanure_grid.write_ascii_file(params.fileNmanure)
    Pmanure_grid.write_ascii_file(params.filePmanure)

    total = sum(agri_area.values)
    print "Total N manure in kg N: ", sum(Nmanure_grid.values)
    print "Total P manure in kg P: ", sum(Pmanure_grid.values)
    print "Total N manure in kg N/ha: ", sum(Nmanure_grid.values) / (100 * total)
    print "Total P manure in kg P/ha: ", sum(Pmanure_grid.values) / (100 * total)

    fp = open(os.path.join(params.outputdir, "manure.csv"), "w")
    lheader = True
    for item in range(len(animals)):
        animals[item].write(fp, sep=";", lheader=lheader, NoneValue="")
        lheader = False
    fp.close()

    return Nmanure_grid, Pmanure_grid
Exemple #5
0
import ascraster
import general_class

data_file = 'C:/Users/schul028/OneDrive - WageningenUR/critload_project/lsu data/figure15data.dat'

# Read asciigrid (world)
grid = ascraster.Asciigrid(ncols=720,
                           nrows=360,
                           nodata_value=-1,
                           xllcorner=-180,
                           yllcorner=-90,
                           cellsize=0.5)

# Read file with coordinates
data_org = general_class.read_general_file(data_file, sep=" ", out_type="list")
#coordin = [[5.2,52.3]]

# Get index in the grid for this coordinate
for item in range(len(data_org)):
    x = float(data_org[item].get_val("Lon"))
    y = float(data_org[item].get_val("Lat"))
    val = float(data_org[item].get_val("LSUfao"))
    icell = grid.get_index_from_coordin(x, y)

    # Set new data
    if (icell > -1):
        grid.set_data(icell, val)

# Effe iets meer waarden in de kaart gooien.
#for icell in range(5000,10000):