def run_checks(self):
        """
        Check options
        """
        self.validate_directory(self.options.root, bool_write=False)
       
        # Create new output directory if necessary
    	if not os.path.exists(self.options.outputdir):
            os.makedirs(self.options.outputdir)
            
        self.validate_directory(self.options.outputdir, bool_write=True)
        self.validate_directory(self.options.inputdir, bool_write=False)
	self.validate_directory(self.options.fixinputdir, bool_write=False)
               
        # Check the existence of all given input files                      
        if (self.options.lmask):
            self.validate_file(self.options.file_mask)

        self.validate_file(self.options.fileiso)
        self.validate_file(self.options.landarea)
        self.validate_file(self.options.fileprov_marine)
        self.validate_file(self.options.filecoast)

        # Grid files on input directory
        self.validate_file(self.options.fileriverlength)
        self.validate_file(self.options.fileweigh_marine)
        self.validate_file(self.options.filepondarea)
               
        # input filenames tables
        self.validate_file(self.options.fileproduction)
        self.validate_file(self.options.fileN_excretion)
        self.validate_file(self.options.fileP_excretion)

        # Check whether the ascraster files have the same header.
        equal = ascraster.compare_grids(self.options.fileiso,\
                                        self.options.landarea,\
                                        self.options.fileprov_marine,\
                                        self.options.filecoast,\
                                        self.options.fileriverlength,\
                                        self.options.fileweigh_marine,\
					self.options.filepondarea)

        if (not equal):
            raise optparse.OptionValueError("Error: input grids do not have the same projections.")

        if (self.options.lmask):
            # Check whether the ascraster files have the same header.
            equal = ascraster.compare_grids(self.options.fileiso,\
                                            self.options.file_mask)
            if (not equal):
                raise optparse.OptionValueError("Error: iso map and maskmap do not have the same projections.")
    def run_checks(self):
        """
        Check options
        """
        self.validate_directory(self.options.root, bool_write=False)
       
        # Create new output directory if necessary
    	if not os.path.exists(self.options.outputdir):
            os.makedirs(self.options.outputdir)
            
        self.validate_directory(self.options.outputdir, bool_write=True)
        self.validate_directory(self.options.inputdir, bool_write=False)
	self.validate_directory(self.options.fixinputdir, bool_write=False)
               
        # Check the existence of all given input files                      
        if (self.options.lmask):
            self.validate_file(self.options.file_mask)

        self.validate_file(self.options.fileiso)
        self.validate_file(self.options.landarea)
        self.validate_file(self.options.fileregion)

        
        # input filenames tables
        self.validate_file(self.options.filefertarea)
        self.validate_file(self.options.fileNfertuse)
        self.validate_file(self.options.filePfertuse)
        self.validate_file(self.options.fileanimal_heads)
        self.validate_file(self.options.fileanimal_excretion)

        # Check whether the ascraster files have the same header.
        equal = ascraster.compare_grids(self.options.fileiso,\
                                        self.options.landarea,\
					self.options.fileregion)
        if (not equal):
            raise optparse.OptionValueError("Error: iso map, landarea map and coastal grid fraction do not have the same projections.")

        if (self.options.lmask):
            # Check whether the ascraster files have the same header.
            equal = ascraster.compare_grids(self.options.fileiso,\
                                            self.options.file_mask)
            if (not equal):
                raise optparse.OptionValueError("Error: iso map and maskmap do not have the same projections.")
    def run_checks(self):
        """
        Check options
        """
        self.validate_directory(self.options.root, bool_write=False)
       
        # Create new output directory if necessary
    	if not os.path.exists(self.options.outputdir):
            os.makedirs(self.options.outputdir)
            
        self.validate_directory(self.options.outputdir, bool_write=True)
        self.validate_directory(self.options.inputdir, bool_write=False)
	self.validate_directory(self.options.fixinputdir, bool_write=False)
               
        # Check the existence of all given input files                      
        if (self.options.lmask):
            self.validate_file(self.options.file_mask)

        self.validate_file(self.options.fileiso)
        self.validate_file(self.options.landarea)
        
        # input filenames tables
        self.validate_file(self.options.fileprotein)
        self.validate_file(self.options.file_retail_loss)
        self.validate_file(self.options.file_household_loss)
        self.validate_file(self.options.filefactor_industry)
        self.validate_file(self.options.file_agri_reuse)
        self.validate_file(self.options.filePdishwasher)
        self.validate_file(self.options.filePlaundry)
        self.validate_file(self.options.filePopUrb)
        self.validate_file(self.options.filePopCon)

        self.validate_file(self.options.filePopNum)
        self.validate_file(self.options.filePopNumgrid)
	self.validate_file(self.options.file_fr_coastgrid)
	

        self.validate_file(self.options.filePrimTreatment)
        self.validate_file(self.options.fileSecTreatment)
        self.validate_file(self.options.fileTertTreatment)
        self.validate_file(self.options.fileN_PrimRemoval)
        self.validate_file(self.options.fileN_SecRemoval)
        self.validate_file(self.options.fileN_TertRemoval)
        self.validate_file(self.options.fileP_PrimRemoval)
        self.validate_file(self.options.fileP_SecRemoval)
        self.validate_file(self.options.fileP_TertRemoval)
	self.validate_file(self.options.fileSeptic_rural)

        if (self.options.isocodes_by_table == 1):
            self.validate_file(self.options.fileisocode_table)
        
        if (self.options.aggregationgrid != None):
            self.validate_file(self.options.aggregationgrid)
                
        # Check whether the ascraster files have the same header.
        equal = ascraster.compare_grids(self.options.fileiso,\
                                        self.options.landarea,\
					self.options.file_fr_coastgrid)
        if (not equal):
            raise optparse.OptionValueError("Error: iso map, landarea map and coastal grid fraction do not have the same projections.")

        if (self.options.lmask):
            # Check whether the ascraster files have the same header.
            equal = ascraster.compare_grids(self.options.fileiso,\
                                            self.options.file_mask)
            if (not equal):
                raise optparse.OptionValueError("Error: iso map and maskmap do not have the same projections.")

        if (self.options.aggregationgrid != None):
            # Check whether the ascraster files have the same header.
            equal = ascraster.compare_grids(self.options.fileiso,\
                                            self.options.aggregationgrid)
            if (not equal):
                raise optparse.OptionValueError("Error: iso map and aggregationgrid do not have the same projections.")
Example #4
0
    def make_value_list(self,params,mask,list_attribute_names):
        '''
        The litho grid is given in classes. For each cell we know the fraction of that class in the cell.
        Grid0 is the nodata information. When grid0 is 1.0 then the cell is 100% nodata.
        A list with length of grids (normally length of mask) is returned with on each entry the
        value of the attribute given in the list_attribute_names (and also the order as given in this list).
        '''
        # Compare first grid with another grid of the run
        equal = ascraster.compare_grids(params.pgrass,os.path.join(params.litho_dir,"nodatgrid.asc"))
        if (not equal):
            raise MyError("Litho maps don't have same header as other input files.")
        
        grid0 = ascraster.Asciigrid(ascii_file=os.path.join(params.litho_dir,"nodatgrid.asc"),\
                                       mask=mask,numtype=float)

        # Check whether there are fractions given in grid0
        max_value = max(grid0.values)
        if (max_value > 1.0):
            raise MyError("Litho maps must be a fraction. Found values greater than 1 (found: " + str(max_value) + ").")

    
        # Make a default output list:
        out = []
        for i in range(grid0.length):
            out.append(len(list_attribute_names) * [0.0])
        
        # Loop over all the classes of litho map. Start with class 3 because first two are major water bodies and ice:
        for iclass in range(3,18):
            # Get the litho_class values for this list_attributes_names
            values = []
            for item in range(len(list_attribute_names)):
                values.append(float(getattr(litho_lib[iclass],list_attribute_names[item])))

            # Check header of input file
            equal = ascraster.compare_grids(params.pgrass,os.path.join(params.litho_dir,"grid" + str(iclass) + ".asc"))
            if (not equal):
                raise MyError("Litho maps don't have same header as other input files.")                

            grid = ascraster.Asciigrid(ascii_file=os.path.join(params.litho_dir,"grid" + str(iclass) + ".asc"),\
                                       mask=mask,numtype=float)
           
            for icell in range(0,grid0.length):
                grid0_cell = grid0.get_data(icell,0.0)
                if (grid0_cell < 1.0):
                    # There is something to do.
                    grid_cell = grid.get_data(icell,0.0)

                    if (grid_cell > 0.0):
                        # There is something of this class in this cell:                       
                        for item in range(len(list_attribute_names)):
                            out[icell][item] += grid_cell * values[item]

        correc = grid0.length * [0.0]
        
        # Correction loop to correct for the nodata of the cell
        for iclass in range(1,3):
            # Get the litho_class values for this list_attributes_names

            # Check header of input file
            equal = ascraster.compare_grids(params.pgrass,os.path.join(params.litho_dir,"grid" + str(iclass) + ".asc"))
            if (not equal):
                raise MyError("Litho maps don't have same header as other input files.")                

            grid = ascraster.Asciigrid(ascii_file=os.path.join(params.litho_dir,"grid" + str(iclass) + ".asc"),\
                                       mask=mask,numtype=float)
           
            for icell in range(0,grid0.length):
                grid0_cell = grid0.get_data(icell,0.0)
                if (grid0_cell < 1.0):
                    # There is something to do.
                    if (iclass == 1):
                        correc[icell] += grid0_cell

                    grid_cell = grid.get_data(icell,0.0)
                    if (grid_cell > 0.0):
                        correc[icell] += grid_cell

        # Make correction for the situation where there is water bodies or ice or a part of the cell is nodata.
        for icell in range(len(correc)):
            if (correc[icell] > 0.0):
                # There was ice or water bodies or part of the cell is nodata

                if (correc[icell] < 1.0):
                    for item in range(len(list_attribute_names)):
                        out[icell][item] /= (1.0 - correc[icell])
        return out
Example #5
0
def calculate_water_litho(params,mask):
    '''
    Calculate the total discharge for each litho class. 
    This is needed to fill in the table of litho_lib with information from Jens Hartmann
    '''
    pnet = ascraster.Asciigrid(ascii_file=params.pnet,mask=mask,numtype=float)
    if params.ldebug: print(params.pnet  + " has been read.")  

    landarea = ascraster.Asciigrid(ascii_file=params.landarea,mask=mask,numtype=float)
    if params.ldebug: print(params.landarea  + " has been read.")    
    
    # Calculate accumulated water flux of each grid cell to the mouth of the river basin. Make discharge in km3
    # Convert pnet [mm/yr] to km3 by multiplying with landarea and 10e-6 (conversion from mm to km)
    water = ascraster.duplicategrid(landarea)    
    for icell in range(landarea.length):
        wt = pnet.get_data(icell,0.0) * landarea.get_data(icell,0.0) * 1.0e-6
        water.set_data(icell,wt)

    # Compare first grid with another grid of the run
    equal = ascraster.compare_grids(params.pgrass,os.path.join(params.litho_dir,"nodatgrid.asc"))
    if (not equal):
        raise MyError("Litho maps don't have same header as other input files.")
        
    grid0 = ascraster.Asciigrid(ascii_file=os.path.join(params.litho_dir,"nodatgrid.asc"),\
                                mask=mask,numtype=float)

    # Check whether there are fractions given in grid0
    max_value = max(grid0.values)
    if (max_value > 1.0):
        raise MyError("Litho maps must be a fraction. Found values greater than 1 (found: " + str(max_value) + ").")
  
    # Make a default output list:
    out = len(litho_lib) * [0.0]
    area = len(litho_lib) * [0.0]

    # Loop over all the classes of litho map. Start with class 3 because first two are major water bodies and ice:
    for iclass in range(1,18):
        print(iclass)
        # Check header of input file
        equal = ascraster.compare_grids(params.pgrass,os.path.join(params.litho_dir,"grid" + str(iclass) + ".asc"))
        if (not equal):
            raise MyError("Litho maps don't have same header as other input files.")                

        grid = ascraster.Asciigrid(ascii_file=os.path.join(params.litho_dir,"grid" + str(iclass) + ".asc"),\
                                   mask=mask,numtype=float)
           
        print(os.path.join(params.litho_dir,"grid" + str(iclass) + ".asc") + " read.")
        for icell in range(0,grid0.length):
            grid0_cell = grid0.get_data(icell,0.0)
            if (grid0_cell < 1.0):
                # There is something to do.
                grid_cell = grid.get_data(icell,0.0)

                if (grid_cell > 0.0):
                    # There is something of this class in this cell:                       
                    out[iclass] += grid_cell * water.get_data(icell,0.0)
                    area[iclass] += grid_cell * landarea.get_data(icell,0.0)
            
            if (iclass == 1):
                out[0]  += grid0_cell * water.get_data(icell,0.0)
                area[0] += grid0_cell * landarea.get_data(icell,0.0)

    return out,area
    def run_checks(self):
        """
        Check options
        """
        self.validate_directory(self.options.root, bool_write=False)
       
        # Create new output directory if necessary
    	if not os.path.exists(self.options.outputdir):
            os.makedirs(self.options.outputdir)
            
        self.validate_directory(self.options.outputdir, bool_write=True)
        self.validate_directory(self.options.inputdir, bool_write=False)
	self.validate_directory(self.options.fixinputdir, bool_write=False)
	self.validate_directory(self.options.pointsourcedir, bool_write=False)
               
        # Check the existence of all given input files                      
        if (self.options.lmask):
            self.validate_file(self.options.file_mask)

        self.validate_file(self.options.landarea)
        if (self.options.lcreate_ldd_basinid != 1):
            self.validate_file(self.options.file_basinid)
            self.validate_file(self.options.file_ldd)
        self.validate_file(self.options.file_gemnsw)
        self.validate_file(self.options.file_gempsw)
        self.validate_file(self.options.fileNfert)
        self.validate_file(self.options.filePfert)
        self.validate_file(self.options.fileNmanure)
        self.validate_file(self.options.filePmanure)
        self.validate_file(self.options.fileNaqua)
        self.validate_file(self.options.filePaqua)
        self.validate_file(self.options.filegEmNcoast)
        self.validate_file(self.options.filegEmPcoast)
        self.validate_file(self.options.fileNmarine)
        self.validate_file(self.options.filePmarine)

        # Check whether the ascraster files have the same header.
        if (self.options.lcreate_ldd_basinid != 1):
            equal = ascraster.compare_grids(self.options.landarea,\
					self.options.file_basinid,\
					self.options.file_ldd)
            if (not equal):
                raise optparse.OptionValueError("Error: input maps ldd or basinid do not have the same projections.")

                
        # Check whether the ascraster files have the same header.
        equal = ascraster.compare_grids(self.options.landarea,\
					self.options.file_gemnsw,\
					self.options.file_gempsw,\
                                        self.options.fileNfert,\
                                        self.options.filePfert,\
                                        self.options.fileNmanure,\
                                        self.options.filePmanure,\
                                        self.options.fileNaqua,\
                                        self.options.filePaqua,\
                                        self.options.filegEmNcoast,\
                                        self.options.filegEmPcoast,\
                                        self.options.fileNmarine,\
					self.options.filePmarine\
                                        )

        if (not equal):
            raise optparse.OptionValueError("Error: input maps do not have the same projections.")

        if (self.options.lmask):
            # Check whether the ascraster files have the same header.
            equal = ascraster.compare_grids(self.options.landarea,\
                                            self.options.file_mask)
            if (not equal):
                raise optparse.OptionValueError("Error: landarea and maskmap do not have the same projections.")
Example #7
0
    def make_value_list(self,params,mask,list_attribute_names):
        '''
        The litho grid is given in classes. For each cell we know the fraction of that class in the cell.
        Grid0 is the nodata information. When grid0 is 1.0 then the cell is 100% nodata.
        A list with length of grids (normally length of mask) is returned with on each entry the
        value of the attribute given in the list_attribute_names (and also the order as given in this list).
        '''
        # Compare first grid with another grid of the run
        equal = ascraster.compare_grids(params.pgrass,os.path.join(params.litho_dir,"nodatgrid.asc"))
        if (not equal):
            raise MyError("Litho maps don't have same header as other input files.")
        
        grid0 = ascraster.Asciigrid(ascii_file=os.path.join(params.litho_dir,"nodatgrid.asc"),\
                                       mask=mask,numtype=float)

        # Check whether there are fractions given in grid0
        max_value = max(grid0.values)
        if (max_value > 1.0):
            raise MyError("Litho maps must be a fraction. Found values greater than 1 (found: " + str(max_value) + ").")

    
        # Make a default output list:
        out = []
        for i in range(grid0.length):
            out.append(len(list_attribute_names) * [0.0])
        
        # Loop over all the classes of litho map. Start with class 3 because first two are major water bodies and ice:
        for iclass in range(3,18):
            # Get the litho_class values for this list_attributes_names
            values = []
            for item in range(len(list_attribute_names)):
                values.append(float(getattr(litho_lib[iclass],list_attribute_names[item])))

            # Check header of input file
            equal = ascraster.compare_grids(params.pgrass,os.path.join(params.litho_dir,"grid" + str(iclass) + ".asc"))
            if (not equal):
                raise MyError("Litho maps don't have same header as other input files.")                

            grid = ascraster.Asciigrid(ascii_file=os.path.join(params.litho_dir,"grid" + str(iclass) + ".asc"),\
                                       mask=mask,numtype=float)
           
            for icell in range(0,grid0.length):
                grid0_cell = grid0.get_data(icell,0.0)
                if (grid0_cell < 1.0):
                    # There is something to do.
                    grid_cell = grid.get_data(icell,0.0)

                    if (grid_cell > 0.0):
                        # There is something of this class in this cell:                       
                        for item in range(len(list_attribute_names)):
                            out[icell][item] += grid_cell * values[item]

        correc = grid0.length * [0.0]
        
        # Correction loop to correct for the nodata of the cell
        for iclass in range(1,3):
            # Get the litho_class values for this list_attributes_names

            # Check header of input file
            equal = ascraster.compare_grids(params.pgrass,os.path.join(params.litho_dir,"grid" + str(iclass) + ".asc"))
            if (not equal):
                raise MyError("Litho maps don't have same header as other input files.")                

            grid = ascraster.Asciigrid(ascii_file=os.path.join(params.litho_dir,"grid" + str(iclass) + ".asc"),\
                                       mask=mask,numtype=float)
           
            for icell in range(0,grid0.length):
                grid0_cell = grid0.get_data(icell,0.0)
                if (grid0_cell < 1.0):
                    # There is something to do.
                    if (iclass == 1):
                        correc[icell] += grid0_cell

                    grid_cell = grid.get_data(icell,0.0)
                    if (grid_cell > 0.0):
                        correc[icell] += grid_cell

        # Make correction for the situation where there is water bodies or ice or a part of the cell is nodata.
        for icell in range(len(correc)):
            if (correc[icell] > 0.0):
                # There was ice or water bodies or part of the cell is nodata

                if (correc[icell] < 1.0):
                    for item in range(len(list_attribute_names)):
                        out[icell][item] /= (1.0 - correc[icell])
        return out
Example #8
0
def calculate_water_litho(params,mask):
    '''
    Calculate the total discharge for each litho class. 
    This is needed to fill in the table of litho_lib with information from Jens Hartmann
    '''
    pnet = ascraster.Asciigrid(ascii_file=params.pnet,mask=mask,numtype=float)
    if params.ldebug: print params.pnet  + " has been read."  

    landarea = ascraster.Asciigrid(ascii_file=params.landarea,mask=mask,numtype=float)
    if params.ldebug: print params.landarea  + " has been read."    
    
    # Calculate accumulated water flux of each grid cell to the mouth of the river basin. Make discharge in km3
    # Convert pnet [mm/yr] to km3 by multiplying with landarea and 10e-6 (conversion from mm to km)
    water = ascraster.duplicategrid(landarea)    
    for icell in range(landarea.length):
        wt = pnet.get_data(icell,0.0) * landarea.get_data(icell,0.0) * 1.0e-6
        water.set_data(icell,wt)

    # Compare first grid with another grid of the run
    equal = ascraster.compare_grids(params.pgrass,os.path.join(params.litho_dir,"nodatgrid.asc"))
    if (not equal):
        raise MyError("Litho maps don't have same header as other input files.")
        
    grid0 = ascraster.Asciigrid(ascii_file=os.path.join(params.litho_dir,"nodatgrid.asc"),\
                                mask=mask,numtype=float)

    # Check whether there are fractions given in grid0
    max_value = max(grid0.values)
    if (max_value > 1.0):
        raise MyError("Litho maps must be a fraction. Found values greater than 1 (found: " + str(max_value) + ").")
  
    # Make a default output list:
    out = len(litho_lib) * [0.0]
    area = len(litho_lib) * [0.0]

    # Loop over all the classes of litho map. Start with class 3 because first two are major water bodies and ice:
    for iclass in range(1,18):
        print iclass
        # Check header of input file
        equal = ascraster.compare_grids(params.pgrass,os.path.join(params.litho_dir,"grid" + str(iclass) + ".asc"))
        if (not equal):
            raise MyError("Litho maps don't have same header as other input files.")                

        grid = ascraster.Asciigrid(ascii_file=os.path.join(params.litho_dir,"grid" + str(iclass) + ".asc"),\
                                   mask=mask,numtype=float)
           
        print os.path.join(params.litho_dir,"grid" + str(iclass) + ".asc") + " read."
        for icell in range(0,grid0.length):
            grid0_cell = grid0.get_data(icell,0.0)
            if (grid0_cell < 1.0):
                # There is something to do.
                grid_cell = grid.get_data(icell,0.0)

                if (grid_cell > 0.0):
                    # There is something of this class in this cell:                       
                    out[iclass] += grid_cell * water.get_data(icell,0.0)
                    area[iclass] += grid_cell * landarea.get_data(icell,0.0)
            
            if (iclass == 1):
                out[0]  += grid0_cell * water.get_data(icell,0.0)
                area[0] += grid0_cell * landarea.get_data(icell,0.0)

    return out,area