示例#1
0
    def _copy_fits_files(self, bundle_segment: str, mast_downloads_dir: str,
                         primary_files_dir: str) -> None:
        if not os.path.isdir(mast_downloads_dir):
            raise ValueError(f"{mast_downloads_dir} doesn't exist.")
        try:
            PDS_LOGGER.open("Copy fits files to corresponding directories")
            with make_osfs(
                    mast_downloads_dir) as mast_downloads_fs, make_sv_osfs(
                        primary_files_dir) as primary_files_fs:

                # Walk the mast_downloads_dir for FITS file and file
                # them into the COW filesystem.
                for filepath in mast_downloads_fs.walk.files(
                        filter=["*.fits"]):
                    parts = fs.path.iteratepath(filepath)
                    depth = len(parts)
                    if depth != 3:
                        raise ValueError(f"{parts} length is not 3.")
                    # New way: product name comes from the filename
                    _, _, filename = parts
                    filename = filename.lower()
                    hst_filename = HstFilename(filename)
                    product = hst_filename.rootname()
                    instrument_name = hst_filename.instrument_name()
                    suffix = hst_filename.suffix()

                    collection_type = get_collection_type(
                        suffix=suffix, instrument_id=instrument_name)
                    coll = f"{collection_type}_{instrument_name.lower()}_{suffix}"

                    new_path = fs.path.join(
                        to_segment_dir(bundle_segment),
                        to_segment_dir(coll),
                        to_segment_dir(product),
                        filename,
                    )
                    dirs, filename = fs.path.split(new_path)
                    primary_files_fs.makedirs(dirs)
                    PDS_LOGGER.log("info", f"Copy {filename} to {new_path}")
                    fs.copy.copy_file(mast_downloads_fs, filepath,
                                      primary_files_fs, new_path)

            if not os.path.isdir(primary_files_dir + "-sv"):
                raise ValueError(f"{primary_files_dir + '-sv'} doesn't exist.")
            # # If I made it to here, it should be safe to delete the downloads
            # shutil.rmtree(mast_downloads_dir)
            # assert not os.path.isdir(mast_downloads_dir)
        except Exception as e:
            PDS_LOGGER.exception(e)
        finally:
            PDS_LOGGER.close()
示例#2
0
 def test_rootname(self) -> None:
     hst = HstFilename(_TEST_FILENAME)
     self.assertEqual("j6gp01mmq", hst.rootname())