Example #1
0
def addwenss(aipsname,
             indisk,
             ra,
             dec,
             maxoff,
             pmin,
             pmax,
             w_max=1000.,
             incl='IMAP'):
    if have_astLib:
        wenss = np.load('wenss2000.npy')
        wenss = wenss[wenss[:, 2] < 2]  # only single or centre of multiple
        wenss = wenss[wenss[:, 3] > w_max]  # only sources >w_max mJy
        a = correlate(np.array([[ra, dec]]), 0, 1, wenss, 0, 1, maxoff)
        fo = open('starsfile', 'w')
        for i in a:
            idx = int(i[1])
            sra = astCoords.decimal2hms(wenss[idx, 0], ' ')
            sdec = astCoords.decimal2dms(wenss[idx, 1], ' ')
            fo.write('%s %s\n' % (sra, sdec))
        fo.close()
        stars(aipsname,
              incl,
              indisk,
              intext='./starsfile',
              logfiledir=logfiledir)
    greys(aipsname, incl, indisk, pmin, pmax, 5, 1, logfiledir=logfiledir)
    lwpla(aipsname,
          incl,
          indisk,
          './' + aipsname + '_plot.ps',
          logfiledir=logfiledir)
    AIPSImage(aipsname, incl, indisk, 1).zap()
    os.system('rm starsfile')
Example #2
0
def read_secat(catfile, fitsfile, logfile=False, wcs=False):
  patchtable = [[] for dummy in xrange(3)]  #ra, dec, NHI
  #Must catch empty catalog file:
  try: #Call will fail if the catalog file is empty or does not have the expected format
    catalog = se_catalog(catfile, readfile=True, preserve_case=False)
  except:
    print "Warning: no clumps found. Creating empty entry."
    patchtable[0].append(0), patchtable[1].append(0)
    patchtable[2].append(0)
    return patchtable

  records = len(catalog.alphapeak_j2000)
  header, image = open_image(fitsfile)
  for n in range(records):
    a, d = catalog.alphapeak_j2000[n], catalog.deltapeak_j2000[n]
    x, y = header.wcs2pix(a, d)
    x = int(np.round(x))
    y = int(np.round(y))
    maxHI = image[y, x]
    print "a,d: ", a, d, "x,y: ", x, y, "value: ", maxHI #no offset? Verify!!
    patchtable[0].append(astCoords.decimal2hms(a, " "))
    patchtable[1].append(astCoords.decimal2dms(d, " "))
    patchtable[2].append(maxHI)
    #patchtable[3].append(0.1)    #error now calculated after calling this routine
    #print "Warning: hardcoding sNHI at 0.1"
    logdump(logfile, "{} {} {}\n".format(a, d, maxHI))

  return patchtable #calling function administrates PDRID
Example #3
0
    def _outputMinima(self):
        """
        Outputs the results to a file and also to the screen if debugging was turned on.
        """
        if 'chi' in self.fitting['method']:
            str = '{0:.2f}\t{1:.0f}\t{2:.0f}\t{3:.0f}\t{4:.0f}\t{5:.2f}\t\t{6:.2f}\n'.format(self.result['rotation'],
                                                                                             self.result['xcenter'],
                                                                                             self.result['ycenter'],
                                                                                             self.result['x'],
                                                                                             self.result['y'],
                                                                                             self.result[
                                                                                             'minimaPosition'][3],
                                                                                             self.result[
                                                                                             'minimaPosition'][4])
        elif 'corr' in self.fitting['method']:
            str = '{0:.2f}\t{1:.0f}\t{2:.0f}\t{3:.0f}\t{4:.0f}\t{5:.5f}\n'.format(self.result['rotation'],
                                                                                  self.result['xcenter'],
                                                                                  self.result['ycenter'],
                                                                                  self.result['x'],
                                                                                  self.result['y'],
                                                                                  self.result['minimaPosition'][6])
        else:
            raise NotImplementedError, 'This minimization method has not yet been implemented...'

        fh1 = open('min.txt', 'a')
        fh1.write(str)
        fh1.close()

        if self.debug:
            if 'chi' in self.fitting['method']:
                print '\n\nr \t x \t y \txoff \tyoff \tchi**2   reduced chi**2'
            elif 'corr' in self.fitting['method']:
                print '\n\nr \t x \t y \txoff \tyoff \tcorrelation coefficient'
            else:
                raise NotImplementedError, 'This minimization method has not yet been implemented...'
            print str
            print '\nInitial RA and DEC (of the centre of the centre slit)'
            print astCoords.decimal2hms(self.result['RAinit'], ':'), astCoords.decimal2dms(self.result['DECinit'], ':')
            print '\nFinal RA and DEC (of the centre of the centre slit)'
            print astCoords.decimal2hms(self.result['RA'], ':'), astCoords.decimal2dms(self.result['DEC'], ':')
            print '\nDistance on the sky (in arcseconds)'
            print astCoords.calcAngSepDeg(self.result['RAinit'],
                                          self.result['DECinit'],
                                          self.result['RA'],
                                          self.result['DEC']) * 3600
Example #4
0
def work(outpath, ra, dec, name):

    ra_sex = decimal2hms(ra, ':')
    dec_sex = decimal2dms(dec, ':')

    cmd = 'panstamps --width=40 --filters grizy '
    cmd += f'--downloadFolder={outpath}/{name} stack {ra_sex} {dec_sex}'
    print(cmd)
    p = subprocess.Popen(shlex.split(cmd))

    p.wait()
Example #5
0
 def info(self,showHeader=False):
     """
     @brief pretty print informstion sbout the litMap
     """
     arcmin = 180*60./np.pi
     print "Dimensions (Ny,Nx) = (%d,%d)"%(self.Ny,self.Nx)
     print "Pixel Scales: (%f,%f) arcmins. "%(self.pixScaleY*arcmin,self.pixScaleX*arcmin)
     print "Map Bounds: [(x0,y0), (x1,y1)]: [(%f,%f),(%f,%f)] (degrees)"%(self.x0,self.y0,self.x1,self.y1)
     print "Map Bounds: [(x0,y0), (x1,y1)]: [(%s,%s),(%s,%s)]"%\
           (astCoords.decimal2hms(self.x0,':'),\
            astCoords.decimal2dms(self.y0,':'),\
            astCoords.decimal2hms(self.x1,':'),\
            astCoords.decimal2dms(self.y1,':'))
     
     print "Map area = %f sq. degrees."%(self.area)
     print "Map mean = %f"%(self.data.mean())
     print "Map std = %f"%(self.data.std())
     
     if showHeader:
         print "Map header \n %s"%(self.header)
Example #6
0
 def info(self,showHeader=False):
     """
     @brief pretty print informstion sbout the litMap
     """
     arcmin = 180*60./np.pi
     print "Dimensions (Ny,Nx) = (%d,%d)"%(self.Ny,self.Nx)
     print "Pixel Scales: (%f,%f) arcmins. "%(self.pixScaleY*arcmin,self.pixScaleX*arcmin)
     print "Map Bounds: [(x0,y0), (x1,y1)]: [(%f,%f),(%f,%f)] (degrees)"%(self.x0,self.y0,self.x1,self.y1)
     print "Map Bounds: [(x0,y0), (x1,y1)]: [(%s,%s),(%s,%s)]"%\
           (astCoords.decimal2hms(self.x0,':'),\
            astCoords.decimal2dms(self.y0,':'),\
            astCoords.decimal2hms(self.x1,':'),\
            astCoords.decimal2dms(self.y1,':'))
     
     print "Map area = %f sq. degrees."%(self.area)
     print "Map mean = %f"%(self.data.mean())
     print "Map std = %f"%(self.data.std())
     
     if showHeader:
         print "Map header \n %s"%(self.header)
Example #7
0
def writeDS9(nodes,regionfile):
	f = open(regionfile,'w')
	f.write('# Region file format: DS9 version 4.1\n')
	f.write('global color=green dashlist=8 3 width=1 font="helvetica 10 normal roman" select=1 highlite=1 dash=0 fixed=0 edit=1 move=1 delete=1 include=1 source=1\n')
	f.write('fk5\n')
	for i in range(0,len(nodes)):
		ra = nodes[i][0]
		dec = nodes[i][1]
		ra_hms = ac.decimal2hms(ra,delimiter=':')
		dec_dms = ac.decimal2dms(dec,delimiter=':')
		f.write('circle('+ra_hms+','+dec_dms+',1.0)\n')
	f.close()
Example #8
0
def create_lobos_ms (infile='',ra='',dec='',nP=3,radius=2.5):
    infile='L401323_SB349_uv.dppp.MS'
    ra,dec,radius,nP='13:40:00','55:00:00',2.5,3
    try:    # ra, dec input can either be decimal or hh:mm:ss, dd:mm:ss
        ra, dec = 1.0*ra, 1.0*dec
        hexra = astCoords.decimal2hms(ra,':')
        hexdec = astCoords.decimal2dms(dec,':')
    except:
        hexra, hexdec = ra, dec
        ra = astCoords.hms2decimal(ra,':')
        dec = astCoords.dms2decimal(dec,':')
    if not os.path.isfile ('lobos_stats.sum'):
        os.system ('wget http://www.jb.man.ac.uk/~njj/lobos_stats.sum')
    lobos = np.loadtxt('lobos_stats.sum',dtype='S')
    for l in lobos:
        new = np.array([astCoords.hms2decimal(l[1],':'),\
                        astCoords.dms2decimal(l[2],':')])
        try:
            lobos_coord = np.vstack((lobos_coord,new))
        except:
            lobos_coord = np.copy(new)
    a = correlate(np.array([[ra,dec]]),0,1,lobos_coord,0,1,radius)
    nlobos = 0
    os.system('rm lshift_*')
    for i in np.asarray(a[:,1],dtype='int'):
        if lobos[i][5].count('P')>=nP:
            oroot = infile.replace('uv.dppp.MS','')
            namera = lobos[i,1].replace(':','').split('.')[0]
            namedec = lobos[i,2].replace(':','').split('.')[0]
            dpppra = lobos[i,1].replace(':','h',1).replace(':','m',1)+'s'
            dpppdec = lobos[i,2].replace(':','d',1).replace(':','m',1)+'s'
            hemisphere = '-' if '-' in hexdec else '+'
            f = open('lshift_%03d'%nlobos, 'w')
            f.write('msin = '+infile+'\n')
            f.write('msout = '+oroot+namera+hemisphere+namedec+'.ms\n')
            f.write('msin.datacolumn = DATA\n')
            f.write('steps = [shift,adder,filter,avg]\n')
            f.write('shift.type = \'phaseshift\'\n')
            f.write('shift.phasecenter = [\''+dpppra+'\',\''+dpppdec+'\']\n')
            f.write('adder.type = \'stationadder\'\n')
            f.write('adder.stations = {TS001:\'CS*\'}\n')
            f.write('filter.type = \'filter\'\n')
            f.write('filter.baseline = \'!CS*&*\'\n')
            f.write('avg.type = average\n')
            f.write('avg.freqstep = 16\n')
            f.write('avg.timestep = 20\n')
            f.close()
            nlobos+=1

    lshift_thread.parallel = parallel_function(lshift_thread)
    k = range(nlobos)
    parallel_result = lshift_thread.parallel(k)
Example #9
0
def addwenss (aipsname,indisk,ra,dec,maxoff,pmin,pmax,w_max=1000.,incl='IMAP'):
    if have_astLib:
        wenss = np.load('wenss2000.npy')
        wenss = wenss[wenss[:,2]<2]   # only single or centre of multiple
        wenss = wenss[wenss[:,3]>w_max]  # only sources >w_max mJy
        a = correlate (np.array([[ra,dec]]),0,1,wenss,0,1,maxoff)
        fo = open('starsfile','w')
        for i in a:
            idx = int(i[1])
            sra = astCoords.decimal2hms(wenss[idx,0],' ')
            sdec = astCoords.decimal2dms(wenss[idx,1],' ')
            fo.write('%s %s\n' % (sra,sdec))
        fo.close()
        stars (aipsname,incl,indisk,intext='./starsfile',logfiledir=logfiledir)
    greys (aipsname,incl,indisk,pmin,pmax,5,1,logfiledir=logfiledir)
    lwpla (aipsname,incl,indisk,'./'+aipsname+'_plot.ps',logfiledir=logfiledir)
    AIPSImage(aipsname,incl,indisk,1).zap()
    os.system('rm starsfile')
Example #10
0
def first_download (ra,dec,outfile='',gif=0,fits=1,imsize=2.0,\
    imserver='third.ucllnl.org'):
    format = 'hms'
    try:
        if np.isreal(ra):
            format = 'decimal'
    except:
        pass
    if format=='decimal':
        ra=astCoords.decimal2hms(ra,' ')
        dec=astCoords.decimal2dms(dec,' ')
    if outfile=='':
        outfile=ra.replace(' ','')+dec.replace(' ','')
        outfile=outfile + ('.fits' if fits==1 else '.gif')
    ra=ra.split()
    dec=dec.split()
    command = ('wget -O %s "http://%s/cgi-bin/firstimage?RA=%s%%20%s%%20%s%%20%s%%20%s%%20%s&Dec=&Equinox=J2000&ImageSize=%.1f&MaxInt=10&GIF=%d&FITS=%d&Download=1"'%(outfile,imserver,ra[0],ra[1],ra[2],dec[0],dec[1],dec[2],imsize,gif,fits))
    print (command)
    os.system(command)
Example #11
0
def wcslabels(wcs, xlim, ylim, xsep='00:00:01', ysep='00:00:15',
              ax=None, label_color='k', rotate_x=0, rotate_y=90):
    """
    Get WCS ticklabels

    Parameters
    ----------
        wcs     : astWCS.WCS instance
                  the wcs of the image to be shown
        xlim    : sequence of length 2
                  the minimum and maximum values of the x axis
        ylim    : sequence of length 2
                  the minimum and maximum values of the y axis
        xsep    : string
                  separation of right ascension ticks in the x axis,
                  in colon-separated hms format
        xsep    : string
                  separation of declination ticks in the y axis, in
                  colon-separated dms format
        ax      : matplotlib.Axes instance (optional)
                  if provided, the ticks will be displayed on it
        label_color : string or matplotlib color
                  color with which the tick labels will be displayed,
                  if ax is provided
        rotate_x : float
                  by how much to rotate the x tick labels if ax is
                  provided
        rotate_y : float
                  by how much to rotate the y tick labels if ax is
                  provided

    Returns
    -------
        [xticks, xticklabels] : lists containing the positions and
                  labels for right ascension hms labels
        [yticks, yticklabels] : lists containing the positions and
                  labels for declination dms labels

    """
    def format_wcs(x):
        """
        replace the 60's for 0's and change other values consistently,
        and add 0's at the beginning of single-digit values
        """
        x = x.split(':')
        x[2] = round(float(x[2]), 0)
        x[2] = '{0:.0f}'.format(x[2]) if x[2] >= 10 \
                else '0{0:.0f}'.format(x[2])
        for i in (1, 0):
            if x[i+1] == '60':
                if x[0][0] == '-':
                    if i == 0:
                        x[i] = '-{0}'.format(str(int(x[i]) - 1))
                    else:
                        x[i] = str(int(x[i]) - 1)
                else:
                    x[i] = str(int(x[i]) + 1)
                x[i+1] = '00'
        for i in xrange(len(x)):
            if 0 <= int(x[i]) < 10:
                x[i] = '0{:.0f}'.format(int(x[i]))
            elif -10 < int(x[i]) < 0:
                x[i] = '-0{:.0f}'.format(-int(x[i]))
        return ':'.join(x)
    left, right = xlim
    bottom, top = ylim
    wcslim = [wcs.pix2wcs(left, bottom), wcs.pix2wcs(right, top)]
    ralim, declim = numpy.transpose(wcslim)
    rasep = astCoords.hms2decimal(xsep, ':')
    decsep = astCoords.dms2decimal(ysep, ':')
    raticks = numpy.arange(0, max(ralim), rasep)
    raticks = raticks[raticks > min(ralim)]
    decticks = numpy.arange(-90, max(declim), decsep)
    decticks = decticks[decticks > min(declim)]
    # this assumes that the rotation angle of the image is 0/90/180/270
    # degrees
    xticks = [wcs.wcs2pix(x, declim[0])[0] for x in raticks]
    yticks = [wcs.wcs2pix(ralim[0], y)[0] for y in decticks]
    xticklabels = [astCoords.decimal2hms(t, ':') for t in raticks]
    yticklabels = [astCoords.decimal2dms(t, ':').replace('+', '')
                   for t in decticks]
    # format properly (remove 60's and add 0's)
    xticklabels = [format_wcs(xt) for xt in xticklabels]
    yticklabels = [format_wcs(yt) for yt in yticklabels]
    # get tick positions for rounded labels
    raticks = [astCoords.hms2decimal(xt, ':') for xt in xticklabels]
    decticks = [astCoords.dms2decimal(yt, ':') for yt in yticklabels]
    xticks = [wcs.wcs2pix(x, declim[0])[0] for x in raticks]
    yticks = [wcs.wcs2pix(ralim[0], y)[1] for y in decticks]
    # display?
    if ax:
        ax.set_xticks(xticks)
        ax.set_yticks(yticks)
        ax.set_xticklabels(xticklabels, color=label_color, rotation=rotate_x)
        ax.set_yticklabels(yticklabels, color=label_color, rotation=rotate_y)
    return [xticks, xticklabels], [yticks, yticklabels]
Example #12
0
def main():

    __version_info__ = (0,0,1)
    __version__ = ".".join( map(str,__version_info__) )

    for i, arg in enumerate(sys.argv):
        if (arg[0] == '-') and arg[1].isdigit(): sys.argv[i] = ' ' + arg

    parser = ArgumentParser(description='Image based stacking tool S Makhathini <*****@*****.**>')

    add = parser.add_argument
    add("-v","--version", action='version',version='{:s} version {:s}'.format(parser.prog, __version__))

    add("-i", "--image", 
            help="FITS image name")

    add("-c", "--catalog", metavar="CATALOG_NAME:DELIMITER",
            help="Catalog name. Default delimiter is a comma. Format: 'ra,dec,freq' ")

    add("-p", "--prefix", default="gota_stackem_all",
            help="Prefix for output products.")

    add("-w", "--width", type=float, default=0,
            help="For line stacking: Band width [MHz] to sample across frequency (for line stacking). Default is 1."
                 "For continuum stacking: Width (in beams) of subregion to stack. Default is 10 beams")

    add("-vbl", "--vebosity-level", dest="vbl", choices=["0", "1", "2", "3"], default="0",
            help="Verbosity level. 0-> INFO, 1-> DEBUG, 2-> ERROR, 3-> CRITICAL. Default is 0")

    add("-b", "--beam", metavar="BMIN[:BMIN:BPA]",
            help="PSF (a.k.a dirty beam) FWHM in degrees. No default")

    add("-b2p", "--beam2pix", action="store_true",
            help="Do Jy/beam to Jy/pixel conversion")

    add("-L", "--line", action="store_true",
            help="Do line stacking")

    add("-C", "--cont", action="store_true",
            help="Do continuum stacking")

    add("-mc", "--monte-carlo", metavar="SAMPLES:START:FINISH:N", default=False,
            help="Do a monte carlo analysis of the noise. That is, "
                  "stack on START random positions NSAMPLES times. "
                  "Repeat this N times with (FINISH-START)/N increments."
                  "if set -mc/--mont-carlo=yes, default is '400:1000:8000:7'")

    args = parser.parse_args()

    if args.catalog:
        catalog_string = args.catalog.split(":")
        if len(catalog_string)>1:
            catalgname, delimiter = catalog_string
        else:
            catalogname, delimiter = catalog_string[0], ","

    if args.beam:
        beam = args.beam.split(":")
        if len(beam)==1:
            beam = float(beam[0])
        elif len(beam)==2:
            beam = map(float, beam) + [0]
        else:
            beam = map(float, beam)
    else:
        beam = None

    pylab.clf()

    prefix = args.prefix or "stackem_default"
    pylab.figure(figsize=(15,10))

    if args.line:
        from Stackem import LineStacker

        stack = LineStacker.load(args.image, catalogname, delimiter=delimiter,
                beam=beam, width=args.width, beam2pix=args.beam2pix,
                verbosity=args.vbl)

        stacked_line = stack.stack()*1e6 # convert to uJy
        peak, nu, sigma = gfit_params = stack.fit_gaussian(stacked_line)
        gfit = utils.gauss(range(stack.width), *gfit_params)

        freqs = (numpy.linspace(-stack.width/2, stack.width/2, stack.width)*stack.dfreq - stack.freq0)*1e-9 # convert to GHz

        # plot profile
        pylab.plot(freqs, stacked_line, "r.", label="stacked profile")
        pylab.plot(freqs, gfit, "k-", label="Gaussian fit")
        pylab.grid()
        pylab.xlim(freqs[0], freqs[-1])
        pylab.xlabel("Frequency [GHz]")
        pylab.ylabel("Flux density [mJy/{:s}]".format("beam" if args.beam2pix else "pixel"))
        pylab.legend(loc=1)
        pylab.savefig(prefix+"-line.png")
        pylab.clf()

        # save
        numpy.savetxt(prefix+"-line.txt", stacked_line, delimiter=",")

        with open(prefix+"-line_stats.txt", "w") as info:
            tot = sigma*peak*math.sqrt(2*math.pi)

            stack.log.info("Gaussian Fit parameters.")
            info.write("Gaussian Fit parameters\n")

            info.write("Peak Flux : {:.4g} uJy\n".format(peak))
            stack.log.info("Peak Flux : {:.4g} uJy".format(peak))

            info.write("Integrated Flux : {:.4g} uJy\n".format(tot))
            stack.log.info("Integrated Flux : {:.4g} uJy".format(tot))

            info.write("Profile width : {:.4g} MHz \n".format(sigma*stack.dfreq))
            stack.log.info("Profile width : {:.4g} kHz".format(sigma*stack.dfreq*1e-3))

        stack.log.info("Line stacking ran successfully. Find your outputs at {:s}-line*".format(prefix))

    if args.cont:
        pylab.figure(figsize=(20, 20))
        from Stackem import ContStacker

        stack = ContStacker.load(args.image, catalogname, delimiter=delimiter, 
                beam=beam, beam2pix=args.beam2pix, width=int(args.width),
                verbosity=args.vbl)

        stacked = stack.stack()

        mask = utils.elliptical_mask(stacked, stack.bmajPix/2., stack.bminPix/2., stack.bpa)

        flux = utils.gauss_weights(stacked, stack.bmajPix/2., stack.bmajPix/2., mask=mask)
        flux = flux.sum()

        rms = utils.negnoise(stacked)

        with open(prefix+"-cont.txt", "w") as cont:
            stack.log.info("Stacked flux: {:.3g} +/- {:.3g} uJy".format(flux*1e6, rms*1e6))
            cont.write("Stacked flux: {:.3g} +/- {:.3g} uJy/beam".format(flux*1e6, rms*1e6))

        # Plot stacked image, and cross-sections

        rotated = numpy.rot90(stacked.T)
        import matplotlib.gridspec as gridspec
        
        gs = gridspec.GridSpec(2, 2,
                               width_ratios = [4,1],
                               height_ratios = [1,4])

        gs.update(wspace=0.05, hspace=0.05)
        ax1 = pylab.subplot(gs[2])
        ax2 = pylab.subplot(gs[0], sharex=ax1)
        ax3 = pylab.subplot(gs[3], sharey=ax1)

        pylab.setp(ax3.get_yticklabels(), visible=False)
        pylab.setp(ax3.get_xticklabels(), rotation=90)
        pylab.setp(ax2.get_xticklabels(), visible=False)

        ax1.imshow(stacked, interpolation="nearest")
        ax1.set_aspect("auto")
        ra0, dec0 = stack.wcs.getCentreWCSCoords()
        ras = numpy.linspace(stack.width/2, -stack.width/2, stack.width)*stack.cell - ra0
        decs = numpy.linspace(-stack.width/2, stack.width/2, stack.width)*stack.cell - dec0

        ax1.set_xticklabels( map(lambda x: coords.decimal2hms(x, ":"), ras) )
        ax1.set_yticklabels( map(lambda x: coords.decimal2dms(x, ":"), decs) )
        ax1.set_xlabel("RA [hms]")
        ax1.set_ylabel("DEC [dms]")
        pylab.setp(ax1.get_xticklabels(), rotation=90)

        ax2.plot(rotated[:, stack.width/2]*1e6)
        ax3.plot(rotated[stack.width/2,:][::-1]*1e6, range(stack.width))
        ax3.set_ylim(0, stack.width-1)
        ax2.set_xlim(0, stack.width-1)
        ax2.set_ylabel("Flux density [uJy/beam]")
        ax3.set_xlabel("Flux density [uJy/beam]")
        pylab.savefig(prefix+"-cont.png")

        # save stacked image as fits
        hdr = stack.hdr
        hdr["crpix1"] = stack.width/2 
        hdr["crpix2"] = stack.width/2 
        pyfits.writeto(prefix+"-cont.fits", stacked, hdr, clobber=True)

        stack.log.info("Continuum stacking ran successfully. Find your outputs at {:s}-line*".format(prefix))

    if args.monte_carlo:
        if args.monte_carlo in ["yes", "1", "True", "true"]:
            runs, stacks = 400, xrange(1000, 8000, 1000)
        else:
            a, b, c, d = map(int, args.monte_carlo.split(":"))
            runs, stacks = a, xrange(b, c, (c-b)/d)

        from Stackem import ContStacker
        
        noise = ContStacker.mc_noise_stack(args.image, runs=runs, stacks=stacks, 
                beam=beam, beam2pix=args.beam2pix, width=args.width,
                verbosity=args.vbl)

        pylab.clf()
        pylab.loglog(stacks, noise)
        pylab.grid()
        pylab.ylabel("Log (Noise [Jy/beam])")
        pylab.xlabel("Log (Random Stacks). {:d} Samples".format(runs))
        pylab.savefig(prefix+"-cont_mc.png")
        pylab.clf()
Example #13
0
def update_data_object(args):
    import pickle as p
    from astLib import astCoords
    from glob import glob

    ### ALL QSOs observed and added to the web:
    allQSOs = glob('/Users/krogager/Sites/redQSOs/images/*.png')
    observedQSOs = []
    for qso in allQSOs:
        name = qso.split('/')[-1].split('.')[0]
        observedQSOs.append(name)

    SDSS = p.load(open('/Users/krogager/Projects/redQSOs/OptNIR/SDSS_phot.p'))

    metaData = np.genfromtxt(
        '/Users/krogager/Projects/redQSOs/QSO_sampleData.txt',
        delimiter='\t',
        dtype={
            'names': [
                'name', 'z', 'Ab', 'IR', 'BAL', 'nored', 'SMC', 'other',
                'noabs', 'narrow', 'strong', 'weak', 'intervening'
            ],
            'formats': ['S11', 'f8', 'f8'] + ['i8' for i in range(10)]
        })

    js_out = []
    for id, objID, ra, dec, type, u, e_u, g, e_g, r, e_r, i, e_i, z, e_z, specObjID, redshift, primTarget in SDSS:
        ra_hex = astCoords.decimal2hms(ra, ':')
        dec_hex = astCoords.decimal2dms(dec, ':')
        if id in observedQSOs:
            obs = 1
        else:
            obs = 0

        if id in metaData['name']:
            ij = metaData['name'].tolist().index(id)
            spec_z = metaData['z'][ij]
            Ab = metaData['Ab'][ij]
            BAL = int(metaData['BAL'][ij])
            nored = int(metaData['nored'][ij])
            SMC = int(metaData['SMC'][ij])
            other = int(metaData['other'][ij])
            noabs = int(metaData['noabs'][ij])
            narrow = int(metaData['narrow'][ij])
            strong = int(metaData['strong'][ij])
            weak = int(metaData['weak'][ij])
            DLA = int(metaData['intervening'][ij])
        else:
            spec_z = -1
            Ab = -1
            BAL = 0
            nored = 0
            SMC = 0
            other = 0
            noabs = 0
            narrow = 0
            strong = 0
            weak = 0
            DLA = 0

        js_out.append("'%s': {id:%d, sid:%d, ra:%f, dec:%f, ra_hex:'%s', dec_hex:'%s', redshift:%s,\
u:%.2f, g:%.2f, r:%.2f, i:%.2f, z:%.2f, obs:%i, spec_z:%.2f, Ab:%.2f, BAL:%i,nored:%i,SMC:%i,other:%i,\
noabs:%i,narrow:%i,strong:%i,weak:%i,DLA:%i}"       \
         %(id,objID,specObjID,ra,dec,ra_hex,dec_hex,redshift,u,g,r,i,z,obs,spec_z,Ab,BAL,nored,SMC,other,\
          noabs,narrow,strong,weak,DLA) )

    js = open('/Users/krogager/Sites/redQSOs/redQSOs_data.js', 'w')
    now = datetime.datetime.now()
    js.write("//Last update:\n")
    js.write("var last_update='" + now.strftime("%B %d %Y") + "';\n\n")

    js.write("var observedQSOs=['")
    js.write("','".join(observedQSOs))
    js.write("'];\n")

    js.write("\n")

    js.write("var redQSOs={\n")
    js.write(',\n'.join(js_out))
    js.write("};\n")
    js.close()

    if not args.quiet:
        print "\n [OK]  Successfully updated redQSOs_data.js \n"
# our info
table['RA'] = nan
table['DEC'] = nan
table['Dist'] = nan
table['z'] = m['zBCG_boada']
table['Mag Lim'] = m['Cmag_i']
#table['N'] = nan

# extern info
table['RA EX'] = m['RA_y']
table['DEC EX'] = m['DEC_y']
table['Dist EX'] = nan
table['z EX'] = m['z_extern']
#table['N EX'] = m['R']
table['Flag'] = m['Flag']
table['Ref'] = m['REDSHIFT_SOURCE']

fixed1 = m.loc[pd.notnull(m['RAJ2000']),
               'RAJ2000'].apply(lambda x: astCoords.decimal2hms(x, ':'))
fixed2 = m.loc[pd.notnull(m['DEJ2000']),
               'DEJ2000'].apply(lambda x: astCoords.decimal2dms(x, ':'))

table.loc[fixed1.index, 'RA EX'] = fixed1
table.loc[fixed2.index, 'DEC EX'] = fixed2

for i, row in table.iterrows():
    table.loc[i, 'Dist EX'] = astCoords.calcAngSepDeg(
        row['RA PSZ'], row['DEC PSZ'], row['RA EX'], row['DEC EX']) * 60

print('done')
Example #15
0
 def testdecimal2dms(self):
     for dec, result in self.decimal2dms:
         answer = astCoords.decimal2dms(dec, ':')
         self.assertEqual(result, answer)
Example #16
0
 def dms(self):
     dms_orig = '12:34:56.78'
     decimal = astCoords.dms2decimal(dms_orig, ':')
     dms_new = astCoords.decimal2dms(decimal, ':')
     self.assertEqual(dms_new, dms_orig)
Example #17
0
    def wds9reg(self,
                regfile=None,
                color='red',
                circlesize=10,
                width=1,
                xcol='X_IMAGE',
                ycol='Y_IMAGE',
                textcol=None,
                criteria=None):
        """ write sources to a ds9 region file.
        circlesize may be a scalar or an array with one size 
        for each object in the catalog. Criteria can now be used to only write certian regions 
        to the file from the catelog. criteria is a tuple with values (colname,comparison_operator,value).
        Eg.     criteria = ('EXT_IMAGE','==',1)
        """
        import numpy as np
        import os
        #import exceptions

        if (xcol not in self.colnames or ycol not in self.colnames):
            print("ERROR: cannot make a ds9 region file without %s,%s" %
                  (xcol, ycol))
            return (-1)

        xcolname = xcol.lower()
        convertRADEC = False
        if ('alpha' in xcolname) or ('ra' in xcolname) or (
                'r.a.' in xcolname) or ('world' in xcolname):
            if isinstance(self.coldata[xcol][0], float):
                try:
                    from astLib import astCoords
                    print(
                        "Converting RA,DEC from decimal degrees to hh:mm:ss,dd:mm:ss"
                    )
                    convertRADEC = True
                except:
                    print("Can't load astLib for RA,DEC conversion!")
                    return (-1)

        if not regfile:
            regfile = os.path.splitext(self.filename)[0] + '.reg'

        fout = open(regfile, 'w')
        print >> fout, "# Region file format: DS9 version 5.4"
        print >> fout, """global color=%s font="times 16 bold" select=1 edit=1 move=0 delete=1 include=1 fixed=0 width=%i""" % (
            color, width)
        for irow in range(self.nrows):
            if np.iterable(circlesize): cs = circlesize[irow]
            else: cs = circlesize

            textstr = ''
            if textcol:
                # textcol may be a comma-sep'd list of col names.
                textvals = []
                for tc in textcol.split(','):
                    tval = self.coldata[tc][irow]
                    if type(tval) in [
                            int, np.int, np.int16, np.int32, np.int64, np.int8,
                            np.int_
                    ]:
                        textvals.append('%i' % tval)
                    elif type(tval) in [
                            float, np.float, np.float32, np.float64,
                            np.float128, np.float_
                    ]:
                        textvals.append('%.2f' % tval)
                    else:
                        textvals.append(str(tval))
                textstr = '# text={%s}' % ':'.join(textvals)

            #Only write the line to the file if it passes the criteria passed to this function.
            #This allows us to separate extensions for makeing flt region files in the fakes
            if criteria is None: write_line = True
            else:
                write_line = eval("self.coldata['%s'][irow] %s %s" % criteria)

            if write_line:
                cstr = '%.1f' % cs
                x, y = self.coldata[xcol][irow], self.coldata[ycol][irow]
                if isinstance(x, float):
                    xstr, ystr = '%.8f' % x, '%.8f' % y
                    if convertRADEC:
                        xstr = astCoords.decimal2hms(x, delimiter=':')
                        ystr = astCoords.decimal2dms(y, delimiter=':')
                        if cs > 1: cs = min(0.75, cs * 0.06)
                        cstr = '%.3f"' % (cs)
                print >> fout, 'circle(%s,%s,%s)%s' % (xstr, ystr, cstr,
                                                       textstr)

        fout.close()
        return (regfile)
Example #18
0
def dms(dec):
    try:
        return astCoords.decimal2dms(dec, ':')
    except UnboundLocalError:
        return 'NaN'
Example #19
0
def main(round=3):
    ''' This creates a simple latex table with the results using the columns
    specified below. A few things will need to be done by hand after this is
    created. The corrected errors to redshifts, and corrected Ngals will need
    to be manually added by looking at the results images. The cluster finder
    doesn't save those columns by default.

    '''

    # the confirmed = True gets the 12 confirmed clusters
    results = loadClusters(round=round, confirmed=True)

    # load the master spreadsheet
    t_ss = Table.read('../catalogs/PSZ2_unconfirmed_catalog - current.csv')
    df_ss = t_ss.to_pandas()

    observed = df_ss.loc[~df_ss['MOSAIC Imaging'].isnull()]

    confirmed = observed.merge(results, left_on='Name', right_on='Cluster',
                               how='left')

    # load the PSZ1 catalog
    t_1 = Table.read('../catalogs/PSZ1v2.1.fits')
    df1 = t_1.to_pandas()

    # now we add all of the extra info
    # Berrena paper
    Bpaper = Table.read('../papers/1803.05764/Barrena_tbl3.csv')
    df_paper = Bpaper.to_pandas()

    complete = confirmed.merge(df_paper, left_on='Name', right_on='Planck Name',
                               how='left')

    # tack on the PSZ1 catalog
    complete = complete.merge(df1, left_on='PSZ1 Indx', right_on='INDEX',
                              how='left')

    # combine some columns to get extra info
    complete['z_extern'] = complete['PSZ1 Redshift'].fillna(complete['z_cl'])
    complete['S/N'] = complete['SNR_PSZ2'].fillna(complete['SNR_PSZ1'])

    # get the columns we want
    cols = ['Name', 'PSZ1 Indx', 'PSZ2 Indx', 'S/N', 'RA_SEX', 'DEC_SEX',
            'dist_BCG', 'z_cl_boada', 'z_clerr_boada', 'Ngal_c_boada', 'Cmag_i',
            'z_extern', 'REDSHIFT_SOURCE', 'BCG_boada']

    c = complete.loc[:, cols]

    c.loc[:, ('RA_SEX', 'DEC_SEX')] = nan

    c.loc[(~c['z_extern'].isnull()) & (c['REDSHIFT_SOURCE'] == -1.0),
          'REDSHIFT_SOURCE'] = 99

    # let's the the RA/DEC of our BCGs
    m = c.loc[c['BCG_boada'].notnull()]

    for i, row in m.iterrows():
        mems = loadMembers('boada', row['Name'], round=round)
        try:
            ra = mems.loc[mems['ID'] == row['BCG_boada'], 'RA'].values[0]
        except IndexError:
            continue
        dec = mems.loc[mems['ID'] == row['BCG_boada'], 'DEC'].values[0]
        # convert to sexigesimal
        ra_sex = astCoords.decimal2hms(ra, ':')
        dec_sex = astCoords.decimal2dms(dec, ':')

        # compute the distance to the PSZ position
        psz_ra = complete.iloc[i]['RA_x']
        psz_dec = complete.iloc[i]['DEC_x']

        m.loc[i, 'dist_BCG'] = astCoords.calcAngSepDeg(psz_ra,
                                                       psz_dec, ra, dec) * 60

        # write it back into the main frame
        m.loc[i, 'RA_SEX'] = ra_sex
        m.loc[i, 'DEC_SEX'] = dec_sex

    return m
Example #20
0
    reader = open(inFile, "r")
    lines = reader.readlines()
    reader.close()

    writer = open(outFile, "w")
    for row in lines:
        if len(row)>1:
            if "#" not in row[:1]:
                rowBits = row.split("\t")
                if way == "to_decimal":
                    RADeg = astCoords.hms2decimal(rowBits[RACol], delimiter)
                    decDeg = astCoords.dms2decimal(rowBits[decCol], delimiter)
                if way == "to_hmsdms":
                    RADeg = astCoords.decimal2hms(float(rowBits[RACol]),
                            delimiter)
                    decDeg = astCoords.decimal2dms(float(rowBits[decCol]),
                            delimiter)
                writeString = ""
                for i in range(len(rowBits)):
                    if i == RACol:
                        writeString = writeString+str(RADeg)+"\t"
                    elif i == decCol:
                        writeString = writeString+str(decDeg)+"\t"
                    elif rowBits[i].find("\n") != -1:
                        writeString = writeString+str(rowBits[i])
                    else:
                        writeString = writeString+str(rowBits[i])+"\t"
                # new line character already included
                writer.write(writeString.replace("\n", "").replace("\t",
                    colDelim)+"\n")
            else:
                writer.write(row.replace("\t", colDelim))
def testRandomizer():
    #calculate a test case
    conversion = 0.000277777778 # degree to arcsecond
    #random distance between z = 0 and 5
    z = np.random.rand() * 5.0
    #random separation of the main galaxy and the subhalo
    physical_distance = np.random.rand() * 1e3 #in kpc
    #get the angular diameter distance to the galaxy
    dd1 = cosmocalc(z, 71.0, 0.28)['PS_kpc'] #in kpc / arc seconds
    dd = (physical_distance / dd1) * conversion # to degrees
    # RA and DEC of the main galaxy, first one in GOODS south
    ra_main = 52.904892 #in degrees
    dec_main = -27.757082 #in degrees
    # get the random position
    rds = rand.randomUnitSphere(points=1)
    # convert the position to Cartesian coord
    # when dd is the radius of the sphere coordinates
    # the dd is in units of degrees here so the results
    # are also in degrees.
    rd = conv.convertSphericalToCartesian(dd,
        rds['theta'],
        rds['phi'])


    # Make the assumption that x is towards the observer
    # z is north and y is west. Now if we assume that our z and y
    # are Standard Coordinates, i.e., the projection of the RA and DEC
    # of an object onto the tangent plane of the sky. We can now
    # assume that the y coordinate is aligned with RA and the z coordinate
    # is aligned with DEC. The origin of this system is at the tangent point
    # in the dark matter halo.
    # Poor man's solution to the problem would be:
    new_ra = ra_main - (rd['y'][0] / np.cos(rd['z'][0]))
    new_dec = dec_main + rd['z'][0]
    # However, this only works if one is away from the pole.
    # More general solution can be derived using spherical geometry:
    data = {}
    data['CD'] = np.matrix('-1 0; 0 1')
    data['RA'] = ra_main
    data['DEC'] = dec_main
    data['X'] = rd['y'][0]
    data['Y'] = rd['z'][0]
    result = conv.RAandDECfromStandardCoordinates(data)

    #print the output
    print 'Redshift of the galaxy is %.3f while the subhaloes distance is %0.2f kpc' % (z, physical_distance)
    print '\nCoordinates of the main halo galaxy are (RA and DEC):'
    print '%.7f  %.7f' % (ra_main, dec_main)
    print astCoords.decimal2hms(ra_main, ':'), astCoords.decimal2dms(dec_main, ':')
    print '\nCoordinates for the subhalo galaxy are (RA and DEC):'
    print '%.7f  %.7f' % (new_ra, new_dec)
    print astCoords.decimal2hms(new_ra, ':'), astCoords.decimal2dms(new_dec, ':')
    print 'or when using more accurate technique'
    print '%.7f  %.7f' % (result['RA'], result['DEC'])
    print astCoords.decimal2hms(result['RA'], ':'), astCoords.decimal2dms(result['DEC'], ':')
    #print 'Shift in RA and DEC [degrees]:'
    #print  rd['y'][0], (rd['z'][0]/np.cos(rd['z'][0]))
    print '\nShift in RA and DEC [seconds]:'
    print  -rd['y'][0] / np.cos(rd['z'][0]) / conversion, rd['z'][0] / conversion
    print 'or again with the better method:'
    print (result['RA'] - ra_main) / conversion, (result['DEC'] - dec_main) / conversion
Example #22
0
def main():

    __version_info__ = (0, 0, 1)
    __version__ = ".".join(map(str, __version_info__))

    for i, arg in enumerate(sys.argv):
        if (arg[0] == '-') and arg[1].isdigit(): sys.argv[i] = ' ' + arg

    parser = ArgumentParser(
        description=
        'Image based stacking tool S Makhathini <*****@*****.**>')

    add = parser.add_argument
    add("-v",
        "--version",
        action='version',
        version='{:s} version {:s}'.format(parser.prog, __version__))

    add("-i", "--image", help="FITS image name")

    add("-c",
        "--catalog",
        metavar="CATALOG_NAME:DELIMITER",
        help=
        "Catalog name. Default delimiter is a comma. Format: 'ra,dec,freq' ")

    add("-p",
        "--prefix",
        default="gota_stackem_all",
        help="Prefix for output products.")

    add("-w",
        "--width",
        type=float,
        default=0,
        help=
        "For line stacking: Band width [MHz] to sample across frequency (for line stacking). Default is 1."
        "For continuum stacking: Width (in beams) of subregion to stack. Default is 10 beams"
        )

    add("-vbl",
        "--vebosity-level",
        dest="vbl",
        choices=["0", "1", "2", "3"],
        default="0",
        help=
        "Verbosity level. 0-> INFO, 1-> DEBUG, 2-> ERROR, 3-> CRITICAL. Default is 0"
        )

    add("-b",
        "--beam",
        metavar="BMIN[:BMIN:BPA]",
        help="PSF (a.k.a dirty beam) FWHM in degrees. No default")

    add("-b2p",
        "--beam2pix",
        action="store_true",
        help="Do Jy/beam to Jy/pixel conversion")

    add("-L", "--line", action="store_true", help="Do line stacking")

    add("-C", "--cont", action="store_true", help="Do continuum stacking")

    add("-mc",
        "--monte-carlo",
        metavar="SAMPLES:START:FINISH:N",
        default=False,
        help="Do a monte carlo analysis of the noise. That is, "
        "stack on START random positions NSAMPLES times. "
        "Repeat this N times with (FINISH-START)/N increments."
        "if set -mc/--mont-carlo=yes, default is '400:1000:8000:7'")

    args = parser.parse_args()

    if args.catalog:
        catalog_string = args.catalog.split(":")
        if len(catalog_string) > 1:
            catalgname, delimiter = catalog_string
        else:
            catalogname, delimiter = catalog_string[0], ","

    if args.beam:
        beam = args.beam.split(":")
        if len(beam) == 1:
            beam = float(beam[0])
        elif len(beam) == 2:
            beam = map(float, beam) + [0]
        else:
            beam = map(float, beam)
    else:
        beam = None

    pylab.clf()

    prefix = args.prefix or "stackem_default"
    pylab.figure(figsize=(15, 10))

    if args.line:
        from Stackem import LineStacker

        stack = LineStacker.load(args.image,
                                 catalogname,
                                 delimiter=delimiter,
                                 beam=beam,
                                 width=args.width,
                                 beam2pix=args.beam2pix,
                                 verbosity=args.vbl)

        stacked_line = stack.stack() * 1e6  # convert to uJy
        peak, nu, sigma = gfit_params = stack.fit_gaussian(stacked_line)
        gfit = utils.gauss(range(stack.width), *gfit_params)

        freqs = (numpy.linspace(-stack.width / 2, stack.width / 2, stack.width)
                 * stack.dfreq - stack.freq0) * 1e-9  # convert to GHz

        # plot profile
        pylab.plot(freqs, stacked_line, "r.", label="stacked profile")
        pylab.plot(freqs, gfit, "k-", label="Gaussian fit")
        pylab.grid()
        pylab.xlim(freqs[0], freqs[-1])
        pylab.xlabel("Frequency [GHz]")
        pylab.ylabel("Flux density [mJy/{:s}]".format(
            "beam" if args.beam2pix else "pixel"))
        pylab.legend(loc=1)
        pylab.savefig(prefix + "-line.png")
        pylab.clf()

        # save
        numpy.savetxt(prefix + "-line.txt", stacked_line, delimiter=",")

        with open(prefix + "-line_stats.txt", "w") as info:
            tot = sigma * peak * math.sqrt(2 * math.pi)

            stack.log.info("Gaussian Fit parameters.")
            info.write("Gaussian Fit parameters\n")

            info.write("Peak Flux : {:.4g} uJy\n".format(peak))
            stack.log.info("Peak Flux : {:.4g} uJy".format(peak))

            info.write("Integrated Flux : {:.4g} uJy\n".format(tot))
            stack.log.info("Integrated Flux : {:.4g} uJy".format(tot))

            info.write("Profile width : {:.4g} MHz \n".format(sigma *
                                                              stack.dfreq))
            stack.log.info("Profile width : {:.4g} kHz".format(
                sigma * stack.dfreq * 1e-3))

        stack.log.info(
            "Line stacking ran successfully. Find your outputs at {:s}-line*".
            format(prefix))

    if args.cont:
        pylab.figure(figsize=(20, 20))
        from Stackem import ContStacker

        stack = ContStacker.load(args.image,
                                 catalogname,
                                 delimiter=delimiter,
                                 beam=beam,
                                 beam2pix=args.beam2pix,
                                 width=int(args.width),
                                 verbosity=args.vbl)

        stacked = stack.stack()

        mask = utils.elliptical_mask(stacked, stack.bmajPix / 2.,
                                     stack.bminPix / 2., stack.bpa)

        flux = utils.gauss_weights(stacked,
                                   stack.bmajPix / 2.,
                                   stack.bmajPix / 2.,
                                   mask=mask)
        flux = flux.sum()

        rms = utils.negnoise(stacked)

        with open(prefix + "-cont.txt", "w") as cont:
            stack.log.info("Stacked flux: {:.3g} +/- {:.3g} uJy".format(
                flux * 1e6, rms * 1e6))
            cont.write("Stacked flux: {:.3g} +/- {:.3g} uJy/beam".format(
                flux * 1e6, rms * 1e6))

        # Plot stacked image, and cross-sections

        rotated = numpy.rot90(stacked.T)
        import matplotlib.gridspec as gridspec

        gs = gridspec.GridSpec(2, 2, width_ratios=[4, 1], height_ratios=[1, 4])

        gs.update(wspace=0.05, hspace=0.05)
        ax1 = pylab.subplot(gs[2])
        ax2 = pylab.subplot(gs[0], sharex=ax1)
        ax3 = pylab.subplot(gs[3], sharey=ax1)

        pylab.setp(ax3.get_yticklabels(), visible=False)
        pylab.setp(ax3.get_xticklabels(), rotation=90)
        pylab.setp(ax2.get_xticklabels(), visible=False)

        ax1.imshow(stacked, interpolation="nearest")
        ax1.set_aspect("auto")
        ra0, dec0 = stack.wcs.getCentreWCSCoords()
        ras = numpy.linspace(stack.width / 2, -stack.width / 2,
                             stack.width) * stack.cell - ra0
        decs = numpy.linspace(-stack.width / 2, stack.width / 2,
                              stack.width) * stack.cell - dec0

        ax1.set_xticklabels(map(lambda x: coords.decimal2hms(x, ":"), ras))
        ax1.set_yticklabels(map(lambda x: coords.decimal2dms(x, ":"), decs))
        ax1.set_xlabel("RA [hms]")
        ax1.set_ylabel("DEC [dms]")
        pylab.setp(ax1.get_xticklabels(), rotation=90)

        ax2.plot(rotated[:, stack.width / 2] * 1e6)
        ax3.plot(rotated[stack.width / 2, :][::-1] * 1e6, range(stack.width))
        ax3.set_ylim(0, stack.width - 1)
        ax2.set_xlim(0, stack.width - 1)
        ax2.set_ylabel("Flux density [uJy/beam]")
        ax3.set_xlabel("Flux density [uJy/beam]")
        pylab.savefig(prefix + "-cont.png")

        # save stacked image as fits
        hdr = stack.hdr
        hdr["crpix1"] = stack.width / 2
        hdr["crpix2"] = stack.width / 2
        pyfits.writeto(prefix + "-cont.fits", stacked, hdr, clobber=True)

        stack.log.info(
            "Continuum stacking ran successfully. Find your outputs at {:s}-line*"
            .format(prefix))

    if args.monte_carlo:
        if args.monte_carlo in ["yes", "1", "True", "true"]:
            runs, stacks = 400, xrange(1000, 8000, 1000)
        else:
            a, b, c, d = map(int, args.monte_carlo.split(":"))
            runs, stacks = a, xrange(b, c, (c - b) / d)

        from Stackem import ContStacker

        noise = ContStacker.mc_noise_stack(args.image,
                                           runs=runs,
                                           stacks=stacks,
                                           beam=beam,
                                           beam2pix=args.beam2pix,
                                           width=args.width,
                                           verbosity=args.vbl)

        pylab.clf()
        pylab.loglog(stacks, noise)
        pylab.grid()
        pylab.ylabel("Log (Noise [Jy/beam])")
        pylab.xlabel("Log (Random Stacks). {:d} Samples".format(runs))
        pylab.savefig(prefix + "-cont_mc.png")
        pylab.clf()
Example #23
0
def testRandomizer():
    #calculate a test case
    conversion = 0.000277777778  # degree to arcsecond
    #random distance between z = 0 and 5
    z = np.random.rand() * 5.0
    #random separation of the main galaxy and the subhalo
    physical_distance = np.random.rand() * 1e3  #in kpc
    #get the angular diameter distance to the galaxy
    dd1 = cosmocalc(z, 71.0, 0.28)['PS_kpc']  #in kpc / arc seconds
    dd = (physical_distance / dd1) * conversion  # to degrees
    # RA and DEC of the main galaxy, first one in GOODS south
    ra_main = 52.904892  #in degrees
    dec_main = -27.757082  #in degrees
    # get the random position
    rds = rand.randomUnitSphere(points=1)
    # convert the position to Cartesian coord
    # when dd is the radius of the sphere coordinates
    # the dd is in units of degrees here so the results
    # are also in degrees.
    rd = conv.convertSphericalToCartesian(dd, rds['theta'], rds['phi'])

    # Make the assumption that x is towards the observer
    # z is north and y is west. Now if we assume that our z and y
    # are Standard Coordinates, i.e., the projection of the RA and DEC
    # of an object onto the tangent plane of the sky. We can now
    # assume that the y coordinate is aligned with RA and the z coordinate
    # is aligned with DEC. The origin of this system is at the tangent point
    # in the dark matter halo.
    # Poor man's solution to the problem would be:
    new_ra = ra_main - (rd['y'][0] / np.cos(rd['z'][0]))
    new_dec = dec_main + rd['z'][0]
    # However, this only works if one is away from the pole.
    # More general solution can be derived using spherical geometry:
    data = {}
    data['CD'] = np.matrix('-1 0; 0 1')
    data['RA'] = ra_main
    data['DEC'] = dec_main
    data['X'] = rd['y'][0]
    data['Y'] = rd['z'][0]
    result = conv.RAandDECfromStandardCoordinates(data)

    #print the output
    print 'Redshift of the galaxy is %.3f while the subhaloes distance is %0.2f kpc' % (
        z, physical_distance)
    print '\nCoordinates of the main halo galaxy are (RA and DEC):'
    print '%.7f  %.7f' % (ra_main, dec_main)
    print astCoords.decimal2hms(ra_main,
                                ':'), astCoords.decimal2dms(dec_main, ':')
    print '\nCoordinates for the subhalo galaxy are (RA and DEC):'
    print '%.7f  %.7f' % (new_ra, new_dec)
    print astCoords.decimal2hms(new_ra,
                                ':'), astCoords.decimal2dms(new_dec, ':')
    print 'or when using more accurate technique'
    print '%.7f  %.7f' % (result['RA'], result['DEC'])
    print astCoords.decimal2hms(result['RA'], ':'), astCoords.decimal2dms(
        result['DEC'], ':')
    #print 'Shift in RA and DEC [degrees]:'
    #print  rd['y'][0], (rd['z'][0]/np.cos(rd['z'][0]))
    print '\nShift in RA and DEC [seconds]:'
    print -rd['y'][0] / np.cos(
        rd['z'][0]) / conversion, rd['z'][0] / conversion
    print 'or again with the better method:'
    print(result['RA'] - ra_main) / conversion, (result['DEC'] -
                                                 dec_main) / conversion