def test_files_with_usgs_id(self):
        # No MDF directory, only files. Can we still extract enough info?
        d = write_files({
            '446.000.2013254233714881.ASA': 'a',
            '447.000.2013254233711482.ASA': 'a',
            'LC80880750762013254ASA00_IDF.xml': 'a',
            'LC80880750762013254ASA00_MD5.txt': 'a',
        })

        def _test_mdf_output(metadata):
            self.assertEqual(metadata.usgs.interval_id,
                             'LC80880750762013254ASA00')
            self.assertEqual(metadata.platform.code, 'LANDSAT_8')
            self.assertEqual(metadata.instrument.name, 'OLI_TIRS')
            self.assertEqual(metadata.format_.name, 'MD')
            self.assertEqual(metadata.ga_level, 'P00')
            self.assertEqual(metadata.image.satellite_ref_point_start,
                             ptype.Point(88, 75))
            self.assertEqual(metadata.image.satellite_ref_point_end,
                             ptype.Point(88, 76))
            self.assertEqual(metadata.acquisition.groundstation.code, 'ASA')
            # No longer bother with vague center date.
            # self.assertEqual(metadata.extent.center_dt, datetime.date(2013, 9, 11))
            self.assertEqual(
                metadata.acquisition.aos,
                datetime.datetime(2013, 9, 11, 23, 36, 11, 482000))
            self.assertEqual(
                metadata.acquisition.los,
                datetime.datetime(2013, 9, 11, 23, 37, 14, 881000))

        metadata = mdf.extract_md(ptype.DatasetMetadata(), d)
        _test_mdf_output(metadata)
    def test_unchanged_without_id(self):
        # No MDF directory, only files. Don't try to extract information from the files.
        d = write_files({
            '446.000.2013254233714881.ASA': 'a',
            '447.000.2013254233711482.ASA': 'a',
        })

        id_ = uuid.uuid1()
        date = datetime.datetime.utcnow()
        metadata = mdf.extract_md(
            ptype.DatasetMetadata(id_=id_, creation_dt=date), d)
        # Should be unchanged: No USGS ID found.
        assert_same(metadata, ptype.DatasetMetadata(id_=id_, creation_dt=date))
    def test_directory(self):
        d = write_files({
            'LC80880750762013254ASA00': {
                '446.000.2013254233714881.ASA': 'a',
                '447.000.2013254233711482.ASA': 'a',
                'LC80880750762013254ASA00_IDF.xml': 'a',
                'LC80880750762013254ASA00_MD5.txt': 'a',
            }
        })

        def _test_mdf_output(metadata):
            self.assertEqual(metadata.usgs.interval_id,
                             'LC80880750762013254ASA00')
            self.assertEqual(metadata.platform.code, 'LANDSAT_8')
            self.assertEqual(metadata.instrument.name, 'OLI_TIRS')
            self.assertEqual(metadata.format_.name, 'MD')
            self.assertEqual(metadata.ga_level, 'P00')
            self.assertEqual(metadata.image.satellite_ref_point_start,
                             ptype.Point(88, 75))
            self.assertEqual(metadata.image.satellite_ref_point_end,
                             ptype.Point(88, 76))
            self.assertEqual(metadata.acquisition.groundstation.code, 'ASA')
            # No longer bother with vague center date.
            # self.assertEqual(metadata.extent.center_dt, datetime.date(2013, 9, 11))
            self.assertEqual(
                metadata.acquisition.aos,
                datetime.datetime(2013, 9, 11, 23, 36, 11, 482000))
            self.assertEqual(
                metadata.acquisition.los,
                datetime.datetime(2013, 9, 11, 23, 37, 14, 881000))

        metadata = mdf.extract_md(ptype.DatasetMetadata(), d)
        _test_mdf_output(metadata)

        # It should also work when given the specific MDF folder.
        metadata = mdf.extract_md(ptype.DatasetMetadata(),
                                  d.joinpath('LC80880750762013254ASA00'))
        _test_mdf_output(metadata)
    def test_no_directory(self):
        d = write_files({
            'L7EB2013259012832ASN213I00.data': 'nothing',
            'L7EB2013259012832ASN213Q00.data': 'nothing'
        })

        self.assertEqual((None, set()), mdf.find_mdf_files(d))

        # Make sure that metadata is not modified when no MDF is found.
        starting_md = ptype.DatasetMetadata()
        id_ = starting_md.id_
        creation_dt = starting_md.creation_dt
        expected_dt = ptype.DatasetMetadata(id_=id_, creation_dt=creation_dt)

        output = mdf.extract_md(starting_md, d)
        self.assertEqual(expected_dt, output)
    def fill_metadata(self, dataset, path, additional_files=()):
        """
        :type additional_files: tuple[Path]
        :type dataset: ptype.DatasetMetadata
        :type path: Path
        :rtype: ptype.DatasetMetadata
        """
        dataset = adsfolder.extract_md(dataset, path)
        dataset = rccfile.extract_md(dataset, path)
        dataset = mdf.extract_md(dataset, path)
        dataset = passinfo.extract_md(dataset, path)
        dataset = pds.extract_md(dataset, path)
        dataset = npphdf5.extract_md(dataset, path)

        # TODO: Antenna coords for groundstation? Heading?
        # TODO: Bands? (or eg. I/Q files?)
        return dataset