#!/usr/bin/env python3 """ Program to read astronomical observation data and convert observation dates to Julian Day. Uses astropy Table for simple and efficient data handling. Authors: Tõnis Eenmäe and Claude.ai """ import sys from astropy.table import Table from astropy.time import Time from astropy.io import ascii import astropy.units as u def convert_observations_to_jd(input_file, output_file=None): """ Read observation data and convert dates to Julian Day. Parameters: ----------- input_file : str Path to input file containing observation data output_file : str, optional Path to output file. If None, prints to stdout """ # Read the table (astropy automatically handles the parsing) table = ascii.read(input_file, names=['Object', 'Date', 'Time', 'Mag', 'Error', 'Band', 'OID'], data_start=1) # Print the input for debug, comment out when not needed print("Input table is:") print(table) # Combine date and time columns into datetime strings datetime_strings = [f"{date} {time}" for date, time in zip(table['Date'], table['Time'])] # Convert to Time objects (in UTC) and extract Julian Day times = Time(datetime_strings, format='iso', scale='utc') table['JD'] = times.jd # Create output table with desired columns (comment corresponding line out when not needed) output_table = Table() output_table['Object'] = table['Object'] output_table['JD'] = table['JD'] #output_table['Date'] = table['Date'] #output_table['Time'] = table['Time'] output_table['Mag'] = table['Mag'] output_table['Error'] = table['Error'] output_table['Band'] = table['Band'] output_table['OID'] = table['OID'] # Limit JD in the table to 5 decimals, change the number between . and f when needed output_table['JD'].format = '.5f' # Write or print the output, to change field delimiter, change to ' ' for space if output_file: output_table.write(output_file, format='ascii', delimiter=',', overwrite=True) print(f"Output written to {output_file}") else: print("Output table is:") output_table.pprint_all() return def main(): if len(sys.argv) < 2: print("Usage: python convert_to_jd_simple.py [output_file]") print("\nExample:") print(" python convert_to_jd_simple.py observations.txt") print(" python convert_to_jd_simple.py observations.txt output.txt") sys.exit(1) input_file = sys.argv[1] output_file = sys.argv[2] if len(sys.argv) > 2 else None convert_observations_to_jd(input_file, output_file) if __name__ == "__main__": main()