-
Notifications
You must be signed in to change notification settings - Fork 2
/
aartfaacfile.h
144 lines (116 loc) · 3.75 KB
/
aartfaacfile.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ifndef AARTFAAC_FILE_H
#define AARTFAAC_FILE_H
#include "aartfaacheader.h"
#include "aartfaacmode.h"
#include <cstdint>
#include <complex>
#include <fstream>
#include <stdexcept>
#include <iostream>
struct Timestep
{
double startTime, endTime;
};
// An AARTFAAC file has a header followed by the data, which is written as:
// std::complex<float> visibilities[nr_baselines][nr_channels][nr_pols][nr_pols];
class AartfaacFile {
public:
AartfaacFile(const char* filename, AartfaacMode mode) :
_file(filename), _mode(mode), _blockPos(0)
{
_file.seekg(0, std::ios::end);
_filesize = _file.tellg();
// Read first header
_file.seekg(0, std::ios::beg);
_file.read(reinterpret_cast<char*>(&_header), sizeof(AartfaacHeader));
_header.Check();
_blockSize = sizeof(std::complex<float>) * _header.VisPerTimestep();
std::string fn(filename);
size_t sbIndex = fn.rfind("SB");
if(sbIndex == std::string::npos || sbIndex+5 > fn.size())
throw std::runtime_error("Filename should contain subband index preceded by 'SB' in it");
_sbIndex = std::stoi(fn.substr(sbIndex+2, 3));
std::cout << "Calculating frequency for " << fn.substr(sbIndex, 5) << ".\n";
// This is from:
// http://astron.nl/radio-observatory/astronomers/users/
// technical-information/frequency-selection/station-clocks-and-rcu
_bandwidth = mode.Bandwidth();
const double frequencyOffset = mode.FrequencyOffset();
_frequency = _bandwidth * _sbIndex + frequencyOffset;
SeekToTimestep(0);
}
AartfaacFile(const char* filename) :
_file(filename), _mode(AartfaacMode::Unused), _blockPos(0)
{
_file.seekg(0, std::ios::end);
_filesize = _file.tellg();
// Read first header
_file.seekg(0, std::ios::beg);
_file.read(reinterpret_cast<char*>(&_header), sizeof(AartfaacHeader));
_header.Check();
_blockSize = sizeof(std::complex<float>) * _header.VisPerTimestep();
SeekToTimestep(0);
}
void SkipTimesteps(int count)
{
_file.seekg(count * (sizeof(AartfaacHeader) + _blockSize), std::ios::cur);
_blockPos += count;
}
void SeekToTimestep(size_t timestep)
{
_file.seekg(timestep * (sizeof(AartfaacHeader) + _blockSize), std::ios::beg);
_blockPos = timestep;
}
Timestep ReadTimestep(std::complex<float>* buffer)
{
AartfaacHeader h;
_file.read(reinterpret_cast<char*>(&h), sizeof(AartfaacHeader));
_file.read(reinterpret_cast<char*>(buffer), _blockSize);
if(!_file)
throw std::runtime_error("Error reading file");
++_blockPos;
return Timestep{TimeToCasa(h.startTime), TimeToCasa(h.endTime) };
}
Timestep ReadMetadata()
{
AartfaacHeader h;
_file.read(reinterpret_cast<char*>(&h), sizeof(AartfaacHeader));
if(!_file)
throw std::runtime_error("Error reading file");
SeekToTimestep(_blockPos);
return Timestep{TimeToCasa(h.startTime), TimeToCasa(h.endTime) };
}
bool HasMore() const
{
return _blockPos < (_filesize / _blockSize);
}
size_t NTimesteps() const
{
return _filesize / _blockSize;
}
size_t VisPerTimestep() const
{
return _header.VisPerTimestep();
}
size_t NChannels() const { return _header.nrChannels; }
size_t NAntennas() const { return _header.nrReceivers; }
uint8_t CorrelationMode() const { return _header.correlationMode; }
double Bandwidth() const { return _bandwidth; }
double StartTime() const { return TimeToCasa(_header.startTime); }
double Frequency() const { return _frequency; }
double IntegrationTime() const { return _header.endTime-_header.startTime; }
/**
* CASA times are in MJD, but in s.
*/
static double TimeToCasa(double timestamp)
{
return timestamp + ((2440587.5 - 2400000.5) * 86400.0);
}
private:
std::ifstream _file;
AartfaacHeader _header;
AartfaacMode _mode;
size_t _blockSize, _filesize, _blockPos, _sbIndex;
double _frequency, _bandwidth;
};
#endif