Ejemplo n.º 1
0
    def loadModel(self, tcf_path, arg_dict={}):
        """Load a full tuflow model from the given tcf path."""
        self._resetLoader()

        if 'scenario' in arg_dict.keys():
            self._has_scenario = True

            self.user_variables.has_cmd_vals = True
            self.scenario_vals = arg_dict['scenario']
            for key, val in arg_dict['scenario'].items():
                self.user_variables.add(tuflowpart.TuflowModelVariable.noParent(key, val), 'scen')

        if 'event' in arg_dict.keys():
            self._has_event = True

            self.user_variables.has_cmd_vals = True
            self.event_vals = arg_dict['event']
            for key, val in arg_dict['event'].items():
                self.user_variables.add(tuflowpart.TuflowModelVariable.noParent(key, val), 'evnt')

        # Check that the tcf exists
        if not os.path.exists(tcf_path):
            raise IOError('Tcf file at %s does not exist' % tcf_path)
        root, tcf_name = os.path.split(tcf_path)
        root = uf.encodeStr(root)
#         root = unicode(root)
        tcf_name = uf.encodeStr(tcf_name)

        self.tuflow_model = TuflowModel(root)

        # Parse the tuflow control files
        main_file = tuflowpart.ModelFile(None, **{'path': tcf_name, 'command': None,
                                                  'comment': None, 'model_type': 'TCF',
                                                  'root': root})
        tcf_path = main_file.absolutePath()
#         self.tuflow_model.main_file = main_file

        # Setup the file and object holders
        self._file_queue.enqueue(main_file)

        # Read the control files and their contents into memory
        self._fetchTuflowModel(root)

        # Order the input and create the actual ControlFile objects
        self._orderModel(tcf_path)

        # Return the loaded model
        self.tuflow_model.root = root
        self.tuflow_model.control_files = self._control_files
        self.tuflow_model.bc_event = self._bc_event
        self.tuflow_model.user_variables = self.user_variables
        self.tuflow_model.control_files['TCF'].add_callback = self.tuflow_model.addTcfModelFile
        self.tuflow_model.control_files['TCF'].remove_callback = self.tuflow_model.removeTcfModelFile
        self.tuflow_model.control_files['TCF'].replace_callback = self.tuflow_model.replaceTcfModelFile
        self.tuflow_model.missing_model_files = self.missing_model_files
        return self.tuflow_model
Ejemplo n.º 2
0
    def loadModel(self, tcf_path, arg_dict={}):
        """Load a full tuflow model from the given tcf path."""
        self._resetLoader()
        
        if 'scenario' in arg_dict.keys():
            self._has_scenario = True 

            self.user_Variables.has_cmd_vals = True
            self.scenario_vals = arg_dict['scenario']
            for key, val in arg_dict['scenario'].items():
                self.user_variables.addVariable(tuflowpart.TuflowModelVariable.noParent(key, val), 'scen')
        
        if 'event' in arg_dict.keys(): 
            self._has_event = True 

            self.user_Variables.has_cmd_vals = True
            self.event_vals = arg_dict['event']
            for key, val in arg_dict['event'].items():
                self.user_variables.addVariable(tuflowpart.TuflowModelVariable.noParent(key, val), 'evnt')

        # Check that the tcf exists
        if not os.path.exists(tcf_path):
            raise IOError('Tcf file at %s does not exist' % tcf_path)
        root, tcf_name = os.path.split(tcf_path)
        root = uf.encodeStr(root)
#         root = unicode(root)
        tcf_name = uf.encodeStr(tcf_name)

        self.tuflow_model = TuflowModel(root)
        
        # Parse the tuflow control files
        main_file = tuflowpart.ModelFile(None, **{'path': tcf_name, 'command': None,
                                                'comment': None, 'model_type': 'TCF',
                                                'root': root})
        tcf_path = main_file.absolutePath()
#         self.tuflow_model.main_file = main_file
        
        # Setup the file and object holders
        self._file_queue.enqueue(main_file)
        
        # Read the control files and their contents into memory
        self._fetchTuflowModel(root)
        
        # Order the input and create the actual ControlFile objects
        self._orderModel(tcf_path)
        
        # Return the loaded model
        self.tuflow_model.root = root
        self.tuflow_model.control_files = self._control_files
        self.tuflow_model.bc_event = self._bc_event
        self.tuflow_model.user_variables = self.user_variables
        self.tuflow_model.control_files['TCF'].add_callback = self.tuflow_model.addTcfModelFile
        self.tuflow_model.control_files['TCF'].remove_callback = self.tuflow_model.removeTcfModelFile
        self.tuflow_model.control_files['TCF'].replace_callback = self.tuflow_model.replaceTcfModelFile
        self.tuflow_model.missing_model_files = self.missing_model_files
        return self.tuflow_model
Ejemplo n.º 3
0
def getFile(file_path):
    """Text file reader.

    Reads a text file, appending each new line to a list.
    
    Args:
        filePath (str): File path for text file to load.
    
    Returns:
        List - contents of text file split by new lines.
    
    Raises:
        IOError: if problem in reading file.
        TypeError: if string not given for file_path
    """
    file_contents = []
    line = ""

    try:
        with open(file_path, 'rU') as f:
            for line in f:
                file_contents.append(uf.encodeStr(line))
    except IOError:
        logger.error('Read file IOError')
        raise IOError('Unable to read file at: ' + file_path)
    except TypeError:
        logger.error('Read file TypeError')
        raise TypeError

    return file_contents
Ejemplo n.º 4
0
 def linkLabels(self):
     """Overriddes superclass method."""
     out = {}
     for i, name in enumerate(self.head_data['names']):
         namekey = uf.encodeStr('name_' + str(i))
         out[namekey] = name
     return out
Ejemplo n.º 5
0
 def linkLabels(self):
     """Overriddes superclass method."""
     out = {}
     for i, name in enumerate(self.head_data['names']):
         namekey = uf.encodeStr('name_' + str(i))
         out[namekey] = name
     return out
Ejemplo n.º 6
0
 def _addModelVariable(self, part):
     """Adds any model variables read in to the global variables dict.
     
     The dict will only be updated if there are no values in it already.
     This is inline with the Tuflow manual guidance.
     
     Args:
         part(TuflowVariable): with self.filepart_type == FILEPART_TYPE.MODEL_VARIABLE.
     """
     if not part.filepart_type == fpt.MODEL_VARIABLE: return
     if 'EVENT' in part.command.upper():
         if not self.event_vals:
             for i, e in enumerate(part.split_variable):
                 self.event_vals['e' + uf.encodeStr(i+1)] = e
     else:
         if not self.scenario_vals:
             for i, s in enumerate(part.split_variable):
                 self.scenario_vals['s' + uf.encodeStr(i+1)] = s
Ejemplo n.º 7
0
 def _addModelVariable(self, part):
     """Adds any model variables read in to the global variables dict.
     
     The dict will only be updated if there are no values in it already.
     This is inline with the Tuflow manual guidance.
     
     Args:
         part(TuflowVariable): with self.filepart_type == FILEPART_TYPE.MODEL_VARIABLE.
     """
     if not part.filepart_type == fpt.MODEL_VARIABLE: return
     if 'EVENT' in part.command.upper():
         if not self.event_vals:
             for i, e in enumerate(part.split_variable):
                 self.event_vals['e' + uf.encodeStr(i + 1)] = e
     else:
         if not self.scenario_vals:
             for i, s in enumerate(part.split_variable):
                 self.scenario_vals['s' + uf.encodeStr(i + 1)] = s
Ejemplo n.º 8
0
 def createModelVariableType(line, parent, **kwargs):
     command, kwargs['comment'], cchar = separateComment(line)
     kwargs['command'], variable = breakLine(command)
     split_var = variable.split('|')
     
     if 'MODEL SCENARIOS' in kwargs['command'].upper():
         prefix = 's'
     else:
         prefix = 'e'
     
     parts = []
     for i, s in enumerate(split_var):
         s = s.strip()
         name = prefix + uf.encodeStr(str(i+1))
         parts.append(tuflowpart.TuflowModelVariable(parent, **{
                 'logic': kwargs.get('logic', None), 'command': kwargs['command'],
                 'comment': kwargs['comment'], 'variable': s, 'name': name
         }))
     
     parts = assignSiblings(parts)
     return parts
Ejemplo n.º 9
0
 def getPrintableContents(self, **kwargs):
     return self.buildPrintline(self.command, uf.encodeStr(self._variable),
                                self.comment), False