Пример #1
0
def remove_products_from_xml(self):
    """Remove the specified products from the XML file

    The file is read into memory, processed, and written back out with out
    the specified products.
    """
    # Create and load the metadata object
    espa_metadata = Metadata(xml_filename=self._xml_filename)

    # Search for and remove the items
    for band in espa_metadata.xml_object.bands.band:
        if band.attrib['product'] in products_to_remove:
            # Business logic to always keep the radsat_qa band if bt,
            # or toa, or sr output was chosen
            if (band.attrib['name'] == ''
                    and (options['include_sr'] or options['include_sr_toa']
                         or options['include_sr_thermal'])):
                continue
            else:
                self.remove_band_from_xml(band)

    # Validate the XML
    espa_metadata.validate()

    # Write it to the XML file
    espa_metadata.write(xml_filename=self._xml_filename)
def main(inputxmlfile, outputxmlfile, hdffile):
    mm = Metadata(xml_filename=inputxmlfile)
    mm.parse()
    hls_product = 'hls'
    if hdffile == 'one':
        for band in mm.xml_object.bands.iterchildren():
            if band.get('name') == 'sr_band1':
                band.set('name', 'band01')
                band.set('product', hls_product)
            elif band.get('name') == 'sr_band2':
                band.set('name', 'blue')
                band.set('product', hls_product)
            elif band.get('name') == 'sr_band3':
                band.set('name', 'green')
                band.set('product', hls_product)
            elif band.get('name') == 'sr_band4':
                band.set('name', 'red')
                band.set('product', hls_product)
            elif band.get('name') == 'sr_band5':
                band.set('name', 'band05')
                band.set('product', hls_product)
            elif band.get('name') == 'sr_band6':
                band.set('name', 'band06')
                band.set('product', hls_product)
            elif band.get('name') == 'sr_band7':
                band.set('name', 'band07')
                band.set('product', hls_product)
            elif band.get('name') == 'sr_band8':
                band.set('name', 'band08')
                band.set('product', hls_product)
    elif hdffile == 'two':
        for band in mm.xml_object.bands.iterchildren():
            if band.get('name') == 'sr_band8a':
                band.set('name', 'band8a')
                band.set('product', hls_product)
            elif band.get('name') == 'sr_band9':
                band.set('name', 'band09')
                band.set('product', hls_product)
            elif band.get('name') == 'sr_band10':
                band.set('name', 'band10')
                band.set('product', hls_product)
            elif band.get('name') == 'sr_band11':
                band.set('name', 'band11')
                band.set('product', hls_product)
            elif band.get('name') == 'sr_band12':
                band.set('name', 'band12')
                band.set('product', hls_product)
            elif band.get('name') == 'sr_aerosol':
                band.set('name', 'CLOUD')
                band.set('product', hls_product)

    for band in mm.xml_object.bands.iterchildren():
        if band.get('product') != hls_product:
            print((band.get('name')))
            mm.xml_object.bands.remove(band)

    mm.write(xml_filename=outputxmlfile)
class TestMetadata(unittest.TestCase):
    """Test a few things, and expand on it someday"""

    def setUp(self):

        xml_filename = 'unittests/test.xml'
        self.mm = Metadata(xml_filename=xml_filename)
        self.mm.parse()

    def tearDown(self):
        pass

    def test_find_version(self):
        self.assertEqual(self.mm.xml_object.get('version'), '2.0')

    def test_find_corners(self):
        self.assertEqual(self.mm.xml_object.global_metadata.corner[0]
                         .attrib['location'], 'UL')
        self.assertEqual(self.mm.xml_object.global_metadata.corner[1]
                         .attrib['location'], 'LR')

    def test_find_band_names(self):
        self.assertEqual(self.mm.xml_object.bands.band[0].get('name'),
                         'band1')
        self.assertEqual(self.mm.xml_object.bands.band[1].get('name'),
                         'band2')
        self.assertEqual(self.mm.xml_object.bands.band[2].get('name'),
                         'band3')
        self.assertEqual(self.mm.xml_object.bands.band[3].get('name'),
                         'band4')
        self.assertEqual(self.mm.xml_object.bands.band[4].get('name'),
                         'band5')
        self.assertEqual(self.mm.xml_object.bands.band[5].get('name'),
                         'band6')

    def test_write_success(self):
        # Also tests for successful validation
        self.mm.write(xml_filename='walnuts_pass.xml')
        self.assertTrue(os.path.exists('walnuts_pass.xml') == 1)
        os.unlink('walnuts_pass.xml')

    def test_validation_fail(self):
        myE = objectify.ElementMaker(annotate=False, namespace=None,
                                     nsmap=None)
        self.mm.xml_object.animals = myE.root()
        self.mm.xml_object.animals.tiger = myE.frog('white')
        self.mm.xml_object.animals.frog = myE.frog('green')

        with self.assertRaises(XMLError):
            self.mm.validate()
def main(inputxmlfile, outputxmlfile,):
    mm = Metadata(xml_filename=inputxmlfile)
    mm.parse()
    hls_product = 'hls'
    for band in mm.xml_object.bands.iterchildren():
        if band.get('name') == 'sr_band1':
            band.set('name', 'band01')
            band.set('product', hls_product)
        elif band.get('name') == 'sr_band2':
            band.set('name', 'band02-blue')
            band.set('product', hls_product)
        elif band.get('name') == 'sr_band3':
            band.set('name', 'band03-green')
            band.set('product', hls_product)
        elif band.get('name') == 'sr_band4':
            band.set('name', 'band04-red')
            band.set('product', hls_product)
        elif band.get('name') == 'sr_band5':
            band.set('name', 'band05')
            band.set('product', hls_product)
        elif band.get('name') == 'sr_band6':
            band.set('name', 'band06')
            band.set('product', hls_product)
        elif band.get('name') == 'sr_band7':
            band.set('name', 'band07')
            band.set('product', hls_product)
        elif band.get('name') == 'radsat_qa':
            band.set('name', 'bandQA')
            band.set('product', hls_product)
        elif band.get('name') == 'toa_band9':
            band.set('name', 'band09')
            band.set('product', hls_product)
        elif band.get('name') == 'bt_band10':
            band.set('name', 'band10')
            band.set('product', hls_product)
        elif band.get('name') == 'bt_band11':
            band.set('name', 'band11')
            band.set('product', hls_product)
        elif band.get('name') == 'sr_aerosol':
            band.set('name', 'CLOUD')
            band.set('product', hls_product)

    for band in mm.xml_object.bands.iterchildren():
        if band.get('product') != hls_product:
            mm.xml_object.bands.remove(band)

    mm.write(xml_filename=outputxmlfile)
class TestMetadata(unittest.TestCase):
    """Test a few things, and expand on it someday"""
    def setUp(self):

        xml_filename = 'unittests/test.xml'
        self.mm = Metadata(xml_filename=xml_filename)
        self.mm.parse()

    def tearDown(self):
        pass

    def test_find_version(self):
        self.assertEqual(self.mm.xml_object.get('version'), '2.0')

    def test_find_corners(self):
        self.assertEqual(
            self.mm.xml_object.global_metadata.corner[0].attrib['location'],
            'UL')
        self.assertEqual(
            self.mm.xml_object.global_metadata.corner[1].attrib['location'],
            'LR')

    def test_find_band_names(self):
        self.assertEqual(self.mm.xml_object.bands.band[0].get('name'), 'band1')
        self.assertEqual(self.mm.xml_object.bands.band[1].get('name'), 'band2')
        self.assertEqual(self.mm.xml_object.bands.band[2].get('name'), 'band3')
        self.assertEqual(self.mm.xml_object.bands.band[3].get('name'), 'band4')
        self.assertEqual(self.mm.xml_object.bands.band[4].get('name'), 'band5')
        self.assertEqual(self.mm.xml_object.bands.band[5].get('name'), 'band6')

    def test_write_success(self):
        # Also tests for successful validation
        self.mm.write(xml_filename='walnuts_pass.xml')
        self.assertTrue(os.path.exists('walnuts_pass.xml') == 1)
        os.unlink('walnuts_pass.xml')

    def test_validation_fail(self):
        myE = objectify.ElementMaker(annotate=False,
                                     namespace=None,
                                     nsmap=None)
        self.mm.xml_object.animals = myE.root()
        self.mm.xml_object.animals.tiger = myE.frog('white')
        self.mm.xml_object.animals.frog = myE.frog('green')

        with self.assertRaises(XMLError):
            self.mm.validate()