Beispiel #1
0
    def test_generate_sdfs(self):
        """Test generated IDFs from the IDF."""

        project = idf.parse_idf(drxFile)
        sdfs = project.generate_sdfs()

        project = idf.parse_idf(solFile)
        sdfs = project.generate_sdfs()

        project = idf.parse_idf(jovFile)
        sdfs = project.generate_sdfs()
Beispiel #2
0
    def test_auto_update(self):
        """Test project auto-update on render."""

        # Part 1 - frequency and duration
        project = idf.parse_idf(drxFile)
        project.runs[0].scans[0].frequency1 = 75e6
        project.runs[0].scans[0].duration = '00:01:31.000'

        fh = open(os.path.join(self.testPath, 'idf.txt'), 'w')
        fh.write(project.render())
        fh.close()

        project = idf.parse_idf(os.path.join(self.testPath, 'idf.txt'))
        self.assertEqual(project.runs[0].scans[0].freq1, 1643482384)
        self.assertEqual(project.runs[0].scans[0].dur, 91000)

        # Part 2 - frequency and duration (timedelta)
        project = idf.parse_idf(drxFile)
        project.runs[0].scans[0].frequency1 = 75e6
        project.runs[0].scans[0].duration = timedelta(minutes=1,
                                                      seconds=31,
                                                      microseconds=1000)

        fh = open(os.path.join(self.testPath, 'idf.txt'), 'w')
        fh.write(project.render())
        fh.close()

        project = idf.parse_idf(os.path.join(self.testPath, 'idf.txt'))
        self.assertEqual(project.runs[0].scans[0].freq1, 1643482384)
        self.assertEqual(project.runs[0].scans[0].dur, 91001)

        # Part 3 - frequency and start time
        project = idf.parse_idf(drxFile)
        project.runs[0].scans[0].frequency2 = 75e6
        project.runs[0].scans[0].start = "MST 2011 Feb 23 14:00:15"

        fh = open(os.path.join(self.testPath, 'idf.txt'), 'w')
        fh.write(project.render())
        fh.close()

        project = idf.parse_idf(os.path.join(self.testPath, 'idf.txt'))
        self.assertEqual(project.runs[0].scans[0].freq2, 1643482384)
        self.assertEqual(project.runs[0].scans[0].mjd, 55615)
        self.assertEqual(project.runs[0].scans[0].mpm, 75615000)

        # Part 4 - frequency and start time (timedelta)
        project = idf.parse_idf(drxFile)
        _MST = pytz.timezone('US/Mountain')
        project.runs[0].scans[0].frequency2 = 75e6
        project.runs[0].scans[0].start = _MST.localize(
            datetime(2011, 2, 23, 14, 00, 30, 1000))

        fh = open(os.path.join(self.testPath, 'idf.txt'), 'w')
        fh.write(project.render())
        fh.close()

        project = idf.parse_idf(os.path.join(self.testPath, 'idf.txt'))
        self.assertEqual(project.runs[0].scans[0].freq2, 1643482384)
        self.assertEqual(project.runs[0].scans[0].mjd, 55615)
        self.assertEqual(project.runs[0].scans[0].mpm, 75630001)
Beispiel #3
0
    def test_drx_alt_update(self):
        """Test updating TRK_RADEC values with other phase centers."""

        project = idf.parse_idf(altFile)
        project.runs[0].scans[0].start = "MST 2011 Feb 23 17:00:15"
        project.runs[0].scans[0].duration = timedelta(seconds=15)
        project.runs[0].scans[0].frequency1 = 75e6
        project.runs[0].scans[0].frequency2 = 76e6
        project.runs[0].scans[0].ra = ephem.hours('5:30:00')
        project.runs[0].scans[0].dec = ephem.degrees('+22:30:00')
        project.runs[0].scans[0].alt_phase_centers[0].ra = ephem.hours(
            '5:35:00')
        project.runs[0].scans[0].alt_phase_centers[1].ra = ephem.hours(
            '5:25:00')

        self.assertEqual(project.runs[0].scans[0].mjd, 55616)
        self.assertEqual(project.runs[0].scans[0].mpm, 15000)
        self.assertEqual(project.runs[0].scans[0].dur, 15000)
        self.assertEqual(project.runs[0].scans[0].freq1, 1643482384)
        self.assertEqual(project.runs[0].scans[0].freq2, 1665395482)
        self.assertAlmostEqual(project.runs[0].scans[0].ra, 5.5, 6)
        self.assertAlmostEqual(project.runs[0].scans[0].dec, 22.5, 6)
        self.assertAlmostEqual(
            project.runs[0].scans[0].alt_phase_centers[0].ra, 5.583333, 6)
        self.assertAlmostEqual(
            project.runs[0].scans[0].alt_phase_centers[1].ra, 5.416667, 6)
Beispiel #4
0
    def test_drx_update(self):
        """Test updating TRK_RADEC values."""

        project = idf.parse_idf(drxFile)
        project.runs[0].scans[0].start = "MST 2011 Feb 23 17:00:15"
        project.runs[0].scans[0].duration = timedelta(seconds=15)
        project.runs[0].scans[0].frequency1 = 75e6
        project.runs[0].scans[0].frequency2 = 76e6
        project.runs[0].scans[0].ra = AstroAngle('5:30:00', unit='hourangle')
        project.runs[0].scans[0].dec = ephem.degrees('+22:30:00')

        self.assertEqual(project.runs[0].scans[0].mjd, 55616)
        self.assertEqual(project.runs[0].scans[0].mpm, 15000)
        self.assertEqual(project.runs[0].scans[0].dur, 15000)
        self.assertEqual(project.runs[0].scans[0].freq1, 1643482384)
        self.assertEqual(project.runs[0].scans[0].freq2, 1665395482)
        self.assertAlmostEqual(project.runs[0].scans[0].ra, 5.5, 6)
        self.assertAlmostEqual(project.runs[0].scans[0].dec, 22.5, 6)

        project.runs[0].scans[1].ra = '5h45m00s'
        project.runs[0].scans[1].dec = '+22d15m00s'

        self.assertAlmostEqual(project.runs[0].scans[1].ra, 5.75, 6)
        self.assertAlmostEqual(project.runs[0].scans[1].dec, 22.25, 6)

        dt0, dt1 = idf.get_scan_start_stop(project.runs[0].scans[0])
        self.assertEqual(dt0.year, 2011)
        self.assertEqual(dt0.month, 2)
        self.assertEqual(dt0.day, 24)
        self.assertEqual(dt0.hour, 0)
        self.assertEqual(dt0.minute, 0)
        self.assertEqual(dt0.second, 15)
        self.assertEqual(dt0.microsecond, 0)
        self.assertEqual(dt1.year, 2011)
        self.assertEqual(dt1.month, 2)
        self.assertEqual(dt1.day, 24)
        self.assertEqual(dt1.hour, 0)
        self.assertEqual(dt1.minute, 0)
        self.assertEqual(dt1.second, 30)
        self.assertEqual(dt1.microsecond, 0)

        for scan in project.runs[0].scans:
            scan.mjd += 1
            scan.mpm += 1000
        self.assertEqual(project.runs[0].scans[0].mjd, 55617)
        self.assertEqual(project.runs[0].scans[0].mpm, 16000)
        self.assertEqual(project.runs[0].scans[0].start,
                         'UTC 2011/02/25 00:00:16.000000')

        project.runs[0].scans[0].duration = 16.0
        self.assertEqual(project.runs[0].scans[0].dur, 16000)

        project.runs[0].scans[0].duration = '16.1'
        self.assertEqual(project.runs[0].scans[0].dur, 16100)

        project.runs[0].scans[0].duration = '0:01:01.501'
        self.assertEqual(project.runs[0].scans[0].dur, 61501)
Beispiel #5
0
    def test_username(self):
        """Test setting auto-copy parameters."""

        project = idf.parse_idf(drxFile)
        project.runs[0].data_return_method = 'UCF'
        project.runs[0].ucf_username = '******'
        out = project.render()

        self.assertTrue(out.find('Requested data return method is UCF') > 0)
        self.assertTrue(out.find('ucfuser:jdowell') > 0)

        project.writeto(os.path.join(self.testPath, 'idf.txt'))

        project = idf.parse_idf(os.path.join(self.testPath, 'idf.txt'))
        out = project.render()

        self.assertTrue(out.find('Requested data return method is UCF') > 0)
        self.assertTrue(out.find('ucfuser:jdowell') > 0)
Beispiel #6
0
    def test_jov_update(self):
        """Test updating TRK_JOV values."""

        project = idf.parse_idf(jovFile)
        project.runs[0].scans[0].start = "MST 2011 Feb 23 17:00:15"
        project.runs[0].scans[0].duration = timedelta(seconds=15)
        project.runs[0].scans[0].frequency1 = 75e6
        project.runs[0].scans[0].frequency2 = 76e6

        self.assertEqual(project.runs[0].scans[0].mjd, 55616)
        self.assertEqual(project.runs[0].scans[0].mpm, 15000)
        self.assertEqual(project.runs[0].scans[0].dur, 15000)
        self.assertEqual(project.runs[0].scans[0].freq1, 1643482384)
        self.assertEqual(project.runs[0].scans[0].freq2, 1665395482)
Beispiel #7
0
    def test_jov_errors(self):
        """Test various TRK_JOV IDF errors."""

        project = idf.parse_idf(jovFile)

        # Bad interferometer
        with self.assertRaises(ValueError):
            project.runs[0].stations = [
                lwa1,
            ]

        # Bad correlator channel count
        with self.assertRaises(ValueError):
            project.runs[0].correlator_channels = 129

        # Bad correlator integration time
        with self.assertRaises(ValueError):
            project.runs[0].correlator_inttime = 1e-6

        # Bad correlator output polarization basis
        with self.assertRaises(ValueError):
            project.runs[0].correlator_basis = 'cats'

        # Bad intent
        with self.assertRaises(ValueError):
            project.runs[0].scans[0].intent = 'cats'

        # Bad filter
        project.runs[0].scans[0].intent = 'Target'
        project.runs[0].scans[0].filter = 8
        project.runs[0].scans[0].update()
        self.assertFalse(project.validate())

        # Bad frequency
        project.runs[0].scans[0].filter = 6
        project.runs[0].scans[0].frequency1 = 90.0e6
        project.runs[0].scans[0].update()
        self.assertFalse(project.validate())

        project.runs[0].scans[0].frequency1 = 38.0e6
        project.runs[0].scans[0].frequency2 = 90.0e6
        project.runs[0].scans[0].update()
        self.assertFalse(project.validate())

        # Bad duration
        project.runs[0].scans[0].frequency2 = 38.0e6
        project.runs[0].scans[0].duration = '96:00:00.000'
        project.runs[0].scans[0].update()
        self.assertFalse(project.validate())
Beispiel #8
0
    def test_drx_parse(self):
        """Test reading in a TRK_RADEC IDF file."""

        project = idf.parse_idf(drxFile)

        # Basic file structure
        self.assertEqual(len(project.runs), 1)
        self.assertEqual(len(project.runs[0].scans), 2)

        # Correlator setup
        self.assertEqual(project.runs[0].correlator_channels, 256)
        self.assertAlmostEqual(project.runs[0].correlator_inttime, 1.0, 6)
        self.assertEqual(project.runs[0].correlator_basis, 'linear')

        # Observational setup - 1
        self.assertEqual(project.runs[0].scans[0].mode, 'TRK_RADEC')
        self.assertEqual(project.runs[0].scans[0].mjd, 58490)
        self.assertEqual(project.runs[0].scans[0].mpm, 74580000)
        self.assertEqual(project.runs[0].scans[0].dur, 7200000)
        self.assertEqual(project.runs[0].scans[0].freq1, 766958446)
        self.assertEqual(project.runs[0].scans[0].freq2, 1643482384)
        self.assertEqual(project.runs[0].scans[0].filter, 6)
        self.assertAlmostEqual(project.runs[0].scans[0].ra, 19.991210200, 6)
        self.assertAlmostEqual(project.runs[0].scans[0].dec, 40.733916000, 6)

        # Ordering
        scan = copy.deepcopy(project.runs[0].scans[0])
        scan.start = "UTC 2019/1/7 22:43:05"
        project.runs[0].append(scan)

        self.assertFalse(project.runs[0] < project.runs[0])
        self.assertTrue(project.runs[0] <= project.runs[0])
        self.assertFalse(project.runs[0] > project.runs[0])
        self.assertTrue(project.runs[0] >= project.runs[0])
        self.assertFalse(project.runs[0] != project.runs[0])
        self.assertTrue(project.runs[0] == project.runs[0])

        self.assertTrue(project.runs[0].scans[0] < project.runs[0].scans[1])
        self.assertTrue(project.runs[0].scans[0] <= project.runs[0].scans[1])
        self.assertFalse(project.runs[0].scans[0] > project.runs[0].scans[1])
        self.assertFalse(project.runs[0].scans[0] >= project.runs[0].scans[1])
        self.assertTrue(project.runs[0].scans[0] != project.runs[0].scans[1])
        self.assertFalse(project.runs[0].scans[0] == project.runs[0].scans[1])
Beispiel #9
0
    def test_jov_parse(self):
        """Test reading in a TRK_JOV IDF file."""

        project = idf.parse_idf(jovFile)

        # Basic file structure
        self.assertEqual(len(project.runs), 1)
        self.assertEqual(len(project.runs[0].scans), 1)

        # Correlator setup
        self.assertEqual(project.runs[0].correlator_channels, 512)
        self.assertAlmostEqual(project.runs[0].correlator_inttime, 0.1, 6)
        self.assertEqual(project.runs[0].correlator_basis, 'circular')

        # Observational setup - 1
        self.assertEqual(project.runs[0].scans[0].mode, 'TRK_JOV')
        self.assertEqual(project.runs[0].scans[0].mjd, 58490)
        self.assertEqual(project.runs[0].scans[0].mpm, 74580000)
        self.assertEqual(project.runs[0].scans[0].dur, 3600000)
        self.assertEqual(project.runs[0].scans[0].freq1, 766958446)
        self.assertEqual(project.runs[0].scans[0].freq2, 1643482384)
        self.assertEqual(project.runs[0].scans[0].filter, 6)
Beispiel #10
0
    def test_drx_proper_motion(self):
        """Test proper motion handling in a TRK_RADEC IDF file."""

        project = idf.parse_idf(drxFile)
        project.runs[0].scans[0].pm = [3182.7, 592.1]

        self.assertAlmostEqual(project.runs[0].scans[0].pm[0], 3182.7, 1)
        self.assertAlmostEqual(project.runs[0].scans[0].pm[1], 592.1, 1)

        ## TODO: Coordinate test?
        sdfs = project.generate_sdfs()
        for sdf in sdfs:
            for o in range(len(project.runs[0].scans)):
                bdy = project.runs[0].scans[o].fixed_body
                bdy.compute(project.runs[0].scans[o].mjd + MJD_OFFSET -
                            DJD_OFFSET +
                            project.runs[0].scans[o].mjd / 1000.0 / 86400.0)
                self.assertAlmostEqual(
                    bdy.a_ra, sdf.sessions[0].observations[o].ra * pi / 12.0,
                    5)
                self.assertAlmostEqual(
                    bdy.a_dec,
                    sdf.sessions[0].observations[o].dec * pi / 180.0, 5)
Beispiel #11
0
    def test_drx_alt_parse(self):
        """Test reading in a TRK_RADEC IDF file with other phase centers."""

        project = idf.parse_idf(altFile)

        # Basic file structure
        self.assertEqual(len(project.runs), 1)
        self.assertEqual(len(project.runs[0].scans), 1)

        # Correlator setup
        self.assertEqual(project.runs[0].correlator_channels, 256)
        self.assertAlmostEqual(project.runs[0].correlator_inttime, 1.0, 6)
        self.assertEqual(project.runs[0].correlator_basis, 'linear')

        # Observational setup - 1
        self.assertEqual(project.runs[0].scans[0].mode, 'TRK_RADEC')
        self.assertEqual(project.runs[0].scans[0].mjd, 58490)
        self.assertEqual(project.runs[0].scans[0].mpm, 74580000)
        self.assertEqual(project.runs[0].scans[0].dur, 7200000)
        self.assertEqual(project.runs[0].scans[0].freq1, 766958446)
        self.assertEqual(project.runs[0].scans[0].freq2, 1643482384)
        self.assertEqual(project.runs[0].scans[0].filter, 6)
        self.assertAlmostEqual(project.runs[0].scans[0].ra, 19.991210200, 6)
        self.assertAlmostEqual(project.runs[0].scans[0].dec, 40.733916000, 6)

        # Phase center - 1
        self.assertEqual(len(project.runs[0].scans[0].alt_phase_centers), 2)
        self.assertAlmostEqual(
            project.runs[0].scans[0].alt_phase_centers[0].ra, 19.991310200, 6)
        self.assertAlmostEqual(
            project.runs[0].scans[0].alt_phase_centers[0].dec, 40.733916000, 6)

        # Phase center - 2
        self.assertAlmostEqual(
            project.runs[0].scans[0].alt_phase_centers[1].ra, 19.991210200, 6)
        self.assertAlmostEqual(
            project.runs[0].scans[0].alt_phase_centers[1].dec, 40.734016000, 6)
Beispiel #12
0
    def test_drx_alt_proper_motion(self):
        """Test proper motion handling in a TRK_RADEC IDF file with other phase centers."""

        project = idf.parse_idf(altFile)
        project.runs[0].scans[0].alt_phase_centers[0].pm = [3182.7, 592.1]

        self.assertAlmostEqual(
            project.runs[0].scans[0].alt_phase_centers[0].pm[0], 3182.7, 1)
        self.assertAlmostEqual(
            project.runs[0].scans[0].alt_phase_centers[0].pm[1], 592.1, 1)
        self.assertAlmostEqual(
            project.runs[0].scans[0].alt_phase_centers[1].pm[0], 0.0, 1)
        self.assertAlmostEqual(
            project.runs[0].scans[0].alt_phase_centers[1].pm[1], 0.0, 1)

        ## TODO: Coordinate test?
        sdfs = project.generate_sdfs()
        for sdf in sdfs:
            for o in range(len(project.runs[0].scans)):
                sdf_phase_centers = sdf.project_office.observations[0][o]
                for i, phase_center in enumerate(
                        project.runs[0].scans[o].alt_phase_centers):
                    bdy = phase_center.fixed_body
                    bdy.compute(project.runs[0].scans[o].mjd + MJD_OFFSET -
                                DJD_OFFSET + project.runs[0].scans[o].mjd /
                                1000.0 / 86400.0)

                    ra = re.search("altra%i:(?P<ra>\d+(.\d*)?)" % (i + 1, ),
                                   sdf_phase_centers)
                    ra = float(ra.group('ra'))
                    dec = re.search(
                        "altdec%i:(?P<dec>[-+]?\d+(.\d*)?)" % (i + 1, ),
                        sdf_phase_centers)
                    dec = float(dec.group('dec'))
                    self.assertAlmostEqual(bdy.a_ra, ra * pi / 12.0, 5)
                    self.assertAlmostEqual(bdy.a_dec, dec * pi / 180.0, 5)
Beispiel #13
0
    def test_jov_write(self):
        """Test writing a TRK_JOV IDF file."""

        project = idf.parse_idf(jovFile)
        out = project.render()
Beispiel #14
0
    def test_drx_alt_write(self):
        """Test writing a TRK_RADEC IDF file with other phase centers."""

        project = idf.parse_idf(altFile)
        out = project.render()
Beispiel #15
0
    def test_drx_errors(self):
        """Test various TRK_RADEC IDF errors."""

        project = idf.parse_idf(drxFile)

        with _SilentVerbose() as sv:
            # Bad project
            old_id = project.id
            project.id = 'ThisIsReallyLong'
            self.assertFalse(project.validate(verbose=True))

            # Bad session
            project.id = old_id
            old_id = project.runs[0].id
            project.runs[0].id = 10001
            self.assertFalse(project.validate(verbose=True))

            # Bad interferometer
            project.runs[0].id = old_id
            with self.assertRaises(ValueError):
                project.runs[0].stations = [
                    lwa1,
                ]

            # Bad correlator channel count
            project.runs[0].stations = [lwa1, lwasv]
            with self.assertRaises(ValueError):
                project.runs[0].correlator_channels = 129

            # Bad correlator integration time
            project.runs[0].correlator_channels = 128
            with self.assertRaises(ValueError):
                project.runs[0].correlator_inttime = 1e-6

            # Bad correlator output polarization basis
            project.runs[0].correlator_inttime = 1.0
            with self.assertRaises(ValueError):
                project.runs[0].correlator_basis = 'cats'

            # Bad intent
            project.runs[0].correlator_basis = 'linear'
            with self.assertRaises(ValueError):
                project.runs[0].scans[0].intent = 'cats'

            # Good filter
            project.runs[0].scans[0].intent = 'Target'
            project.runs[0].scans[0].filter = 7
            project.runs[0].scans[1].filter = 7
            project.runs[0].scans[0].update()
            self.assertTrue(project.validate(verbose=True))

            # Bad filter
            project.runs[0].scans[0].filter = 8
            project.runs[0].scans[0].filter = 6
            project.runs[0].scans[0].update()
            self.assertFalse(project.validate(verbose=True))

            # Mis-matches filter
            project.runs[0].scans[0].filter = 6
            project.runs[0].scans[1].filter = 7
            project.runs[0].scans[0].update()
            self.assertFalse(project.validate(verbose=True))

            # Bad frequency
            project.runs[0].scans[1].filter = 6
            project.runs[0].scans[0].frequency1 = 10.0e6
            project.runs[0].scans[0].update()
            self.assertFalse(project.validate(verbose=True))

            project.runs[0].scans[0].filter = 6
            project.runs[0].scans[0].frequency1 = 90.0e6
            project.runs[0].scans[0].update()
            self.assertFalse(project.validate(verbose=True))

            project.runs[0].scans[0].frequency1 = 38.0e6
            project.runs[0].scans[0].frequency2 = 90.0e6
            project.runs[0].scans[0].update()
            self.assertFalse(project.validate(verbose=True))

            # Bad duration
            project.runs[0].scans[0].frequency2 = 38.0e6
            project.runs[0].scans[0].duration = '96:00:00.000'
            project.runs[0].scans[0].update()
            self.assertFalse(project.validate(verbose=True))

            # Bad pointing
            project.runs[0].scans[0].duration = '00:00:01.000'
            project.runs[0].scans[0].dec = -72.0
            project.runs[0].scans[0].update()
            self.assertFalse(project.validate(verbose=True))
Beispiel #16
0
    def test_drx_write(self):
        """Test writing a TRK_RADEC IDF file."""

        project = idf.parse_idf(drxFile)
        with _SilentVerbose() as sv:
            out = project.render(verbose=True)
Beispiel #17
0
    def test_set_stations(self):
        """Test the set stations functionlity."""

        project = idf.parse_idf(drxFile)
        project.runs[0].stations = [lwasv, lwa1]
        self.assertTrue(project.validate())
Beispiel #18
0
    def test_drx_alt_errors(self):
        """Test various TRK_RADEC IDF errors with other phase centers."""

        project = idf.parse_idf(altFile)

        with _SilentVerbose() as sv:
            # Bad interferometer
            with self.assertRaises(ValueError):
                project.runs[0].stations = [
                    lwa1,
                ]

            # Bad correlator channel count
            with self.assertRaises(ValueError):
                project.runs[0].correlator_channels = 129

            # Bad correlator integration time
            with self.assertRaises(ValueError):
                project.runs[0].correlator_inttime = 1e-6

            # Bad correlator output polarization basis
            with self.assertRaises(ValueError):
                project.runs[0].correlator_basis = 'cats'

            # Bad intent
            with self.assertRaises(ValueError):
                project.runs[0].scans[0].intent = 'cats'

            # Bad filter
            project.runs[0].scans[0].intent = 'Target'
            project.runs[0].scans[0].filter = 8
            project.runs[0].scans[0].update()
            self.assertFalse(project.validate(verbose=True))

            # Bad frequency
            project.runs[0].scans[0].filter = 6
            project.runs[0].scans[0].frequency1 = 90.0e6
            project.runs[0].scans[0].update()
            self.assertFalse(project.validate(verbose=True))

            project.runs[0].scans[0].frequency1 = 38.0e6
            project.runs[0].scans[0].frequency2 = 90.0e6
            project.runs[0].scans[0].update()
            self.assertFalse(project.validate(verbose=True))

            # Bad duration
            project.runs[0].scans[0].frequency2 = 38.0e6
            project.runs[0].scans[0].duration = '96:00:00.000'
            project.runs[0].scans[0].update()
            self.assertFalse(project.validate(verbose=True))

            # Bad pointing
            project.runs[0].scans[0].duration = '00:00:01.000'
            project.runs[0].scans[0].dec = -72.0
            project.runs[0].scans[0].update()
            self.assertFalse(project.validate(verbose=True))

            # Bad alternate phase center
            project.runs[0].scans[0].dec = 40.733916000
            project.runs[0].scans[0].alt_phase_centers[0].dec = 45.0
            project.runs[0].scans[0].update()
            self.assertFalse(project.validate(verbose=True))

            # Bad alternate phase center intent
            project.runs[0].scans[0].alt_phase_centers[0].dec = 40.733916000
            with self.assertRaises(ValueError):
                project.runs[0].scans[0].alt_phase_centers[0].intent = 'cats'

            # Too many phase centers
            project.runs[0].scans[0].alt_phase_centers[0].intent = 'PhaseCal'
            for i in range(40):
                project.runs[0].scans[0].add_alt_phase_center(
                    'test', 'Target', 19.991210200, 40.733916000)
            project.runs[0].scans[0].update()
            self.assertFalse(project.validate(verbose=True))
def main(args):
    # Filenames in an easier format - input
    inputIDF  = args.filename
    if args.date is not None:
        y, m, d = args.date.split('/', 2)
        args.date = date(int(y,10), int(m,10), int(d,10))
    if args.time is not None:
        h, m, s = args.time.split(':', 2)
        us = int((float(s) - int(float(s)))*1e6)
        s = int(float(s))
        if us >= 1000000:
            us -= 1000000
            s += 1
        args.time = time(int(h,10), int(m,10), s, us)
        
    # Parse the input file and get the dates of the scans
    station = stations.lwa1
    project = idf.parse_idf(inputIDF)
    
    # Load the station and objects to find the Sun and Jupiter
    observer = station.get_observer()
    Sun = ephem.Sun()
    Jupiter = ephem.Jupiter()
    
    nObs = len(project.runs[0].scans)
    tStart = [None,]*nObs
    for i in range(nObs):
        tStart[i]  = utcjd_to_unix(project.runs[0].scans[i].mjd + MJD_OFFSET)
        tStart[i] += project.runs[0].scans[i].mpm / 1000.0
        tStart[i]  = datetime.utcfromtimestamp(tStart[i])
        tStart[i]  = _UTC.localize(tStart[i])
        
    # Get the LST at the start
    observer.date = (min(tStart)).strftime('%Y/%m/%d %H:%M:%S')
    lst = observer.sidereal_time()
    
    # Report on the file
    print("Filename: %s" % inputIDF)
    print(" Project ID: %s" % project.id)
    print(" Run ID: %i" % project.runs[0].id)
    print(" Scans appear to start at %s" % (min(tStart)).strftime(formatString))
    print(" -> LST at %s for this date/time is %s" % (station.name, lst))
    
    # Filenames in an easier format - output
    if not args.query:
        if args.outname is not None:
            outputIDF = args.outname
        else:
            outputIDF  = None
            
    # Query only mode starts here...
    if args.query:
        lastDur = project.runs[0].scans[nObs-1].dur
        lastDur = timedelta(seconds=int(lastDur/1000), microseconds=(lastDur*1000) % 1000000)
        runDur = max(tStart) - min(tStart) + lastDur
        
        print(" ")
        print(" Total Run Duration: %s" % runDur)
        print(" -> First scan starts at %s" % min(tStart).strftime(formatString))
        print(" -> Last scan ends at %s" % (max(tStart) + lastDur).strftime(formatString))
        print(" Correlator Setup:")
        print(" -> %i channels" % project.runs[0].corr_channels)
        print(" -> %.3f s integration time" % project.runs[0].corr_inttime)
        print(" -> %s output polarization basis" % project.runs[0].corr_basis)
        
        print(" ")
        print(" Number of scans: %i" % nObs)
        print(" Scan Detail:")
        for i in range(nObs):
            currDur = project.runs[0].scans[i].dur
            currDur = timedelta(seconds=int(currDur/1000), microseconds=(currDur*1000) % 1000000)
            
            print("  Scan #%i" % (i+1,))
            
            ## Basic setup
            print("   Target: %s" % project.runs[0].scans[i].target)
            print("   Intent: %s" % project.runs[0].scans[i].intent)
            print("   Start:")
            print("    MJD: %i" % project.runs[0].scans[i].mjd)
            print("    MPM: %i" % project.runs[0].scans[i].mpm)
            print("    -> %s" % get_scan_start_stop(project.runs[0].scans[i])[0].strftime(formatString))
            print("   Duration: %s" % currDur)
            
            ## DP setup
            print("   Tuning 1: %.3f MHz" % (project.runs[0].scans[i].frequency1/1e6,))
            print("   Tuning 2: %.3f MHz" % (project.runs[0].scans[i].frequency2/1e6,))
            print("   Filter code: %i" % project.runs[0].scans[i].filter)
            
            ## Comments/notes
            print("   Observer Comments: %s" % project.runs[0].scans[i].comments)
            
        # Valid?
        print(" ")
        try:
            if project.validate():
                print(" Valid?  Yes")
            else:
                print(" Valid?  No")
        except:
            print(" Valid?  No")
            
        # And then exits
        sys.exit()
        
    #
    # Query the time and compute the time shifts
    #
    if (not args.no_update):
        # Get the new start date/time in UTC and report on the difference
        if args.lst:
            if args.date is None:
                print(" ")
                print("Enter the new UTC start date:")
                tNewStart = input('YYYY/MM/DD-> ')
                try:
                    fields = tNewStart.split('/', 2)
                    fields = [int(f) for f in fields]
                    tNewStart = date(fields[0], fields[1], fields[2])
                    tNewStart = datetime.combine(tNewStart, min(tStart).time())
                except Exception as e:
                    print("Error: %s" % str(e))
                    sys.exit(1)
                    
            else:
                tNewStart = datetime.combine(args.date, min(tStart).time())
                
            tNewStart = _UTC.localize(tNewStart)
            
            # Figure out a new start time on the correct day
            diff = ((tNewStart - min(tStart)).days) * siderealRegression
            ## Try to make sure that the timedelta object is less than 1 day
            while diff.days > 0:
                diff -= siderealDay
            while diff.days < -1:
                diff += siderealDay
            ## Come up with the new start time
            siderealShift = tNewStart - diff
            ## Another check to make sure we are are the right day
            if siderealShift.date() < tNewStart.date():
                siderealShift += siderealDay
            if siderealShift.date() > tNewStart.date():
                siderealShift -= siderealDay
            ## And yet another one to deal with the corner case that scan starts at ~UT 00:00
            if min(tStart) == siderealShift:
                newSiderealShift1 = siderealShift + siderealDay
                newSiderealShift2 = siderealShift - siderealDay
                if newSiderealShift1.date() == tNewStart.date():
                    siderealShift = newSiderealShift1
                elif newSiderealShift2.date() == tNewStart.date():
                    siderealShift = newSiderealShift2
            tNewStart = siderealShift
            
        else:
            if args.date is None or args.time is None:
                print(" ")
                print("Enter the new UTC start date/time:")
                tNewStart = input('YYYY/MM/DD HH:MM:SS.SSS -> ')
                try:
                    tNewStart = datetime.strptime(tNewStart, '%Y/%m/%d %H:%M:%S.%f')
                except ValueError:
                    try:
                        tNewStart = datetime.strptime(tNewStart, '%Y/%m/%d %H:%M:%S')
                    except Exception as e:
                        print("Error: %s" % str(e))
                        sys.exit(1)
                        
            else:
                tNewStart = datetime.combine(args.date, args.time)
            
            tNewStart = _UTC.localize(tNewStart)
            
        # Get the new shift needed to translate the old times to the new times
        tShift = tNewStart - min(tStart)
        
        # Get the LST at the new start
        observer.date = (tNewStart).strftime('%Y/%m/%d %H:%M:%S')
        lst = observer.sidereal_time()
        
        print(" ")
        print("Shifting scans to start at %s" % tNewStart.strftime(formatString))
        print("-> Difference of %i days, %.3f seconds" % (tShift.days, (tShift.seconds + tShift.microseconds/1000000.0),))
        print("-> LST at %s for this date/time is %s" % (station.name, lst))
        if tShift.days == 0 and tShift.seconds == 0 and tShift.microseconds == 0:
            print(" ")
            print("The current shift is zero.  Do you want to continue anyways?")
            yesNo = input("-> [y/N] ")
            if yesNo not in ('y', 'Y'):
                sys.exit()
                
    else:
        tShift = timedelta(seconds=0)
        
    # Shift the start times and recompute the MJD and MPM values
    for i in range(nObs):
        tStart[i] += tShift
        
    #
    # Query and set the new run ID
    #
    print(" ")
    if args.rid is None:
        print("Enter the new run ID or return to keep current:")
        sid = input('-> ')
        if len(sid) > 0:
            sid = int(sid)
        else:
            sid = project.runs[0].id
    else:
        sid = args.rid
    print("Shifting run ID from %i to %i" % (project.runs[0].id, sid))
    project.runs[0].id = sid
    
    #
    # Go! (apply the changes to the scans)
    #
    print(" ")
    newPOOC = []
    for i in range(nObs):
        print("Working on Scan #%i" % (i+1,))
        newPOOC.append("")
        
        #
        # Start MJD,MPM Shifting
        #
        if (not args.no_update) and tShift != timedelta(seconds=0):
            if len(newPOOC[-1]) != 0:
                newPOOC[-1] += ';;'
            newPOOC[-1] += 'Original MJD:%i,MPM:%i' % (project.runs[0].scans[i].mjd, project.runs[0].scans[i].mpm)
            
            start = tStart[i].strftime("%Z %Y %m %d %H:%M:%S.%f")
            start = start[:-3]

            utc = Time(tStart[i], format=Time.FORMAT_PY_DATE)
            mjd = int(utc.utc_mjd)
            
            utcMidnight = datetime(tStart[i].year, tStart[i].month, tStart[i].day, 0, 0, 0, tzinfo=_UTC)
            diff = tStart[i] - utcMidnight
            mpm = int(round((diff.seconds + diff.microseconds/1000000.0)*1000.0))
            
            print(" Time shifting")
            print("  MJD: %8i -> %8i" % (project.runs[0].scans[i].mjd, mjd))
            print("  MPM: %8i -> %8i" % (project.runs[0].scans[i].mpm, mpm))
            
            project.runs[0].scans[i].mjd = mjd
            project.runs[0].scans[i].mpm = mpm
            project.runs[0].scans[i].start = start
            
    #
    # Project office comments
    #
    # Update the project office comments with this change
    newPOSC = "Shifted IDF with shiftIDF.py (v%s);;Time Shift? %s" % (__version__, 'Yes' if (not args.no_update) else 'No')
    
    if project.project_office.runs[0] is None:
        project.project_office.runs[0] = newPOSC
    else:
        project.project_office.runs[0] += ';;%s' % newPOSC
        
    for i in range(nObs):
        try:
            project.project_office.scans[0][i] += ';;%s' % newPOOC[i]
        except Exception as e:
            print(e)
            project.project_office.scans[0][i] = '%s' % newPOOC[i]
            
    #
    # Save
    #
    if outputIDF is None:
        pID = project.id
        rID = project.runs[0].id
        foStart = min(tStart)
        outputIDF = '%s_%s_%s_%04i.idf' % (pID, foStart.strftime('%y%m%d'), foStart.strftime('%H%M'), rID)
        
    print(" ")
    print("Saving to: %s" % outputIDF)
    fh = open(outputIDF, 'w')
    if not project.validate():
        # Make sure we are about to be valid
        project.validate(verbose=True)
        raise RuntimeError("Cannot validate IDF file")
        
    fh.write( project.render() )
    fh.close()
Beispiel #20
0
    def test_sol_write(self):
        """Test writing a TRK_SOL IDF file."""

        project = idf.parse_idf(solFile)
        out = project.render()