Skip to content
Snippets Groups Projects
copy_hourly.py 2.26 KiB
Newer Older
#!/usr/bin/env python3
"""
Copy the SEVIRI files at whole hours to a subdirectory

"""

import os
import argparse
import glob
import shutil


# Source/destination directories and matching glob patterns
SOURCE_DEST_DIRS = {
    "/viper/nadir/nvap/IcelandEurope_1088_0088_1537x494/" :
    ["/viper/nadir/nvap/IcelandEurope_1088_0088_1537x494/HOURLY",
     "*00_flat.nc"],
    "/viper/nadir/nvap/IcelandEurope_1088_0088_1537x494/SEVIRI-CLOUDS/" :
    ["/viper/nadir/nvap/IcelandEurope_1088_0088_1537x494/HOURLY/SEVIRI-CLOUDS/",
     "*0000_reg2d.nc"]
}



def copy_files(dry_run=False):
    """
    Copy files

    """

    for d_key, v_val in SOURCE_DEST_DIRS.items():
        source_files = glob.glob(os.path.join(d_key, v_val[1]))
        dest_files = [os.path.join(v_val[0], os.path.basename(f)) for f in source_files]

        if dry_run:
            # Print some of the files that would be copied
            print("Copying files from \n", source_files[:10], "to\n", dest_files[:10])

    if not dry_run:
        for src, dest in zip(source_files, dest_files):
            try:
                shutil.copyfile(src, dest)
            except Exception as e_err:
                # Things that can go wrong: no write permission,
                # file has been deleted in the meantime etc.
                print("ERROR: Failed to copy file {} to {}\n{}".format(src, dest, e_err))



if __name__ == "__main__":

    parser = argparse.ArgumentParser(prog="copy_hourly",
                                     description=("Copy SEVIRI files."),
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument('-r', '--run_copy',
                        dest='run_copy',
                        help="Run the copying script",
                        action='store_true',
                        default=False)

    parser.add_argument('-d', '--dry_run',
                        dest='dry_run',
                        help="Run the copying script but only show the files that would be copied",
                        action='store_true',
                        default=False)

    args = parser.parse_args()

    if args.run_copy:
        copy_files(dry_run=args.dry_run)
    else:
        print("\nNothing to do, here is some help:\n")
        parser.print_help()