Skip to content

Commit

Permalink
Merge pull request #65 from opengisch/refactor
Browse files Browse the repository at this point in the history
Refactor Grid3D and related modules
  • Loading branch information
suricactus authored Mar 26, 2024
2 parents 36bfea8 + 0838a2a commit 708dc07
Show file tree
Hide file tree
Showing 14 changed files with 474 additions and 1,442 deletions.
11 changes: 8 additions & 3 deletions open_alaqs/core/alaqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from open_alaqs.core import alaqsdblite, alaqsutils
from open_alaqs.core.alaqslogging import get_logger
from open_alaqs.core.tools.create_output import create_alaqs_output
from open_alaqs.core.tools.sql_interface import SqlExpression, update_table
from open_alaqs.typing import AirportDict, StudySetup

logger = get_logger(__name__)
Expand Down Expand Up @@ -72,10 +73,11 @@ def load_study_setup() -> StudySetup:
sqlite3.Row - resulting user study setup
"""
return alaqsdblite.execute_sql(
alaqsdblite.ProjectDatabase().path,
"""
SELECT *
FROM user_study_setup
"""
""",
)


Expand All @@ -99,11 +101,12 @@ def save_study_setup(study_setup: StudySetup) -> None:
if param == "":
raise Exception("Study setup parameters cannot be blank")

alaqsdblite.update_table(
update_table(
alaqsdblite.ProjectDatabase().path,
"user_study_setup",
{
**study_setup,
"date_modified": alaqsdblite.SqlExpression("DATETIME('now')"),
"date_modified": SqlExpression("DATETIME('now')"),
},
{
"airport_id": study_setup["airport_id"],
Expand All @@ -115,6 +118,7 @@ def save_study_setup(study_setup: StudySetup) -> None:
def get_airport_codes() -> list[str]:
"""Return a list of airport ICAO codes"""
return alaqsdblite.execute_sql(
alaqsdblite.ProjectDatabase().path,
"""
SELECT airport_code
FROM default_airports
Expand All @@ -135,6 +139,7 @@ def airport_lookup(icao_code: str) -> Optional[AirportDict]:
AirportDict | None: airport data
"""
return alaqsdblite.execute_sql(
alaqsdblite.ProjectDatabase().path,
"""
SELECT *
FROM default_airports
Expand Down
107 changes: 3 additions & 104 deletions open_alaqs/core/alaqsdblite.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import shutil
import sqlite3 as sqlite
from pathlib import Path
from typing import Any, Union

import pandas as pd
from qgis.utils import spatialite_connect

from open_alaqs.alaqs_config import ALAQS_ROOT_PATH, ALAQS_TEMPLATE_DB_FILENAME
from open_alaqs.core import alaqsutils
from open_alaqs.core.alaqslogging import get_logger
from open_alaqs.core.tools.sql_interface import execute_sql

logger = get_logger(__name__)

Expand Down Expand Up @@ -186,114 +186,13 @@ def create_project_database(alaqs_db_filename: str) -> None:

# Update the study created date to now
execute_sql(
alaqs_db_filename,
"""
UPDATE user_study_setup SET date_created = DATETIME('now')
"""
""",
)


def execute_sql(
sql: str, params: list[Any] = [], fetchone: bool = True
) -> Union[dict, list]:
"""Executes provided SQL query.
Args:
sql (str): SQL query to be executed
params (list[Any]): SQL query parameters. The list size should match the number of `?` placeholders in the sql query.
fetchone (bool): whether to expect maximum one row or to return all rows. Default: True
Returns:
list: all rows that are returned by the query
"""

try:
# Tidy up the string a bit. Mainly cosmetic for log file
sql = sql.replace(" ", "")

with connect_to_alaqs_db() as conn:
conn.text_factory = str
conn.row_factory = sqlite.Row
cur = conn.cursor()

cur.execute(sql, params)
conn.commit()

if fetchone:
result = cur.fetchone()

if result is None:
return None
else:
return dict(result)
else:
return cur.fetchall()

except Exception as e:
if "no results to fetch" in str(e):
logger.debug('INFO: Query "%s" executed successfully' % sql)
else:
alaqsutils.print_error(query_string.__name__, Exception, e, log=logger)


def quote_identifier(identifier: str) -> str:
return f'''"{identifier.replace('"', '""')}"'''


class SqlExpression:
def __init__(self, expression: str, *values: Any) -> None:
self.expression = expression
self.values = values

def __str__(self) -> str:
return self.expression


def update_table(
table_name: str,
attribute_values: dict[str, Any],
where_values: dict[str, Any],
) -> list[sqlite.Row]:
value_pairs = []
values = []

for attr_name, attr_value in attribute_values.items():
if isinstance(attr_value, SqlExpression):
expression = attr_value.expression
values += attr_value.values
else:
expression = "?"
values.append(attr_value)

value_pairs.append(f"{quote_identifier(attr_name)} = {expression}")

values_str = ", ".join(value_pairs)
sql = f"""
UPDATE {quote_identifier(table_name)}
SET {values_str}
"""

if where_values:
where_value_pairs = []

for attr_name, attr_value in where_values.items():
if isinstance(attr_value, SqlExpression):
expression = attr_value.expression
values += attr_value.values
else:
expression = "?"
values.append(attr_value)

where_value_pairs.append(f"{quote_identifier(attr_name)} = {expression}")

where_values_str = " AND ".join(where_value_pairs)

sql += f"""
WHERE {where_values_str}
"""

return execute_sql(sql, values)


def query_string(sql_text: str) -> list:
"""
A specific query for accessing project databases. Checks the query
Expand Down
118 changes: 0 additions & 118 deletions open_alaqs/core/interfaces/APU.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,121 +276,3 @@ def __init__(

if self._db_path and deserialize:
self.deserialize()


# if __name__ == "__main__":
# # import sys
# # print sys.path
#
# #add APU emissions table to databases
# for path_to_database in [
# # os.path.join("..", "..", "core", "templates/new_blank_study.alaqs")
# os.path.join("..", "..", "example/CAEPport_training/caepport_out.alaqs")
# ]:
#
# # db = APUDatabase(path_to_database, deserialize=True)
# #
# # for index, entry in enumerate([{
# # "aircraft_group":"JET LARGE OLD",
# # "FF_NL_in_kg_s":205./3600.,
# # "FF_NR_in_kg_s":300./3600.,
# # "FF_HL_in_kg_s":345./3600.,
# # "NOx_NL_in_g_s":1.137/3.6,
# # "NOx_NR_in_g_s":2.071/3.6,
# # "NOx_HL_in_g_s":2.645/3.6,
# # "HC_NL_in_g_s":0.302/3.6,
# # "HC_NR_in_g_s":0.153/3.6,
# # "HC_HL_in_g_s":0.125/3.6,
# # "CO_NL_in_g_s":5.4/3.6,
# # "CO_NR_in_g_s":3.695/3.6,
# # "CO_HL_in_g_s":2.55/3.6
# # },
# # {
# # "aircraft_group":"JET LARGE",
# # "FF_NL_in_kg_s":170./3600.,
# # "FF_NR_in_kg_s":235./3600.,
# # "FF_HL_in_kg_s":315./3600.,
# # "NOx_NL_in_g_s":1.21/3.6,
# # "NOx_NR_in_g_s":2.892/3.6,
# # "NOx_HL_in_g_s":4.048/3.6,
# # "HC_NL_in_g_s":0.18/3.6,
# # "HC_NR_in_g_s":0.078/3.6,
# # "HC_HL_in_g_s":0.076/3.6,
# # "CO_NL_in_g_s":1.486/3.6,
# # "CO_NR_in_g_s":0.149/3.6,
# # "CO_HL_in_g_s":0.192/3.6
# # },
# # {
# # "aircraft_group":"JET SMALL",
# # "FF_NL_in_kg_s":75./3600.,
# # "FF_NR_in_kg_s":100./3600.,
# # "FF_HL_in_kg_s":125./3600.,
# # "NOx_NL_in_g_s":0.364/3.6,
# # "NOx_NR_in_g_s":0.805/3.6,
# # "NOx_HL_in_g_s":1.015/3.6,
# # "HC_NL_in_g_s":2.662/3.6,
# # "HC_NR_in_g_s":0.094/3.6,
# # "HC_HL_in_g_s":0.091/3.6,
# # "CO_NL_in_g_s":3.734/3.6,
# # "CO_NR_in_g_s":0.419/3.6,
# # "CO_HL_in_g_s":0.495/3.6
# # },
# # {
# # "aircraft_group":"JET MEDIUM",
# # "FF_NL_in_kg_s":105./3600.,
# # "FF_NR_in_kg_s":180./3600.,
# # "FF_HL_in_kg_s":200./3600.,
# # "NOx_NL_in_g_s":0.798/3.6,
# # "NOx_NR_in_g_s":1.756/3.6,
# # "NOx_HL_in_g_s":2.091/3.6,
# # "HC_NL_in_g_s":0.243/3.6,
# # "HC_NR_in_g_s":0.07/3.6,
# # "HC_HL_in_g_s":0.059/3.6,
# # "CO_NL_in_g_s":0.982/3.6,
# # "CO_NR_in_g_s":0.248/3.6,
# # "CO_HL_in_g_s":0.239/3.6
# # },
# # {
# # "aircraft_group":"JET REGIONAL",
# # "FF_NL_in_kg_s":50./3600.,
# # "FF_NR_in_kg_s":90./3600.,
# # "FF_HL_in_kg_s":105./3600.,
# # "NOx_NL_in_g_s":0.275/3.6,
# # "NOx_NR_in_g_s":0.452/3.6,
# # "NOx_HL_in_g_s":0.53/3.6,
# # "HC_NL_in_g_s":0.107/3.6,
# # "HC_NR_in_g_s":0.044/3.6,
# # "HC_HL_in_g_s":0.042/3.6,
# # "CO_NL_in_g_s":1.019/3.6,
# # "CO_NR_in_g_s":0.799/3.6,
# # "CO_HL_in_g_s":0.805/3.6
# # },
# # {
# # "aircraft_group":"JET BUSINESS",
# # "FF_NL_in_kg_s":50./3600.,
# # "FF_NR_in_kg_s":90./3600.,
# # "FF_HL_in_kg_s":105./3600.,
# # "NOx_NL_in_g_s":0.275/3.6,
# # "NOx_NR_in_g_s":0.452/3.6,
# # "NOx_HL_in_g_s":0.53/3.6,
# # "HC_NL_in_g_s":0.107/3.6,
# # "HC_NR_in_g_s":0.044/3.6,
# # "HC_HL_in_g_s":0.042/3.6,
# # "CO_NL_in_g_s":1.019/3.6,
# # "CO_NR_in_g_s":0.799/3.6,
# # "CO_HL_in_g_s":0.805/3.6
# # }]):
# # entry["oid"] = index
# # db.setEntry(index, entry)
# #
# # db.serialize()
#
# if not os.path.isfile(path_to_database):
# print("file %s not found"%path_to_database)
#
# store = APUStore(path_to_database)
# for apu_name, apu in list(store.getObjects().items()):
# # fix_print_with_import
# print(apu)
#
# # logger.debug(apu)
32 changes: 0 additions & 32 deletions open_alaqs/core/interfaces/EmissionDynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,35 +161,3 @@ def __init__(

if self._db_path and deserialize:
self.deserialize()


# if __name__ == "__main__":
#
# #add Emission Dynamics table to databases
# for path_to_database in [
# os.path.join("..", "..", "example/", "test_movs.alaqs")
# # os.path.join("..", "..", "example", "blank.alaqs")
# # os.path.join("..", "templates", "new_blank_study.alaqs")
# # os.path.join("..", "templates", "inventory_template.alaqs")
# ]:
# if not os.path.isfile(path_to_database):
# raise Exception("File %s doesn't exist !")
#
# db = EmissionDynamicsDatabase(path_to_database, deserialize=True)
#
# # for index, entry in enumerate([{
# # "aircraft_group":"JET LARGE+APU",
#
# # },
# # }]):
# # entry["oid"] = index
# # db.setEntry(index, entry)
# #
# # db.serialize()
#
# ed_store = EmissionDynamicsStore(path_to_database)
#
# # for ed_name, ed_ in ed_store.getObjects().items():
# # print ed_name, ed_.getDynamicsGroup()
# # print "Default: %s" % ed_.getEmissionDynamics('default')
# # print "Smooth & Shift: %s"% ed_.getEmissionDynamics('sas')
Loading

0 comments on commit 708dc07

Please sign in to comment.