Beispiel #1
0
    def _checkValue(self, value, **kwargs): 
        if self.allow_blank and value == '': return value
        dtype = self.kwargs.get('dtype', dt.STRING)
        default = self.kwargs.get('default', None)

        if dtype == dt.STRING: 
            if not uf.isString(value):
                if default is not None: return default
                raise ValueError('value %s is not compatible with dtype STRING' % value)
            else:
                return value
        if dtype == dt.INT:
            if not uf.isNumeric(value): 
                if default is not None: return default
                raise ValueError('value %s is not compatible with dtype INT' % value)
            else:
                return int(value)
        if dtype == dt.FLOAT:
            if not uf.isNumeric(value): 
                if default is not None: return default
                raise ValueError('value %s is not compatible with dtype FLOAT' % value)
            else:
                return float(value)
        if dtype == dt.CONSTANT:
            choices = self.kwargs['choices']
            if not value in choices:
                raise ValueError("value %s is not in CONSTANT 'choices' tuple %s" % (value, choices))
            else:
                return  value
Beispiel #2
0
    def _checkValue(self, value, **kwargs):
        if self.allow_blank and value == '': return value
        dtype = self.kwargs.get('dtype', dt.STRING)
        default = self.kwargs.get('default', None)

        if dtype == dt.STRING:
            if not uf.isString(value):
                if default is not None: return default
                raise ValueError(
                    'value %s is not compatible with dtype STRING' % value)
            else:
                return value
        if dtype == dt.INT:
            if not uf.isNumeric(value):
                if default is not None: return default
                raise ValueError('value %s is not compatible with dtype INT' %
                                 value)
            else:
                return int(value)
        if dtype == dt.FLOAT:
            if not uf.isNumeric(value):
                if default is not None: return default
                raise ValueError(
                    'value %s is not compatible with dtype FLOAT' % value)
            else:
                return float(value)
        if dtype == dt.CONSTANT:
            choices = self.kwargs['choices']
            if not value in choices:
                raise ValueError(
                    "value %s is not in CONSTANT 'choices' tuple %s" %
                    (value, choices))
            else:
                return value
Beispiel #3
0
    def _readHeadData(self, unit_data, file_line):            
        """Reads the data in the file header section into the class.
        
        Args:
            unit_data (list): contains data for this unit.
        """
        self.head_data['comment'].value = unit_data[file_line][5:].strip()
        self._name = unit_data[file_line + 1][:12].strip()

        l = unit_data[file_line+2]
        rows = 1
        if 'LUNAR MONTHS' in l:
            l = l.replace('LUNAR MONTHS', 'LUNAR-MONTHS')
            l = ' '.join(l.split())
            vars = l.split()
            vars[1] = 'LUNAR MONTHS'
        else:
            l = ' '.join(l.split())
            vars = l.split()

        rows = int(vars[0])
        if uf.isNumeric(vars[1]):
            self.head_data['time_units'].value = 'USER SET'
            self.head_data['multiplier'].value = vars[1]
        else:
            self.head_data['time_units'].value = vars[1]
        
        self.head_data['extending_method'].value = vars[2] 
        self.head_data['interpolation'].value = vars[3] 

        return file_line + 3, rows
Beispiel #4
0
 def addSnapshot(self, snapshot_path, time):
     """Add a new snapshot.
     
     Args:
         snapshot_path(str): the path for the snapshot.
         time(float): the time to assign to the snapshot.
     """
     if self.snapshots is None: self.snapshots = []
     if not uf.isNumeric(time):
         raise ValueError ('time is not a numeric value')
     
     self.snapshots.append({'time': time, 'file': snapshot_path})
Beispiel #5
0
    def _scanfile(filepath):
        """Scans the file before we do any loading to identify the contents.
        Need to do this because the file can be setup in so many way that it
        becomes a headache to work it out in advance. Better to take a little
        bit of extra processing time and do some quick checks first.
         
        Arguments:
            file_path (str): the path to the subfile.
        
        Return:
            tuple:
                 list: booleans with whether the column contains
                       data that we want or not.
                 int:  length of the cols list.
                 list: containing all of the first row column data
                 int:  first row with usable data on.
        """ 
        logger.debug('Scanning Materials file - %s' 
                                        % (filepath))
             
        with open(filepath, 'rb') as csv_file:
             
            csv_file = csv.reader(csv_file)
             
            cols = []
            head_list = []
            start_row = -1
            for i, row in enumerate(csv_file, 0): 
                if "".join(row).strip() == "":
                    break
 
                for j, col in enumerate(row, 0):
                    if i == 0:
                        cols.append(False)
                        head_list = row
                    elif uuf.isNumeric(col):
                        cols[j] = True
                        if start_row == -1:
                            start_row = i
                    elif cols[j] == True:
                        break
         
        return cols, len(cols), head_list, start_row
Beispiel #6
0
    def _scanfile(filepath):
        """Scans the file before we do any loading to identify the contents.
        Need to do this because the file can be setup in so many way that it
        becomes a headache to work it out in advance. Better to take a little
        bit of extra processing time and do some quick checks first.
         
        Arguments:
            file_path (str): the path to the subfile.
        
        Return:
            tuple:
                 list: booleans with whether the column contains
                       data that we want or not.
                 int:  length of the cols list.
                 list: containing all of the first row column data
                 int:  first row with usable data on.
        """
        logger.debug('Scanning Materials file - %s' % (filepath))

        with open(filepath, 'rb') as csv_file:

            csv_file = csv.reader(csv_file)

            cols = []
            head_list = []
            start_row = -1
            for i, row in enumerate(csv_file, 0):
                if "".join(row).strip() == "":
                    break

                for j, col in enumerate(row, 0):
                    if i == 0:
                        cols.append(False)
                        head_list = row
                    elif uuf.isNumeric(col):
                        cols[j] = True
                        if start_row == -1:
                            start_row = i
                    elif cols[j] == True:
                        break

        return cols, len(cols), head_list, start_row
Beispiel #7
0
    def _readHeadData(self, unit_data, file_line):
        """Reads the data in the file header section into the class.

        Args:
            unit_data (list): contains data for this unit.
        """
        self.head_data['comment'].value = unit_data[file_line][5:].strip()
        self._name = unit_data[file_line + 1][:12].strip()

        l = unit_data[file_line + 2]
        rows = 1

        # The default in FMP when a new unit is created is:
        # "         1                         HOURS"
        # But this is interpretted as EXTEND, LINEAR, SECONDS??!!
        # If we find this line we need to convert to those explicitely
        if l.strip() == '1                         HOURS':
            self.head_data['time_units'].value = 'SECONDS'
            self.head_data['extending_method'].value = 'EXTEND'
            self.head_data['interpolation'].value = 'LINEAR'
        else:
            if 'LUNAR MONTHS' in l:
                l = l.replace('LUNAR MONTHS', 'LUNAR-MONTHS')
                l = ' '.join(l.split())
                vars = l.split()
                vars[1] = 'LUNAR MONTHS'
            else:
                l = ' '.join(l.split())
                vars = l.split()

            rows = int(vars[0])
            if uf.isNumeric(vars[1]):
                self.head_data['time_units'].value = 'USER SET'
                self.head_data['multiplier'].value = vars[1]
            else:
                self.head_data['time_units'].value = vars[1]

            self.head_data['extending_method'].value = vars[2]
            self.head_data['interpolation'].value = vars[3]

        return file_line + 3, rows
Beispiel #8
0
    def _disectEntry(col_no, entry, new_row):
        """Breaks the row values into the appropriate object values.
        
        The materials file can have Excel style sub-values. i.e. it can have
        seperate columns defined within a bigger one. This function will break
        those values down into a format usable by the values initiated in the
        rowdatacollection.
        
        Args:
            col_no(int): the current column number.
            entry(string): the value of the current column.
            new_row(list): the row values to update.
            
        Return:
            list containing the updated row values.
        
        Note:
            This isn't very nice. Need to clean it up and find a better, safer
            way of dealing with breaking the row data up. It may be excess work
            but perhaps creating an xml converter could work quite will and
            make dealing with the file a bit easier?
        """
        made_change = False

        # Put in ID and Hazard as normal
        if col_no == 0:
            new_row[0] = entry
        elif col_no == 11:
            new_row[11] = entry
        # Possible break up Manning's entry further
        elif col_no == 1:
            # See if there's more than one value in the Manning's category.
            splitval = entry.split(',')

            # If there is and it's numeric then it's a single value for 'n'
            if len(splitval) == 1:
                if uuf.isNumeric(splitval[0]):
                    new_row[1] = splitval[0]

                # Otherwise it's a filename. These can be further separated
                # into two column headers to read from the sub files.
                else:
                    strsplit = splitval[0].split('|')
                    if len(strsplit) == 1:
                        subfile_details[strsplit[0].strip()] = []
                        new_row[6] = strsplit[0].strip()
                    elif len(strsplit) == 2:
                        subfile_details[strsplit[0]] = [strsplit[1].strip()]
                        new_row[6] = strsplit[0].strip()
                        new_row[7] = strsplit[1].strip()
                    else:
                        subfile_details[strsplit[0]] = [
                            strsplit[1].strip(), strsplit[2].strip()
                        ]
                        new_row[6] = strsplit[0].strip()
                        new_row[7] = strsplit[1].strip()
                        new_row[8] = strsplit[2].strip()

            # If there's more than one value then it must be the Manning's
            # depth curve values (N1, Y1, N2, Y2).
            else:
                new_row[2] = splitval[0]
                new_row[3] = splitval[1]
                new_row[4] = splitval[2]
                new_row[5] = splitval[3]

        # Finally grab the infiltration parameters (IL, CL)
        elif col_no == 2:
            splitval = entry.split(',')
            new_row[9] = splitval[0]
            new_row[10] = splitval[1]

        return new_row
Beispiel #9
0
    def _disectEntry(col_no, entry, new_row):
        """Breaks the row values into the appropriate object values.
        
        The materials file can have Excel style sub-values. i.e. it can have
        seperate columns defined within a bigger one. This function will break
        those values down into a format usable by the values initiated in the
        rowdatacollection.
        
        Args:
            col_no(int): the current column number.
            entry(string): the value of the current column.
            new_row(list): the row values to update.
            
        Return:
            list containing the updated row values.
        
        Note:
            This isn't very nice. Need to clean it up and find a better, safer
            way of dealing with breaking the row data up. It may be excess work
            but perhaps creating an xml converter could work quite will and
            make dealing with the file a bit easier?
        """
        made_change = False

        # Put in ID and Hazard as normal
        if col_no == 0:
            new_row[0] = entry
        elif col_no == 11:
            new_row[11] = entry
        # Possible break up Manning's entry further
        elif col_no == 1:
            # See if there's more than one value in the Manning's category.
            splitval = entry.split(',')
             
            # If there is and it's numeric then it's a single value for 'n'
            if len(splitval) == 1:
                if uuf.isNumeric(splitval[0]):
                    new_row[1] = splitval[0]
             
                # Otherwise it's a filename. These can be further separated 
                # into two column headers to read from the sub files.
                else:
                    strsplit = splitval[0].split('|')
                    if len(strsplit) == 1:
                        subfile_details[strsplit[0].strip()] = []
                        new_row[6] = strsplit[0].strip()
                    elif len(strsplit) == 2:
                        subfile_details[strsplit[0]] = [strsplit[1].strip()]
                        new_row[6] = strsplit[0].strip()
                        new_row[7] = strsplit[1].strip()
                    else:
                        subfile_details[strsplit[0]] = [strsplit[1].strip(), strsplit[2].strip()]
                        new_row[6] = strsplit[0].strip()
                        new_row[7] = strsplit[1].strip()
                        new_row[8] = strsplit[2].strip()
                          
            # If there's more than one value then it must be the Manning's
            # depth curve values (N1, Y1, N2, Y2).
            else:
                new_row[2] = splitval[0]
                new_row[3] = splitval[1]
                new_row[4] = splitval[2]
                new_row[5] = splitval[3]

        # Finally grab the infiltration parameters (IL, CL)
        elif col_no == 2:
            splitval = entry.split(',')
            new_row[9] = splitval[0]
            new_row[10] = splitval[1]
        
        
        return new_row