get_DSCOVR.cpp
4 KB
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
/////////////////
// CPP HEADERS //
/////////////////
#include <iostream>
#include <fstream>
#include <stdlib.h>
////////////
/// DDLIB //
////////////
#include "DD.h"
#define NAN (0.0/0.0)
////////////////
// NAMESPACES //
///////////////
using namespace std;
int main (int argc, char const *argv[]) throw(std::string)
{
if (argc != 5)
{
cout << "[ERROR] Usage : ./get_DSCOVR (plasma filename) (mag filename) (ddStart) (ddInterval)" << endl;
exit(EXIT_FAILURE);
}
string dataFilename = argv[1];
string dataFilename1 = argv[2];
string ddStart = argv[3];
string ddInterval = argv[4];
string plasmaVi = "dsc_fc_1m";
string timeParameter = "Time";
string DensParameter = "proton_density";
string TempParameter = "proton_temperature";
string VNormParameter = "vp_gse";
string magVi = "dsc_mag_1m";
string magParameter = "b_gse";
ofstream plasmaFile(dataFilename.c_str(), ios::out);
ofstream magFile(dataFilename1.c_str(), ios::out);
int id_sw = DD_SetVariable( const_cast<char*>( plasmaVi.c_str() ) );
int id_mag = DD_SetVariable( const_cast<char*>( magVi.c_str() ) );
char *st = const_cast<char*>( ddStart.c_str() );
int error = DD_SetTime(id_sw, st);
if (error < 0)
{
std::string ddSetTimeErr = "[ERROR] Bad time pointer init in DD_SetTime SW -> err value : " + error;
printf(" bad time pointer plasma %d %d\n", id_sw, error);
throw ddSetTimeErr;
}
error = DD_SetTime(id_mag, st);
if (error < 0)
{
std::string ddSetTimeErr = "[ERROR] Bad time pointer init in DD_SetTime Mag -> err value : " + error;
printf(" bad time pointer mag %d\n", error);
throw ddSetTimeErr;
}
// GET DATA
char *params[4];
params[0] = const_cast<char*>( timeParameter.c_str() );
params[1] = const_cast<char*>( DensParameter.c_str() );
params[2] = const_cast<char*>( TempParameter.c_str() );
params[3] = const_cast<char*>( VNormParameter.c_str() );
char *params_mag[2];
params_mag[0] = const_cast<char*>( timeParameter.c_str() );
params_mag[1] = const_cast<char*>( magParameter.c_str() );
char *timeIntervall = const_cast<char*>( ddInterval.c_str() );
DD_data_t *data;
int status = 0;
plasmaFile << "Time,Density,Temperature,Vx,Vy,Vz" << endl;
do {
status = DD_GetMultiData(id_sw, 4, static_cast<char**>(params), timeIntervall, &data, 1);
if (status < 0)
{
std::string ddGetDataErr = "[ERROR] Failed to get SW data -> status : " + status;
throw std::string(ddGetDataErr);
}
for (int j = 0; j < data->VarNumber; ++j)
{
float density = *(static_cast<float*>(data[1].Variables[j]));
if (density < -1000.) density = NAN;
float temperature = *(static_cast<float*>(data[2].Variables[j]));
if (temperature < -1000.) temperature = NAN;
float vx = static_cast<float*>(data[3].Variables[j])[0];
float vy = static_cast<float*>(data[3].Variables[j])[1];
float vz = static_cast<float*>(data[3].Variables[j])[2];
if (vx < -9000.) {
vx = NAN; vy = NAN; vz = NAN;
}
plasmaFile << static_cast<char*>(data[0].Variables[j]) << ","
<< density << ","
<< temperature << ","
<< vx << ","
<< vy << ","
<< vz << endl;
}
} while (status == MOREDATA);
DD_Close(id_sw);
status = 0;
magFile << "Time,Bx,By,Bz" << endl;
do {
status = DD_GetMultiData(id_mag, 2, static_cast<char**>(params_mag), timeIntervall, &data, 1);
if (status < 0)
{
std::string ddGetDataErr = "[ERROR] Failed to get MAG data -> status : " + status;
throw std::string(ddGetDataErr);
}
for (int j = 0; j < data->VarNumber; ++j)
{
float bx = static_cast<float*>(data[1].Variables[j])[0];
float by = static_cast<float*>(data[1].Variables[j])[1];
float bz = static_cast<float*>(data[1].Variables[j])[2];
if (bx < -9000.) {
bx = NAN; by = NAN; bz = NAN;
}
magFile << static_cast<char*>(data[0].Variables[j]) << ","
<< bx << ","
<< by << ","
<< bz << endl;
}
} while (status == MOREDATA);
DD_Close(id_mag);
plasmaFile.close();
magFile.close();
return 0;
}