-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathafedit.cpp
210 lines (195 loc) · 6.05 KB
/
afedit.cpp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <iostream>
#include <vector>
#include "aartfaacfile.h"
#include "aartfaacheader.h"
#include "optional.h"
#include "timerange.h"
#include "units/radeccoord.h"
#include <casacore/measures/Measures/MCEpoch.h>
#include <casacore/measures/Measures/MeasConvert.h>
#include <casacore/measures/Measures/MEpoch.h>
#include <casacore/casa/Quanta/MVTime.h>
#include <casacore/measures/Measures/MPosition.h>
// Convert casacore time to ISO 8601 format
std::string TimeToString(const casacore::MVTime& time)
{
std::string out = time.string((casacore::MVTime::formatTypes)(casacore::MVTime::YMD), 8);
out[4] = '-'; out[7] = '-'; out[10] = 'T';
return out;
}
// Convert ISO 8601 time format to casacore time value (in seconds). Example format: "2020-08-13T16:01.001"
double StringToEpoch(const std::string& datestring)
{
casacore::Quantity mytime_q;
bool succes = casacore::MVTime::read(mytime_q, datestring);
if (!succes) {
throw std::runtime_error("Could not interpret as datestring: " + datestring);
}
return casacore::MEpoch(mytime_q, casacore::MEpoch::UTC).getValue().getTime("s").getValue();
}
int main(int argc, char* argv[])
{
int argi = 1;
Optional<size_t> intervalStart, intervalEnd;
Optional<double> lstStart, lstEnd;
Optional<double> utcStart, utcEnd;
bool showLst = false;
while(argi < argc && argv[argi][0] == '-')
{
std::string p(&argv[argi][1]);
if(p == "trim-start")
{
++argi;
intervalStart = atoi(argv[argi]);
}
else if(p == "trim-end")
{
++argi;
intervalEnd = atoi(argv[argi]);
}
else if(p == "lst-start")
{
++argi;
lstStart = atof(argv[argi]);
}
else if(p == "lst-end")
{
++argi;
lstEnd = atof(argv[argi]);
}
else if(p == "utc-start")
{
++argi;
utcStart = StringToEpoch(argv[argi]);
}
else if(p == "utc-end")
{
++argi;
utcEnd = StringToEpoch(argv[argi]);
}
else if(p == "show-lst")
{
showLst = true;
}
else {
std::cerr << "Invalid parameter -" << p << '\n';
return 1;
}
++argi;
}
if((argi+2 > argc && !showLst) || argi+1 > argc )
{
std::cerr <<
"Syntax: afedit [options] <input filename> [<output filename>]\n"
"options:\n"
" -trim-start <start index>\n"
" -trim-end <end index>\n"
" -lst-start <start lst>\n"
" -lst-end <end lst>\n"
" -utc-start <start utc>\n"
" -utc-end <end utc>\n"
" -show-lst <lst>\n";
}
const char *inputFilename(argv[argi]);
const char *outputFilename;
if(showLst)
outputFilename = nullptr;
else
outputFilename = argv[argi+1];
if(lstStart.HasValue() || lstEnd.HasValue() || utcStart.HasValue() || utcEnd.HasValue() || showLst)
{
AartfaacFile file(inputFilename);
casacore::MPosition aartfaacPos(casacore::MVPosition(3826577.022720000, 461022.995082000, 5064892.814), casacore::MPosition::ITRF);
casacore::MeasFrame frame(aartfaacPos);
TimeRange lstRange(lstStart.ValueOr(0.0), lstEnd.ValueOr(24.0));
TimeRange utcRange(utcStart.ValueOr(0.0), utcEnd.ValueOr(1e12));
size_t selectionStartIndex = file.NTimesteps(), selectionEndIndex = 0;
double firstLst = 0.0, lastLst = 0.0;
casacore::MVTime firstUtc, lastUtc;
size_t
tStart = intervalStart.ValueOr(0),
tEnd = intervalEnd.ValueOr(file.NTimesteps());
// TODO this could be done with binary search to make it faster
for(size_t timestep=tStart; timestep!=tEnd; ++timestep)
{
file.SeekToTimestep(timestep);
Timestep t = file.ReadMetadata();
double obsTime = (t.startTime + t.endTime) * 0.5;
casacore::MEpoch timeEpoch = casacore::MEpoch(casacore::MVEpoch(obsTime/86400.0), casacore::MEpoch::UTC);
double timeEpochValue = timeEpoch.getValue().getTime("s").getValue();
casacore::MEpoch lst = casacore::MEpoch::Convert(timeEpoch, casacore::MEpoch::Ref(casacore::MEpoch::LAST, frame))();
double hour = lst.getValue().getDayFraction() * 24.0;
if(lstRange.Contains(hour) && utcRange.Contains(timeEpochValue))
{
if(selectionStartIndex == file.NTimesteps())
selectionStartIndex = timestep;
selectionEndIndex = timestep;
}
if(timestep == tStart)
{
firstLst = hour;
firstUtc = timeEpoch.getValue();
}
if(timestep+1 == tEnd) {
lastLst = hour;
lastUtc = timeEpoch.getValue();
}
}
std::cout << "UTC range of observation: " << TimeToString(firstUtc) << " - " << TimeToString(lastUtc) << ".\n";
std::cout << "LST range of observation: " << RaDecCoord::RAToString(firstLst*(M_PI/12.0)) << " - " << RaDecCoord::RAToString(lastLst*(M_PI/12.0)) << " (in hours: " << firstLst << " - " << lastLst << ").\n";
if(showLst)
return 0;
if(selectionStartIndex == file.NTimesteps())
{
std::cerr << "File has no timesteps in given interval.\n";
return 1;
}
++selectionEndIndex;
std::cout << "Selected timesteps from interval: " << selectionStartIndex << " - " << selectionEndIndex << '\n';
intervalStart = selectionStartIndex;
intervalEnd = selectionEndIndex;
}
std::ifstream inFile(inputFilename);
inFile.seekg(0, std::ios::end);
size_t filesize = inFile.tellg();
if(!inFile || filesize == 0)
{
std::cerr << "Error reading file " << inputFilename << ".\n";
return 1;
}
// Read first header
AartfaacHeader header;
inFile.seekg(0, std::ios::beg);
inFile.read(reinterpret_cast<char*>(&header), sizeof(AartfaacHeader));
header.Check();
size_t blockSize = sizeof(std::complex<float>) * header.VisPerTimestep();
size_t timesteps = filesize / blockSize;
if(!intervalStart.HasValue())
intervalStart = 0;
if(!intervalEnd.HasValue())
intervalEnd = timesteps;
if(*intervalStart >= *intervalEnd || *intervalEnd > timesteps)
{
std::cerr << "Invalid trimming interval.\n";
return 1;
}
// Copy the interval
std::ofstream outFile(outputFilename);
std::vector<char> block(sizeof(AartfaacHeader) + blockSize);
inFile.seekg(block.size() * (*intervalStart), std::ios::beg);
for(size_t timestep = *intervalStart; timestep != *intervalEnd; ++timestep)
{
inFile.read(block.data(), block.size());
if(!inFile)
{
std::cerr << "Error reading input file.\n";
return 1;
}
outFile.write(block.data(), block.size());
if(!outFile)
{
std::cerr << "Error writing output file.\n";
return 1;
}
}
}