def copy_stations_to_sqlite(src_dsn, dest_dsn): src_sesh = sessionmaker(bind=create_engine(src_dsn))() dest_sesh = sessionmaker(bind=create_engine(dest_dsn))() net = Network(name="MoTIe") dest_sesh.add(net) dest_sesh.flush() q = (src_sesh.query(Station).join(History).join(Network).filter( Network.name == "MoTIe")) for stn in q.all(): histories = [ History(station_name=hist.station_name) for hist in stn.histories ] new_obj = Station(native_id=stn.native_id, network_id=net.id, histories=histories) dest_sesh.add(new_obj) dest_sesh.commit() q = src_sesh.query(Variable).join(Network).filter(Network.name == "MoTIe") for var in q.all(): v = Variable( name=var.name, standard_name=var.standard_name, cell_method=var.cell_method, network_id=net.id, unit=var.unit, ) dest_sesh.add(v) dest_sesh.commit()
def test_nested_transactions_3(crmp_session): fake_network = Network(name="Fake Network") try: crmp_session.begin_nested() crmp_session.add(fake_network) except Exception: crmp_session.rollback() else: crmp_session.commit()
def test_nested_transactions_1(crmp_session): fake_network = Network(name="Fake Network") try: logging.debug("in try") with crmp_session.begin_nested(): logging.debug("in with") crmp_session.merge(fake_network) logging.debug("added network") except Exception as e: logging.debug("caught exception, raising") raise e
def test_diagnostic(crmp_session, diag, count): dt = datetime.now() rows = ( (dt, 0, "test_var_a", "degrees", "MoTIe", "noname", 40, -120), (dt, 0, "test_var_a", "degrees", "MoTIe", "noname", 40, -120), (dt, 0, "test_var_b", "mm", "MoTIe", "noname", 40, -120), ) rows = (Row(*x) for x in rows) crmp_session.add(Network(name="MoTIe")) infer(crmp_session, rows, diag) q = crmp_session.query(Variable).filter(Variable.name.like("test_var%")) assert q.count() == count
def test_nested_transactions_2(crmp_session): fake_network = Network(name="Fake Network") with crmp_session.begin_nested(): logging.debug("in with") crmp_session.add(fake_network) logging.debug("added moti network")
def test_session(crmp_session, caplog): """ Yields a PostGIS enabled session with CRMP schema and test data """ caplog.set_level(logging.ERROR, logger="sqlalchemy.engine") moti = Network(name="MoTIe") ec = Network(name="EC_raw") wmb = Network(name="FLNRO-WMB") wamr = Network(name="ENV-AQN") crmp_session.add_all([moti, ec, wmb, wamr]) simon = Contact(name="Simon", networks=[moti]) eric = Contact(name="Eric", networks=[wmb]) pat = Contact(name="Pat", networks=[ec]) crmp_session.add_all([simon, eric, pat]) brandy_hist = History(station_name="Brandywine") five_mile_hist = History(station_name="FIVE MILE") beaver_air_hist = History( id=12345, station_name="Beaver Creek Airport", the_geom=("SRID=4326;POINT(-140.866667 " "62.416667)"), ) stewart_air_hist = History( id=10, station_name="Stewart Airport", the_geom=("SRID=4326;POINT(-129.985 " "55.9361111111111)"), ) sechelt1 = History( id=20, station_name="Sechelt", sdate="2012-09-24", edate="2012-09-26", the_geom="SRID=4326;POINT(-123.7 49.45)", ) sechelt2 = History( id=21, station_name="Sechelt", sdate="2012-09-26", the_geom=("SRID=4326;POINT(-123.7152625 " "49.4579966666667)"), ) warfield = History(station_name="Warfield Elementary", sdate="2005-01-12") arkham = History(station_name="Arkham Asylum") stations = [ Station(native_id="11091", network=moti, histories=[brandy_hist]), Station(native_id="1029", network=wmb, histories=[five_mile_hist, arkham]), Station(native_id="2100160", network=ec, histories=[beaver_air_hist]), Station(native_id="1067742", network=ec, histories=[stewart_air_hist]), Station(native_id="1047172", network=ec, histories=[sechelt1, sechelt2]), Station(native_id="0260011", network=wamr, histories=[warfield]), ] crmp_session.add_all(stations) moti_air_temp = Variable(name="CURRENT_AIR_TEMPERATURE1", unit="celsius", network=moti) ec_precip = Variable(name="precipitation", unit="mm", network=ec) wmb_humitidy = Variable(name="relative_humidity", unit="percent", network=wmb) wamr_temp = Variable(name="TEMP_MEAN", unit="celsius", network=wamr) bad_var = Variable(name="no_unit", network=wamr) crmp_session.add_all( [moti_air_temp, ec_precip, wmb_humitidy, wamr_temp, bad_var]) obs = [ Obs( history=sechelt1, datum=2.5, variable=ec_precip, time=datetime(2012, 9, 24, 6, tzinfo=pytz.utc), ), Obs( history=sechelt1, datum=2.7, variable=ec_precip, time=datetime(2012, 9, 26, 6, tzinfo=pytz.utc), ), Obs( history=sechelt2, datum=2.5, variable=ec_precip, time=datetime(2012, 9, 26, 18, tzinfo=pytz.utc), ), ] crmp_session.add_all(obs) crmp_session.commit() yield crmp_session
def ec_session(crmp_session, caplog): """ Yields a PostGIS enabled session with CRMP schema and test data """ caplog.set_level(logging.ERROR, logger="sqlalchemy.engine") ec = Network(name="EC_raw") crmp_session.add(ec) pat = Contact(name="Pat", networks=[ec]) crmp_session.add(pat) beaver_air_hist = History( id=10000, station_name="Beaver Creek Airport", the_geom=("SRID=4326;POINT(-140.866667 " "62.416667)"), ) stewart_air_hist = History( id=10001, station_name="Stewart Airport", the_geom=("SRID=4326;POINT(-129.985 " "55.9361111111111)"), ) sechelt1 = History( id=20000, station_name="Sechelt", freq="1-hourly", sdate="2012-09-24", edate="2012-09-26", the_geom="SRID=4326;POINT(-123.7 49.45)", ) sechelt2 = History( id=20001, station_name="Sechelt", freq="1-hourly", sdate="2012-09-26", the_geom=("SRID=4326;POINT(-123.7152625 " "49.4579966666667)"), ) stations = [ Station(native_id="2100160", network=ec, histories=[beaver_air_hist]), Station(native_id="1067742", network=ec, histories=[stewart_air_hist]), Station(native_id="1047172", network=ec, histories=[sechelt1, sechelt2]), ] crmp_session.add_all(stations) ec_precip = Variable(id=100, name="total_precipitation", unit="mm", network=ec) ec_precip = Variable(id=101, name="air_temperature", unit="Celsius", network=ec) crmp_session.add(ec_precip) obs = [ Obs( history=sechelt1, datum=2.5, variable=ec_precip, time=datetime(2012, 9, 24, 6), ), Obs( history=sechelt1, datum=2.7, variable=ec_precip, time=datetime(2012, 9, 26, 6), ), Obs( history=sechelt2, datum=2.5, variable=ec_precip, time=datetime(2012, 9, 26, 18), ), ] crmp_session.add_all(obs) crmp_session.commit() yield crmp_session
def cv_network(): """Network for baseline climate variables; name is prescribed by pycds""" return Network(name=pycds.climate_baseline_helpers.pcic_climate_variable_network_name)
def stn_networks(): """Networks for stations""" return [Network(name='Station Network')]