Source code for gfinder.commands.definitions

"""GFINDER CLI `definitions` command."""

import click
from .docstring import set_docstring

from gfinder.datastore import DataStore

@click.command(name='definitions')
@click.option('--obs-type', type=click.STRING, help='Filter by observation type')
@click.option('--target', type=click.STRING, help='Filter by target name')
def definitions_cmd(obs_type, target):
    """List available observation opportunity definitions (ODF files), grouped by observation type.
    """
    definitions(obs_type=obs_type, target=target)


[docs]@set_docstring(definitions_cmd) def definitions(obs_type=None, target=None): # Init data store datastore = DataStore() opportunity_definitions_dicts = datastore.get_definitions(obs_type=obs_type, target=target) n_definitions = len(opportunity_definitions_dicts) rogue_opportunity_definitions_dicts = [] if n_definitions > 0: if n_definitions == 1: click.echo('One opportunity definition file (ODF) matching input filter:') else: click.echo(f'{n_definitions} opportunity definition files (ODF) matching input filter:') click.echo() # List valid definitions, grouped by observation type. observation_types = [] for observation_types_dict in datastore.observation_types: observation_types.append(observation_types_dict['mnemonic']) valid_opportunity_definitions_dicts = [] rogue_opportunity_definitions_dicts = [] for opportunity_definition_dict in opportunity_definitions_dicts: if opportunity_definition_dict['observation_type'] in observation_types: # and valid # TODO: validate that this opportunity definition is an instance of master ODF. valid_opportunity_definitions_dicts.append(opportunity_definition_dict) else: rogue_opportunity_definitions_dicts.append(opportunity_definition_dict) click.echo(f' {"Observation Type":<35} {"ODF Path":<40} {"Target":<10} {"Observer":<10} {"Detector":<20} {"Pointing ODF":<40} {"PTR Pointing Type":<20}') click.echo(f' {"-" * 35} {"-" * 40} {"-" * 10} {"-" * 10} {"-" * 20} {"-" * 40} {"-" * 20}') for observation_types_dict in datastore.observation_types: i = 0 for valid_opportunity_definitions_dict in valid_opportunity_definitions_dicts: if observation_types_dict['mnemonic'] == valid_opportunity_definitions_dict['observation_type']: master_odf_str = '' if i == 0: obs_type_str = valid_opportunity_definitions_dict["observation_type"] else: obs_type_str = '' i += 1 if valid_opportunity_definitions_dict["odf_path"] == observation_types_dict['master_odf_file']: master_odf_str = ' (master)' click.echo(f' {obs_type_str:<35} ' f'{valid_opportunity_definitions_dict["odf_path"] + master_odf_str:<40} ' f'{valid_opportunity_definitions_dict["target"]:<10} ' f'{valid_opportunity_definitions_dict["observer"]:<10} ' f'{valid_opportunity_definitions_dict["detector"]:<20} ' f'{valid_opportunity_definitions_dict["pointing_odf"]:<40} ' f'{valid_opportunity_definitions_dict["ptr_pointing_type"]:<20}') else: click.echo('No opportunity definition files (ODF) matching input filter.') if len(rogue_opportunity_definitions_dicts) > 0: click.echo() click.echo(' No associated observation type:') for rogue_opportunity_definitions_dict in rogue_opportunity_definitions_dicts: click.echo(f' {rogue_opportunity_definitions_dict["observation_type"]:<35} ' f'{rogue_opportunity_definitions_dict["odf_path"]:<40} ' f'{rogue_opportunity_definitions_dict["target"]:<10} ' f'{rogue_opportunity_definitions_dict["observer"]:<10} ' f'{rogue_opportunity_definitions_dict["detector"]:<20}') click.echo()