Source code for gfinder.commands.search

"""GFINDER CLI `search` command."""

import click
from .docstring import set_docstring

from gfinder.config import Config, DEFAULT_N_STEPS, DEFAULT_TIME_STEP
from gfinder.datastore import DataStore
from gfinder.opportunity import Opportunity, TimeInputs, OpportunityDefinition


@click.command(name='search', context_settings=dict(
    ignore_unknown_options=True,
    allow_extra_args=True,
))
@click.argument('odf_file')
@click.option('--opportunity-id', type=click.STRING, help='Identifier of an opportunity to used as input time window input (cascading search).')
@click.option('--start-time', type=click.STRING, help='Start time of the input time window (UTC), or relative start time string if --event-id is used.')
@click.option('--stop-time', type=click.STRING, help='Stop time of the input time window (UTC), or relative stop time string if --event-id is used.')
@click.option('--event-id', type=click.STRING, help='Identifier of the mission event (phase/segment) to be used as input time window (based on Mission Event file).')
@click.option('--n-steps', type=click.INT, help=f'Number of time steps used to derive time step (default: {DEFAULT_N_STEPS}).', default=DEFAULT_N_STEPS)
@click.option('--time-step', type=click.FLOAT, help=f'Time step, in seconds, to be used to bracket roots (default: {DEFAULT_TIME_STEP} sec.).', default=DEFAULT_TIME_STEP)
@click.option('--mission-scenario-id', type=click.STRING, help=f'Mission scenario ID (default: {Config().default_mission_scenario_id})', default=Config().default_mission_scenario_id)
@click.option('--target', type=click.STRING, help='Target name ODF overwrite.')
@click.option('--compute/--no-compute', help='Run observation geometry data computation for all opportunity windows.', default='False')
@click.option('--overwrite/--no-overwrite', help='Overwrite existing opportunity data files.', default='False')
@click.option('--suffix', type=click.STRING, help='Add suffix string to the searched opportunity ID.')
@click.pass_context
def search_cmd(ctx, odf_file, opportunity_id, start_time, stop_time, event_id, n_steps, time_step, mission_scenario_id, target, compute, overwrite, suffix):
    """Search observation opportunities.

    This command takes as input an Opportunity Definition File (ODF) file, containing the geometry condition(s) and
    geometry parameters corresponding to a user-defined observation opportunity.

    Search time window  can be provided as timing inputs (using start_time, stop_time, event_name options), or as an
    'opportunity' resulting from a previous search.

    It produces a set of output geometry event (GeoEvent) files in JSON format bundled in an 'opportunity' directory.
    It also produces companion GeoJSON files when applicable. The identifier of an opportunity is automatically
    assigned.
    """

    # Retrieve extra arguments to be passed to the command function.
    kwargs = []
    for arg in ctx.args:
        pair = arg[2:].split('=')
        key = pair[0].replace('-', '_')
        try:
            val = float(pair[1])
        except:
            val = pair[1]
        kwargs.append((key,val))
    kwargs = dict(kwargs)

    search(odf_file, opportunity_id=opportunity_id, start_time=start_time, stop_time=stop_time, event_id=event_id,
           n_steps=n_steps, time_step=time_step, mission_scenario_id=mission_scenario_id, target=target, compute=compute,
           overwrite=overwrite, suffix=suffix, **kwargs)