Skip to content

Commit

Permalink
python3 compat
Browse files Browse the repository at this point in the history
  • Loading branch information
mhardcastle committed Jun 12, 2020
1 parent 44d6a6b commit cb6cbec
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
27 changes: 14 additions & 13 deletions radioflux/radioflux-ds9.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python

from __future__ import print_function
import pyregion
from astropy.io import fits
from astropy import wcs
Expand All @@ -21,26 +22,26 @@

bgsub=args.bgsub

print '-------------------------------------------------------------'
print('-------------------------------------------------------------')

print 'Filename is',filename
print 'FG region is <<'+f_region+'>>'
print 'BG region is <<'+b_region+'>>'
print('Filename is',filename)
print('FG region is <<'+f_region+'>>')
print('BG region is <<'+b_region+'>>')

if f_region=="":
print "FATAL ERROR: code can't work without at least one foreground region."
print("FATAL ERROR: code can't work without at least one foreground region.")
sys.exit()

try:
f=fits.open(filename)
except IOError as e:
print 'FATAL ERROR: ',e
print('FATAL ERROR: ',e)
sys.exit()

try:
rm=radiomap(f,verbose=True)
except RadioError as e:
print 'FATAL ERROR: ',e
print('FATAL ERROR: ',e)
sys.exit()

#print 'Frequency is %g Hz' % rm.frq
Expand All @@ -50,10 +51,10 @@
if b_region:
bg_ir=pyregion.parse(b_region).as_imagecoord(rm.headers[0])
bg=applyregion(rm,bg_ir)
print 'Pixels in background region',bg.pixels
print('Pixels in background region',bg.pixels)
for i in range(rm.nchans):
print '%8.4g Hz Background rms is %f Jy/beam' % (rm.frq[i],bg.rms[i])
print ' Background mean is',bg.mean[i],'Jy/beam'
print('%8.4g Hz Background rms is %f Jy/beam' % (rm.frq[i],bg.rms[i]))
print(' Background mean is',bg.mean[i],'Jy/beam')
noise=bg.rms
else:
if bgsub:
Expand All @@ -66,10 +67,10 @@
else:
fg=applyregion(rm,fg_ir,offsource=noise)

print 'Pixels in foreground region',fg.pixels
print('Pixels in foreground region',fg.pixels)
for i in range(rm.nchans):
freq=rm.frq[i]
if noise:
print '%8.4g Hz Region flux is %g +/- %g Jy' % (freq,fg.flux[i],fg.error[i])
print('%8.4g Hz Region flux is %g +/- %g Jy' % (freq,fg.flux[i],fg.error[i]))
else:
print '%8.4g Hz Region flux is %g Jy' % (freq,fg.flux[i])
print('%8.4g Hz Region flux is %g Jy' % (freq,fg.flux[i]))
25 changes: 13 additions & 12 deletions radioflux/radioflux.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python

from __future__ import print_function
import pyregion
import scipy.stats
from astropy.io import fits
Expand Down Expand Up @@ -66,7 +67,7 @@ def __init__(self, fitsfile,verbose=False):
if self.units is None:
self.units=self.prhd.get('UNIT')
if self.units!='JY/BEAM' and self.units!='Jy/beam':
print 'Warning: units are',self.units,'but code expects JY/BEAM'
print('Warning: units are',self.units,'but code expects JY/BEAM')
self.bmaj=self.prhd.get('BMAJ')
self.bmin=self.prhd.get('BMIN')
if self.bmaj is None:
Expand All @@ -75,7 +76,7 @@ def __init__(self, fitsfile,verbose=False):
self.bmin=self.prhd.get('RESOL2')
if self.bmaj is None:
if verbose:
print 'Can\'t find BMAJ in headers, checking history'
print('Can\'t find BMAJ in headers, checking history')
try:
history=self.prhd['HISTORY']
except KeyError:
Expand All @@ -101,11 +102,11 @@ def __init__(self, fitsfile,verbose=False):
self.bmaj/=cd1
self.bmin/=cd2
if verbose:
print 'beam is',self.bmaj,'by',self.bmin,'pixels'
print('beam is',self.bmaj,'by',self.bmin,'pixels')

self.area=2.0*np.pi*(self.bmaj*self.bmin)/(gfactor*gfactor)
if verbose:
print 'beam area is',self.area,'pixels'
print('beam area is',self.area,'pixels')

# Remove any PC... keywords we may have, they confuse the pyregion WCS
for i in range(1,5):
Expand All @@ -114,7 +115,7 @@ def __init__(self, fitsfile,verbose=False):

# Now check what sort of a map we have
naxis=len(fitsfile[0].data.shape)
if verbose: print 'We have',naxis,'axes'
if verbose: print('We have',naxis,'axes')
self.cube=False
if naxis<2 or naxis>4:
raise RadioError('Too many or too few axes to proceed (%i)' % naxis)
Expand All @@ -133,17 +134,17 @@ def __init__(self, fitsfile,verbose=False):
elif 'VOPT' in ctype:
pass
else:
print 'Warning: unknown CTYPE %i = %s' % (i,ctype)
print('Warning: unknown CTYPE %i = %s' % (i,ctype))
if verbose:
print 'This is a cube with freq axis %i and Stokes axis %i' % (freqaxis, stokesaxis)
print('This is a cube with freq axis %i and Stokes axis %i' % (freqaxis, stokesaxis))
if stokesaxis>0:
nstokes=self.prhd.get('NAXIS%i' % stokesaxis)
if nstokes>1:
raise RadioError('Multiple Stokes parameters present, not handled')
if freqaxis>0:
nchans=self.prhd.get('NAXIS%i' % freqaxis)
if verbose:
print 'There are %i channel(s)' % nchans
print('There are %i channel(s)' % nchans)
self.nchans=nchans
else:
self.nchans=1
Expand Down Expand Up @@ -198,7 +199,7 @@ def __init__(self, fitsfile,verbose=False):
print('Warning, can\'t get frequency %i -- set to zero' % i)
self.frq[i]=0
if verbose:
print 'Frequencies are',self.frq,'Hz'
print('Frequencies are',self.frq,'Hz')

def quiet_remove(self,keyname):
if self.prhd.get(keyname,None) is not None:
Expand Down Expand Up @@ -239,13 +240,13 @@ def printflux(filename,rm,region,noise,bgsub,background=0,label='',verbose=False
fg=applyregion(rm,region,offsource=noise)

if verbose:
print 'RMS values used:',fg.rms
print('RMS values used:',fg.rms)
for i in range(rm.nchans):
freq=rm.frq[i]
if noise:
print filename,label,'%8.4g %10.6g %10.6g' % (freq,fg.flux[i],fg.error[i])
print(filename,label,'%8.4g %10.6g %10.6g' % (freq,fg.flux[i],fg.error[i]))
else:
print filename,label,'%8.4g %10.6g' % (freq,fg.flux[i])
print(filename,label,'%8.4g %10.6g' % (freq,fg.flux[i]))

def flux_for_files(files,fgr,bgr=None,individual=False,bgsub=False,action=printflux,verbose=False):
"""Determine the flux in a region file for a set of files. This is the
Expand Down

0 comments on commit cb6cbec

Please sign in to comment.