Exemplo n.º 1
0
        def check_raise(data, exception, regexp):
            """Make sure that load_coordinates() raises 'exception'
            when a file containing 'data' is parsed. 'regexp' is the regular
            expression that must be matched by the string representation of
            the raised exception"""

            with tempinput(data) as path:
                with self.assertRaisesRegexp(exception, regexp):
                    list(load_coordinates(path))
Exemplo n.º 2
0
    def test_load_coordinates_scientific_notation_with_propper_motion(self):

        data = "7.9720694373e-05 44.6352243008 [0.00123] [0.0000432]"
        with tempinput(data) as path:
            coordinates = load_coordinates(path)
            coords_list = list(coordinates)
            self.assertEqual(len(coords_list), 1)
            ra, dec, pm_ra, pm_dec = coords_list[0]
            self.assertAlmostEqual(ra, 7.9720694373e-05)
            self.assertAlmostEqual(dec, 44.6352243008)
            self.assertAlmostEqual(pm_ra, 0.00123)
            self.assertAlmostEqual(pm_dec, 4.32e-05)
Exemplo n.º 3
0
    def test_load_coordinates_scientific_notation(self):

        # For some datasets that generate coords so close to zero that they end up in scientific notation

        data = "7.9720694373e-05 44.6352243008"
        with tempinput(data) as path:
            coordinates = load_coordinates(path)
            coords_list = list(coordinates)
            self.assertEqual(len(coords_list), 1)
            ra, dec, pm_ra, pm_dec = coords_list[0]
            self.assertAlmostEqual(ra, 7.9720694373e-05)
            self.assertAlmostEqual(dec, 44.6352243008)
Exemplo n.º 4
0
    def test_get_coords_file(self):
        def c(*args):
            """ Return an astromatic.Coordinates object """
            return astromatic.Coordinates(*args)

        coordinates = (
            c(316.724802, 38.74944, 4.16831, 3.2692),  # 61 Cygni A
            c(316.730266, 38.742056, 4.1069, 3.14468),  # 61 Cygni B
            c(165.834142, 35.96988, -0.58027, -4.76585),  # Lalande 21185
            c(152.11717, 12.3065, -0.000114, -0.000126),  # Leo I
            c(346.466817, -35.853069, 6.7682, 1.32752),  # HD 217987
            c(348.992913, 31.462856, None, None),  # WASP-10 b
            c(97.19046, 38.962963)  # HD 45350 b
        )

        # There is no need to write code to test that proper-motion correction
        # is correctly calculated: we do already have a unit test for exactly
        # that: Coordinates.get_exact_coordinates(). Parse the output file of
        # get_coords_file() and make sure that the coordinates written to it
        # match those returned by get_exact_coordinates() for the same year
        # and epoch.

        for _ in xrange(NITERS):

            year = random.uniform(1900, 2050)
            epoch = random.choice([1950, 2000])

            # The proper-motion corrected astromatic.Coordinates objects. We
            # only correct those objects with proper motions (that is, those
            # for which both 'pm_ra' and 'pm_dec' are not None neither zero).

            func = operator.methodcaller('get_exact_coordinates', year, epoch)
            has_pm = lambda c: c.pm_ra and c.pm_dec
            expected = (func(x) if has_pm(x) else x for x in coordinates)

            output_path = qphot.get_coords_file(coordinates, year, epoch)

            try:
                # The coordinates written by get_coords_file(), returned as
                # four-element tuples (ra, dec, pm_ra, pm_dec)
                output = load_coordinates(output_path)
                for c1, c2 in zip(output, expected):
                    self.assertAlmostEqual(c1[0], c2.ra)
                    self.assertAlmostEqual(c1[1], c2.dec)
            finally:
                os.unlink(output_path)
Exemplo n.º 5
0
    def test_load_coordinates(self):

        for _ in xrange(NITERS):

            # Randomly choose some of the SIMBAD astronomical objects and write
            # them to a temporary file, formatting their coordinates and proper
            # motions in four columns (or just two, if the proper motions are
            # not known) and inserting a random number of separators before,
            # between and after the columns and brackets. Then make sure that
            # load_coordinates() returns the same astronomical objects, in the
            # same order and with the same coordinates that we wrote.

            n = random.randint(*self.NCOORDS)
            objects = random.sample(self.COORDINATES.values(), n)
            data = self.get_coords_data(objects)
            with tempinput(data) as path:
                coordinates = load_coordinates(path)
                for coords, expected in zip(coordinates, objects):
                    self.assertEqual(coords, expected)
Exemplo n.º 6
0
    def __init__(self, img_path, coords_path):
        """Instantiation method for the QPhot class.

        img_path - path to the FITS image on which to do photometry.
        coords_path - path to the text file with the celestial coordinates
                      (right ascension and declination) of the astronomical
                      objects to be measured. These objects must be listed one
                      per line, in two columns. Note that this class does *not*
                      apply proper-motion correction, so the coordinates must
                      be corrected before being written to the file. In case
                      the proper motions of the objects are listed in the file,
                      in columns third and fourth, ValueError is raised.

        """

        super(list, self).__init__()
        self.image = fitsimage.FITSImage(img_path)
        self.coords_path = coords_path

        for ra, dec, pm_ra, pm_dec in util.load_coordinates(self.coords_path):
            if ra == 0 and dec == 0:
                msg = (
                    "the right ascension and declination of one or more "
                    "astronomical objects in '%s' is zero. This is a very bad "
                    "sign: these are the celestial coordinates that SExtractor "
                    "uses for sources detected on a FITS image that has not been "
                    "calibrated astrometrically (may that be your case?), and "
                    "without that it is impossible to do photometry on the "
                    "desired coordinates" % self.coords_path)
                # Emit warning only once
                warnings.warn(msg)
                break

            if pm_ra is not None or pm_dec is not None:
                msg = ("at least one object in the '%s' file lists its proper "
                       "motions. This is not allowed. The coordinates must be "
                       "written to the file already adjusted for their proper "
                       "motions, as this class cannot apply any correction" %
                       self.coords_path)
                raise ValueError(msg)
Exemplo n.º 7
0
    def test_load_coordinates_empty_lines_and_comments(self):

        # The same as test_load_coordinates(), but randomly inserting a few
        # empty and comment lines, as well as inline comments, all of which
        # must be ignored by load_coordinates().

        for _ in xrange(NITERS):
            n = random.randint(*self.NCOORDS)
            objects = random.sample(self.COORDINATES.values(), n)
            data = self.get_coords_data(objects)

            lines = data.split("\n")

            # Randomly insert inline comments
            for index in range(len(lines)):
                if random.random() < self.COMMENT_PROB:
                    sep = self.get_seps(0)
                    comment = self.get_comment()
                    lines[index] += sep + comment

            # Randomly insert empty lines
            for _ in range(random.randint(*self.NEMPTY)):
                index = random.randint(0, len(lines))
                empty = self.get_seps(0)
                lines.insert(index, empty)

            # Randomly insert comment lines
            for _ in range(random.randint(*self.NCOMMENTS)):
                index = random.randint(0, len(lines))
                sep = self.get_seps(0)
                comment = self.get_comment()
                lines.insert(index, sep + comment)

            data = "\n".join(lines)

            with tempinput(data) as path:
                coordinates = load_coordinates(path)
                for coords, expected in zip(coordinates, objects):
                    self.assertEqual(coords, expected)
Exemplo n.º 8
0
 def test_load_coordinates_empty_file(self):
     # If the file is empty, nothing is returned
     with tempinput("") as path:
         self.assertEqual([], list(load_coordinates(path)))