def checkSignificance(self, fermi, tstart, tstop): fermi_data = pd.read_csv(fermi, header=0, sep=" ") ntrials = 0 nsig = 0 for time in range(int(tstart), int(tstop)): #print(time) tstart_tt = AstroUtils.time_mjd_to_agile_seconds(time) tstop_tt = AstroUtils.time_mjd_to_agile_seconds(time+1) fermi_data2 = fermi_data[fermi_data.tstart >= tstart_tt] fermi_data2 = fermi_data2[fermi_data.tstop <= tstop_tt] fermimean = fermi_data2["cts"].mean() fermistd = fermi_data2["cts"].std() n=0 for cts in fermi_data2["cts"]: ntrials = ntrials + 1 #print(time, time+1, cts, fermimean, fermistd, fermimean + 3 * fermistd, cts >= (fermimean + 3 * fermistd)) if cts >= (fermimean + 5 * fermistd): self.logger.info(self, "####") self.logger.info(self, f"{fermi_data2['tstart']}") #print(fermi_data2["tstart"][n]) nsig = nsig + 1 break n = n + 1 self.logger.info(self, f"ntrials {ntrials}") self.logger.info(self, f"nsig {nsig}")
def load_and_plot(self, agile, fermi, tstart, tstop, path, lines=[], plotrate=False): """Main function, it loads and plots the data """ #---- Loading data ----- agile_data = pd.read_csv(agile, header=0, sep=" ") fermi_data = pd.read_csv(fermi, header=0, sep=" ") #---Converting times tstart_tt = AstroUtils.time_mjd_to_agile_seconds(tstart) tstop_tt = AstroUtils.time_mjd_to_agile_seconds(tstop) #---- Selecting data agile_data = agile_data[agile_data.tstart >= tstart_tt] agile_data = agile_data[agile_data.tstop <= tstop_tt] fermi_data = fermi_data[fermi_data.tstart >= tstart_tt] fermi_data = fermi_data[fermi_data.tstop <= tstop_tt] #------Plotting data f, (ax1, ax2) = plt.subplots(2, figsize=(12.18,10)) self.plot_offaxis(ax1, ax2, path, tstart, tstop, 60, 1, 0, lines) self.plot(ax2, agile_data, fermi_data, lines, plotrate) plt.show() f.savefig('merged_plot_'+str(tstart)+'_'+str(tstop)+'.'+str('pdf'), format="pdf")
def _setTime(confDict): if confDict["selection"]["timetype"] == "MJD": confDict["selection"][ "tmax"] = AstroUtils.time_mjd_to_agile_seconds( confDict["selection"]["tmax"]) confDict["selection"][ "tmin"] = AstroUtils.time_mjd_to_agile_seconds( confDict["selection"]["tmin"]) confDict["selection"]["timetype"] = "TT"
def test_mjd_conversion(input_date, expected): assert AstroUtils.time_mjd_to_agile_seconds( input_date) == expected["agile_seconds"] assert AstroUtils.time_mjd_to_jd(input_date) == pytest.approx( expected["jd"], 0.00001) assert AstroUtils.time_mjd_to_unix(input_date) == expected["unix"] assert AstroUtils.time_mjd_to_fits(input_date) == expected["fits"] assert AstroUtils.time_mjd_to_iso(input_date) == expected["iso"]
def test_setOptionTimeMJD(self): test_out_dir = self.set_outputfolder("test_setOptionTimeMJD") ag = AGAnalysis(self.agilepyConf) tmin1 = 58030.0 tmax1 = 58035.0 tmintt = AstroUtils.time_mjd_to_agile_seconds(tmin1) tmaxtt = AstroUtils.time_mjd_to_agile_seconds(tmax1) ag.setOptionTimeMJD(tmin=tmin1, tmax=tmax1) tmin2 = ag.getOption("tmin") tmax2 = ag.getOption("tmax") self.assertEqual(tmintt, tmin2) self.assertEqual(tmaxtt, tmax2) ag.destroy()
def _validateTimeInIndex(confDict): errors = {} if (confDict["input"]["userestapi"] == True): return errors (first, last) = Utils._getFirstAndLastLineInFile(confDict["input"]["evtfile"]) idxTmin = Utils._extractTimes(first)[0] idxTmax = Utils._extractTimes(last)[1] userTmin = confDict["selection"]["tmin"] userTmax = confDict["selection"]["tmax"] timetype = confDict["selection"]["timetype"] if timetype == "MJD": userTmin = AstroUtils.time_mjd_to_agile_seconds(userTmin) userTmax = AstroUtils.time_mjd_to_agile_seconds(userTmax) if float(userTmin) < float(idxTmin): errors["input/tmin"]="tmin: {} is outside the time range of {} (tmin < indexTmin). Index file time range: [{}, {}]" \ .format(userTmin, confDict["input"]["evtfile"], idxTmin, idxTmax) if float(userTmin) > float(idxTmax): errors["input/tmin"]="tmin: {} is outside the time range of {} (tmin > indexTmax). Index file time range: [{}, {}]" \ .format(userTmin, confDict["input"]["evtfile"], idxTmin, idxTmax) if float(userTmax) > float(idxTmax): errors["input/tmax"]="tmax: {} is outside the time range of {} (tmax > indexTmax). Index file time range: [{}, {}]" \ .format(userTmax, confDict["input"]["evtfile"], idxTmin, idxTmax) if float(userTmax) < float(idxTmin): errors["input/tmax"]="tmax: {} is outside the time range of {} (tmax < indexTmin). Index file time range: [{}, {}]" \ .format(userTmax, confDict["input"]["evtfile"], idxTmin, idxTmax) return errors
def test_astro_utils_time_mjd_to_agile_seconds(self): sec_tolerance = 0.001 tt = AstroUtils.time_mjd_to_agile_seconds( 58871.45616898) # 506861812.99987227 assert abs(506861813 - tt) <= sec_tolerance
def _computePointingDistancesFromSource(self, logfilesIndex, tmin, tmax, src_x, src_y, ref, zmax, step, writeFiles): """ It computes the angular separations between the center of the AGILE GRID field of view and the coordinates for a given position in the sky, given by src_ra and src_dec. Args: tmin (float): inferior observation time limit to analize. tmax (float): superior observation time limit to analize. src_x (float): source position x (unit: degrees) src_y (float): source position y (unit: degrees) zmax (float): maximum zenith distance of the source to the center of the detector (unit: degrees) step (integer): time interval in seconds between 2 consecutive points in the resulting plot. Minimum accepted value: 0.1 s. writeFiles (bool): if True, two text files with the separions data will be written on file. logfilesIndex (str) (optional): the index file for the logs files. If specified it will ovverride the one in the configuration file. Returns: separations (List): the angular separations ti_tt (List): tf_tt (List): ti_mjd (List): tf_mjd (List): skyCordsFK5.ra.deg skyCordsFK5.dec.deg """ self.logger.info( self, "Computing pointing distances from source (%f, %f) %s in [%f, %f]", src_x, src_y, ref, tmin, tmax) if not logfilesIndex: raise ValueError("'logfilesIndex' cannot be None") if ref == "equ": skyCordsFK5 = SkyCoord(ra=src_x * u.degree, dec=src_y * u.degree, frame='fk5') elif ref == "gal": skyCordsGAL = SkyCoord(l=src_x * u.degree, b=src_y * u.degree, frame='galactic') #skyCordsICRS = skyCordsGAL.transform_to('icrs') skyCordsFK5 = skyCordsGAL.transform_to('fk5') else: self.logger.critical(self, "Reference system '%s' is not supported", ref) raise WrongCoordinateSystemError( "Reference system '%s' is not supported" % (ref)) if step < 0.1: self.logger.critical(self, "step %f cannot be < 0.1", step) raise ValueError("'step' %f cannot be < 0.1" % (step)) self.logger.debug(self, "Galactict coords: l:%f b:%f", skyCordsGAL.l.deg, skyCordsGAL.b.deg) self.logger.debug(self, "FK5 coords: ra:%f dec:%f ", skyCordsFK5.ra.deg, skyCordsFK5.dec.deg) logFiles = self._getLogsFileInInterval(logfilesIndex, tmin, tmax) self.logger.info(self, "%d log files satisfy the interval %f-%f", len(logFiles), tmin, tmax) if not logFiles: self.logger.warning( self, "No log files can are compatible with tmin %f and tmax %f", tmin, tmax) return [], [], [], [], [], skyCordsFK5.ra.deg, skyCordsFK5.dec.deg, None total = len(logFiles) tmin_start = tmin tmax_start = tmax separation_tot = None ti_tt_tot = None tf_tt_tot = None init = False self.logger.info(self, "Computing pointing distances. Please wait..") for idx, logFile in enumerate(logFiles): idx = idx + 1 self.logger.info(self, "%d/%d %s", idx, total, logFile) if idx == 1 or idx == total: doTimeMask = True else: doTimeMask = False separation, ti_tt, tf_tt = self._computeSeparationPerFile( doTimeMask, logFile, tmin_start, tmax_start, skyCordsFK5, zmax, step) if not init: separation_tot = separation ti_tt_tot = ti_tt tf_tt_tot = tf_tt init = True else: separation_tot = np.concatenate((separation_tot, separation), axis=0) ti_tt_tot = np.concatenate((ti_tt_tot, ti_tt), axis=0) tf_tt_tot = np.concatenate((tf_tt_tot, tf_tt), axis=0) self.logger.debug(self, "Total computed separations: %d", len(separation_tot)) # Conversion TT => MJD self.logger.info( self, "Converting ti_tt_tot from TT to MJD..Number of elements=%d", len(ti_tt_tot)) ti_mjd = AstroUtils.time_mjd_to_agile_seconds(ti_tt_tot) self.logger.info( self, "Converting tf_tt_tot from TT to MJD..Number of elements=%d", len(tf_tt_tot)) tf_mjd = AstroUtils.time_mjd_to_agile_seconds(tf_tt_tot) """ self.logger.info(self, "Computig meantimes..Number of elements=%d", len(ti_mjd)) meantimes = (ti_mjd+tf_mjd)/2. if writeFiles: zmax = zmax*u.deg ttotal_under_zmax = np.sum(tf_tt_tot[separation_tot<zmax]-ti_tt_tot[separation_tot<zmax]) ttotal_above_zmax = np.sum(tf_tt_tot[separation_tot>zmax]-ti_tt_tot[separation_tot>zmax]) kk = open(join(self.outdir,"times_bins_vs_separation.txt"), "w") filesep = open(join(self.outdir,'time_vs_separation_agile.txt'), 'w') for i in np.arange(len(separation_tot)): filesep.write("{} {}\n".format(meantimes[i], separation_tot[i])) kk.write("{} {} {}\n".format(ti_tt_tot[i], tf_tt_tot[i], separation_tot[i])) filesep.close() kk.close() """ self.logger.debug(self, "separation_tot len: %d", len(separation_tot)) self.logger.debug(self, "ti_tt_tot len: %d", len(ti_tt_tot)) self.logger.debug(self, "tf_tt_tot len: %d", len(tf_tt_tot)) filenamePath = None if writeFiles: filename = f"offaxis_distances_{tmin}_{tmax}" outdirPath = Path(self.outdir).joinpath("offaxis_data") outdirPath.mkdir(parents=True, exist_ok=True) filenamePath = outdirPath.joinpath(filename) np.save(filenamePath, separation_tot.degree, allow_pickle=True) self.logger.info(self, "Produced: %s", filenamePath) return separation_tot.degree, ti_tt_tot, tf_tt_tot, ti_mjd, tf_mjd, skyCordsFK5.ra.deg, skyCordsFK5.dec.deg, filenamePath