def create_regiondata(params,isocode):
    '''
    Create a dictionary with key = isocode and each value is a RegionData class.
    Here the isocode and the regionname is filled. 
    '''

    # We need a input file with information about the isocode and the regionname.
    dum_data = data_class.read_datafile(params.filePopNum,sep=params.sep)

    regiondata_out = {}
    for key in isocode:
        ind = data_class.get_index(dum_data,"isocode",key)
        if (ind < 0):
            regiondata_out[key] = regiondata.RegionData(isocode=key,regionname="Not given")
        else:
            regiondata_out[key] = regiondata.RegionData(isocode=key,regionname=dum_data[ind].countryname)
    
    return regiondata_out
示例#2
0
def make_iso_list(params,mask):
    '''
    Make a list of all iso codes that we want to calculate
    Could be done by reading a grid, or reading a input file (table) with a complet set of isocodes.
    '''

    # Determine the list of isocodes, which are used to perform the calculations.
    isocode = []

    # Read table information. All isocodes mentioned in this file, will be used. 
    dum_data = data_class.read_datafile(params.fileisocode_table,sep=params.sep)
    # Put isocodes in a list
    for item in range(len(dum_data)):
        isocode.append(int(float(dum_data[item].isocode)))
    # Make a unique list of isocodes
    isocode = list(set(isocode))
   
    # Sort list of isocodes.
    isocode.sort()
    
    return isocode
    def table_scalar(self,label,filename,method="mult",xmin=None,xmax=None,dictfactor=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
            # Check whether excel file exist:
            if not os.path.isfile(filename_old):
                raise MyError("Input file " + filename_old + " does not exist.")

            # Read input file:
            full_data = data_class.read_datafile(filename_old,sep=self.options.sep)

            # Change the original data
            if (method == "mult"):
                # Multiply with given value or dictionary
                if (dictfactor == None):
                    value = float(getattr(self.options,label))
                    for item in range(len(full_data)):                     
                        # Multiply with given value
                        full_data[item].mult(value)
                else: 
                    # Multiply with given value per country   
                    for item in range(len(full_data)):                     
                        value = dictfactor[full_data[item].isocode]
                        # Multiply with given value for that country
                        full_data[item].mult(value)
            elif (method == "add"):
                # Add with given value or dictionary
                if (dictfactor == None):
                    value = float(getattr(self.options,label))
                    for item in range(len(full_data)):                     
                        # Add with given value
                        full_data[item].add(value)
                else: 
                    # Add with given value per country   
                    for item in range(len(full_data)):                     
                        value = dictfactor[full_data[item].isocode]
                        # Multiply with given value for that country
                        full_data[item].add(value)
            else:
                raise MyError("Method : " + method + " is not implemented in excel_sheet_scalar.")

            # Check the range of the new values and write to output file
            fp = open(filename,"w")
            lheader = True
            for item in range(len(full_data)):
                # Check range
                full_data[item].check_range(xmin=xmin,xmax=xmax)
                # Write to output
                full_data[item].write(fp,sep=self.options.sep,lheader=lheader)
                lheader = False

            # Close output file
            fp.close()

        else:
            print label + ' is NOT found.'

        # Return new file name
        return filename