Compare commits

..

No commits in common. "9494b53105858d26ca83a0738a17be3f19875865" and "0e54cd0b141c346302895223ba2ba72dff6615da" have entirely different histories.

2 changed files with 13 additions and 14 deletions

View file

@ -109,6 +109,7 @@ 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)
@ -206,7 +207,7 @@ class Database():
self.db.execute(sql, values) self.db.execute(sql, values)
def query_sql(self, table, sql, values): def query_sql(self, table, sql, values=list()):
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, DatabaseOrder from xmet.db import Database, DatabaseTable
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(DatabaseTable): class Sounding():
__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,9 +124,7 @@ class Sounding(DatabaseTable):
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
@ -150,26 +148,25 @@ class Sounding(DatabaseTable):
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', DatabaseOrder.ASC]]).fetchall() }, ['elapsed']).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, *,
data_source_pressure, data_source_other, ST_Distance(location, MakePoint(:lon, :lat, {crs}),
ST_AsText(location) as location,
ST_Distance(location, MakePoint(:lon, :lat, {crs})) as distance
from from
xmet_sounding xmet_sounding
where where
timestamp_released <= :timestamp distance <= :radius
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)
@ -180,12 +177,13 @@ class Sounding(DatabaseTable):
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', DatabaseOrder.ASC]]).fetchall() }, ['elapsed']).fetchall()
return sounding return sounding