Compare commits

...

3 commits

2 changed files with 14 additions and 13 deletions

View file

@ -109,7 +109,6 @@ class Database():
db.enable_load_extension(True) db.enable_load_extension(True)
db.execute("select load_extension('mod_spatialite.so.8')") db.execute("select load_extension('mod_spatialite.so.8')")
db.execute("select InitSpatialMetadata(1)")
return Database(db) return Database(db)
@ -207,7 +206,7 @@ class Database():
self.db.execute(sql, values) self.db.execute(sql, values)
def query_sql(self, table, sql, values=list()): def query_sql(self, table, sql, values):
cr = DatabaseTableCursor(table, self.db.cursor()) cr = DatabaseTableCursor(table, self.db.cursor())
cr.execute(sql, values) cr.execute(sql, values)

View file

@ -1,7 +1,7 @@
import datetime import datetime
import shapely import shapely
from xmet.db import Database, DatabaseTable from xmet.db import Database, DatabaseTable, DatabaseOrder
from xmet.coord import COORD_SYSTEM from xmet.coord import COORD_SYSTEM
LAPSE_RATE_DRY = 9.8 # degrees C per 1000m LAPSE_RATE_DRY = 9.8 # degrees C per 1000m
@ -75,7 +75,7 @@ class SoundingSample(DatabaseTable):
return self.temp - (rate * (hd / 1000)) return self.temp - (rate * (hd / 1000))
class Sounding(): class Sounding(DatabaseTable):
__slots__ = ( __slots__ = (
'id', 'station', 'timestamp_observed', 'timestamp_released', 'id', 'station', 'timestamp_observed', 'timestamp_released',
'data_source_pressure', 'data_source_other', 'samples', 'location' 'data_source_pressure', 'data_source_other', 'samples', 'location'
@ -124,7 +124,9 @@ class Sounding():
timestamp: datetime.datetime=None): timestamp: datetime.datetime=None):
sql = """ sql = """
select select
* id, station, timestamp_observed, timestamp_released,
data_source_pressure, data_source_other,
ST_AsText(location) as location
from from
xmet_sounding xmet_sounding
where where
@ -148,25 +150,26 @@ class Sounding():
sounding = st.fetchone() sounding = st.fetchone()
sounding.samples = db.query(SoundingSample, { sounding.samples = db.query(SoundingSample, {
'sounding_id': sounding.id 'sounding_id': sounding.id
}, ['elapsed']).fetchall() }, [['elapsed', DatabaseOrder.ASC]]).fetchall()
return sounding return sounding
@staticmethod @staticmethod
def valid_by_location(db: Database, def valid_by_location(db: Database,
location: shapely.Point, location: shapely.Point,
radius: float,
timestamp: datetime.datetime): timestamp: datetime.datetime):
sql = """ sql = """
select select
*, id, station, timestamp_observed, timestamp_released,
ST_Distance(location, MakePoint(:lon, :lat, {crs}), data_source_pressure, data_source_other,
ST_AsText(location) as location,
ST_Distance(location, MakePoint(:lon, :lat, {crs})) as distance
from from
xmet_sounding xmet_sounding
where where
distance <= :radius timestamp_released <= :timestamp
and timestamp_released <= :timestamp
order by order by
distance desc,
timestamp_released desc timestamp_released desc
limit 1 limit 1
""".format(crs=COORD_SYSTEM) """.format(crs=COORD_SYSTEM)
@ -177,13 +180,12 @@ class Sounding():
st = db.query_sql(Sounding, sql, { st = db.query_sql(Sounding, sql, {
'lon': location.x, 'lon': location.x,
'lat': location.y, 'lat': location.y,
'radius': radius,
'timestamp': timestamp 'timestamp': timestamp
}) })
sounding = st.fetchone() sounding = st.fetchone()
sounding.samples = db.query(SoundingSample, { sounding.samples = db.query(SoundingSample, {
'sounding_id': sounding.id 'sounding_id': sounding.id
}, ['elapsed']).fetchall() }, [['elapsed', DatabaseOrder.ASC]]).fetchall()
return sounding return sounding