Skip to content

Commit

Permalink
Reworking how ingest_spectrum works using the ORM (#501)
Browse files Browse the repository at this point in the history
* First pass at reworking how ingest_spectrum works using the ORM

* Fixing some issues in unit tests

* Partial implementation of new flags response

* Iterating on error message handling for ingest_spectrum

* Reworking tests to be more compact

* Expanding validation to check for missing telescope, instrument, and mode

* Minor cleanup

* Apply suggestions from code review

Co-authored-by: Kelle Cruz <[email protected]>

* Update simple/utils/spectra.py

Co-authored-by: Kelle Cruz <[email protected]>

* Adding warning when converting date into ISO format

---------

Co-authored-by: Kelle Cruz <[email protected]>
  • Loading branch information
dr-rodriguez and kelle authored Jul 16, 2024
1 parent 3fe432f commit 6df3d0e
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 371 deletions.
38 changes: 29 additions & 9 deletions simple/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import enum
from datetime import datetime

import sqlalchemy as sa
from astrodbkit2.astrodb import Base
Expand Down Expand Up @@ -344,28 +345,30 @@ class Gravities(Base):
class Spectra(Base):
# Table to store references to spectra
__tablename__ = 'Spectra'

source = Column(
String(100),
ForeignKey("Sources.source", ondelete="cascade", onupdate="cascade"),
nullable=False,
primary_key=True,
)

# Data
access_url = Column(String(1000), nullable=False) # URL of spectrum location
original_spectrum = Column(
String(1000)
) # URL of original spectrum location, if applicable
local_spectrum = Column(
String(1000)
) # local directory (via environment variable) of spectrum location

# URL of original spectrum location, if applicable
original_spectrum = Column(String(1000))
# local directory (via environment variable) of spectrum location
local_spectrum = Column(String(1000))

regime = Column(
String(30),
ForeignKey("Regimes.regime", ondelete="cascade", onupdate="cascade"),
primary_key=True,
)
telescope = Column(String(30))
instrument = Column(String(30))
mode = Column(String(30)) # eg, Prism, Echelle, etc
telescope = Column(String(30), nullable=False)
instrument = Column(String(30), nullable=False)
mode = Column(String(30), nullable=False) # eg, Prism, Echelle, etc
observation_date = Column(DateTime, primary_key=True)

# Common metadata
Expand All @@ -387,6 +390,23 @@ class Spectra(Base):
{},
)

@validates("access_url", "regime", "source", "telescope", "instrument", "mode")
def validate_required(self, key, value):
if value is None:
raise ValueError(f"Value required for {key}")
return value

@validates("observation_date")
def validate_date(self, key, value):
if value is None:
raise ValueError(f"Invalid date received: {value}")
elif not isinstance(value, datetime):
# Convert to datetime for storing in the database
# Will throw error if unable to convert
print("WARNING: Value will be converted to ISO format.")
value = datetime.fromisoformat(value)
return value


class ModeledParameters(Base):
# Table to store derived/inferred paramaters from models
Expand Down
Loading

0 comments on commit 6df3d0e

Please sign in to comment.