|
| 1 | +import typer |
| 2 | +from typing import Optional, List |
| 3 | +from astropy.table import Table as AstropyTable |
| 4 | +from astroquery.eso import Eso |
| 5 | +from ..i18n import get_translator |
| 6 | +from ..utils import ( |
| 7 | + console, |
| 8 | + display_table, |
| 9 | + handle_astroquery_exception, |
| 10 | + common_output_options, |
| 11 | + save_table_to_file, |
| 12 | + global_keyboard_interrupt_handler, |
| 13 | +) |
| 14 | +import os |
| 15 | +import re |
| 16 | +from io import StringIO |
| 17 | +from contextlib import redirect_stdout |
| 18 | +from astroquery_cli.common_options import setup_debug_context |
| 19 | +from astroquery_cli.debug import debug |
| 20 | + |
| 21 | +def get_app(): |
| 22 | + import builtins |
| 23 | + _ = builtins._ |
| 24 | + app = typer.Typer( |
| 25 | + name="eso", |
| 26 | + help=builtins._("Query the European Southern Observatory (ESO) archive."), |
| 27 | + invoke_without_command=True, |
| 28 | + no_args_is_help=False |
| 29 | + ) |
| 30 | + |
| 31 | + @app.callback() |
| 32 | + def eso_callback( |
| 33 | + ctx: typer.Context, |
| 34 | + debug: bool = typer.Option( |
| 35 | + False, |
| 36 | + "-t", |
| 37 | + "--debug", |
| 38 | + help=_("Enable debug mode with verbose output."), |
| 39 | + envvar="AQC_DEBUG" |
| 40 | + ), |
| 41 | + verbose: bool = typer.Option( |
| 42 | + False, |
| 43 | + "-v", |
| 44 | + "--verbose", |
| 45 | + help=_("Enable verbose output.") |
| 46 | + ) |
| 47 | + ): |
| 48 | + setup_debug_context(ctx, debug, verbose) |
| 49 | + |
| 50 | + if ctx.invoked_subcommand is None and \ |
| 51 | + not any(arg in ["-h", "--help"] for arg in ctx.args): |
| 52 | + help_output_capture = StringIO() |
| 53 | + with redirect_stdout(help_output_capture): |
| 54 | + try: |
| 55 | + app(ctx.args + ["--help"]) |
| 56 | + except SystemExit: |
| 57 | + pass |
| 58 | + full_help_text = help_output_capture.getvalue() |
| 59 | + |
| 60 | + commands_match = re.search(r'╭─ Commands ─.*?(\n(?:│.*?\n)*)╰─.*─╯', full_help_text, re.DOTALL) |
| 61 | + if commands_match: |
| 62 | + commands_section = commands_match.group(0) |
| 63 | + filtered_commands_section = "\n".join([ |
| 64 | + line for line in commands_section.splitlines() if "Usage:" not in line |
| 65 | + ]) |
| 66 | + console.print(filtered_commands_section) |
| 67 | + else: |
| 68 | + console.print(full_help_text) |
| 69 | + raise typer.Exit() |
| 70 | + |
| 71 | + @app.command(name="query", help=builtins._("Perform a query on ESO archive.")) |
| 72 | + @global_keyboard_interrupt_handler |
| 73 | + def query_eso( |
| 74 | + ctx: typer.Context, |
| 75 | + object_name: Optional[str] = typer.Option(None, help=builtins._("Object name to query (e.g., 'M31').")), |
| 76 | + ra: Optional[float] = typer.Option(None, help=builtins._("Right Ascension in degrees.")), |
| 77 | + dec: Optional[float] = typer.Option(None, help=builtins._("Declination in degrees.")), |
| 78 | + radius: Optional[float] = typer.Option(None, help=builtins._("Search radius in degrees (for cone search).")), |
| 79 | + instrument: Optional[str] = typer.Option(None, help=builtins._("Instrument name (e.g., 'FORS2').")), |
| 80 | + output_file: Optional[str] = common_output_options["output_file"], |
| 81 | + output_format: Optional[str] = common_output_options["output_format"], |
| 82 | + max_rows_display: int = typer.Option( |
| 83 | + 25, help=builtins._("Maximum number of rows to display. Use -1 for all rows.") |
| 84 | + ), |
| 85 | + show_all_columns: bool = typer.Option( |
| 86 | + False, "--show-all-cols", help=builtins._("Show all columns in the output table.") |
| 87 | + ), |
| 88 | + ): |
| 89 | + if ctx.obj.get("DEBUG"): |
| 90 | + debug(f"query_eso - object_name: {object_name}, ra: {ra}, dec: {dec}, radius: {radius}, instrument: {instrument}") |
| 91 | + |
| 92 | + console.print(f"[cyan]{_('Querying ESO archive...')}[/cyan]") |
| 93 | + |
| 94 | + try: |
| 95 | + eso = Eso() |
| 96 | + results = None |
| 97 | + |
| 98 | + if object_name: |
| 99 | + results = eso.query_object(object_name=object_name, instrument=instrument) |
| 100 | + elif ra is not None and dec is not None: |
| 101 | + if radius is None: |
| 102 | + console.print(_("[red]Error: --radius is required when --ra and --dec are provided for a cone search.[/red]")) |
| 103 | + raise typer.Exit(code=1) |
| 104 | + results = eso.query_region(ra=ra, dec=dec, radius=radius, instrument=instrument) |
| 105 | + else: |
| 106 | + console.print(_("[red]Error: Please provide either --object-name or both --ra and --dec (with --radius).[/red]")) |
| 107 | + raise typer.Exit(code=1) |
| 108 | + |
| 109 | + if results and len(results) > 0: |
| 110 | + console.print(_("[green]Found {count} result(s) from ESO.[/green]").format(count=len(results))) |
| 111 | + display_table(ctx, results, title=_("ESO Query Results"), max_rows=max_rows_display, show_all_columns=show_all_columns) |
| 112 | + if output_file: |
| 113 | + save_table_to_file(ctx, results, output_file, output_format, _("ESO query")) |
| 114 | + else: |
| 115 | + console.print(_("[yellow]No results found for your ESO query.[/yellow]")) |
| 116 | + |
| 117 | + except Exception as e: |
| 118 | + handle_astroquery_exception(ctx, e, _("ESO query")) |
| 119 | + raise typer.Exit(code=1) |
| 120 | + |
| 121 | + @app.command(name="list-instruments", help=builtins._("List available ESO instruments.")) |
| 122 | + @global_keyboard_interrupt_handler |
| 123 | + def list_instruments(ctx: typer.Context): |
| 124 | + console.print(f"[cyan]{_('Listing ESO instruments...')}[/cyan]") |
| 125 | + try: |
| 126 | + instruments = Eso.list_instruments() |
| 127 | + if instruments: |
| 128 | + console.print(_("[green]Available ESO Instruments:[/green]")) |
| 129 | + for i in instruments: |
| 130 | + console.print(f"- {i}") |
| 131 | + else: |
| 132 | + console.print(_("[yellow]No ESO instruments found.[/yellow]")) |
| 133 | + except Exception as e: |
| 134 | + handle_astroquery_exception(ctx, e, _("ESO list instruments")) |
| 135 | + raise typer.Exit(code=1) |
| 136 | + |
| 137 | + return app |
0 commit comments