Exemplo n.º 1
0
def separation(ra1, dec1, ra2, dec2, pa, incl, D_gal, wcsheader):
  logfile = "rhodump.log"
  logdump(logfile, "{0} {1} {2} {3}\n".format(ra1, dec1, ra2, dec2))

  #Assuming ra & dec come in as strings as "hh mm ss.sss"
  #1. calculate separation angle between the two coordinates, giving us r_raw
  #Strip: can't have leading or trailing whitespaces
  r1, d1 = astCoords.hms2decimal(strip(ra1), " "), astCoords.dms2decimal(strip(dec1), " ")
  r2, d2 = astCoords.hms2decimal(strip(ra2), " "), astCoords.dms2decimal(strip(dec2), " ")
  angle = astCoords.calcAngSepDeg(r1, d1, r2, d2)
  r_raw = D_gal * m.radians(angle)
  #print r_raw, "r_raw"

  #2. correct for pa, i by deprojecting radius
  #For this we need x and y coordinates through the wcs header
  x1, y1 = wcsheader.wcs2pix(r1, d1)
  x2, y2 = wcsheader.wcs2pix(r2, d2)
  e_a = e_angle_rad(x1, y1, x2, y2, pa) #get the angle
  esqd = 1 - (m.cos(m.radians(incl)))**2. #deproject
  sqrtf = m.sqrt((1-esqd)/(1-esqd*m.cos(e_a)**2.))
  #print esqd, m.cos(e_a), sqrtf, "esqd cos(angle) sqrtf"
  #calculate separation in parsec from angle and ellipse deprojection

  logdump(logfile, "{0} {1} {2}\n".format(r_raw, sqrtf, r_raw/sqrtf))

  #Append to a ds9-style file - replace our ' ' with ':'
  r1s = ra1.strip().replace(' ', ':')
  d1s = dec1.strip().replace(' ', ':')
  r2s = ra2.strip().replace(' ', ':')
  d2s = dec2.strip().replace(' ', ':')
  logdump("rholog.reg", "circle({0},{1},{2}\")\n".format(r1s, d1s, r_raw/sqrtf/D_gal*3600))
  logdump("rholog.reg", "point({0},{1})\n".format(r2s,d2s))

  return r_raw / sqrtf #in units of D_gal (e.g. kpc)
Exemplo n.º 2
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)
Exemplo n.º 3
0
def get_filenames(self):
    ''' This script will get called if we choose not to swarp the images.
    This makes sure that all of the variables are defined.

    '''
    # this is to convert the hms/dms coordinates back into decimal degrees.
    # I don't really want to use this, but I am not rewriting things.
    from astLib import astCoords

    self.combima = {}
    self.combcat = {}
    self.weightima = {}
    self.outdir = os.path.join(self.outpath, self.tilename)

    self.center_dither(dryrun=True)
    self.xo = astCoords.hms2decimal(self.xo, ':')
    self.yo = astCoords.dms2decimal(self.yo, ':')

    for filter in self.filters:
        # Store the names
        self.combima[filter] = "%s%s" % (self.tilename, filter)
        self.weightima[filter] = "%s%s_weight.fits" % (self.tilename,
                                                       filter)
        # make the combined catalog filenames
        if os.path.isfile('{}{}.cat'.format(self.tilename, filter)):
            self.combcat[filter] = '{}{}.cat'.format(self.tilename, filter)
        elif os.path.isfile('{}{}_cal.cat'.format(self.tilename, filter)):
            self.combcat[filter] = '{}{}_cal.cat'.format(self.tilename,
                                                         filter)
    # The default output names
    self.colorCat = self.tilename + ".color"
    self.columnsFile = self.tilename + ".columns"
    return
Exemplo n.º 4
0
    def __init__(self,name,RA,DEC):
        self.name = name
        if type(RA) == str:
            self.RA = astCoords.hms2decimal(RA,":")
        else: 
            self.RA = RA
        
        if type(DEC) == str:
            self.DEC = astCoords.dms2decimal(DEC,":")
        else: 
            self.DEC = DEC

        self.observations = []
Exemplo n.º 5
0
 def _getDistance(self, threshold=3.0):
     """
     looking for nearby fibres. The unit of distance is arcmin (')
     """
     fibrelist = self.FEList.translate.keys()
     fibdis = {}
     for fibre in fibrelist:
         fRA = ac.hms2decimal(self._get_coord(fibre)[0], ':')
         fDEC = ac.dms2decimal(self._get_coord(fibre)[1], ':')
         
         temp_neigbhour = []
         for key, value in self.FEList.translate.iteritems():
             if key == fibre:    # if the two fibres are actually the same, do nothing
                 pass
             else:
                 dRA = ac.hms2decimal(self._get_coord(key)[0], ':')
                 dDEC = ac.dms2decimal(self._get_coord(key)[1], ':')
                 distance = np.sqrt((dRA - fRA)**2 + (dDEC - fDEC)**2)*60.0
                 if distance <= threshold:
                     temp_neigbhour.append(int(key))
         fibdis[int(fibre)] = temp_neigbhour
     return fibdis
Exemplo n.º 6
0
  def parseForm(self,cd):
    coordstr = cd['coords'].replace(',',' ').strip()
    try:  
      radius = cd['radius_arcmin']
      units = 'arcminutes'
    except KeyError:
      radius = cd['radius_arcsec']
      units = 'arcseconds'

    # User input data validation
    if ':' in coordstr:
      # Remember to add sanity checks!    
      try:
        c = coordstr.split()
        ra = astCoords.hms2decimal(c[0],':')
        dec = astCoords.dms2decimal(c[1],':') 
      except:
        raise CoordinateParseError
    else:
      try:
        c = coordstr.split()
        ra,dec = map(float,c)
      except:
        raise CoordinateParseError  

    try:
      radius = min(float(radius),300)
      radius = max(radius,1)
    except:
      raise AreaParseError

    try:
      n_bands = float(cd['n_bands'])
    except:
      n_bands = None

    try:
      forcedetect = cd['forcedetect']
    except:
      forcedetect = None


    formdata = {
      'radius':radius,
      'ra':ra,
      'dec':dec,
      'units':units,
      'n_bands':n_bands,
      'forcedetect':forcedetect,
      }
    return formdata
Exemplo n.º 7
0
    def _getDistance(self, threshold=3.0):
        """
        looking for nearby fibres. The unit of distance is arcmin (')
        """
        fibrelist = self.fibreExtDic.keys()
        fibdis = {}
        for fibre in fibrelist:
            fRA = ac.hms2decimal(self.getCoordinate(fibre)[0], ':')
            fDEC = ac.dms2decimal(self.getCoordinate(fibre)[1], ':')

            temp_neigbhour = []
            for key, value in self.fibreExtDic.iteritems():
                if key == fibre:  # if the two fibres are actually the same, do nothing
                    pass
                else:
                    dRA = ac.hms2decimal(self.getCoordinate(key)[0], ':')
                    dDEC = ac.dms2decimal(self.getCoordinate(key)[1], ':')
                    distance = np.sqrt((dRA - fRA)**2 +
                                       (dDEC - fDEC)**2) * 60.0
                    if distance <= threshold:
                        temp_neigbhour.append(int(key))
            fibdis[int(fibre)] = temp_neigbhour
        return fibdis
Exemplo n.º 8
0
def load_coords(fibers, fiberPositions):
    # determines which fibers should be included and returns the fiber number
    # along with the position (RA,DEC) of each.

    coords = np.genfromtxt(fiberPositions, dtype=str)

    temp = []
    for f in fibers:
        ra = coords[f-1, 1]
        dec = coords[f-1,2]
        ra_deg = astCoords.hms2decimal(ra, ':')
        dec_deg = astCoords.dms2decimal(dec, ':')
        #temp.append({'fiber':f, 'ra':ra_deg, 'dec':dec_deg})
        temp.append((ra_deg, dec_deg))

    return temp
Exemplo n.º 9
0
  def map():
    #p: [[RA,DEC],[center_RA,center_DEC], [header, image, solar, err_img], [pa, incl, D_gal(kpc), Rgal], astWCS header]

    #Returns the pixel value at a given RA, DEC
    #p: [[RA, DEC], astWCS header, numpy image array]
    #print "Converting to d/d0 with solar 12+log(O/H)={0}".format(p[2][2])

    r, d = astCoords.hms2decimal(strip(p[0][0]), " "), astCoords.dms2decimal(strip(p[0][1]), " ")
    oh = soh = 0
    if p[2][0].coordsAreInImage(r, d):
      x, y = np.round(p[2][0].wcs2pix(r, d))
      #astLib returns indices for the image array; 1 lower in value than DS9 x, y
    #Assume the image is the first entry in the hdulist
    #PyFits Handbook p. 16: slow axis first, last axis last,

    #!At this time we assume a 12+log(O/H) map so the conversion is hardcoded and should be parameters!!:
      oh =  p[2][1][y,x]
      if (m.isnan(oh) or oh==0):
        #Even if p[2][0] is in image, this box might not be
        try:
          #If there is no value at this location we will take the highest value within a small box and warn about it (ignore NaNs)
          oh = np.nanmax(p[2][1][y-2:y+3,x-2:x+3])
        except:
          print "Tried to scan outside image; setting oh to 0"
        if (m.isnan(oh) or oh==0):
          print "Warning: no value found"
        else:
          soh = p[2][3][y-2:y+3,x-2:x+3].ravel()[np.nanargmax(p[2][1][y-2:y+3,x-2:x+3])]
          #Note that the proper index applies to a subselection of both images ^
          print "Read 12+log(O/H) = {} +/- {} -- maximum value nearby".format(oh, soh)
      else:
        soh = p[2][3][y,x]
        print "Read 12+log(O/H) = {} +/- {}".format(oh, soh)
    else: #coordinate is not on the map
        print "Warning: coordinate outside map; setting 0"
    #R = separation(p[0][0], p[0][1], p[1][0], p[1][1], p[3][0], p[3][1], p[3][2], p[4])

    if (oh > 9.5):
      print "Warning: OH > 9.5 rejected, set to 0"
      oh = 0

    dd0 = 10**(oh - p[2][2])
    sdd0 = dd0 * p[2][2] * soh / 10. #absolute error; all errors absolute to avoid confusion
    #sdd0 = p[2][2] * soh / 10. #relative error
    #a value of oh = 0 will result in artificially high n
    return dd0, sdd0
Exemplo n.º 10
0
def make_images(cd,radius=10):
  bands = cd['bands']
  coordstr = cd['coords'].replace(',',' ').strip()
  area = cd['area']
  unit_area = cd['unit_area']
  
  #-------------------------------------
  # User input data validation
  if ':' in coordstr:
    # Remember to add sanity checks!    
    try:
      c = coordstr.split()
      ra = astCoords.hms2decimal(c[0],':')
      dec = astCoords.dms2decimal(c[1],':') 
    except:
      raise CoordinateParseError
  else:
    try:
      c = coordstr.split()
      ra,dec = map(float,c)
    except:
      raise CoordinateParseError
  try:
    float(area)
  except:
    raise AreaParseError
  #-------------------------------------
  
  results = ImageHeader.objects.filter(FILTER__in=bands).imagePositionFilter(ra,dec,radius=radius,units='degrees')
  if not results:
    raise NoCoverageError(radius=radius)
  images = []
  for i in results:
    image = i.__dict__
    image['DATE_OBS'] = image['DATE_OBS'].replace('T',' ')
    d = pyfits.open(i.PATH)[0].data
    unique_filename = uuid.uuid4()
    fname = '%s.png' % unique_filename
    image['PATH_PNG'] = fname
    image['PATH_RAW'] = i.PATH
    astImages.saveBitmap(os.path.join(MEDIA_ROOT,fname),d,cutLevels=["smart", 99.5],size=300,colorMapName='gray')
    images.append(image)  
    print image
  return images
Exemplo n.º 11
0
    def _calculatePosition(self):
        """
        This method can be used to calculate the WCS values.
        
        Uses the pywcs to calculate the pixel to RA and DEC transformations.
        Uses astLib.astCoords to transform RA and DEC to decimal degrees.
        """
        #get RA and DEC from the header
        ra = self.slits[self.centralFile]['header0']['RA']
        dec = self.slits[self.centralFile]['header0']['DEC']
        ra = astCoords.hms2decimal(ra, ':')
        dec = astCoords.dms2decimal(dec, ':')
        pix = self.direct['WCS'].wcs_sky2pix(np.array([[ra, dec], ]), 1)
        self.direct['xposition'] = int(pix[0, 0])
        self.direct['yposition'] = int(pix[0, 1])
        self.result['RAinit'] = ra
        self.result['DECinit'] = dec

        if self.debug:
            print self.direct['xposition'], self.direct['yposition']
Exemplo n.º 12
0
    def SetUpFramecoords(self):
        """ Set up the boundary and center coordinates of the grid.

            Keep in mind that in RA/DEC space the frame is rectangular.
            As such the "left" RA_low coordinate is not equal to the 
             "left" RA_high coordinate.
        """
        centercoords = self.obRun.obFrameCoords[self.gridnr]
        RA_c = centercoords[0]
        DEC_c = centercoords[1]
        self.RA_c = astCoords.hms2decimal(RA_c, ":")
        self.DEC_c = astCoords.dms2decimal(DEC_c, ":")
        DEC_lo = self.DEC_c - 0.5 * self.obRun.aperture_DEC
        DEC_hi = self.DEC_c + 0.5 * self.obRun.aperture_DEC
        if DEC_hi < DEC_lo:
            DEC_lo, DEC_hi = DEC_hi, DEC_lo
        RA_lo_l = self.RA_c - (0.5 * self.obRun.aperture_RA /
                               np.cos(DEC_lo * (np.pi / 180.)))
        RA_lo_r = self.RA_c + (0.5 * self.obRun.aperture_RA /
                               np.cos(DEC_lo * (np.pi / 180.)))
        RA_hi_l = self.RA_c - (0.5 * self.obRun.aperture_RA /
                               np.cos(DEC_hi * (np.pi / 180.)))
        RA_hi_r = self.RA_c + (0.5 * self.obRun.aperture_RA /
                               np.cos(DEC_hi * (np.pi / 180.)))
        if RA_lo_r < RA_lo_l:
            RA_lo_r, RA_lo_l = RA_lo_l, RA_lo_r
        if RA_hi_r < RA_hi_l:
            RA_hi_r, RA_hi_l = RA_hi_l, RA_hi_r
        if RA_hi_l < RA_lo_l:
            RA_lo_r, RA_hi_r, RA_lo_l, RA_hi_l = \
                RA_hi_r, RA_lo_r, RA_hi_l, RA_lo_l
        self.DEC_lo = DEC_lo
        self.DEC_hi = DEC_hi
        self.RA_lo_l = RA_lo_l
        self.RA_lo_r = RA_lo_r
        self.RA_hi_l = RA_hi_l
        self.RA_hi_r = RA_hi_r
Exemplo n.º 13
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]
Exemplo n.º 14
0
import os
from astLib import astCoords
import glob
import numpy as np

for files in glob.glob('*.txt'):
    fbits = files.split('_')
    if not os.path.isfile(fbits[0] + '_combined_tiles.txt'):
        tiles = glob.glob(fbits[0] + '*')
        with open(fbits[0] + '_combined_tiles.txt', 'wt') as newf:
            newf.writelines('# tile dither fiber fiber_ra fiber_dec\n')
            for f in tiles:
                tile = f.split('_')[1]
                dither = f.split('_')[2]
                print(fbits[0], tile, dither)
                data = np.genfromtxt(f, dtype='str')
                for fiber, ra, dec in zip(
                        range(1, len(data[:, 0]) + 1), data[:, 1], data[:, 2]):
                    ra_deg = astCoords.hms2decimal(ra, ':')
                    dec_deg = astCoords.dms2decimal(dec, ':')
                    newf.writelines('%s %s %s %s %s\n' %
                                    (tile, dither, fiber, ra_deg, dec_deg))
Exemplo n.º 15
0
    line = result.readline()
    if line.startswith("ERROR".encode()):
        print('ERROR')
        ofp = sys.stderr
    else:
        with open(outfile, 'wt') as ofp:
            cnt = 0
            while line:
                ofp.write(line.rstrip().decode() + os.linesep)
                line = result.readline()
                cnt += 1
        if cnt < 3:
            os.remove(outfile)


# get file data
data = np.genfromtxt('../catalogs/PSZ2_unconfirmed_catalog - Master.csv',
                     delimiter=',',
                     names=True,
                     dtype=None)

for i, (ra, dec, name) in enumerate(zip(data['RA'], data['DEC'],
                                        data['Name'])):
    print(data['Name'][i])

    ra = astCoords.hms2decimal(ra, ':')
    dec = astCoords.dms2decimal(dec, ':')
    work(ra, dec, './SDSS/%s_SDSS_catalog.csv' % (name.decode()))
    sleep(1)
Exemplo n.º 16
0
cwd = os.getcwd()

for f in filelist:
    data = np.loadtxt(f, dtype='str')
    fBits = f.split('_')
    print fBits[0]
    if not os.path.isdir(fBits[0]):
        os.mkdir(fBits[0])
    os.chdir(fBits[0])
    if not os.path.isdir(fBits[1]):
        os.mkdir(fBits[1])
    os.chdir(fBits[1])
    if not os.path.isdir(fBits[2]):
        os.mkdir(fBits[2])
    os.chdir(fBits[2])

    for fiber, ra, dec in zip(data[:,0], data[:,1], data[:,2]):
        if not os.path.isfile(fiber.zfill(3)+'.txt'):
            ra = astCoords.hms2decimal(ra, ':')
            dec = astCoords.dms2decimal(dec, ':')
            work(ra, dec, fiber.zfill(3)+'.txt')
            #print fiber, ra, dec

            time.sleep(2)
        else:
            pass

    os.chdir(cwd)

Exemplo n.º 17
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)
Exemplo n.º 18
0
 def testdms2decimal(self):
     for dms, result in self.dms2decimal:
         answer = astCoords.dms2decimal(dms, ':')
         self.assertEqual(result, answer)
Exemplo n.º 19
0
downloads = []

for field in fields:
    f_ra_h = field[1:3]
    f_ra_m = field[3:5]
    f_dec_sign_str = field[5]
    if f_dec_sign_str == 'M':
        f_dec_sign = '-'
    else:
        f_dec_sign = ''
    f_dec_d = field[6:8]
    f_ra_hms = f_ra_h + ':' + f_ra_m + ':00'
    f_dec_dms = f_dec_sign + f_dec_d + ':00:00'
    f_ra = ac.hms2decimal(f_ra_hms, delimiter=':')
    f_dec = ac.dms2decimal(f_dec_dms, delimiter=':')
    if in_area(f_ra, f_dec, ra_min, ra_max, dec_min, dec_max):
        downloads.append(field)

for dl in downloads:
    get_nvss(dl)
    os.system('gunzip ' + dl + '.gz')
    os.system('mv ' + dl + ' ' + dl + '.fits')
    os.system('mSubimage -p ' + dl + '.fits ' + dl +
              '_crop.fits 0 0 1024 1024')
    os.system('rm ' + dl + '.fits')
    os.system('mv ' + dl + '_crop.fits ' + tempdir)

os.chdir(tempdir)
os.system('mImgtbl . images.tbl')
os.system('mMakeHdr images.tbl template.hdr')
Exemplo n.º 20
0
def query(ra, dec, radius=2., unit='arcmin', z=0., cosmo=None,
          catalogs=None, return_single=True, squeeze=False,
          return_values=('name','ra','dec','z','index','dist','dz')):
    """
    Query different catalogs for clusters close to a given set of coordinates.

    To-do's:
        -possibility to return survey observables

    Parameters
    ----------
      ra        : (array of) float or str
                  if float, should be the RA in decimal degrees; if str,
                  should be in hms format ('hh:mm:ss', requires astLib)
      dec       : (array of) float or str
                  if float, should be the Dec in decimal degrees; if str,
                  should be in dms format ('dd:mm:ss', requires astLib)
      radius    : float (default 2)
                  the search radius, in units given by argumetn "unit"
      unit      : {'arcmin', 'Mpc'}
                  the units of argument "radius". If Mpc, then argument "z"
                  must be larger than zero.
      z         : (array of) float (optional)
                  redshift(s) of the cluster(s). Must be >0 if unit=='Mpc'.
      cosmo     : module astro.cosmology (optional)
                  if the matching is done in physical distance then pass
                  this module to make sure all cosmological parameters are
                  used consistently with the parent code!
      catalogs  : str (optional)
                  list or comma-separated names of catalogs to be searched.
                  If not given, all available catalogs are searched. Allowed
                  values are:
                        * 'maxbcg' (Koester et al. 2007)
                        * 'gmbcg' (Hao et al. 2010)
                        * 'hecs2013' (Rines et al. 2013)
                        * 'hecs2016' (Rines et al. 2016) NOT YET
                        * 'orca' (Geach, Murphy & Bower 2011)
                        * 'psz1' (Planck Collaboration XXIX 2014)
                        * 'psz2' (Planck Collaboration XXVII 2016)
                        * 'redmapper' (Rykoff et al. 2014, v5.10)
                        * 'whl' (Wen, Han & Liu 2012, Wen & Han 2015)
      return_single : bool
                  whether to return the single closest matching cluster (if
                  within the search radius) or all those falling within the
                  search radius
      return_values : any subset of ('name','ra','dec','z','index','dist','dz')
                  what elements to return. 'index', 'dist' and 'dz' refer to
                  the index in the catalog and the distances of the matching
                  cluster(s) in arcmin and redshift space, respectively.
                  NOTE: altering the order of the elements in return_values
                  will have no effect in the order in which they are returned!
      squeeze   : bool
                  whether to return a list instead of a dictionary if only
                  one catalog is provided

    Returns
    -------
      matches   : dict
                  matching elements per catalog. Each requested catalog is
                  a key of this dictionary if more than one catalog was
                  searched or if squeeze==False. If only one catalog was
                  provided and squeeze==True, then return a list with
                  matching entry/ies.
      withmatch : dict
                  for each searched catalog, contains hte indices of the
                  provided clusters for which at least one match was found.
                  The same formatting as for "matches" applies.

    """
    available = ('maxbcg', 'gmbcg', 'hecs2013', 'orca', 'psz1', 'psz2',
                 'redmapper', 'whl')
    # some formatting for convenience
    if not hasattr(ra, '__iter__'):
        ra = [ra]
        dec = [dec]
    if not hasattr(z, '__iter__'):
        z = array([z])
    # in the case of matching by physical radius, demand z > 0
    if unit == 'Mpc' and npany(z <= 0):
        msg = "ERROR: in catalogs.query:"
        msg += " if unit=='Mpc' then z must be larger than 0"
        print msg
        exit()
    if unit == 'Mpc':
        if cosmo is None:
            cosmo = cosmology
        dproj = cosmo.dProj
    # will this fail for numpy.string_?
    if isinstance(ra[0], basestring):
        ra = array([hms2decimal(x, ':') for x in ra])
        dec = array([dms2decimal(y, ':') for y in dec])
    if unit == 'arcmin':
        radius = ones(ra.size) * radius# / 60
    else:
        radius = array([dproj(zi, radius, input_unit='Mpc', unit='arcmin')
                        for zi in z])
    if catalogs is None:
        catalogs = available
    else:
        try:
            catalogs = catalogs.split(',')
        # if this happens then we assume catalogs is already a list
        except ValueError:
            pass
    for name in catalogs:
        if name not in available:
            msg = 'WARNING: catalog {0} not available'.format(name)
            print msg
    labels = {'maxbcg': 'maxBCG', 'gmbcg': 'GMBCG', 'hecs2013': 'HeCS',
              'hecs2016': 'HeCS-SZ', 'orca': 'ORCA', 'psz1': 'PSZ1',
              'psz2': 'PSZ2', 'redmapper': 'redMaPPer', 'whl': 'WHL'}
    filenames = {'maxbcg': 'maxbcg/maxBCG.fits',
                 'gmbcg': 'gmbcg/GMBCG_SDSS_DR7_PUB.fit',
                 'hecs2013': 'hecs/2013/data.fits',
                 'orca': 'orca/fullstripe82.fits',
                 'psz1': osjoin('planck', 'PSZ-2013', 'PLCK-DR1-SZ',
                                'COM_PCCS_SZ-union_R1.11.fits'),
                 'psz2': osjoin('planck', 'PSZ-2015',
                                'HFI_PCCS_SZ-union_R2.08.fits'),
                 'redmapper': 'redmapper/redmapper_dr8_public' + \
                              '_v5.10_catalog.fits',
                 'whl': 'whl/whl2015.fits'}
    columns = {'maxbcg': 'none,RAJ2000,DEJ2000,zph',
               'gmbcg': 'OBJID,RA,DEC,PHOTOZ',
               'hecs2013': 'Name,RAJ2000,DEJ2000,z',
               'orca': 'ID,ra_bcg,dec_bcg,redshift',
               'psz1': 'NAME,RA,DEC,REDSHIFT',
               'psz2': 'NAME,RA,DEC,REDSHIFT',
               'redmapper': 'NAME,RA,DEC,Z_LAMBDA',
               'whl': 'WHL,RAJ2000,DEJ2000,zph'}
    for cat in catalogs:
        filenames[cat] = osjoin(path, filenames[cat])
        columns[cat] = columns[cat].split(',')
    matches = {}
    withmatch = {}
    for cat in available:
        if cat not in catalogs:
            continue
        data = getdata(filenames[cat], ext=1)
        aux = {}
        for name in data.names:
            aux[name] = data[name]
        data = aux
        # if the catalog doesn't give a name
        if columns[cat][0] == 'none':
            columns[cat][0] = 'Name'
            data['Name'] = chararray(data[columns[cat][1]].size, itemsize=4)
            data['Name'][:] = 'none'
        data = [data[v] for v in columns[cat]]
        name, xcat, ycat, zcat = data
        colnames = 'name,ra,dec,z'.split(',')
        close = [(abs(xcat - x) < 2*r/60.) & (abs(ycat - y) < 2*r/60.)
                 for x, y, r in izip(ra, dec, radius)]
        withmatch[cat] = [j for j, c in enumerate(close) if name[c].size]
        dist = [60 * calcAngSepDeg(xcat[j], ycat[j], x, y)
                for j, x, y in izip(close, ra, dec)]
        match = [(d <= r) for d, r in izip(dist, radius)]
        withmatch[cat] = array([w for w, m in izip(count(), match)
                                if w in withmatch[cat] and name[m].size])
        if return_single:
            match = [argmin(d) if d.size else None for d in dist]
        matches[cat] = {}
        # keeping them all now because they may be needed for other properties
        for name, x in izip(colnames, data):
            matches[cat][name] = array([x[j][mj] for w, j, mj
                                        in izip(count(), close, match)
                                        if w in withmatch[cat]])
        if 'index' in return_values:
            matches[cat]['index'] = array([arange(xcat.size)[j][m]
                                           for w, j, m in izip(count(),
                                                               close, match)
                                           if w in withmatch[cat]])
        if 'dist' in return_values:
            matches[cat]['dist'] = array([d[m] for w, d, m
                                          in izip(count(), dist, match)
                                          if w in withmatch[cat]])
            if unit == 'Mpc':
                matches[cat]['dist'] *= array([dproj(zi, 1, unit='Mpc',
                                                     input_unit='arcmin')
                                               for zi in matches[cat]['z']])
        if 'dz' in return_values:
            matches[cat]['dz'] = array([zcat[j][m] - zj for w, j, m, zj
                                        in izip(count(), close, match, z)
                                        if w in withmatch[cat]])
        for key in matches[cat].keys():
            if key not in return_values:
                matches[cat].pop(key)
        if not return_single and name[j][match].size == 1:
            for key in matches[cat].keys():
                matches[cat][key] = matches[cat][key][0]
    if len(catalogs) == 1 and squeeze:
        return matches[catalogs[0]], withmatch[catalogs[0]]
    return matches, withmatch
Exemplo n.º 21
0
"""
Various coordinates utilities

Variables
---------
  epoch      : Epoch. Right now only 'J2000' is implemented
  aNGP       : Right Ascension of the North Galactic Pole, for the chosen epoch
  dNGP       : Declination of the North Galactic Pole, for the chosen epoch

"""

epoch = 'J2000'

if epoch == 'J2000':
    aNGP = astCoords.hms2decimal('12:51:26.28', ':')
    dNGP = astCoords.dms2decimal('+27:07:42.0', ':')


def torad(x):
    """
    Input: coordinate in degrees
    Returns : coordinate in radians
    """
    return numpy.pi * x / 180


def todeg(x):
    """
    Input: coordinate in radians
    Returns: coordinate in degrees
    """
Exemplo n.º 22
0
    def _processDirectImage(self, ext=0):
        """
        Processes the given Direct Image file.

        This method performs several tasks:

          1. trims the image (optional, depends on self.cutout)
          2. supersamples the original image by a given factor
          3. rotates the image by -POSANGLE degrees, note the minus sign
          4. gets the plate scale from the rotated header

        :param ext: FITS extension, change this if the SDSS data changes or if other data are used.
        :type ext: int
        """
        #load images
        fh = pf.open(self.dirfile)
        self.direct['originalHeader0'] = fh[ext].header
        img = fh[ext].data
        fh.close()
        self.direct['originalImage'] = img

        #cut out a region
        if self.cutout:
            ra = self.slits[self.centralFile]['header0']['RA']
            dec = self.slits[self.centralFile]['header0']['DEC']
            ra = astCoords.hms2decimal(ra, ':')
            dec = astCoords.dms2decimal(dec, ':')
            wcs = pywcs.WCS(self.direct['originalHeader0'])
            pix = wcs.wcs_sky2pix(np.array([[ra, dec], ]), 1)
            xmid = pix[0, 0]
            ymid = pix[0, 1]
            xstart = int(xmid - self.direct['xsize'] / 2.)
            xstop = int(xmid + self.direct['xsize'] / 2.)
            ystart = int(ymid - self.direct['ysize'] / 2.)
            ystop = int(ymid + self.direct['ysize'] / 2.)
            imgT, hdrT = modify.hextract(self.dirfile, xstart, xstop, ystart, ystop)
            shape = imgT.shape
            file = 'resized.fits'
        else:
            shape = img.shape
            file = self.dirfile

        #super sample
        if self.direct['factor'] != 1:
            xnew = int(shape[1] * self.direct['factor'])
            ynew = int(shape[0] * self.direct['factor'])
            imgS, hdrS = modify.hrebin(file, xnew, ynew, total=True)
            self.direct['xnew'] = xnew
            self.direct['ynew'] = ynew
            self.direct['supersampledImage'] = imgS
            self.direct['supersampledHeader'] = hdrS
            self.direct['supersampledFile'] = 'rebinned.fits'
        else:
            self.direct['xnew'] = int(shape[1])
            self.direct['ynew'] = int(shape[0])
            self.direct['supersampledImage'] = img
            self.direct['supersampledHeader'] = self.direct['originalHeader0']
            self.direct['supersampledFile'] = self.dirfile

        #set rotation, note that the rotation is in minus direction
        #instead of using SPA one could also calculate this from WCS CD matrix
        self.direct['rotation'] = -self.slits[self.centralFile]['POSANGLE'] + self.direct['originalHeader0']['SPA']
        #make a rotation
        imgR, hdrR = modify.hrot(self.direct['supersampledFile'],
                                 self.direct['rotation'],
                                 xc=None,
                                 yc=None)
        self.direct['rotatedImage'] = imgR
        self.direct['rotatedHeader'] = hdrR
        self.direct['rotatedFile'] = 'rotated.fits'

        #convert the images from nanomaggies to micro Janskys
        self.direct['originalImage'] = convert.nanomaggiesToJansky(self.direct['originalImage']) * 1e6
        self.direct['supersampledImage'] = convert.nanomaggiesToJansky(self.direct['supersampledImage']) * 1e6
        self.direct['rotatedImage'] = convert.nanomaggiesToJansky(self.direct['rotatedImage']) * 1e6

        #WCS info
        self.direct['WCS'] = pywcs.WCS(hdrR)
        #self.direct['WCS'] = wcs.Projection(hdrR)

        if self.debug:
            print '\ndirect:'
            print self.direct
Exemplo n.º 23
0
    else:
        print("\n\tHalting setup process!\n")
        quit()

os.chdir(working_dir + "/redux/")
for raw_dir in raw_dirs: os.mkdir(raw_dir.name)


###  THIS IS WHERE OBJECTS ARE FOUND FROM TARGETS.list
###  ... THEY MUST BE WITHIN 0.04deg (2.4") OF THE TARGETS.list VALUE
for raw_dir in raw_dirs:
    targetstxt = open("targets.txt", "w")
    for target in targets:
        made_dir = False
        target_ra = astCoords.hms2decimal(target[1], ":")
        target_dec = astCoords.dms2decimal(target[2], ":")
        for im in raw_dir.obj:
            obj_ra = astCoords.hms2decimal(pf.getval(working_dir + "/raw/"
                                                     + raw_dir.name + "/"
                                                     + im, "RA"), ":")
            obj_dec = astCoords.dms2decimal(pf.getval(working_dir + "/raw/"
                                                      + raw_dir.name + "/"
                                                      + im, "DEC"), ":")
            if astCoords.calcAngSepDeg(target_ra, target_dec,
                                       obj_ra, obj_dec) < 0.04:
                if not made_dir:
                    os.mkdir(raw_dir.name+"/"+target[0])
                    made_dir = True
                    targetstxt.write(target[0]+"\n")
                os.chdir(raw_dir.name + "/" + target[0])
                ln_cmd = "ln -s " + working_dir + "/raw/" \
Exemplo n.º 24
0
def get_astrometry(self, newfirm=False):
    ''' This calls the pp script to astrometrically correct the images. I'm
    breaking it appart from the rest of the script so I can control when
    things happen. It is mostly for testing.

    '''

    # we have to prepare the images first.
    print()
    check_exe('pp_prepare')
    check_exe('pp_register')

    # little patch to keep it from crashing
    try:
        os.mkdir('.diagnostics')
    except FileExistsError:
        pass

    # make the diagnostics file too
    open('diagnostics.html', 'a').close()

    try:
        if ':' in self.xo or ':' in self.yo:
            from astLib import astCoords
            self.xo = astCoords.hms2decimal(self.xo, ':')
            self.yo = astCoords.dms2decimal(self.yo, ':')
    except TypeError:
        pass

    subprocs = []
    for filter in self.filters:
        if not newfirm and filter == 'K':
            continue
        mosaic = '{}.fits'.format(self.combima[filter])
        if self.pixscale == 0.25:
            cmd = 'pp_prepare -ra {} -dec {} -telescope {} {}'.format(
                self.xo, self.yo, 'KPNO4MOS3', mosaic)
        elif self.pixscale == 0.2666:
            cmd = 'pp_prepare -ra {} -dec {} -telescope {} {}'.format(
                self.xo, self.yo, 'KPNO4MOS1', mosaic)
        else:
            cmd = 'pp_prepare -ra {} -dec {} {}'.format(
                self.xo, self.yo, mosaic)

        if not self.dryrun:
            print(cmd)
            subprocs.append(subprocess.Popen(shlex.split(cmd)))

    [p.wait(timeout=600) for p in subprocs]
    [i.kill() for i in subprocs]

    for filter in self.filters:
        if not newfirm and filter == 'K':
            continue
        mosaic = '{}.fits'.format(self.combima[filter])

        cmd = 'pp_register -snr 10 -minarea 12 {}'.format(mosaic)

        if not self.dryrun:
            subprocs.append(subprocess.Popen(shlex.split(cmd)))

    [p.wait(timeout=600) for p in subprocs]
    [i.kill() for i in subprocs]

    for filter in self.filters:
        if not newfirm and filter == 'K':
            continue
        mosaic = '{}.fits'.format(self.combima[filter])

        # correct the header information to make sure floats are floats and
        # not strings
        with fits.open(mosaic, mode='update') as f:
            header = f[0].header
            for key, val in list(header.items()):
                if 'CD1_' in key or 'CD2_' in key or \
                    'CRVAL' in key or 'CRPIX' in key or \
                        'EQUINOX' in key:
                    f[0].header[key] = float(val)
                if 'PV' in key:
                    f[0].header[key] = str(val)

    return
Exemplo n.º 25
0
    if wayOk == False:
        print("<way>: either \"to_decimal\" or \"to_hmsdms\"")
        sys.exit()

    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"
Exemplo n.º 26
0
print('Obtaining useful sources from catalogue...')

#Get relevant data from the catalogue:
cat = np.loadtxt(catFile, usecols=[0,1,2,3,4,5,7],skiprows=2)

#Get the R.A. in degrees for each source
h,m,s=cat[:,0],cat[:,1],cat[:,2]
RAdeg=[]
for i in range(len(h)):
    RAdeg.append( hms2decimal("%s"%(h[i])+" "+"%s"%(m[i])+" "+"%s"%(s[i]),' ' ))

#Get the Dec. in degrees for each source
d,m,s=cat[:,3],cat[:,4],cat[:,5]
DECdeg=[]
for i in range(len(h)):
    DECdeg.append( dms2decimal("%s"%(d[i])+" "+"%s"%(m[i])+" "+"%s"%(s[i]),' ' ))

#Get the the peak and coordinates for each source as np arrays
Fpeak = cat[:,6]
RAdeg = np.array(RAdeg)
DECdeg = np.array(DECdeg)

#Get initial list of possible filenames
print('Getting initial list of possible associated filenames...')

#The below process should be handled more elegantly, as above.
#Might rewrite this.
filenames = []
f = open(catFile,'r')
for line in f:
    if (line[0] == '#'): continue