def record(image, path): """ only works with lco shit for now """ # bkg = sep.background(image.data) # recarray = sep.extract(image.data - bkg.back(), bkg.globalrms * 3.0) session = db.create_session() cat = image["CAT"] sci = image["SCI"] hash_ = compute_hash(path) img = session.query(db.Image).filter(db.Image.hash == hash_).first() if img is None: datestr = re.search(r"\d{4}-\d{2}-\d{2}", sci.header["DATE"]).group() timestr = re.search(r"\d{2}:\d{2}:\d{2}\.?\d+", sci.header["UTSTART"]).group() dt = datetime.strptime(" ".join([datestr, timestr]), "%Y-%m-%d %H:%M:%S.%f") img = db.Image(path=path, time=dt, hash=compute_hash(path), ra=Angle(sci.header["RA"], unit="hourangle").deg, dec=Angle(sci.header["DEC"], unit="degree").deg) session.add(img) for source in cat.data: # rec = session.query(db.Record).filter(db.Record.ra==r, db.Record.dec==d).first() # if rec is None: # rec = db.Record(ra=r, dec=d) # session.add(rec) s = db.Source(data=source, dtype=cat.data.dtype) session.add(s) # rec.sources.append(s) img.sources.append(s) session.commit()
def check_update(): """ Check if the ATNF Pulsar Catalogue has been updated compared to the version in the cache. Returns: bool: True if the cache can be updated. """ from astropy.utils.data import download_file, get_cached_urls, compute_hash if ATNF_TARBALL not in get_cached_urls(): # can update cache as file is not cached yet return True # get the cached file name cachefile = download_file(ATNF_TARBALL, cache=True) # download a new version of the file and check the hash tmpcache = download_file(ATNF_TARBALL, cache=False) curhash = compute_hash(cachefile) tmphash = compute_hash(tmpcache) if curhash == tmphash: # no update needed return False else: # an update can be obtained return True
def test_compute_hash(tmpdir): from astropy.utils.data import compute_hash rands = b'1234567890abcdefghijklmnopqrstuvwxyz' filename = tmpdir.join('tmp.dat').strpath with open(filename, 'wb') as ntf: ntf.write(rands) ntf.flush() chhash = compute_hash(filename) shash = hashlib.md5(rands).hexdigest() assert chhash == shash
def __init__(self, image_or_path, secid, path=None): im_path = "" img = image_or_path if path is None: im_path = image_or_path img = fits.open(image_or_path) else: im_path = path cat = img["CAT"] sci = img["SCI"] hash_ = compute_hash(im_path) datestr = re.search(r"\d{4}-\d{2}-\d{2}", sci.header["DATE"]).group() timestr = re.search(r"\d{2}:\d{2}:\d{2}\.?\d+", sci.header["UTSTART"]).group() dt = datetime.strptime(" ".join([datestr, timestr]), "%Y-%m-%d %H:%M:%S.%f") self.path = path self.time = dt self.hash = hash_ self.ra = Angle(sci.header["RA"], unit="hourangle").deg self.dec = Angle(sci.header["DEC"], unit="degree").deg self.section_id = secid
def record(image, path, secid, residual_data, temp, db_session=None): """ only works with lco shit for now """ # bkg = sep.background(image.data) # recarray = sep.extract(image.data - bkg.back(), bkg.globalrms * 3.0) session = db_session if session is None: session = db.create_session("/seti_data/sdi.williamtest.db") cat = image["CAT"] sci = image["SCI"] hash_ = compute_hash(path) img = session.query(db.Image).filter(db.Image.hash == hash_).first() if img is None: print(path) img = db.Image(image, secid, path) service = vo.dal.SCSService( "https://heasarc.gsfc.nasa.gov/cgi-bin/vo/cone/coneGet.pl?table=m31stars&" ) w = wcs.WCS(sci.header) for element in residual_data: trans = db.Transient(data=element, w=w) session.add(trans) img.transients.append(trans) temp.transients.append(trans) pixarray = np.array([[element['x'], element['y']]]) radec = w.wcs_pix2world(pixarray, 0) result = service.search((radec[0][0], radec[0][1]), 0.001) if (result): ref = db.Reference(result) session.add(ref) ref.transients.append(trans) appmags = [] fluxes = [] source_list = [] for source in cat.data: # rec = session.query(db.Record).filter(db.Record.ra==r, db.Record.dec==d).first() # if rec is None: # rec = db.Record(ra=r, dec=d) # session.add(rec) s = db.Source(data=source, dtype=cat.data.dtype) session.add(s) # rec.sources.append(s) img.sources.append(s) temp.sources.append(s) result = reference(source) if (result): ref = db.Reference(result[0][1]) session.add(ref) ref.sources.append(s) appmags.append(result[0][1]['appmag'][0]) fluxes.append(source[6]) source_list.append(s) coeff, pcov = curve_fit(normalize, fluxes, appmags) img.coeff_a = coeff[0] img.coeff_b = coeff[1] for elem in source_list: elem.appmag = coeff[0] * np.log(elem.flux) + coeff[1] session.commit()