spencerahill / aospy

Python package for automated analysis and management of gridded climate data
Apache License 2.0
83 stars 12 forks source link

Error when passing non-default date ranges to `calc_suite_specs` #295

Closed jdossgollin closed 5 years ago

jdossgollin commented 5 years ago

When I pass a custom date range, I get an error TypeError: 'datetime.datetime' object is not subscriptable. It's coming from calc.py:

--> 168             self.start_date = utils.times.ensure_datetime(date_range[0])
    169             self.end_date = utils.times.ensure_datetime(date_range[-1])
    170 

Here's an MWE:

from aospy import submit_mult_calcs
from aospy.examples import example_obj_lib as lib
from datetime import datetime
sdate = datetime(4, 1, 1)
edate = datetime(6, 12, 31)
calc_suite_specs = dict(
    library=lib,
    projects=[lib.example_proj],
    models=[lib.example_model],
    runs=[lib.example_run],
    variables=[lib.precip_largescale, lib.precip_convective, lib.precip_total, lib.precip_conv_frac],
    regions='all',
    date_ranges=(sdate, edate),
    output_time_intervals=['ann'],
    output_time_regional_reductions=['av', 'reg.av', 'ts', 'std', 'reg.ts', 'reg.std'],
    output_vertical_reductions=[None],
    input_time_intervals=['monthly'],
    input_time_datatypes=['ts'],
    input_time_offsets=[None],
    input_vertical_datatypes=[False],
)
calc_exec_options = dict(
    prompt_verify=False,
    parallelize=False,
    write_to_tar=False
)
calcs = submit_mult_calcs(calc_suite_specs, calc_exec_options)

and the full error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-a5be8177bb73> in <module>
     25     write_to_tar=False
     26 )
---> 27 calcs = submit_mult_calcs(calc_suite_specs, calc_exec_options)

/usr/local/miniconda3/envs/gcm-lfv/lib/python3.6/site-packages/aospy/automate.py in submit_mult_calcs(calc_suite_specs, exec_options)
    467         _user_verify()
    468     calc_suite = CalcSuite(calc_suite_specs)
--> 469     calcs = calc_suite.create_calcs()
    470     return _exec_calcs(calcs, **exec_options)

/usr/local/miniconda3/envs/gcm-lfv/lib/python3.6/site-packages/aospy/automate.py in create_calcs(self)
    239         """Generate a Calc object for each requested parameter combination."""
    240         return [Calc(CalcInterface(**sp)) for sp in
--> 241                 self._combine_core_aux_specs()]
    242 
    243 

/usr/local/miniconda3/envs/gcm-lfv/lib/python3.6/site-packages/aospy/automate.py in <listcomp>(.0)
    238     def create_calcs(self):
    239         """Generate a Calc object for each requested parameter combination."""
--> 240         return [Calc(CalcInterface(**sp)) for sp in
    241                 self._combine_core_aux_specs()]
    242 

/usr/local/miniconda3/envs/gcm-lfv/lib/python3.6/site-packages/aospy/calc.py in __init__(self, proj, model, run, ens_mem, var, date_range, region, intvl_in, intvl_out, dtype_in_time, dtype_in_vert, dtype_out_time, dtype_out_vert, level, time_offset)
    165                 self.run.default_end_date)
    166         else:
--> 167             self.start_date = utils.times.ensure_datetime(date_range[0])
    168             self.end_date = utils.times.ensure_datetime(date_range[-1])
    169 

TypeError: 'datetime.datetime' object is not subscriptable
spencerkclark commented 5 years ago

@jdossgollin thanks for the issue and nice example -- it looks like our documentation is indeed wrong here. If you're up for it, a pull request to fix it would be welcome!

Changing date_ranges=(sdate, edate) to date_ranges=[(sdate, edate)] should work. Aospy treats a tuple of dates as a single "date range." So much like listing a number of runs or variables to iterate over, one can list a number of date ranges to iterate over too, e.g. date_ranges=[(sdate1, edate1), (sdate2, edate2)]. When specifying a single date range at the moment, one needs to enclose the tuple in a list.

jdossgollin commented 5 years ago

@spencerkclark sounds good. This will be my first PR on someone else's project so apologies in advance if I make a mess of it đŸ˜„ .

spencerkclark commented 5 years ago

No worries -- we're happy to help out if you hit any snags!

jdossgollin commented 5 years ago

@spencerkclark which branch should I try to merge into? Thanks,

spencerkclark commented 5 years ago

Good question -- we use the develop branch as our working branch.

spencerahill commented 5 years ago

Thanks @jdossgollin for catching this and the other things you've brought to our attention. As @spencerkclark said, don't worry at all about being new. We're 100% happy (thrilled even) to get any question, bug reports, and PRs, from anybody.