1+ import logging
2+ import csv
3+ from datetime import datetime
4+ import pandas as pd
5+ from pathlib import Path
6+ from astropy .io import fits
7+ from astrodb_utils .spectra import ingest_spectrum
8+ from astrodb_utils .instruments import ingest_instrument
9+ from astrodb_utils .publications import ingest_publication
10+ from simple import REFERENCE_TABLES
11+ from astrodb_utils import AstroDBError , load_astrodb
12+
13+ """
14+ This script ingest new created spectra FITS files into the SIMPLE database
15+ """
16+
17+ # Set the loggging level of the astrodb_utils logger
18+ astrodb_utils_logger = logging .getLogger ("astrodb_utils" )
19+ astrodb_utils_logger .setLevel (logging .INFO )
20+
21+ # Set up the logging for this ingest script.
22+ logger = logging .getLogger ("astrodb_utils.bones" )
23+ logger .setLevel (logging .INFO )
24+
25+ # Load Database
26+ SAVE_DB = False
27+ RECREATE_DB = True
28+ SCHEMA_PATH = "simple/schema.yaml"
29+ db = load_astrodb (
30+ "SIMPLE.sqlite" ,
31+ recreatedb = RECREATE_DB ,
32+ reference_tables = REFERENCE_TABLES ,
33+ felis_schema = SCHEMA_PATH ,
34+ )
35+
36+ # Paths
37+ spreadsheet_url = "https://docs.google.com/spreadsheets/d/e/2PACX-1vRtZ_Sl9hSi-JdIimRxbRLSTTYozlLOStmlzzcoAM7yB-duaMtzSqAIITI2ioMqlSIc6en8eiZDnUGe/pub?gid=0&single=true&output=csv"
38+ data = pd .read_csv (spreadsheet_url )
39+ fits_file = "/Users/guanying/SIMPLE db/SIMPLE-db/scripts/spectra_convert/BONES Archive/Processed BONES/"
40+ SKIP_FILES = [
41+ "2MASS J12270506-0447207" ,
42+ "WISEA J135501.90−825838.9" ,
43+ "ULAS J124947.04+095019.8" ,
44+ "SDSS J133837.01-022908.4" ,
45+ "LHS 377" ,
46+ "2MASS J16262034+3925190" ,
47+ "2MASS J06453153−6646120" ,
48+ "SDSS J010448.46+153501.8" ,
49+ "ULAS J020858.62+020657.0" ,
50+ "ULAS J021642.96+004005.7" ,
51+ "ULAS J024035.36+060629.3" ,
52+ "ULAS J130710.22+151103.4" ,
53+ "ULAS J135058.86+081506.8" ,
54+ "2MASS J14120397+1216100" ,
55+ "ULAS J151913.03-000030.0" ,
56+ "ULAS J223302.03+062030.8" ,
57+ "ULAS J230711.01+014447.1" ,
58+ ]
59+ def add_instruments ():
60+ INSTRUMENT = [
61+ {
62+ "name" : "BCSpec" ,
63+ "mode" : "spectroscopy" ,
64+ "telescope" : "LCO du Pont" ,
65+ },
66+ {
67+ "name" : "DSpec" ,
68+ "mode" :"spectroscopy" ,
69+ "telescope" : "Palomar Hale" ,
70+ },
71+ {
72+ "name" :"MOSFIRE" ,
73+ "mode" :"Imaging" ,
74+ "telescope" :"Keck I" ,
75+ },
76+ {
77+ "name" :"OSIRIS" ,
78+ "mode" :"Missing" ,
79+ "telescope" :"SOAR" ,
80+ }
81+ ]
82+ for inst in INSTRUMENT :
83+ try :
84+ ingest_instrument (
85+ db ,
86+ instrument = inst ["name" ],
87+ mode = inst ["mode" ],
88+ telescope = inst ["telescope" ]
89+ )
90+ except AstroDBError as e :
91+ logger .error (f"Failed to ingest instrument { inst ['name' ]} : { e } " )
92+
93+
94+ def add_publications ():
95+ try :
96+ ingest_publication (
97+ db ,
98+ reference = "Burg25.79" ,
99+ bibcode = "2025ApJ...982...79B" ,
100+ doi = "10.3847/1538-4357/adb39f" ,
101+ description = "New Cold Subdwarf Discoveries from Backyard Worlds and a Metallicity Classification System for T Subdwarfs"
102+ )
103+ except AstroDBError as e :
104+ logger .error (f"Failed to ingest publication Burg25.79: { e } " )
105+
106+ def modify_date (obs_date ):
107+ if not obs_date :
108+ return None
109+
110+ return datetime .fromisoformat (str (obs_date )) # example: 2004-04-17 04:40:11.761000
111+
112+ def add_access_url (filename ):
113+ filename = filename .replace (".txt" , "_SIMPLE.fits" )
114+ filename = filename .replace (".csv" , "_SIMPLE.fits" )
115+ filename = filename .replace ('+' , '%2B' )
116+ access_url = ("https://bdnyc.s3.us-east-1.amazonaws.com/bones/" + filename )
117+ return access_url
118+
119+ def ingest_spectra ():
120+ added_files = 0
121+ failed_files = 0
122+
123+ for _ , row in data .iloc [1 :83 ].iterrows ():
124+
125+ if row ['NAME' ] in SKIP_FILES :
126+ logger .info (f"Skipping { row ['Filename' ]} due to known issues." )
127+ continue
128+
129+ print (f"Processing { row ['NAME' ]} ..." )
130+
131+ filename = str (row ['Filename' ])
132+ access_url = add_access_url (filename )
133+ original_spectrum = filename # To be updated
134+
135+ obs_date = modify_date (row ['DATE-OBS' ])
136+
137+ try :
138+ ingest_spectrum (
139+ db ,
140+ source = row ['SIMPLE Name' ],
141+ spectrum = access_url ,
142+ # original_spectrum=original_spectrum,
143+ regime = row ['Regime' ],
144+ instrument = row ['INSTRUME' ],
145+ telescope = row ['TELESCOP' ],
146+ mode = row ['mode' ],
147+ obs_date = obs_date ,
148+ reference = row ['Reference' ],
149+ )
150+ logger .info (f"Ingesting spectrum for { row ['NAME' ]} ..." )
151+ print (f"Successfully ingest spectrum for { row ['NAME' ]} " )
152+ added_files += 1
153+ except AstroDBError as e :
154+ logger .error (f"Failed to ingest spectrum for { row ['NAME' ]} : { e } " )
155+ failed_files += 1
156+ print (f"Total spectra added: { added_files } " )
157+ print (f"Total failed spectra: { failed_files } " )
158+
159+ add_instruments ()
160+ add_publications ()
161+ ingest_spectra ()
162+
163+ if SAVE_DB :
164+ db .save_database (directory = "data/" )
165+ logger .info ("Database saved as SIMPLE.sqlite" )
0 commit comments