Exemple #1
0
    def test_astro_utils_time_agile_seconds_to_utc(self):

        sec_tol = 1
        """
        utc = AstroUtils.time_tt_to_utc(506861813)
        dt = datetime.strptime(utc, '%Y-%m-%dT%H:%M:%S.%f')

        assert dt.year == 2020
        assert dt.month == 1
        assert dt.day == 23
        assert dt.hour == 10
        assert dt.minute == 56
        assert abs(53 - dt.second) <= sec_tol
        """

        # This date would result in "0 days"
        sec_tolerance = 0.0000001
        fitstime = AstroUtils.time_agile_seconds_to_fits(449582332)
        dt = datetime.strptime(fitstime, '%Y-%m-%dT%H:%M:%S.%f')

        assert dt.year == 2018
        assert dt.month == 3
        assert dt.day == 31
        assert dt.hour == 11
        assert dt.minute == 58
        assert abs(52 - dt.second) <= sec_tol
def test_unix_conversion(input_date, expected):

    assert AstroUtils.time_agile_seconds_to_iso(input_date) == expected["iso"]
    assert AstroUtils.time_agile_seconds_to_jd(input_date) == pytest.approx(
        expected["jd"], 0.00001)
    assert AstroUtils.time_agile_seconds_to_mjd(input_date) == pytest.approx(
        expected["mjd"], 0.00001)
    assert AstroUtils.time_agile_seconds_to_unix(
        input_date) == expected["unix"]
    assert AstroUtils.time_agile_seconds_to_fits(
        input_date) == expected["fits"]
    def plotDataAvailability(self, qFilePath, indexFilePath, saveImage=False):

        queryDatesDF = pd.read_csv(qFilePath,
                                   header=None,
                                   sep=" ",
                                   names=["tmin", "tmax"],
                                   parse_dates=["tmin", "tmax"])

        # df = pd.read_csv( "/agilepy/testing/unittesting/utils/data/EVT.index", header=None, sep=" ", names=["name","tmin", "tmax", "type"])
        indexDatesDF = pd.read_csv(indexFilePath,
                                   header=None,
                                   sep=" ",
                                   names=["name", "tmin", "tmax", "type"])

        dateformat = "%Y-%m-%d"
        print(indexDatesDF)

        indexDatesDF["tmin"] = AstroUtils.time_agile_seconds_to_fits(
            indexDatesDF["tmin"])
        indexDatesDF["tmax"] = AstroUtils.time_agile_seconds_to_fits(
            indexDatesDF["tmax"])

        indexDatesDF["tmin"] = [
            datetime.datetime.strptime(
                tmin, "%Y-%m-%dT%H:%M:%S.%f").strftime(dateformat)
            for tmin in indexDatesDF["tmin"]
        ]

        indexDatesDF["tmax"] = [
            datetime.datetime.strptime(
                tmax, "%Y-%m-%dT%H:%M:%S.%f").strftime(dateformat)
            for tmax in indexDatesDF["tmax"]
        ]

        print(indexDatesDF["tmin"], indexDatesDF["tmax"])

        fig, ax = plt.subplots(1, 1)
        #ax2 = ax.twinx()

        for index, slot in indexDatesDF.iterrows():
            daterange = pd.date_range(start=slot["tmin"],
                                      end=slot["tmax"],
                                      freq="D")
            ax.plot(
                daterange,
                [0.5 for _ in range(len(daterange))],
                color="green",
            )

        for index, slot in queryDatesDF.iterrows():
            daterange = pd.date_range(start=slot["tmin"],
                                      end=slot["tmax"],
                                      freq="D")
            #ax.plot([7 for _ in range(len(daterange))],daterange, color="green", )

            ax.fill_between(daterange,
                            0,
                            1,
                            where=np.ones(shape=len(daterange)),
                            color='green',
                            alpha=0.5,
                            transform=ax.get_xaxis_transform(),
                            edgecolor=None)

            #ax.bar(slot["tmin"], 0.5, width=slot["tmax"]-slot["tmin"])

        fig.autofmt_xdate(rotation=45)

        if saveImage:
            filePath = join(self.outdir, "data_availability.png")
            print("filePath", filePath)
            self.logger.info(self, f"Plot at: {filePath}")
            fig.savefig(filePath)
            return filePath
        else:
            fig.show()
            return None