Skip to content
Snippets Groups Projects
Commit 648be010 authored by Alan De Smet's avatar Alan De Smet
Browse files

Start of aitf-data-for-run (probably broken)

parent c24f9772
No related branches found
No related tags found
No related merge requests found
#! /usr/bin/python3
# System modules
import os
import sys
import logging
import re
sys.path.append(os.path.abspath('..'))
import csppfetch
import aitf.ancil
import aitf.conlog
def parse_args():
from aitf.argparsetools import add_cache_dir_arg, add_verbose_quiet_args, \
process_verbosity, SmartFormatter, arg_duration, arg_datetime, arg_directory
""" Parse arguments. Returns a namespace as returned by argparse.ArgumentParser.parse_args(). """
import argparse
default_expiration_days = 7
ap = argparse.ArgumentParser(
description="Acquire data needed to process a scan with cspp-geo-aitf. Symbolically links required files from the local cache into the destination. Will attempt to download file if preferred files are not locally available.",
epilog="""
A DATE is a date and optional time. The form "2021-02-27T14:50:00"
is recommended. The form "20210581450000" (YYYYjjjHHMMSSt) is also
understood.
""",
formatter_class = SmartFormatter # Protects blank line in epilog
)
ap.add_argument('scan_time', type=arg_datetime,
metavar='DATE',
help='scan time ancillary data is needed for')
ap.add_argument('destination', type=arg_directory,
metavar='FINAL_PATH',
help='write required files here, possibly as symbolic links')
add_cache_dir_arg(ap)
ap.add_argument('--cache-only', action='store_true',
help='do not download; only use files already in cache')
add_verbose_quiet_args(ap)
args = ap.parse_args()
process_verbosity(args)
return args
def hack_oisst_preliminary_filename(filename):
""" Remove '_preliminary' from oisst files
The AIT_Framework can't handle the _preliminary, and working around it
is worse than this hack. (See https://gitlab.ssec.wisc.edu/cspp_geo/cspp-geo-aitf/issues/189 )
"""
return re.sub(r'(avhrr-only-v2.\d{8})_preliminary(.nc)', r'\1\2', filename)
def log_download_report(name, stats):
logging.summary(name)
for x in stats.report():
logging.summary(" "+x)
def main():
aitf.conlog.setup_logging()
args = parse_args()
cache_dir = args.dir
if not os.access(cache_dir, os.W_OK):
logging.warning(f'I do not appear to be able to have write access to "{cache_dir}". Downloading directly into args.destination')
args.dir = args.destination
do_download = not args.cache_only
try:
logging.progress("Acquiring SST")
sststats = aitf.ancil.SST.DownloadStatistics()
sst_files = aitf.ancil.SST.download_for_time(args.scan_time, args.dir, do_download = do_download, download_stats = sststats)
log_download_report("SST Download Summary", sststats)
# using SST instead of GFS to show that
# DownloadStatistics are interchangable.
logging.progress("Acquiring GFS")
gfsstats = aitf.ancil.SST.DownloadStatistics()
gfs_files = aitf.ancil.GFS.download_for_time(args.scan_time, args.dir, do_download = do_download, download_stats = sststats)
log_download_report("GFS Download Summary", gfsstats)
log_download_report("Total Download Summary", sststats + gfsstats)
logging.progress("Symbolically linking into place")
all_files = list(sst_files) + list(gfs_files)
for file in all_files:
f = os.path.relpath(file, args.dir)
src = os.path.join(args.dir, f)
dst = hack_oisst_preliminary_filename(os.path.join(args.destination, f))
dst_dir = os.path.dirname(dst)
os.makedirs(dst_dir, exist_ok=True)
logging.info(src + ' -> ' + dst)
os.symlink(src, dst)
except csppfetch.DownloadsFailedException as e:
logging.fatal(str(e))
return 0
if __name__ == '__main__':
sys.exit(main())
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment