download_tess_kepler: rework to send all requests in batches

This commit is contained in:
2025-08-07 17:06:19 +02:00
parent b0835c523c
commit 298cba6616
+87 -40
View File
@@ -1,14 +1,21 @@
from astroquery.mast import Observations from astroquery.mast import Observations, Catalogs
from astropy import table from astropy import table
from astropy.io import fits from astropy.io import fits
import numpy as np import numpy as np
from copy import deepcopy from copy import deepcopy
from astroquery.simbad import Simbad from astroquery.simbad import Simbad
from enum import Enum
#from main.astrodatagui.db.StarsDB import StarDB import sqlite3
import time
DEFAULT_DB = "stars.db" DEFAULT_DB = "stars.db"
BATCH_SIZE = 500
class StarDBError(Enum):
NO_ERROR = 0
DB_CONNECTION_FAILED = 1
class StarDB(): class StarDB():
__instances = list() __instances = list()
__dbNames = list() __dbNames = list()
@@ -206,64 +213,72 @@ def fitsFilenameFilterFunc(table, key_colnames):
return True return True
return False return False
starDB: StarDB = StarDB.getInstance(DEFAULT_DB) def split_table(table, batch_size):
simbad = Simbad() """Yield successive batch_size-sized chunks from an Astropy Table."""
simbad.add_votable_fields("sptype") for start in range(0, len(table), batch_size):
simbad.add_votable_fields("velocity") yield table[start:start + batch_size]
simbad.add_votable_fields("diameter")
simbad.add_votable_fields("distance")
tessObs = Observations.query_criteria(obs_collection="TESS", intentType="science", calib_level=2) def getProductList(obsList):
tessDataProducts = Observations.filter_products(Observations.get_product_list(tessObs), productSubGroupDescription=["LC"]) productList = []
for i, batch in enumerate(split_table(obsList, BATCH_SIZE)):
print(f"[{i+1}] Getting products for batch of {len(batch)} observations...")
try:
prod = Observations.get_product_list(batch)
productList.append(prod)
except Exception as e:
print(f"Failed on batch {i+1}: {e}")
time.sleep(100) # delay so we dont immediately send another request
return table.vstack(productList)
keplerObs = Observations.query_criteria(obs_collection="Kepler", intentType="science", calib_level=2) def getManifest(dataProduct):
keplerDataProducts = Observations.filter_products(Observations.get_product_list(keplerObs), productSubGroupDescription=["SLC"]) manifest = []
for batch in split_table(dataProduct, BATCH_SIZE//10):
k2Obs = Observations.query_criteria(obs_collection="K2", intentType="science", calib_level=2) partial_manifest = parse_manifest(Observations.download_products(batch))
k2DataProducts = Observations.filter_products(Observations.get_product_list(k2Obs), productSubGroupDescription=["SLC"]) manifest.append(partial_manifest)
time.sleep(100) # delay so we dont immediately send another request
productsWanted = table.vstack([tessDataProducts, keplerDataProducts, k2DataProducts]) return table.vstack(manifest)
productsWanted = productsWanted.group_by("productFilename")
productsWanted = productsWanted.groups.filter(fitsFilenameFilterFunc)
manifest = parse_manifest(Observations.download_products(productsWanted))
def insertManifestIntoDB(manifest):
for pm in manifest: for pm in manifest:
result = self.simbad.query_object(pm["starID"])[0] result = pm["starID"]
try: dist = -1
mainName = result["MAIN_ID"] distUnit = ""
except: rotVel = -1
self.showErrorMessage("Main Identifier Error", "Failed to grab main identifier, aborting...") rotVelUnit = ""
continue specType = "-"
try: try:
altNames = Simbad.query_objectids(mainName) result = simbad.query_object(pm["starID"])
mainName = result["MAIN_ID"][0]
except: except:
self.showErrorMessage("Identifier Error", "Failed to grab all identifiers, aborting...") print("Main Identifier Error", "Failed to grab main identifier, using TIC...")
continue noAltNames = True
try: try:
specType = result["SP_TYPE"] altNames = simbad.query_objectids(mainName)
except:
print("Identifier Error", "Failed to grab all identifiers, aborting...")
altNames = []
try:
specType = result["SP_TYPE"][0]
if(specType == ""): if(specType == ""):
specType = "-" specType = "-"
except: except:
print("No spectral type found") print("No spectral type found")
specType = "-"
rotVel = -1
rotVelUnit = ""
try: try:
if(result["RVZ_TYPE"] == "v"): if(result["RVZ_TYPE"][0] == "v"):
rotVel = result["RV_VALUE"] rotVel = result["RV_VALUE"][0]
rotVelUnit = str(result["RV_VALUE"].unit) rotVelUnit = str(result["RV_VALUE"][0].unit)
except: except:
print("No radial velocity found") print("No radial velocity found")
try: try:
dist = float(result["Distance_distance"]) dist = float(result["Distance_distance"][0])
if(np.isnan(dist)): if(np.isnan(dist)):
dist = -1 dist = -1
distUnit = result["Distance_unit"] distUnit = result["Distance_unit"][0]
except: except:
dist = -1 dist = -1
distUnit = "" distUnit = ""
@@ -271,3 +286,35 @@ for pm in manifest:
starDB.insertStar(mainName, altNames, pm, starDB.insertStar(mainName, altNames, pm,
specType, rotVel, rotVelUnit, dist, distUnit) specType, rotVel, rotVelUnit, dist, distUnit)
starDB: StarDB = StarDB.getInstance(DEFAULT_DB)
simbad = Simbad()
simbad.add_votable_fields("sptype")
simbad.add_votable_fields("velocity")
simbad.add_votable_fields("diameter")
simbad.add_votable_fields("distance")
# TESS
tessObs = Observations.query_criteria(obs_collection="TESS")
tessProductList = getProductList(tessObs)
tessDataProducts = Observations.filter_products(tessProductList, productSubGroupDescription=["LC"], extension="fits")
tessManifest = getManifest(tessDataProducts)
insertManifestIntoDB(tessManifest)
# Kepler
keplerObs = Observations.query_criteria(obs_collection="Kepler")
keplerProductList = getProductList(keplerObs)
keplerDataProducts = Observations.filter_products(keplerProductList, productSubGroupDescription=["SLC"], extension="fits")
keplerManifest = getManifest(keplerDataProducts)
insertManifestIntoDB(keplerManifest)
# K2
k2Obs = Observations.query_criteria(obs_collection="K2")
k2ProductList = getProductList(k2Obs)
k2DataProducts = Observations.filter_products(k2ProductList, productSubGroupDescription=["SLC"], extension="fits")
k2Manifest = getManifest(k2DataProducts)
insertManifestIntoDB(k2Manifest)