Notebook
connection = engine.connect()def Sal_PSU_to_SA(S,press,lon,lat): SA=gsw.SA_from_SP(S,press,lon,lat) if SA>=40: print('Err: SA=',SA,', None entered') SA=None elif SA<0: SA=None print('Err: SA=',SA,', None entered') return(SA) def T_to_CT(SA,T,press): CT=gsw.CT_from_t(SA,T,press) if CT>=40: print('Err: CT=',CT,', None entered') CT=None elif CT<-5: CT=None print('Err: CT=',CT,', None entered') return(CT) def p_to_Z(press,lat): Z=-1.0*gsw.z_from_p(press,lat) return(Z) connection.connection.create_function("Sal_PSU_to_SA_DB",4,Sal_PSU_to_SA) connection.connection.create_function("T_to_CT_DB",3,T_to_CT) connection.connection.create_function("p_to_Z_DB",2,p_to_Z)connection.execute("""UPDATE CalcsTBL SET Temperature_CT = T_to_CT_DB(CalcsTBL.Salinity_SA,ObsTBL.Temperature,ObsTBL.Pressure), Temperature_Primary_CT = T_to_CT_DB(CalcsTBL.Salinity_T0_C0_SA,ObsTBL.Temperature,ObsTBL.Pressure), Temperature_Secondary_CT = T_to_CT_DB(CalcsTBL.Salinity_T1_C1_SA,ObsTBL.Temperature,ObsTBL.Pressure), FROM ((CalcsTBL INNER JOIN ObsTBL ON CalcsTBL.ObsID = ObsTBL.ID) INNER JOIN StationTBL ON CalcsTBL.StationID = StationTBL.ID)""")test=connection.execute("""SELECT T_to_CT_DB( CASE WHEN CalcsTBL.Salinity_T1_C1_SA IS NOT NULL THEN CalcsTBL.Salinity_T1_C1_SA WHEN CalcsTBL.Salinity_T0_C0_SA IS NOT NULL THEN CalcsTBL.Salinity_T0_C0_SA WHEN CalcsTBL.Salinity_SA IS NOT NULL THEN CalcsTBL.Salinity_SA ELSE NULL END,ObsTBL.Temperature,ObsTBL.Pressure) as T1, T_to_CT_DB( CASE WHEN CalcsTBL.Salinity_T1_C1_SA IS NOT NULL THEN CalcsTBL.Salinity_T1_C1_SA WHEN CalcsTBL.Salinity_T0_C0_SA IS NOT NULL THEN CalcsTBL.Salinity_T0_C0_SA WHEN CalcsTBL.Salinity_SA IS NOT NULL THEN CalcsTBL.Salinity_SA ELSE NULL END,ObsTBL.Temperature,ObsTBL.Pressure) as T2, T_to_CT_DB( CASE WHEN CalcsTBL.Salinity_T1_C1_SA IS NOT NULL THEN CalcsTBL.Salinity_T1_C1_SA WHEN CalcsTBL.Salinity_T0_C0_SA IS NOT NULL THEN CalcsTBL.Salinity_T0_C0_SA WHEN CalcsTBL.Salinity_SA IS NOT NULL THEN CalcsTBL.Salinity_SA ELSE NULL END,ObsTBL.Temperature,ObsTBL.Pressure) as T3 FROM CalcsTBL INNER JOIN ObsTBL ON CalcsTBL.ObsID = ObsTBL.ID INNER JOIN StationTBL ON CalcsTBL.StationID = StationTBL.ID;""") ii=0 for row in test: ii=ii+1 if (ii>100)&(ii<120): print(row)
dfa.drop(['source1','source2'],axis=1,inplace=True)
for aID, bID in dupsQRY.all(): for obsID, in session0.query(ObsTBL0.ID).filter(ObsTBL0.StationTBLID==bID).all(): Rec=session0.query(ObsTBL0).filter(ObsTBL0.ID==obsID).one() setattr(Rec,'StationTBLID',aID) session0.commit()for aID, bID in dupsQRY.all(): # set include to false for second station entry print(aID,bID) sRec=session.query(StationTBL).filter(StationTBL.ID==bID) setattr(sRec,'Include',False) # find all depths in ObsTBL records associated with second station entry # that are also present in records associated with first station entry # and set their Include to False # base depths on pressure because it is present in all records zs1=[pp for pp, in session.query(ObsTBL.Pressure).filter(ObsTBL.StationTBLID==aID).all()] zs2=[pp for pp, in session.query(ObsTBL.Pressure).filter(ObsTBL.StationTBLID==bID).all()] for i2 in zs2: if np.min([np.abs(i2-i1) for i1 in zs1])<=0.5: oRec=session.query(ObsTBL).filter(and_(ObsTBL.StationTBLID==bID,ObsTBL.Pressure==i2)).one() setattr(oRec,'Include',False) # associate all records previously associated with second station entry with first entry for obsID, in session.query(ObsTBL.ID).filter(ObsTBL.StationTBLID==bID).all(): Rec=session.query(ObsTBL).filter(ObsTBL.ID==obsID).one() setattr(Rec,'StationTBLID',aID) session.commit()zs1=[pp for pp, in session.query(ObsTBL.Pressure).filter(ObsTBL.StationTBLID==aID).all()]