我有一个自动气象站(AWS)数据文件。
我想要计算连续时间戳的数据之间的差异,并将其创建为新文件。
原始信息如下所示
我想用这样的值创建一个新文件。只有第一行包含原始值,其余的将是差值。
有人能帮我这个忙吗?谢谢。
附言:我使用的是visual studio代码和c++语言
发布于 2020-05-16 01:07:47
我建议将记录封装在一个结构中:
struct Record
{
std::string date;
std::string time;
double chill;
double dewin;
double dew;
double heatin;
double heat;
double thw;
double humin;
friend std::istream& operator>>(std::istream& input, Record& r);
};
std::istream& operator>>(std::istream& input, Record& r)
{
input >> r.date;
input >> r.time;
input >> r.chill;
input >> r.dewin;
input >> r.dew;
input >> r.heatin;
input >> r.heat;
input >> r.thw;
input >> r.humin;
return input;
}
你也可以添加一个"diff“方法:
struct Record
{
//...
Record diff(const Record& r) const; // Return "r" - "this".
};
Record Record::diff(const Record& r) const
{
Record result;
result.date = r.date;
result.time = r.time;
result.chill = r.chill - chill;
result.dewin = r.dewin - dewin;
//...
return result;
}
您的主代码可能如下所示:
std::vector<Record> database;
Record present;
Record previous;
Record difference;
input_file >> previous;
Output_Record(previous);
database.push_back(previous);
while (input_file >> present)
{
database.push_back(present);
difference = present.diff(previous);
Output_Record(difference);
previous = present;
}
函数Output_Record
将一个Record
实例写入输出文件。
发布于 2020-05-15 21:50:06
这是一些简单的演示代码,它将根据读取数据的文件类型的不同而有所不同。为了简单起见,我使用tsv
文件。
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
int main() {
std::ifstream fin("data.tsv");
std::ofstream fout("compressed.tsv");
double chill1, dewin1, dew1, heatin1, heat1, thw1, humin1, chill2, dewin2, dew2, heatin2, heat2, thw2, humin2;
std::string date, time;
if (fin >> date >> time >> chill1 >> dewin1 >> dew1 >> heatin1 >> heat1 >> thw1 >> humin1)
fout << date << ' ' << time << '\t' << chill1 << '\t' << dewin1 << '\t' <<dew1 << '\t' << heatin1 << '\t' << heat1 << '\t' << thw1 << '\t' << humin1 << '\n';
while (fin >> date >> time >> chill2 >> dewin2 >> dew2 >> heatin2 >> heat2 >> thw2 >> humin2)
fout << date << ' ' << time << '\t' << chill2 - chill1 << '\t' << dewin2 - dewin1 << '\t' << dew2 - dew1 << '\t' << heatin2 - heatin1 << '\t' << heat2 - heat1 << '\t' << thw2 - thw1 << '\t' << humin2 - humin1 << '\n';
fin.close();
fout.close();
// system("cat compressed.tsv");
return 0;
}
data.tsv
文件中的示例内容:
01/01/2018 00:10 21.6 13.7 13.7 11.9 9.5 21.6 13.7
01/01/2018 00:20 21.8 13.8 13.8 12.1 10.2 21.8 13.8
01/01/2018 00:40 22.2 13.7 13.7 12.1 10.1 22.2 13.7
01/01/2018 00:50 22.3 13.7 13.7 12.3 10.3 22.3 13.7
01/01/2018 01:00 22.4 13.7 13.7 12.4 10.3 22.4 13.7
在test run上的结果:
01/01/2018 00:10 21.6 13.7 13.7 11.9 9.5 21.6 13.7
01/01/2018 00:20 0.2 0.1 0.1 0.2 0.7 0.2 0.1
01/01/2018 00:40 0.6 0 0 0.2 0.6 0.6 0
01/01/2018 00:50 0.7 0 0 0.4 0.8 0.7 0
01/01/2018 01:00 0.8 0 0 0.5 0.8 0.8 0
编辑
下面是扩展代码:
#include <fstream>
#include <string>
class Record {
static const char delim = '\t'; // ',' for .csv files and '\t' for .tsv files
std::string date;
std::string time;
double chill;
double dewIn;
double dew;
double heatIn;
double heat;
double thw;
double humIn;
public:
friend std::istream &operator>>(std::istream &, Record &);
friend std::ostream &operator<<(std::ostream &, const Record &);
Record operator-(const Record &);
};
std::istream &operator>>(std::istream &input, Record &r) {
char discard;
input >> r.date >> r.time;
input.get(discard);
input >> r.chill;
input.get(discard);
input >> r.dewIn;
input.get(discard);
input >> r.dew;
input.get(discard);
input >> r.heatIn;
input.get(discard);
input >> r.heat;
input.get(discard);
input >> r.thw;
input.get(discard);
input >> r.humIn;
return input;
}
std::ostream &operator<<(std::ostream &output, const Record &r) {
output << r.date << ' ' << r.time << r.delim
<< r.chill << r.delim
<< r.dewIn << r.delim
<< r.dew << r.delim
<< r.heatIn << r.delim
<< r.heat << r.delim
<< r.thw << r.delim
<< r.humIn << '\n';
return output;
}
Record Record::operator-(const Record &r) {
Record diff;
diff.date = date;
diff.time = time;
diff.chill = chill - r.chill;
diff.dewIn = dewIn - r.dewIn;
diff.dew = dew - r.dew;
diff.heatIn = heatIn - r.heatIn;
diff.heat = heat - r.heat;
diff.thw = thw - r.thw;
diff.humIn = humIn - r.humIn;
return diff;
}
int main() {
std::ifstream input("original.tsv");
std::ofstream output("compressed.tsv");
Record first, current;
if (input >> first) {
output << first;
while (input >> current)
output << current - first;
}
return 0;
}
PS:如果你想要第一个条目和其他条目之间的差异,或者相邻条目之间的差异,有一定的疑问。我的代码给出了你的问题中所示的输出(即在第一个条目和其他条目之间)。如果您希望相邻时间戳之间存在差异,则需要更改main
函数,如下所示:
int main() {
std::ifstream input("original.tsv");
std::ofstream output("compressed.tsv");
Record prev, curr;
if (input >> curr) {
output << curr; // first
prev = curr;
while (input >> curr) {
output << curr - prev;
prev = curr;
}
}
return 0;
}
这会产生如下输出:
01/01/2018 00:10 21.6 13.7 13.7 11.9 9.5 21.6 13.7
01/01/2018 00:20 0.2 0.1 0.1 0.2 0.7 0.2 0.1
01/01/2018 00:40 0.4 -0.1 -0.1 0 -0.1 0.4 -0.1
01/01/2018 00:50 0.1 0 0 0.2 0.2 0.1 0
01/01/2018 01:00 0.1 0 0 0.1 0 0.1 0
https://stackoverflow.com/questions/61819684
复制相似问题