diff --git a/mica/vv/core.py b/mica/vv/core.py index d93b51c1..ffcf9882 100644 --- a/mica/vv/core.py +++ b/mica/vv/core.py @@ -194,16 +194,15 @@ def _aiid_info(self, save_cols=save_asol_header): # this should probably be handled in mica.archive.asp_l1 @staticmethod def _asp1_lookup(obsid, obi, revision): - apstat = Sqsh(dbi="sybase", server="sqlsao", database="axafapstat") - # take these from the first aspect solution file header - aspect_1 = apstat.fetchall( - """SELECT * FROM aspect_1 - WHERE obsid = {obsid} - AND obi = {obi} - AND revision = {revision} - """.format(obsid=obsid, obi=obi, revision=revision) - ) - apstat.conn.close() + with Sqsh(dbi="sybase", server="sqlsao", database="axafapstat") as apstat: + # take these from the first aspect solution file header + aspect_1 = apstat.fetchall( + """SELECT * FROM aspect_1 + WHERE obsid = {obsid} + AND obi = {obi} + AND revision = {revision} + """.format(obsid=obsid, obi=obi, revision=revision) + ) if len(aspect_1) > 1: raise ValueError("More than one entry found for obsid/obi/rev in aspect_1") if len(aspect_1) == 0: @@ -269,7 +268,7 @@ def _save_info_json(self, file=None): logger.info("Saved JSON to {}".format(file)) def slots_to_db(self): - if self.info()["aspect_1_id"] is None: + if self.info().get("aspect_1_id") is None: logger.warning( "Database save not implemented for obsids without aspect_1_ids" ) @@ -301,7 +300,7 @@ def _get_ccd_temp(tstart, tstop): def slots_to_table(self): save = self.info() - if save["aspect_1_id"] is None: + if save.get("aspect_1_id") is None: logger.warning("Table save not implemented for obsids without aspect_1_ids") return mean_aacccdpt = self._get_ccd_temp(save["tstart"], save["tstop"]) @@ -932,13 +931,12 @@ def _get_prop(self, propname, propstring): def _read_ocat_stars(self): obsid = int(self.asol_header["OBS_ID"]) obi = int(self.asol_header["OBI_NUM"]) - ocat_db = Sqsh(dbi="sybase", server="sqlsao", database="axafocat") - stars = ocat_db.fetchall( - "select * from stars where " - "obsid = {} and obi = {} " - "and type != 0".format(obsid, obi) - ) - ocat_db.conn.close() + with Sqsh(dbi="sybase", server="sqlsao", database="axafocat") as ocat_db: + stars = ocat_db.fetchall( + "select * from stars where " + "obsid = {} and obi = {} " + "and type != 0".format(obsid, obi) + ) if len(np.unique(stars["obi"])) > 1: raise ValueError( "Multi-obi observation. OCAT stars unhelpful to identify missing slot"