Source code for gfinder.commands.ptrgen

"""GFINDER CLI `ptrgen` command."""

import click
from datetime import datetime, timedelta
from pathlib import Path

from .docstring import set_docstring

from gfinder.config import Config
from gfinder.datastore import DataStore
from gfinder.ptr_pointing import PTRPointing
from ptr.datetime import dt as ptr_datetime

DEFAULT_START_TIME = '2031-01-19'

@click.command(name='ptrgen', context_settings=dict(
    ignore_unknown_options=True,
    allow_extra_args=True,
))
@click.argument('ptr_pointing_type')
@click.option('--start-time', type=click.STRING, help=f'Start time of the pointing block (UTC). Default is {DEFAULT_START_TIME}.')
@click.option('--stop-time', type=click.STRING, help=f'Stop time of the pointing block (UTC). Default is {DEFAULT_START_TIME} plus 10 minutes.')
@click.option('--metadata', type=click.STRING, help='Additional pointing block metadata (free text).')
@click.option('--params/--no-params', help='Show pointing parameters for a given PTR pointing type.', default='False')
@click.option('--agm-validation/--no-agm-validation', help='Validate generated PRM using AGM.', default='False')
@click.option('--mission-scenario-id', type=click.STRING, help='Mission scenario ID to be used for AGM validation (default: '+Config().default_mission_scenario_id+')')
@click.pass_context
def ptrgen_cmd(ctx, ptr_pointing_type, start_time, stop_time, metadata, params, agm_validation, mission_scenario_id):
    """(beta) Generate a PTR file based on a pre-defined "PTR Pointing Type", and given a set of required
    pointing parameters.

    Use --params to show "pointing parameters" for a given PTR pointing type.

    Examples:

    \b
        $ gfinder ptrgen juice_jupiter_ring --params
        $ gfinder ptrgen juice_jupiter_ring --fixed_offset_x_angle=12.6
        $ gfinder ptrgen juice_jupiter_ring --fixed_offset_x_angle=12.6 --agm-validation

    See Also:

    \b
        - Use the `ptr-pointings` CLI command to list all available PTR Pointing Types, and associated required
          pointing parameters.
        - Use the `export` CLI command to export an opportunity definition to a PTR file, optionally AGM-validated.
    """

    # 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)

    try:
        ptrgen(
            ptr_pointing_type,
            start_time=start_time,
            stop_time=stop_time,
            metadata=metadata,
            params=params,
            agm_validation=agm_validation,
            mission_scenario_id=mission_scenario_id,
            **kwargs
        )
    except Exception as e:
        print(e)
        print('Use --params option to list the PTR pointing parameters for a given type.')


[docs]@set_docstring(ptrgen_cmd, {'ptr_pointing_type': 'PTR pointing type (eg: `juice_jupiter_ring`.'}) def ptrgen(ptr_pointing_type, start_time=None, stop_time=None, metadata=None, params=False, agm_validation=False, mission_scenario_id=None, **kwargs): if not start_time: start_time = DEFAULT_START_TIME start_time = ptr_datetime(start_time) if not stop_time: stop_time = start_time + timedelta(minutes=10) if not metadata: metadata = f'MAJIS MOS `ptrgen` CLI command/function output, {datetime.now().strftime("%d-%m-%Y %H:%M:%S")}' if params: ptr_pointing = PTRPointing(ptr_pointing_type=ptr_pointing_type, mandatory=False, **kwargs) print(ptr_pointing) print() else: ptr_pointing = PTRPointing(ptr_pointing_type=ptr_pointing_type, **kwargs) prm = ptr_pointing.get_prm(start_time, stop_time, metadata=metadata) print(prm) print() if agm_validation: # get selected mission scenario if not mission_scenario_id: mission_scenario_id = Config().default_mission_scenario_id mission_scenario = DataStore().getMissionScenario(mission_scenario_id) if not mission_scenario: print( f'Invalid {mission_scenario_id} mission scenario. Use `scenarios` command to list available mission' f'scenarios, defined scenarios/mission_scenarios.json file.') return # derive metakernel ID from path metakernel_id = Path(mission_scenario.spice_metakernel).stem # set AGM REST API endpoint endpoint = 'JUICE_API' # run AGM validation print(f'Validating PTR using AGM REST API: metakernel={metakernel_id}, endpoint={endpoint}...') results = prm.simulate(metakernel_id, endpoint) # report on results if results.success: print('-> VALID') else: print('-> INVALID') print('\n' + repr(results.log) + '\n')