def test_phasecenter(self): phasecenter_str = '15:30:0 -10.30.00 J2000' phasecenter_rec = imagewriter.parse_phasecenter(phasecenter_str) self.result = phasecenter_rec lonstr, latstr, ref = phasecenter_str.split() qa = casa.CreateCasaQuantity() me = casa.CreateCasaMeasure() lon = qa.convert(qa.quantity(lonstr), 'rad') # to make value in ranges [-pi, pi] pi = qa.constants('pi')['value'] lon['value'] = (lon['value'] + pi) % (2 * pi) - pi lat = qa.quantity(latstr) print(lon) print(lat) print(phasecenter_str) self.assertEqual(me.gettype(phasecenter_rec), 'Direction') self.assertEqual(me.getref(phasecenter_rec), ref) phasecenter_value = me.getvalue(phasecenter_rec) actual_lon = qa.convert(phasecenter_rec['m0'], 'rad')['value'] expected_lon = qa.convert(lon, 'rad')['value'] diff = abs((actual_lon - expected_lon) / expected_lon) eps = 1.0e-15 self.assertLess(diff, eps) self.assertTrue(qa.eq(lat, phasecenter_value['m1']))
def _get_lsr_frequency(self, times, data_desc_ids, field_ids): # sanity check # - consistency of chunk data length assert len(times) == len(data_desc_ids) assert len(times) == len(field_ids) me = casa.CreateCasaMeasure() qa = casa.CreateCasaQuantity() # position measure -- observatory position of ALMA me.doframe(me.observatory('ALMA')) nchunk = len(times) lsr_frequency = {} lsr_width = {} for i in range(nchunk): ddid = data_desc_ids[i] field_id = field_ids[i] spwid = self.dd_spw_map[ddid] freq_ref = self.freq_ref[spwid] chan_freq = self.chan_freq[spwid] chan_width = self.chan_width[spwid] nchan = len(chan_freq) lsr_freq_edge = numpy.empty(nchan + 1, dtype=numpy.float64) lsr_freq_edge[:nchan] = chan_freq - chan_width / 2.0 lsr_freq_edge[nchan] = chan_freq[-1] + chan_width[-1] / 2.0 if freq_ref == 'LSRK': # frequency is in LSRK, no conversion is needed pass else: # require conversion to LSRK # time measure epoch = qa.quantity(times[i], 's') mepoch = me.epoch('utc', epoch) me.doframe(mepoch) # direction measure field_dir = self.field_dir[field_id][:, 0] lon = qa.quantity(field_dir[0], 'rad') lat = qa.quantity(field_dir[1], 'rad') mdirection = me.direction(self.field_ref, lon, lat) me.doframe(mdirection) # frequency measure frequency = qa.quantity(0.0, 'Hz') mfrequency = me.frequency(freq_ref, frequency) for ichan in range(nchan): mfrequency['m0']['value'] = lsr_freq_edge[ ichan] #chan_freq[ichan] - 0.5 * chan_width[ichan] #print 'LOG mfrequency = {0}'.format(mfrequency) # convert converted = me.measure(mfrequency, 'LSRK') lsr_freq_edge[ichan] = converted['m0']['value'] mfrequency['m0']['value'] = lsr_freq_edge[ nchan] #chan_freq[nchan-1] + 0.5 * chan_width[nchan-1] converted = me.measure(mfrequency, 'LSRK') lsr_freq_edge[nchan] = converted['m0']['value'] lsr_frequency[i] = lsr_freq_edge #print 'LOG native = {0}'.format(chan_freq) #print 'LOG converted = {0}'.format(lsr_frequency[i]) #return lsr_frequency, lsr_width return lsr_frequency
def parse_phasecenter(phasecenter_str): qa = casa.CreateCasaQuantity() me = casa.CreateCasaMeasure() if len(phasecenter_str) == 0: # defualt value '0:0:0 0.0.0 J2000' lat = qa.quantity(0.0, 'rad') lon = qa.quantity(0.0, 'rad') ref = 'J2000' else: # expected format: "longitude latitude [ref]" s = phasecenter_str.split() ref = 'J2000' # default reference is J2000 if len(s) == 3: lat = qa.quantity(s[1]) lon = qa.quantity(s[0]) ref = s[2] elif len(s) == 2: lat = qa.quantity(s[1]) lon = qa.quantity(s[0]) else: raise ValueError( 'Invalid phasecenter: "{0}"'.format(phasecenter_str)) #print 'DEBUG rf={0} lon={1} lat={2}'.format(ref, lon, lat) direction = me.direction(rf=ref, v0=lon, v1=lat) return direction
def telescope_position(self, value): me = casa.CreateCasaMeasure() if value is None: # query to CASA database self._telescope_position = me.observatory(self.telescope) if len(self._telescope_position) == 0: raise ValueError('telescope "{0}" is unknown. you have to specify telescope position explicitly.'.format(self.telescope)) elif isinstance(value, dict) and me.ismeasure(value): self._telescope_position = value else: raise ValueError('Invalid telescope position: {0}'.format(value))
def phasecenter_string(self): value = '' me = casa.CreateCasaMeasure() if isinstance(self.phasecenter, str): value = self.phasecenter elif me.ismeasure(self.phasecenter): pdir = self.phasecenter qa = casa.CreateCasaQuantity() ra = qa.formxxx(pdir['m0'], 'hms', prec=8) dec = qa.formxxx(pdir['m1'], 'dms', prec=8) phase_direction = '{0} {1} {2}'.format(ra, dec, pdir['refer']) value = phase_direction else: value = str(self.phasecenter) return value
def test_custom_meta(self): me = casa.CreateCasaMeasure() # default imageparam imageparam = self.get_default_imageparam() # create image array arr = self.make_image_array(imageparam) # custom imagemeta imagemeta = paramcontainer.ImageMetaInfoContainer( observer='Takeshi Nakazato', telescope='NRO', observing_date=me.epoch('UTC', 'today'), rest_frequency='101GHz') # perform test with default imagemeta (None) self._run_test(imageparam, arr, imagemeta)
def _convert(self, timestamp, field_id, mfr, chan_freq): me = casa.CreateCasaMeasure() qa = casa.CreateCasaQuantity() vis = self.vis with casa.OpenTableForRead(os.path.join(vis, 'FIELD')) as tb: reference_dir = tb.getcell('PHASE_DIR', field_id)[:, 0] reference_frame = 'J2000' # hard coded lon = qa.quantity(reference_dir[0], 'rad') lat = qa.quantity(reference_dir[1], 'rad') mdirection = me.direction(rf=reference_frame, v0=lon, v1=lat) mepoch = me.epoch(rf='UTC', v0=qa.quantity(timestamp, 's')) mposition = me.observatory('ALMA') me.doframe(mdirection) me.doframe(mepoch) me.doframe(mposition) lsr_freq = numpy.empty_like(chan_freq) for ichan in range(len(lsr_freq)): mfrequency = me.frequency(rf=mfr, v0=qa.quantity(chan_freq[ichan], 'Hz')) converted = me.measure(mfrequency, rf='LSRK') lsr_freq[ichan] = converted['m0']['value'] return lsr_freq
def observing_date(self, value): me = casa.CreateCasaMeasure() if isinstance(value, dict) and me.ismeasure(value): self._observing_date = value else: self._observing_date = me.epoch('UTC', 'today')
def _setup_coordsys(self): csys = casa.CreateCasaCoordSys() me = casa.CreateCasaMeasure() qa = casa.CreateCasaQuantity() c = csys.newcoordsys(direction=True, spectral=True, stokes=self.imageparam.stokes) # direction coordinate phasecenter = self.imageparam.phasecenter print('DEBUG phasecenter={0}'.format(phasecenter)) refframe = me.getref(phasecenter) refpix = [int(x) // 2 for x in self.imageparam.imsize] center = me.getvalue(phasecenter) refval = [center['m0']['value'], center['m1']['value']] q2s = lambda x: '{0} {1}'.format(x['value'], x['unit']) srefval = list(map(q2s, [center['m0'], center['m1']])) incr = list(map(qa.quantity, self.imageparam.cell)) # increment of horizontal axis should be negative incr[0] = qa.mul(-1.0, incr[0]) sincr = list(map(q2s, incr)) projection = self.imageparam.projection print('DEBUG refpix={0}, refval={1}'.format(refpix, refval)) c.setdirection(refcode=refframe, proj=projection, refpix=refpix, refval=srefval, incr=sincr) # spectral coordinate refframe = 'LSRK' print('start {0} width {1}'.format(self.imageparam.start, self.imageparam.width)) start = qa.convert(self.imageparam.start, 'Hz') width = qa.convert(self.imageparam.width, 'Hz') nchan = self.imageparam.nchan f = numpy.fromiter( (start['value'] + i * width['value'] for i in range(nchan)), dtype=numpy.float64) print('f = {0}'.format(f)) frequencies = qa.quantity(f, 'Hz') veldef = 'radio' if len(f) > 1: c.setspectral(refcode=refframe, frequencies=frequencies, doppler=veldef) else: print('set increment for spectral axis: {0}'.format(width)) #c.setreferencepixel(value=0, type='spectral') #c.setreferencevalue(value=start, type='spectral') #c.setincrement(value=width, type='spectral') r = c.torecord() if 'spectral2' in r: key = 'spectral2' elif 'spectral1' in r: key = 'spectral1' r[key]['wcs']['crpix'] = 0.0 r[key]['wcs']['crval'] = start['value'] r[key]['wcs']['cdelt'] = width['value'] c.fromrecord(r) # Stokes coordinate # currently only 'I' image is supported #c.setstokes('I') #c.setincrement(value=1, type='stokes') # Meta info c.setobserver(self.imagemeta.observer) c.settelescope(self.imagemeta.telescope) c.setepoch(self.imagemeta.observing_date) rest_frequency = self.imagemeta.rest_frequency if isinstance(rest_frequency, str) and len(rest_frequency) == 0: f = frequencies['value'] nchan = len(f) if nchan % 2 == 0: c1 = (nchan - 1) // 2 c2 = c1 + 1 rest_frequency = qa.quantity(0.5 * (f[c1] + f[c2]), frequencies['unit']) else: c1 = (nchan - 1) // 2 rest_frequency = qa.quantity(f[c1], frequencies['unit']) print('rest_frequency={0}'.format(rest_frequency)) c.setrestfrequency(rest_frequency) print(c.summary(list=False)[0]) return c