from casatools import image, measures, quanta
from casatasks import casalog
def imreframe(imagename=None, output=None, outframe=None, epoch=None, restfreq=None):
casalog.origin('imreframe')
outframe=str_lower(outframe)
if(((outframe == 'topo') or (outframe=='geo')) and (epoch != '')):
me.doframe(me.observatory(c.telescope()))
me.doframe(c.referencevalue('m', 'direction')['measure']['direction'])
myret = c.findcoordinate('spectral')
hasspec = myret['return']
raise RuntimeError('Could not find spectral axis')
c.setconversiontype(spectral=outframe)
c.setrestfrequency(value=qa.quantity(restfreq, 'Hz'))
c.setepoch(me.epoch('utc', epoch))
me.doframe(me.epoch('utc', epoch))
myia.fromimage(outfile=output, infile=imagename, overwrite=True)
myia.setcoordsys(c.torecord())
c.setreferencecode(outframe, 'spectral', True)
reffreq=c.referencevalue('m', 'spectral')['measure']['spectral']['frequency']
newreffreq=me.measure(reffreq, outframe)
c.setreferencevalue(qa.tos(newreffreq['m0']), 'spectral')
outname='_temp_regrid_image' if(output=='') else output
ib=myia.regrid(outfile=outname, shape=shp, csys=c.torecord(),
axes=pixax, overwrite=True, asvelocity=False)
ib.setcoordsys(c.torecord())
ib.rename(name=imagename, overwrite=True)