def get_tangent_point(filename): """Return the 'tangent point' for a file or raises an IOError. Given an image, return the ra/dec used in the WCS transform. For tables look for the column with a WCS transform. This is not a general-purpose routine, since it makes a number of assumptions. """ bl = cxcdm.dmBlockOpen(filename) try: btype = cxcdm.dmBlockGetType(bl) if btype == cxcdm.dmIMAGE: (ra0, dec0) = _get_tangent_point_image(filename, bl) elif btype == cxcdm.dmTABLE: (ra0, dec0) = _get_tangent_point_table(filename, bl) else: raise IOError( "Unable to find tangent point for block " + "{} of {}".format(cxcdm.dmBlockGetName(bl), filename)) finally: cxcdm.dmBlockClose(bl) v3("Tangent point in {}: {} {}".format(filename, ra0, dec0)) return (ra0, dec0)
def get_content(f): bl = cxcdm.dmBlockOpen(f, update=False) try: keys = cxcdm.dmKeyRead(bl, 'CONTENT') content = keys[1].decode('ascii') return content finally: cxcdm.dmBlockClose(bl)
def put_history(self, infile ): """ Use the CXCDM to write history records """ from cxcdm import dmBlockOpen, dmBlockClose, dmBlockWriteComment, dmBlockMoveToKey, dmBlockGetNoKeys tab = dmBlockOpen( infile, update=True) dmBlockMoveToKey( tab, dmBlockGetNoKeys(tab)) for t,v in self.history: dmBlockWriteComment( tab, t, v ) dmBlockClose(tab)
def get_subspace(infile, binsize): """Obtain the sky range of a region filtered image, using the file subspace. This may also be used with unfiltered images too""" lo = {"x": None, "y": None} hi = {"x": None, "y": None} v4("get_subspace: infile={} binsize={}".format(infile, binsize)) bl = cxcdm.dmBlockOpen(infile) try: num_compt = cxcdm.dmBlockGetNoSubspaceCpts(bl) v4("get_subspace: found {} sub-space components".format(num_compt)) if num_compt == 0: v1("No subspace components found for " + infile) else: # get the union of all the sky x- and y-ranges from the subspace for i in range(1, num_compt + 1): v4("get_subspace: moving to subspace block #{}".format(i)) cxcdm.dmBlockSetSubspaceCpt(bl, i) for colname in ["x", "y"]: try: col = cxcdm.dmSubspaceColOpen(bl, colname) (subspace_lo, subspace_hi) = cxcdm.dmSubspaceColGet(col) v4("get_subspace: col={} subspace lo={} hi={}".format( colname, subspace_lo, subspace_hi)) for (lo_sub, hi_sub) in zip(subspace_lo, subspace_hi): if lo[colname] is None: lo[colname] = lo_sub else: lo[colname] = min(lo[colname], lo_sub) if hi[colname] is None: hi[colname] = hi_sub else: hi[colname] = max(hi[colname], hi_sub) except RuntimeError: v4("get_subspace: skipping block as no x/y subspace columns" ) # if zero subspace components, exit quietly, since # cxcdm throws a RuntimeError pass finally: cxcdm.dmBlockClose(bl) return (AxisRange(lo["x"], hi["x"], binsize), AxisRange(lo["y"], hi["y"], binsize))
def get_aimpoint(infile): """Return the aim-point ccd_id/chip value by inspecting the contents of the first table block that starts with the name 'GTI'. Raises an IOError if unable to find a block that starts with GTI. """ v3("Looking for aimpoint CCD in {}".format(infile)) try: ds = cxcdm.dmDatasetOpen(infile) except IOError as ioe: msg = str(ioe) if msg.startswith("dmDatasetOpen() "): msg = msg[16:] raise IOError(msg) try: nblocks = cxcdm.dmDatasetGetNoBlocks(ds) v4("File has {} blocks".format(nblocks)) for blnum in range(1, nblocks + 1): blname = cxcdm.dmDatasetGetBlockName(ds, blnum) v4("Block #{} = {}".format(blnum, blname)) if blname.startswith("GTI") and \ cxcdm.dmDatasetGetBlockType(ds, blnum) == cxcdm.dmTABLE: bl = cxcdm.dmDatasetGetBlock(ds, blnum) try: nkeys = cxcdm.dmBlockGetNoKeys(bl) for keynum in range(1, nkeys + 1): dd = cxcdm.dmBlockGetKey(bl, keynum) # TODO: is it CHIP for HRC data? Does it matter? if cxcdm.dmGetName(dd) == 'CCD_ID': aimpoint = cxcdm.dmGetData(dd) v4("Found aimpoint CCD = {}".format(aimpoint)) return aimpoint finally: cxcdm.dmBlockClose(bl) raise IOError( "No CCD_ID keyword found in {} block of {}".format( blname, infile)) raise IOError("No block starting with GTI found in {}".format(infile)) finally: cxcdm.dmDatasetClose(ds)
def get_image_units(fname): """Returns the units of the image, which can be an empty string. A TypeError is raised if the input is not an image.""" # We do not use dmImageOpen since that will promote a # table to an image (if possible) and we do not want that. # bl = cxcdm.dmBlockOpen(fname) try: if cxcdm.dmBlockGetType(bl) != cxcdm.dmIMAGE: raise IOError("The file {} is not an image.".format(fname)) dd = cxcdm.dmImageGetDataDescriptor(bl) unit = cxcdm.dmGetUnit(dd) finally: cxcdm.dmBlockClose(bl) return unit
def get_history(self, filename ): """ Use CXCDM to get history records """ from cxcdm import dmBlockOpen, dmBlockClose, dmBlockReadComment, dmBlockMoveToKey, dmBlockGetNoKeys self.history = [] tab = dmBlockOpen( filename ) for ii in range( 1, dmBlockGetNoKeys(tab)+1 ): dmBlockMoveToKey(tab,ii) while True: hist = dmBlockReadComment(tab) if None == hist: break elif 2 != len(hist): break elif not any(hist): break else: self.history.append(hist) dmBlockClose(tab)