Commit 68965175 authored by Clément Haëck's avatar Clément Haëck
Browse files

Upgrade get_args

Add doc.
Date is stored as datetime. Adapt scripts using it.
parent 52181308
......@@ -25,7 +25,7 @@ def main():
def merge_pigments(args):
log.info('finding pigments')
odir = path.join(root_data, args['region'], 'SOM', 'Pig',
'{:d}days'.format(1), *args['date_str'])
'{:d}days'.format(1), *args['date_str'][:2])
idir = path.join(odir, 'tmp')
pregex = r'%(prefix)_%(time:x)_image_%(image:custom=\d\d\d:)\.nc%(var:char)\.nc'
finder = FileFinder(idir, pregex, prefix='Pig_BMUS_A')
......@@ -33,7 +33,7 @@ def merge_pigments(args):
log.info('found %d pigment files', len(files))
images_all = lib.data.images.get_images_list(region=args['region'],
year=args['date'][0],
year=args['date'].year,
fixes=args['fix'])
images_grp = lib.data.images.regroup_by_file(images_all)
log.info('found %d images', len(images_all))
......
......@@ -141,7 +141,7 @@ def get_finder(data_type, args):
def get_data_dir(data_type, args):
return path.join(root_data, args['region'], data_type,
'{:d}days'.format(1), *args['date_str'])
'{:d}days'.format(1), *args['date_str'][:2])
if __name__ == '__main__':
......
......@@ -88,7 +88,7 @@ def get_l2_filelist(args) -> FileFinder:
def get_data_dir(args):
return path.join(root_data, args['region'], args['data_type']+'_MODIS',
'{:d}days'.format(1), *args['date_str'])
'{:d}days'.format(1), *args['date_str'][:2])
def is_in_bounds(file) -> bool:
......
......@@ -38,7 +38,7 @@ def download_data(args):
log.info("found %d links", n_links)
odir = path.join(root_data, args['region'], args['data_type']+'_MODIS',
'1days', *args['date_str'], 'swaths')
'1days', *args['date_str'][:2], 'swaths')
check_output_dir(odir, log)
link_file = path.join(odir, 'download_list.txt')
......@@ -72,7 +72,7 @@ def download_data(args):
def get_links(args):
date_start = datetime(*args['date'], 1)
date_start = args['date']
date_end = date_start + relativedelta(days=1) - relativedelta(seconds=1)
if args['region'] == 'GS':
......
"""General global variables."""
import argparse
import configparser
from datetime import datetime
import os
from os import path
import re
import argparse
import re
# Read config file
_config = configparser.ConfigParser()
_config_file = path.join(path.dirname(__file__), 'conf.ini')
......@@ -20,11 +21,16 @@ if not path.isdir(root_data):
raise NameError(f"root_data '{root_data}' is not a valid directory.")
root_plot = path.join(root_data, 'Plots')
box_lat = [36., 41.]
box_lon = [-71., -62.]
def check_output_dir(odir, log=None):
"""Check if directory exists, if not create it.
Parameters
----------
odir: str
log: logging.Logger instance
If None, no log will be output.
"""
if log is not None:
log.info('output to %s', odir)
if not path.isdir(odir):
......@@ -34,7 +40,33 @@ def check_output_dir(odir, log=None):
def get_args(args, description='', add_args=None):
"""Get command line arguments.
With lots of typical arguments.
Arguments prefixed by a single hyphen:
`python script.py -region GS -year 2007 -fix d 01`
Returns a dictionnary of arguments.
Parameters
----------
args: list of str
List of typical arguments. Can include: 'data_type' (default 'OC'),
'region' (default 'GS'), 'year' (default '2007'),
'date' (default '200701'), 'fix'.
Those (and other arguments) will be added to the resulting dictionnary.
`dargs[arg] = vars(parser.parse_args())[arg][0]`.
'date' will be transformed into argument 'date': a datetime instance,
and 'date_str', a tuple of strings (year, month, day).
Specifying day is optional.
There can be any number of 'fix' arguments. Each is a couple of
key (int/str) and value (str). Each is appended to dictionnary of fixes.
See xarray_regex.FileFinder.fix_matchers().
add_args: callable
Function that take an argparse.ArgumentParser and add arguments to it.
"""
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter,
description=description)
......@@ -64,9 +96,12 @@ def get_args(args, description='', add_args=None):
dargs['fix'] = fix
if 'date' in dargs:
date = re.match(r'(\d\d\d\d)[-\s/]?(\d\d)(\d\d)?',
date = re.match(r'(\d\d\d\d)[-\s/]?(\d\d)[-\s/]?(\d\d)?',
dargs['date'][0])
dargs['date_str'] = [(date.group(i+1)) for i in range(2)]
dargs['date'] = [int(d) for d in dargs['date_str']]
if date is None:
raise KeyError('-date argument not in correct format (YYYY[- /]MM[- /][DD])')
dargs['date_str'] = [g if g is not None else '01'
for g in date.groups()]
dargs['date'] = datetime(*[int(g) for g in dargs['date_str']])
return dargs
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment