def __init__ (self, path, mode): try: from pyrap.images import image except ImportError: raise UnsupportedError ('cannot open CASAcore images in Pyrap mode without ' 'the Python module "pyrap.images"') super (PyrapImage, self).__init__ (path, mode) # no mode specifiable self._handle = image (path) allinfo = self._handle.info () self.units = maybelower (allinfo.get ('unit')) self.shape = np.asarray (self._handle.shape (), dtype=np.int) self.axdescs = [] if 'coordinates' in allinfo: pc = allinfo['coordinates'].get ('pointingcenter') # initial=True signifies that the pointing center information # hasn't actually been initialized. if pc is not None and not pc['initial']: # This bit of info doesn't have any metadata about units or # whatever; appears to be fixed as RA/Dec in radians. self.pclat = pc['value'][1] self.pclon = pc['value'][0] ii = self._handle.imageinfo () if 'restoringbeam' in ii: self.bmaj = _pyrap_convert (ii['restoringbeam']['major'], 'rad') self.bmin = _pyrap_convert (ii['restoringbeam']['minor'], 'rad') self.bpa = _pyrap_convert (ii['restoringbeam']['positionangle'], 'rad') # Make sure that angular units are always measured in radians, # because anything else is ridiculous. from pyrap.quanta import quantity self._wcscale = wcscale = np.ones (self.shape.size) c = self._handle.coordinates () radian = quantity (1., 'rad') for item in c.get_axes (): if isinstance (item, six.string_types): self.axdescs.append (item.replace (' ', '_')) else: for subitem in item: self.axdescs.append (subitem.replace (' ', '_')) def getconversion (text): q = quantity (1., text) if q.conforms (radian): return q.get_value ('rad') return 1 i = 0 for item in c.get_unit (): if isinstance (item, six.string_types): wcscale[i] = getconversion (item) i += 1 elif len (item) == 0: wcscale[i] = 1 # null unit i += 1 else: for subitem in item: wcscale[i] = getconversion (subitem) i += 1 # Figure out which axes are lat/long/spec. We have some # paranoia code the give up in case there are multiple axes # that appear to be of the same type. This stuff could # be cleaned up. lat = lon = spec = -1 try: logspecidx = c.get_names ().index ('spectral') except ValueError: specaxname = None else: specaxname = c.get_axes ()[logspecidx] for i, name in enumerate (self.axdescs): # These symbolic direction names obtained from # casacore/coordinates/Coordinates/DirectionCoordinate.cc # Would be nice to have a better system for determining # this a la what wcslib provides. if name == specaxname: spec = i elif name in ('Right_Ascension', 'Hour_Angle', 'Longitude'): if lon == -1: lon = i else: lon = -2 elif name in ('Declination', 'Latitude'): if lat == -1: lat = i else: lat = -2 if lat >= 0: self._latax = lat if lon >= 0: self._lonax = lon if spec >= 0: self._specax = spec # Phew, that was gross. if self._specax is not None: sd = c.get_coordinate ('spectral').dict () wi = sd.get ('wcs') if wi is not None: try: from mirtask._miriad_c import mirwcs_compute_freq except ImportError: pass else: spectype = wi['ctype'].replace ('\x00', '')[:4] restfreq = sd.get ('restfreq', 0.) specval = self.toworld (0.5 * (self.shape - 1))[self._specax] self.charfreq = mirwcs_compute_freq (spectype, specval, restfreq) * 1e-9 # TODO: any unit weirdness or whatever here? self.mjd = c.get_obsdate ()['m0']['value']
def __init__(self, path, mode): try: from pyrap.images import image except ImportError: raise UnsupportedError( 'cannot open CASAcore images in Pyrap mode without ' 'the Python module "pyrap.images"') super(PyrapImage, self).__init__(path, mode) # no mode specifiable self._handle = image(path) allinfo = self._handle.info() self.units = maybelower(allinfo.get('unit')) self.shape = np.asarray(self._handle.shape(), dtype=np.int) self.axdescs = [] if 'coordinates' in allinfo: pc = allinfo['coordinates'].get('pointingcenter') # initial=True signifies that the pointing center information # hasn't actually been initialized. if pc is not None and not pc['initial']: # This bit of info doesn't have any metadata about units or # whatever; appears to be fixed as RA/Dec in radians. self.pclat = pc['value'][1] self.pclon = pc['value'][0] ii = self._handle.imageinfo() if 'restoringbeam' in ii: self.bmaj = _pyrap_convert(ii['restoringbeam']['major'], 'rad') self.bmin = _pyrap_convert(ii['restoringbeam']['minor'], 'rad') self.bpa = _pyrap_convert(ii['restoringbeam']['positionangle'], 'rad') # Make sure that angular units are always measured in radians, # because anything else is ridiculous. from pyrap.quanta import quantity self._wcscale = wcscale = np.ones(self.shape.size) c = self._handle.coordinates() radian = quantity(1., 'rad') for item in c.get_axes(): if isinstance(item, six.string_types): self.axdescs.append(item.replace(' ', '_')) else: for subitem in item: self.axdescs.append(subitem.replace(' ', '_')) def getconversion(text): q = quantity(1., text) if q.conforms(radian): return q.get_value('rad') return 1 i = 0 for item in c.get_unit(): if isinstance(item, six.string_types): wcscale[i] = getconversion(item) i += 1 elif len(item) == 0: wcscale[i] = 1 # null unit i += 1 else: for subitem in item: wcscale[i] = getconversion(subitem) i += 1 # Figure out which axes are lat/long/spec. We have some # paranoia code the give up in case there are multiple axes # that appear to be of the same type. This stuff could # be cleaned up. lat = lon = spec = -1 try: logspecidx = c.get_names().index('spectral') except ValueError: specaxname = None else: specaxname = c.get_axes()[logspecidx] for i, name in enumerate(self.axdescs): # These symbolic direction names obtained from # casacore/coordinates/Coordinates/DirectionCoordinate.cc # Would be nice to have a better system for determining # this a la what wcslib provides. if name == specaxname: spec = i elif name in ('Right_Ascension', 'Hour_Angle', 'Longitude'): if lon == -1: lon = i else: lon = -2 elif name in ('Declination', 'Latitude'): if lat == -1: lat = i else: lat = -2 if lat >= 0: self._latax = lat if lon >= 0: self._lonax = lon if spec >= 0: self._specax = spec # Phew, that was gross. if self._specax is not None: sd = c.get_coordinate('spectral').dict() wi = sd.get('wcs') if wi is not None: try: from mirtask._miriad_c import mirwcs_compute_freq except ImportError: pass else: spectype = wi['ctype'].replace('\x00', '')[:4] restfreq = sd.get('restfreq', 0.) specval = self.toworld(0.5 * (self.shape - 1))[self._specax] self.charfreq = mirwcs_compute_freq( spectype, specval, restfreq) * 1e-9 # TODO: any unit weirdness or whatever here? self.mjd = c.get_obsdate()['m0']['value']
def _wcs_get_freq (wcs, specval): from mirtask._miriad_c import mirwcs_compute_freq assert wcs.wcs.spec >= 0 spectype = wcs.wcs.ctype[wcs.wcs.spec][:4] return mirwcs_compute_freq (spectype, specval, wcs.wcs.restfrq) * 1e-9
def _wcs_get_freq(wcs, specval): from mirtask._miriad_c import mirwcs_compute_freq assert wcs.wcs.spec >= 0 spectype = wcs.wcs.ctype[wcs.wcs.spec][:4] return mirwcs_compute_freq(spectype, specval, wcs.wcs.restfrq) * 1e-9