def make_image_crate(pixvals): """Create an image Crate from the given array. Parameters ---------- pixvals The pixel values. Returns ------- icrate An image crate which contains the pixel values and has some metadata items attached to it (CREATOR and DATE keywords). See Also -------- make_table_crate Notes ----- The semantics of whether the input array is copied or just referenced in the crate are currently not specified. Changing the original array after the crate has been created *may* change the value in the Crate, but this behavior should not be taken advantage of. Examples -------- >>> a = np.arange(400 * 500).reshape(400, 500) >>> cr = make_image_crate(a) """ cd = pycrates.CrateData() cd.values = pixvals cr = pycrates.IMAGECrate() cr.name = "IMAGE" cr.add_image(cd) _add_basic_metadata(cr, 'make_image_crate') return cr
def set_image_data(filename, data, header, ascii=False, clobber=False, packup=False): if not packup and os.path.isfile(filename) and not clobber: raise IOErr('filefound', filename) img = pycrates.IMAGECrate() # Write Image Header Keys for key in header.keys(): if header[key] is None: continue _set_key(img, key, header[key]) # Write Image WCS Header Keys if data['eqpos'] is not None: cdeltw = data['eqpos'].cdelt crvalw = data['eqpos'].crval crpixw = data['eqpos'].crpix equin = data['eqpos'].equinox if data['sky'] is not None: cdeltp = data['sky'].cdelt crvalp = data['sky'].crval crpixp = data['sky'].crpix _set_key(img, 'MTYPE1', 'sky ') _set_key(img, 'MFORM1', 'x,y ') _set_key(img, 'CTYPE1P', 'x ') _set_key(img, 'CTYPE2P', 'y ') _set_key(img, 'WCSNAMEP', 'PHYSICAL') _set_key(img, 'CDELT1P', cdeltp[0]) _set_key(img, 'CDELT2P', cdeltp[1]) _set_key(img, 'CRPIX1P', crpixp[0]) _set_key(img, 'CRPIX2P', crpixp[1]) _set_key(img, 'CRVAL1P', crvalp[0]) _set_key(img, 'CRVAL2P', crvalp[1]) if data['eqpos'] is not None: # Simply the inverse of read transformations in get_image_data cdeltw = cdeltw * cdeltp crpixw = ((crpixw - crvalp) / cdeltp + crpixp) if data['eqpos'] is not None: _set_key(img, 'MTYPE2', 'EQPOS ') _set_key(img, 'MFORM2', 'RA,DEC ') _set_key(img, 'CTYPE1', 'RA---TAN') _set_key(img, 'CTYPE2', 'DEC--TAN') _set_key(img, 'CDELT1', cdeltw[0]) _set_key(img, 'CDELT2', cdeltw[1]) _set_key(img, 'CRPIX1', crpixw[0]) _set_key(img, 'CRPIX2', crpixw[1]) _set_key(img, 'CRVAL1', crvalw[0]) _set_key(img, 'CRVAL2', crvalw[1]) _set_key(img, 'EQUINOX', equin) # Write Image pixel values pix_col = pycrates.CrateData() pix_col.values = data['pixels'] # pycrates.add_piximg(img, pix_col) img.add_image(pix_col) if packup: return img if ascii and '[' not in filename and ']' not in filename: # filename += "[opt kernel=text/simple]" raise IOErr('writenoimg') # pycrates.write_file(img, filename) img.write(filename, clobber=True) close_crate_dataset(img.get_dataset())
def __init__(self, inval): """Create the object.""" if hasattr(inval, "get_axisnames") and hasattr(inval, "get_transform"): crate = inval fname = crate.get_filename() else: crate = pycrates.IMAGECrate(inval, mode="r") fname = inval axes = crate.get_axisnames() if axes is None or len(axes) == 0: raise ValueError("No axes found in {}".format(fname)) # get_transform is case-sensitive self.axes = [n.split('(')[0] for n in axes] self.transforms = {} for n in self.axes: try: tr = crate.get_transform(n).copy() except ValueError: raise ValueError("Unable to find the transformation for {}".format(n)) try: ctype = self._coord_mapping[tr.get_className()] except KeyError: continue try: self.transforms[ctype] raise ValueError("Found multiple {} coordinate systems in {}".format(ctype, fname)) except KeyError: self.transforms[ctype] = {"name": n, "type": ctype, "transform": tr} try: world = self.transforms["world"] physical = self.transforms["physical"] except KeyError as ke: raise ValueError("Unable to find the {} system in {}".format(ke.args[0], fname)) # self.conversions[fromsys][tosys] # self.conversions = {} self.conversions["world"] = { "world": None, "physical": {"direction": "invert", "transforms": [world]}, "logical": {"direction": "invert", "transforms": [world, physical]} } self.conversions["physical"] = { "world": {"direction": "apply", "transforms": [world]}, "physical": None, "logical": {"direction": "invert", "transforms": [physical]} } self.conversions["logical"] = { "world": {"direction": "apply", "transforms": [physical, world]}, "physical": {"direction": "apply", "transforms": [physical]}, "logical": None }