Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions include/MFITSWriterL1a.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ class MFITSWriterL1a

bool m_HasEvents;

//! First / last event time seen, RTS (mission seconds since 2025-01-01)
double m_FirstEventTime_RTS;
double m_LastEventTime_RTS;
//! First / last FITS time, elapsed seconds from 2025-01-01T00:00:00 UTC
double m_FirstEventTime;
double m_LastEventTime;

std::vector<double> m_BatchTIME;
std::vector<uint32_t> m_BatchEVENTID;
Expand Down
5 changes: 4 additions & 1 deletion include/MModuleLoaderMeasurementsFITS.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ class MModuleLoaderMeasurementsFITS : public MModuleLoaderMeasurements
//! Total number of rows in the FITS table
long m_TotalRows;

//! Offset from the FITS reference epoch to the internal RTS epoch, in seconds
double m_TimeReferenceOffsetRTS = 0.0;

//! Batch size for reading FITS data
const long m_BatchSize = 100;

Expand All @@ -115,7 +118,7 @@ class MModuleLoaderMeasurementsFITS : public MModuleLoaderMeasurements
std::vector<double> m_BatchTIME;
std::vector<uint32_t> m_BatchEVENTID;
std::vector<uint8_t> m_BatchEVENTTYPE;
std::vector<uint8_t> m_BatchNUMSTRIPHIT;
std::vector<uint16_t> m_BatchNUMSTRIPHIT;

//! Batch data storage for variable-length array columns
std::vector<std::valarray<uint8_t>> m_BatchHITTYPE;
Expand Down
8 changes: 4 additions & 4 deletions include/MModuleSaverMeasurementsFITS.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ class MModuleSaverMeasurementsFITS : public MModule
//! Total number of events skipped (L2 screening)
long m_TotalEventsSkipped;

//! First event time seen, RTS (mission seconds since 2025-01-01)
double m_FirstEventTime_RTS;
//! First event time seen, FITS TIME (elapsed seconds from 2025-01-01T00:00:00 UTC)
double m_FirstEventTime;

//! Last event time seen, RTS (mission seconds since 2025-01-01)
double m_LastEventTime_RTS;
//! Last event time seen, FITS TIME (elapsed seconds from 2025-01-01T00:00:00 UTC)
double m_LastEventTime;

//! Whether any events have been processed yet
bool m_HasEvents;
Expand Down
77 changes: 62 additions & 15 deletions src/MFITSWriterL1a.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "MFITSWriterL1a.h"

// Standard libs:
#include <cmath>

// MEGAlib libs:
#include "MStripHit.h"
Expand Down Expand Up @@ -59,8 +60,8 @@ MFITSWriterL1a::MFITSWriterL1a()
m_BatchStartRow = 1;
m_BatchEventCount = 0;
m_HasEvents = false;
m_FirstEventTime_RTS = 0;
m_LastEventTime_RTS = 0;
m_FirstEventTime = 0;
m_LastEventTime = 0;
}


Expand All @@ -69,8 +70,7 @@ MFITSWriterL1a::MFITSWriterL1a()

MFITSWriterL1a::~MFITSWriterL1a()
{
delete m_FITSFile;
m_FITSFile = nullptr;
Close();
}


Expand All @@ -80,8 +80,13 @@ MFITSWriterL1a::~MFITSWriterL1a()
bool MFITSWriterL1a::Create(const MString& FileName)
{
// Create the FITS file with an empty primary HDU and the GED_L1A extension.
if (m_FITSFile != nullptr) Close();
if (FileName == "") return false;

try {
m_FITSFile = new FITS(string(FileName), RWmode::Write);
string FitsFileName = string(FileName);
if (FitsFileName[0] != '!') FitsFileName.insert(0, "!");
m_FITSFile = new FITS(FitsFileName, RWmode::Write);

// Primary header (Table 6.2d primary)
PHDU& primary = m_FITSFile->pHDU();
Expand Down Expand Up @@ -180,6 +185,9 @@ bool MFITSWriterL1a::Create(const MString& FileName)
return true;
} catch (const FitsException& e) {
if (g_Verbosity >= c_Error) cout << "MFITSWriterL1a: Create error: " << e.message() << endl;
delete m_FITSFile;
m_FITSFile = nullptr;
m_Table = nullptr;
return false;
}
}
Expand All @@ -190,15 +198,30 @@ bool MFITSWriterL1a::Create(const MString& FileName)

bool MFITSWriterL1a::Write(MReadOutAssembly* Event)
{
if (m_FITSFile == nullptr || m_Table == nullptr || Event == nullptr) {
if (g_Verbosity >= c_Error) {
cout << "MFITSWriterL1a: Write error:"
<< (m_FITSFile == nullptr ? " m_FITSFile is null." : "")
<< (m_Table == nullptr ? " m_Table is null." : "")
<< (Event == nullptr ? " Event is null." : "") << endl;
}
return false;
}

double time = Event->GetTimeRTS().GetAsDouble();
// Set Time
MTime TimeUTC = Event->GetTimeUTC();
if (TimeUTC == 0) {
TimeUTC = Event->ComputeUTCfromRTSTime(Event->GetTimeRTS());
}
const MTime FITSEpochUTC(2025, 1, 1, 0, 0, 0, 0);
double secSince2025UTC = (TimeUTC - FITSEpochUTC).GetAsDouble();
if (!m_HasEvents) {
m_FirstEventTime_RTS = time;
m_LastEventTime_RTS = time;
m_FirstEventTime = secSince2025UTC;
m_LastEventTime = secSince2025UTC;
m_HasEvents = true;
} else {
if (time < m_FirstEventTime_RTS) m_FirstEventTime_RTS = time;
if (time > m_LastEventTime_RTS) m_LastEventTime_RTS = time;
if (secSince2025UTC < m_FirstEventTime) m_FirstEventTime = secSince2025UTC;
if (secSince2025UTC > m_LastEventTime) m_LastEventTime = secSince2025UTC;
}

unsigned int nStripHits = Event->GetNStripHits();
Expand All @@ -220,7 +243,7 @@ bool MFITSWriterL1a::Write(MReadOutAssembly* Event)
tac[i] = hit->GetTAC();
}

m_BatchTIME.push_back(time);
m_BatchTIME.push_back(secSince2025UTC);
m_BatchEVENTID.push_back((uint32_t)Event->GetID());
m_BatchEVENTTYPE.push_back(0); // TODO
m_BatchNUMSTRIPHIT.push_back(nStripHits);
Expand All @@ -245,6 +268,14 @@ bool MFITSWriterL1a::Write(MReadOutAssembly* Event)

bool MFITSWriterL1a::FlushBatch()
{
if (m_FITSFile == nullptr || m_Table == nullptr) {
if (g_Verbosity >= c_Error) {
cout << "MFITSWriterL1a: FlushBatch error:"
<< (m_FITSFile == nullptr ? " m_FITSFile is null." : "")
<< (m_Table == nullptr ? " m_Table is null." : "") << endl;
}
return false;
}
if (m_BatchEventCount == 0) return true;
try {
long firstRow = m_BatchStartRow;
Expand Down Expand Up @@ -293,12 +324,12 @@ void MFITSWriterL1a::Close()

if (m_HasEvents && m_Table != nullptr) {
try {
m_Table->addKey("TSTART", m_FirstEventTime_RTS, "[s] Observation start");
m_Table->addKey("TSTOP", m_LastEventTime_RTS, "[s] Observation stop"); // "stopsince" is verbatim from Table 6.2d (doc typo)
m_Table->addKey("TSTART", m_FirstEventTime, "[s] Observation start");
m_Table->addKey("TSTOP", m_LastEventTime, "[s] Observation stop"); // "stopsince" is verbatim from Table 6.2d (doc typo)

constexpr time_t MISSION_EPOCH_UNIX = 1735689600; // 2025-01-01T00:00:00 UTC
time_t startUnix = MISSION_EPOCH_UNIX + (time_t)m_FirstEventTime_RTS;
time_t stopUnix = MISSION_EPOCH_UNIX + (time_t)m_LastEventTime_RTS;
time_t startUnix = MISSION_EPOCH_UNIX + static_cast<time_t>(std::floor(m_FirstEventTime));
time_t stopUnix = MISSION_EPOCH_UNIX + static_cast<time_t>(std::floor(m_LastEventTime));
char startBuf[32], stopBuf[32];
strftime(startBuf, sizeof(startBuf), "%Y-%m-%dT%H:%M:%S", gmtime(&startUnix));
strftime(stopBuf, sizeof(stopBuf), "%Y-%m-%dT%H:%M:%S", gmtime(&stopUnix));
Expand Down Expand Up @@ -332,6 +363,22 @@ void MFITSWriterL1a::Close()
delete m_FITSFile; // CCfits flushes + closes on destruction
m_FITSFile = nullptr;
m_Table = nullptr;
m_BatchStartRow = 1;
m_BatchEventCount = 0;
m_HasEvents = false;
m_FirstEventTime = 0;
m_LastEventTime = 0;
m_BatchTIME.clear();
m_BatchEVENTID.clear();
m_BatchEVENTTYPE.clear();
m_BatchNUMSTRIPHIT.clear();
m_BatchHITTYPE.clear();
m_BatchDETID.clear();
m_BatchSTRIPID.clear();
m_BatchSIDEID.clear();
m_BatchFASTTIME.clear();
m_BatchPHA.clear();
m_BatchTAC.clear();
}


Expand Down
54 changes: 50 additions & 4 deletions src/MModuleLoaderMeasurementsFITS.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,33 @@ bool MModuleLoaderMeasurementsFITS::Initialize()
{
// Initialize the module

// Clean:
// Reset file and batch state so a second Initialize() will start fresh
m_FileType = "Unknown";
if (m_FITSFile != nullptr) {
delete m_FITSFile;
m_FITSFile = nullptr;
}
m_PrimaryHDU = nullptr;
m_ComptonTable = nullptr;
m_CurrentRow = 1;
m_TotalRows = 0;
m_TimeReferenceOffsetRTS = 0.0;
m_BatchStartRow = 1;
m_CurrentEventInBatch = 0;
m_BatchEventCount = 0;
m_NEventsInFile = 0;
m_NGoodEventsInFile = 0;
m_BatchTIME.clear();
m_BatchEVENTID.clear();
m_BatchEVENTTYPE.clear();
m_BatchNUMSTRIPHIT.clear();
m_BatchHITTYPE.clear();
m_BatchDETID.clear();
m_BatchSTRIPID.clear();
m_BatchSIDEID.clear();
m_BatchFASTTIME.clear();
m_BatchPHA.clear();
m_BatchTAC.clear();

if (MFile::Exists(m_FileName) == false) {
if (g_Verbosity >= c_Error) cout<<m_XmlTag<<": The file "<<m_FileName<<" does not exist."<<endl;
Expand Down Expand Up @@ -165,6 +190,25 @@ bool MModuleLoaderMeasurementsFITS::OpenFITSFile(MString FileName)
return false;
}

double ReferenceDay = 0.0;
double ReferenceFraction = 0.0;
string TimeSystem;

m_ComptonTable->readKey("MJDREFI", ReferenceDay);
m_ComptonTable->readKey("MJDREFF", ReferenceFraction);
m_ComptonTable->readKey("TIMESYS", TimeSystem);
if (TimeSystem != "TT") {
if (g_Verbosity >= c_Error) cout << m_XmlTag << ": Unsupported FITS time system: " << TimeSystem << endl;
return false;
}

constexpr double MissionReferenceMJD = 60676.0;
constexpr double SecondsPerDay = 86400.0;

// Difference between the FITS and internal reference epochs
const double ReferenceOffsetDays = (ReferenceDay - MissionReferenceMJD) + ReferenceFraction;
m_TimeReferenceOffsetRTS = ReferenceOffsetDays * SecondsPerDay;

// Validate required columns exist
const vector<string> requiredColumns = {
"TIME", "EVENTID", "EVENTTYPE", "NUMSTRIPHIT", "HITTYPE", "DETID",
Expand Down Expand Up @@ -326,14 +370,16 @@ bool MModuleLoaderMeasurementsFITS::AnalyzeEvent(MReadOutAssembly* Event)
// Extract event-level data
double eventTime = m_BatchTIME[idx];
// uint8_t eventType = m_BatchEVENTTYPE[idx]; //unused variable
uint8_t numStripHit = m_BatchNUMSTRIPHIT[idx];
uint16_t numStripHit = m_BatchNUMSTRIPHIT[idx];

// Set event-level properties
Event->SetID(static_cast<unsigned long>(m_BatchEVENTID[idx]));
Event->SetTimeRTS(eventTime); // Mission time in seconds since Jan 1, 2025
MTime TimeRTS = MTime(eventTime) + MTime(m_TimeReferenceOffsetRTS);
Event->SetTimeRTS(TimeRTS);
Event->SetTimeUTC(Event->ComputeUTCfromRTSTime(TimeRTS));

// Loop through strip hits and create MStripHit objects
for (uint8_t hitIdx = 0; hitIdx < numStripHit; ++hitIdx) {
for (uint16_t hitIdx = 0; hitIdx < numStripHit; ++hitIdx) {
// Extract hit data from arrays
uint8_t typeHit = m_BatchHITTYPE[idx][hitIdx];
int detID = m_BatchDETID[idx][hitIdx];
Expand Down
6 changes: 4 additions & 2 deletions src/MModuleLoaderMeasurementsL0.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,13 @@ bool MModuleLoaderMeasurementsL0::ReadNextPacket(MReadOutAssembly* Event)
}
std::vector<uint8_t> hitData(rest.begin() + cursor, rest.begin() + cursor + hlen);

// Build GPS MTime from seconds and subseconds, then convert and set to RTS
// Build GPS MTime from seconds and subseconds, then convert and set to RTS and UTC
// Subseconds are 40 MHz DCB ticks → ns = ticks * 25
long int pktNanoseconds = (long int)pktSubseconds * 25;
MTime gpsTime((long int)pktSeconds, pktNanoseconds);
Event->SetTimeRTS(Event->ComputeRTSfromGPSTime(gpsTime));
MTime TimeRTS = Event->ComputeRTSfromGPSTime(gpsTime);
Event->SetTimeRTS(TimeRTS);
Event->SetTimeUTC(Event->ComputeUTCfromRTSTime(TimeRTS));

// Decode the bit-packed HIT_DATA into individual MStripHit objects on the event
if (DecodeHitData(hitData, hits, Event) == false) {
Expand Down
Loading
Loading