def test_createXsDataFileObject_Shp(self):
        """Loads a 1d cross section gis file and checks the contents get setup."""

        test_list = [[
            'Section1.csv', 'Section2.csv', 'Section3.csv', 'Section4.csv'
        ], ['xz', 'xz', 'xz', 'xz'], ['', '', '', ''],
                     ['Section1', 'Section2', 'Section3', 'Section4'],
                     ['', '', '', ''], ['', '', '', ''], ['', '', '', ''],
                     ['', '', '', ''], ['', '', '', ''], [0.0, 0.0, 0.0, 0.0],
                     [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0, 1, 2, 3]]

        # First test for Shapefile
        gis_path = os.path.join(self.curd,
                                r'tests\test_data\xs\1d_xs_test.shp')
        hex_hash = hashlib.md5(gis_path.encode())
        hex_hash = hex_hash.hexdigest()
        datafile = GisFile(1, gis_path, hex_hash, 3, 'Read MI Table Links')
        gis = datafileloader.loadDataFile(datafile)

        self.assertIsInstance(gis, XsDataObject,
                              'Factory create XsDataObject fail - SHP')
        self.assertEqual(hex_hash, gis.path_holder.hex_hash,
                         'Hex_hash equality fail - SHP')
        self.assertEqual('Read MI Table Links', gis.path_holder.command,
                         'Command equality fail - SHP')

        row_list = gis.row_collection.getRowDataAsList()
        self.assertListEqual(test_list, row_list,
                             'List contents not equal fail - SHP')

        # Then for MapInfo file
        gis_path = os.path.join(self.curd,
                                r'tests\test_data\xs\1d_xs_test.mif')
        hex_hash = hashlib.md5(gis_path.encode())
        hex_hash = hex_hash.hexdigest()
        datafile = GisFile(1, gis_path, hex_hash, 3, 'Read MI Table Links')
        gis = datafileloader.loadDataFile(datafile)

        self.assertIsInstance(gis, XsDataObject,
                              'Factory create XsDataObject fail - MIF')
        self.assertEqual(hex_hash, gis.path_holder.hex_hash,
                         'Hex_hash equality fail - MIF')
        self.assertEqual('Read MI Table Links', gis.path_holder.command,
                         'Command equality fail - MIF')

        row_list = gis.row_collection.getRowDataAsList()
        self.assertListEqual(test_list, row_list,
                             'List contents not equal fail - MIF')
Exemple #2
0
    def test_MatCsvGetPrintableContents(self):
        """Check the contents of Materials csv file ok"""
        mat_path = os.path.join(
            self.curd, r'tests\test_data\materials_csv\Materials_CSV.csv')
        hex_hash = hashlib.md5(mat_path.encode())
        hex_hash = hex_hash.hexdigest()
        datafile = DataFile(1, mat_path, hex_hash, 3, 'Read Materials File')
        mat = datafileloader.loadDataFile(datafile)

        test_contents = [
            "Material ID, Manning's n, Infiltration Parameters, Land Us Hazard ID, ! Description",
            '1,"0.030,0.100,0.100,0.060","5.000, 2.000",,!Pasture',
            '2,0.022,"0.000, 2.000",,!Roads',
            '3,"0.030,0.020,0.100,3.000","0.000, 2.000",,!Buildings',
            '4,0.030,"5.000, 2.000",,!Ponds and other water',
            '10,0.080,"5.000, 2.000",,!Vegetated creek',
            '11,"0.030,0.100,0.100,0.040","5.000, 2.000",,!Maintained Grass',
            '20,Otherfile.csv | Depth | light values,"0.000, 0.000",,!Subfile light test',
            '21,Otherfile.csv | Depth | heavy values,"0.000, 0.000",,!Subfile heavy test',
            '30,Anotherfile.csv | < 50mm,"0.000, 0.000",,!Anotherfile under 50 test',
            '31,Anotherfile.csv | 50mm - 150mm,"0.000, 0.000",,!Anotherfile over 50 test',
            '40,Onlyname.csv,"0.000, 0.000",,!Onlyname test'
        ]
        contents = mat._getPrintableContents()
        self.assertListEqual(test_contents, contents,
                             'Output contents match fail')
    def test_createBcDataFileObject(self):
        """Loads a boundary condition csv file and checks that it is setup properly"""

        bc_path = os.path.join(self.curd,
                               r'tests\test_data\bc_dbase\bc_dbase_test.csv')
        hex_hash = hashlib.md5(bc_path.encode())
        hex_hash = hex_hash.hexdigest()
        datafile = DataFile(1, bc_path, hex_hash, 3, 'BC Database')
        bc = datafileloader.loadDataFile(datafile)

        self.assertIsInstance(bc, BcDataObject,
                              'Factory create BcDataObject fail')
        self.assertEqual(hex_hash, bc.path_holder.hex_hash,
                         'Hex_hash equality fail')
        self.assertEqual('BC Database', bc.path_holder.command,
                         'Command equality fail')

        test_list = [['Name1', 'Name2', 'Name3'],
                     ['Name1.csv', 'Name2.csv', 'Name2.csv'],
                     ['time', 'time', 'time'], ['Downstream', 'Flow', 'Dummy'],
                     ['', '', ''], ['', '', ''], ['', '', ''], ['', '', ''],
                     ['', '', ''],
                     [
                         'Name', 'Source', 'Column1_or_Time',
                         'Column2_or_Value_or_ID', 'TimeAdd', 'ValueMult',
                         'ValueAdd', 'IMultF', 'IAddF'
                     ], [0, 1, 2]]

        row_list = bc.row_collection.getRowDataAsList()
        self.assertListEqual(test_list, row_list,
                             'List contents not equal fail')
Exemple #4
0
    def test_XsGetPrintableContents(self):
        """Check that this is not implemented."""
        gis_path = os.path.join(self.curd,
                                r'tests\test_data\xs\1d_xs_test.mif')
        hex_hash = hashlib.md5(gis_path.encode())
        hex_hash = hex_hash.hexdigest()
        datafile = GisFile(1, gis_path, hex_hash, 3, 'Read MI Table Links')
        gis = datafileloader.loadDataFile(datafile)

        self.assertRaises(NotImplementedError,
                          lambda: gis._getPrintableContents())
        self.assertRaises(NotImplementedError, lambda: gis.saveData())
def tuflowFileExample():
    """Find all gis and bc database files referenced by a tuflow model.
    
    Uses a .tcf file to load a tuflow model and find all of the gis files and
    BC Database files referenced by it. Also identifies any additional files
    referenced by the BC Database files.
    """
    # Load the tuflow model with a tcf file
    tcf_file = r'C:\path\to\a\tuflow\tcffile.tcf'
    loader = fl.FileLoader()
    tuflow_model = loader.loadFile(tcf_file)
    
    # Get file names and absolute paths of all gis files referenced by the model
    # First we get all GIS type files in the TuflowModel and to avoid getting the
    # same type twice, caused by the same GIS file referenced in two locations
    # in the model control files, we set no_duplicates=True.
    gis_files = tuflow_model.getFiles(file_type=ft.GIS, no_duplicates=True)
    names = []
    paths = []
    for g in gis_files:
        names.append(g.getFileNameAndExtension())
        paths.append(g.getAbsolutePath())

    gis_combined = dict(zip(names, paths))
    
    # Get the data files objs referenced by the model.
    # These are files that point to additional data (tmf, bcdbase, 1d_xs, etc)
    bc_combined = []
    data_objs = tuflow_model.getFiles(file_type=ft.DATA, no_duplicates=True)
    
    # Loop through the data_objs and extract the names and file sources for 
    # each of the BC Database type files
    for data in data_objs:
        if data.command.upper() == 'BC DATABASE':
            
            bc = dfl.loadDataFile(data)
            names = bc.getDataEntryAsList(bc.keys.NAME)
            sources = bc.getDataEntryAsList(bc.keys.SOURCE)
            bc_combined.append((data.getFileNameAndExtension(), dict(zip(names, sources))))
        
    print 'GIS files in model:'
    for name, path in gis_combined.items(): 
        print name + ':\n' + path
    print '\nBC Database files in model:'
    for b in bc_combined:
        print b[0]
        for x in b[1].values(): print x
Exemple #6
0
    def test_MatCsvSubfileGetPrintableContents(self):
        """Checks the contents of subfile is working properly"""
        mat_path = os.path.join(
            self.curd, r'tests\test_data\materials_csv\Materials_CSV.csv')
        hex_hash = hashlib.md5(mat_path.encode())
        hex_hash = hex_hash.hexdigest()
        datafile = DataFile(1, mat_path, hex_hash, 3, 'Read Materials File')
        mat = datafileloader.loadDataFile(datafile)

        test_contents = [
            'Depth, light values, heavy values, , ',
            'y (m) ,      n ,      n ,       ,      ',
            ' 0.030000, 0.480000, 0.800000, , ',
            ' 0.060000, 0.310000, 0.600000, , Some other text that noone cares about',
            ' 0.090000, 0.194000, 0.360000, , '
        ]
        contents = mat.subfiles[0]._getPrintableContents()
        self.assertListEqual(test_contents, contents,
                             'Output contents match fail')
Exemple #7
0
    def test_BcGetPrintableContents(self):
        """
        """
        bc_path = os.path.join(self.curd,
                               r'tests\test_data\bc_dbase\bc_dbase_test.csv')
        hex_hash = hashlib.md5(bc_path.encode())
        hex_hash = hex_hash.hexdigest()
        datafile = DataFile(1, bc_path, hex_hash, 3, 'BC Database')
        bc = datafileloader.loadDataFile(datafile)

        test_contents = [
            'Name, Source, Column1_or_Time, Column2_or_Value_or_ID, TimeAdd, ValueMult, ValueAdd, IMultF, IAddF',
            'Name1, Name1.csv, time, Downstream, , , , , ',
            'Name2, Name2.csv, time, Flow, , , , , ',
            'Name3, Name2.csv, time, Dummy, , , , , '
        ]
        contents = bc._getPrintableContents()
        self.assertListEqual(test_contents, contents,
                             'Output contents match fail')
Exemple #8
0
    def test_TmfGetPrintableContents(self):
        """
        """
        mat_path = os.path.join(self.curd,
                                r'tests\test_data\Materials_TMF.tmf')
        hex_hash = hashlib.md5(mat_path.encode())
        hex_hash = hex_hash.hexdigest()
        datafile = DataFile(1, mat_path, hex_hash, 3, 'Read Materials File')
        mat = datafileloader.loadDataFile(datafile)

        test_contents = [
            '! Materials file test.', '!', '! Some comments at the top', '',
            '1, 0.060, , , , , , , , ,  ! Open pasture',
            '2, 0.025, , , , , , , , ,  ! Roads and Paths',
            '3, 0.040, , , , , , , , ,  ! Tracks',
            '4, 0.080, , , , , , , , ,  ! Trees',
            '5, 0.300, , , , , , , , ,  ! Buildings',
            '6, 0.100, , , , , , , , ,  ! Inland water',
            '7, 0.080, , , , , , , , ,  ! Gardens', ''
        ]
        contents = mat._getPrintableContents()
        self.assertListEqual(test_contents, contents,
                             'Output contents match fail')
    def test_createTmfDataFileObject(self):
        """Loads a .tmf file and checks that it is setup properly"""

        mat_path = os.path.join(self.curd,
                                r'tests\test_data\Materials_TMF.tmf')
        hex_hash = hashlib.md5(mat_path.encode())
        hex_hash = hex_hash.hexdigest()
        datafile = DataFile(1, mat_path, hex_hash, 3, 'Read Materials File')
        mat = datafileloader.loadDataFile(datafile)

        self.assertIsInstance(mat, TmfDataObject,
                              'Factory create TmfDataObject fail')
        self.assertEqual(hex_hash, mat.path_holder.hex_hash,
                         'Hex_hash equality fail')
        self.assertEqual('Read Materials File', mat.path_holder.command,
                         'Command equality fail')

        test_list = [[1, 2, 3, 4, 5, 6, 7],
                     [0.06, 0.025, 0.04, 0.08, 0.3, 0.1, 0.08],
                     ['', '', '', '', '', '',
                      ''], ['', '', '', '', '', '', ''],
                     ['', '', '', '', '', '',
                      ''], ['', '', '', '', '', '', ''],
                     ['', '', '', '', '', '',
                      ''], ['', '', '', '', '', '', ''],
                     ['', '', '', '', '', '',
                      ''], ['', '', '', '', '', '', ''],
                     ['', '', '', '', '', '', ''],
                     [
                         'Open pasture', 'Roads and Paths', 'Tracks', 'Trees',
                         'Buildings', 'Inland water', 'Gardens'
                     ], [0, 1, 2, 3, 4, 5, 6]]

        row_list = mat.row_collection.getRowDataAsList()
        self.assertListEqual(test_list, row_list,
                             'List contents not equal fail')
    def test_createMatCsvDataFileObject(self):
        """Loads a Materials.csv file and checks setup"""

        mat_path = os.path.join(
            self.curd, r'tests\test_data\materials_csv\Materials_CSV.csv')
        hex_hash = hashlib.md5(mat_path.encode())
        hex_hash = hex_hash.hexdigest()
        datafile = DataFile(1, mat_path, hex_hash, 3, 'Read Materials File')
        mat = datafileloader.loadDataFile(datafile)

        self.assertIsInstance(mat, MatCsvDataObject,
                              'Factory create MatCsvDataObject fail')
        self.assertEqual(hex_hash, mat.path_holder.hex_hash,
                         'Hex_hash equality fail')
        self.assertEqual('Read Materials File', mat.path_holder.command,
                         'Command equality fail')

        test_list = [
            ['1', '2', '3', '4', '10', '11', '20', '21', '30', '31', '40'],
            ['', 0.022, '', 0.03, 0.08, '', '', '', '', '', ''],
            [0.03, '', 0.03, '', '', 0.03, '', '', '', '', ''],
            [0.1, '', 0.02, '', '', 0.1, '', '', '', '', ''],
            [0.1, '', 0.1, '', '', 0.1, '', '', '', '', ''],
            [0.06, '', 3.0, '', '', 0.04, '', '', '', '', ''],
            [
                '', '', '', '', '', '', 'Otherfile.csv', 'Otherfile.csv',
                'Anotherfile.csv', 'Anotherfile.csv', 'Onlyname.csv'
            ],
            [
                '', '', '', '', '', '', 'Depth', 'Depth', '< 50mm',
                '50mm - 150mm', ''
            ],
            [
                '', '', '', '', '', '', 'light values', 'heavy values', '', '',
                ''
            ], [5.0, 0.0, 0.0, 5.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0],
            [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0],
            ['', '', '', '', '', '', '', '', '', '', ''],
            [
                '! Description', '!Pasture', '!Roads', '!Buildings',
                '!Ponds and other water', '!Vegetated creek',
                '!Maintained Grass', '!Subfile light test',
                '!Subfile heavy test', '!Anotherfile under 50 test',
                '!Anotherfile over 50 test', '!Onlyname test'
            ],
            [
                'Material ID', "Manning's n", '', '', '', '', '', '', '',
                'Infiltration Parameters', '', 'Land Us Hazard ID'
            ], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
        ]
        row_list = mat.row_collection.getRowDataAsList()
        self.assertListEqual(test_list, row_list,
                             'List contents not equal fail')
        '''
         Check that the sub files loaded properly as well
        '''
        sub_extract = {}
        for sub in mat.subfiles:
            sub_extract[sub.file_name] = sub.row_collection.getRowDataAsList()
            i = 0

        name1 = 'Otherfile'
        otherfile_testlist = [
            [0.03, 0.06, 0.09], [0.48, 0.31, 0.194], [0.8, 0.6, 0.36],
            ['', '', ''], ['', 'Some other text that noone cares about', ''],
            ['Depth', 'light values', 'heavy values', '', ''], [0, 1, 2, 3, 4]
        ]

        name2 = 'Onlyname'
        onlyname_testlist = [[0.002, 0.00309, 0.00331],
                             [0.400786, 0.074916, 0.056848],
                             ['Random header', ''], [0, 1, 2, 3, 4]]

        name3 = 'Anotherfile'
        anotherfile_testlist = [[0.0477, 0.0077, 0.0132, 0.0277, 0.036],
                                [0.88, 0.765, 0.666, 0.646, 0.626],
                                ['', '', '', '', ''],
                                [0.0155, 0.0177, 0.0277, 0.0377, 0.0527],
                                [0.753, 0.74, 0.666, 0.631, 0.5947],
                                ['< 50mm', '', '', '50mm - 150mm', ''],
                                [0, 1, 2, 3, 4, 5, 6]]

        self.assertListEqual(otherfile_testlist, sub_extract[name1],
                             'Otherfile data fail')
        self.assertListEqual(onlyname_testlist, sub_extract[name2],
                             'Onlyname data fail')
        self.assertListEqual(anotherfile_testlist, sub_extract[name3],
                             'Anotherfile data fail')