Commit 7c02e6a3989a3ece55a1405bb5793c3a53fd3309
1 parent
9f7a8309
Exists in
master
src
Showing
11 changed files
with
2403 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,7 @@ | @@ -0,0 +1,7 @@ | ||
1 | +In order to use the footprints code: | ||
2 | + | ||
3 | +1) At the footprints project root (PATH-TO-PROJECT/FOOTPRINTS), set the environment variables: '. env.sh' | ||
4 | + | ||
5 | +2) Go back to the projects directory (/usr/local/soft) and set the PY3 python virtual environment: '. PY3/bin/activate' | ||
6 | + | ||
7 | +3) Finally, go to the footprints project and to the bin directory (/usr/local/soft/FOOTPRINTS/BIN) and run the code: 'python runFootprints.py (XML mission file)' or 'python runAll.py' (to run all missions described into the missions directory (/usr/local/soft/FOOTPRINTS/MISSIONS)) |
@@ -0,0 +1,21 @@ | @@ -0,0 +1,21 @@ | ||
1 | +#!/bin/bash | ||
2 | + | ||
3 | +# SOFT ROOT | ||
4 | +export FOOTPRINTSROOT=/home/budnik/FOOTPRINTS | ||
5 | +# LIBS | ||
6 | +# TSYGANENKO lib | ||
7 | +export GEOPACKDIR=${FOOTPRINTSROOT}/geopack | ||
8 | +# DDClient to access to DD Server | ||
9 | +export DDDIR=/usr/local/DDClient | ||
10 | +export TINYDIR=/usr/local/tinyxml | ||
11 | +export BOOSTDIR=/usr/local/boost | ||
12 | + | ||
13 | +#------- DO NOT EDIT BELOW -------- | ||
14 | +export FOOTPRINTSMISSIONS=$FOOTPRINTSROOT/MISSIONS | ||
15 | +export FOOTPRINTSCONFIG=$FOOTPRINTSROOT/CONFIG | ||
16 | +export LD_LIBRARY_PATH=$GEOPACKDIR/lib:$BOOSTDIR/stage/lib:$DDDIR/lib:$TINYDIR/lib | ||
17 | +export PATH=/bin:/usr/bin:/usr/local/bin:./:$FOOTPRINTSROOT/BIN:$DDDIR/bin | ||
18 | + | ||
19 | +# DATA ROOT | ||
20 | +export FOOTPRINTSDATA=$FOOTPRINTSROOT/DATA | ||
21 | +export FOOTPRINTSFILES=$FOOTPRINTSDATA/FOOTPRINTS | ||
0 | \ No newline at end of file | 22 | \ No newline at end of file |
@@ -0,0 +1,73 @@ | @@ -0,0 +1,73 @@ | ||
1 | +.PHONY: clean, geopack, tinyxml | ||
2 | + | ||
3 | +include ../env.sh | ||
4 | + | ||
5 | +# *************************** COMPILER SETTINGS ***************************** | ||
6 | +CXX = g++ | ||
7 | +CXXFLAGS = -std=c++11 -pedantic -fPIC -Wall -Wextra | ||
8 | +INCLUDE = -I${BOOSTDIR} -I${DDDIR}/include/DDClientLibC -I${TINYDIR}/include | ||
9 | +LIB = -L${GEOPACKDIR}/lib -L${BOOSTDIR}/stage/lib -L${DDDIR}/lib -L${TINYDIR}/lib | ||
10 | +LKLIBS = -lboost_date_time -lboost_regex -lDDClientLibC -ltinyxml -lnetcdf -lm | ||
11 | +LDFLAGS = -fPIC | ||
12 | +CC = gcc | ||
13 | +EXE = GetData Footprints NC | ||
14 | +# ********************* END OF COMPILER SETTINGS ***************************** | ||
15 | + | ||
16 | +# ******************************** LINKING ********************************* | ||
17 | +all: $(EXE) | ||
18 | + install -d -m a+rx,ug+w ../bin | ||
19 | + # mv Inputs ../bin | ||
20 | + mv Footprints ../bin | ||
21 | + mv NC ../bin | ||
22 | + | ||
23 | +NC: makeNcFile.o ctools.o | ||
24 | + gcc -o $@ $^ -lnetcdf -lm | ||
25 | + | ||
26 | +# Footprints: makeFootprints.o geopack.o T96.o | ||
27 | +# gcc -o $@ $^ -lgfortran -lm | ||
28 | + | ||
29 | +Footprints: makeFootprints.o | ||
30 | + gcc -o $@ $^ ${LIB} -lgeopack -lgfortran -lm | ||
31 | + | ||
32 | +GetData: getInputs.o | ||
33 | + ${CXX} -o $@ $^ ${LDFLAGS} ${LIB} ${LKLIBS} | ||
34 | + | ||
35 | +# Inputs: makeInputs.o tools.o | ||
36 | +# ${CXX} -o $@ $^ ${LDFLAGS} ${LIB} ${LKLIBS} | ||
37 | + | ||
38 | +# ************************** END OF LINKING ********************************* | ||
39 | + | ||
40 | +# ******************************** COMPILING ******************************** | ||
41 | +# %.o: %.cpp | ||
42 | +# ${CXX} -c $< -o $@ ${INCLUDE} ${CXXFLAGS} | ||
43 | + | ||
44 | +getInputs.o: getInputs.cpp | ||
45 | + ${CXX} -c $< -o $@ ${INCLUDE} ${CXXFLAGS} | ||
46 | + | ||
47 | +makeFootprints.o: makeFootprints.c | ||
48 | + gcc -O3 -c makeFootprints.c | ||
49 | + | ||
50 | +makeNcFile.o: makeNcFile.c | ||
51 | + gcc -c makeNcFile.c | ||
52 | + | ||
53 | +ctools.o: ctools.c | ||
54 | + gcc -c ctools.c | ||
55 | + | ||
56 | +# ************************* END OF COMPILING ******************************** | ||
57 | + | ||
58 | + | ||
59 | +# ***************************** BUILDING LIBS ******************************* | ||
60 | +# GEOPACK | ||
61 | +# geopack: | ||
62 | +# cd ../LIB/GEOPACK/ && make T96.o | ||
63 | +# cd ../LIB/GEOPACK/ && make geopack.o | ||
64 | + | ||
65 | +# TINYXML | ||
66 | +# tinyxml: | ||
67 | +# cd ../LIB/TINYXML/ && make | ||
68 | +# ********************** END OF BUILDING LIBS ******************************* | ||
69 | + | ||
70 | +# CLEAN | ||
71 | +clean: | ||
72 | + rm -rf *.o | ||
73 | + rm -rf ${EXE} |
@@ -0,0 +1,196 @@ | @@ -0,0 +1,196 @@ | ||
1 | +#include <stdio.h> | ||
2 | +#include <stdlib.h> | ||
3 | +#include <string.h> | ||
4 | +#include <math.h> | ||
5 | +#include <time.h> | ||
6 | + | ||
7 | +#include "ctools.h" | ||
8 | + | ||
9 | + | ||
10 | +/*************************************************************************** | ||
11 | + getError | ||
12 | + | ||
13 | + Name : getError | ||
14 | + Brief : Writes error into the error file | ||
15 | + Arg : String error message to put into the log file | ||
16 | +*****************************************************************************/ | ||
17 | +int getError(char *errorMsg) { | ||
18 | + | ||
19 | + FILE* error_file = NULL; | ||
20 | + time_t timer; | ||
21 | + char dateBuffer[256]; | ||
22 | + | ||
23 | + time(&timer); | ||
24 | + | ||
25 | + error_file = fopen("LOG/error.txt", "a"); | ||
26 | + | ||
27 | + strftime(dateBuffer, sizeof(dateBuffer), "%a %b %d %X", localtime(&timer)); | ||
28 | + | ||
29 | + fprintf(error_file, "%s %s\n", dateBuffer, errorMsg); | ||
30 | + | ||
31 | + fclose(error_file); | ||
32 | + | ||
33 | + return 0; | ||
34 | +} | ||
35 | + | ||
36 | +/***************************************************************************** | ||
37 | + setDateStr | ||
38 | + | ||
39 | + Name : setDateStr | ||
40 | + Brief : Creates a string date (yyyydddHHMMSS###) from integer values | ||
41 | + Arg1 : Integer year (yyy) | ||
42 | + Arg2 : Integer day of year (ddd) --> 000, 001, ... 364 | ||
43 | + Arg3 : Integer hours (HH) | ||
44 | + Arg4 : Integer minutes (MM) | ||
45 | + Arg5 : Integer seconds (SS) | ||
46 | +******************************************************************************/ | ||
47 | +int setDateStr(int year, int doy, int hour, int minutes, int seconds, int mls, | ||
48 | + char ncYear[4], char ncDay[3], char ncHour[2], char ncMin[2], | ||
49 | + char ncSec[2], char ncMls[2], char ncDate[17]) | ||
50 | + | ||
51 | +{ | ||
52 | + // LOCAL VARIABLE | ||
53 | + char errMsg[256]; | ||
54 | + | ||
55 | + // BUILD DATE | ||
56 | + if ( year >= 1000 && year < 10000 ) | ||
57 | + { | ||
58 | + sprintf(ncYear, "%d", year); | ||
59 | + | ||
60 | + } else { | ||
61 | + | ||
62 | + sprintf(ncYear, "%d", year); | ||
63 | + | ||
64 | + strcpy(errMsg, "[ERROR] : NC file --> Bad year : "); | ||
65 | + | ||
66 | + strcat(errMsg, ncYear); | ||
67 | + | ||
68 | + getError(errMsg); | ||
69 | + | ||
70 | + exit(EXIT_FAILURE); | ||
71 | + } | ||
72 | + | ||
73 | + | ||
74 | + if ( doy >= 0 && doy < 10) | ||
75 | + { | ||
76 | + sprintf(ncDay, "00%d", doy); | ||
77 | + | ||
78 | + } else if ( doy >= 10 && doy < 100 ) { | ||
79 | + | ||
80 | + sprintf(ncDay, "0%d", doy); | ||
81 | + | ||
82 | + } else if ( doy >= 100 && doy <= 366 ) { | ||
83 | + | ||
84 | + sprintf(ncDay, "%d", doy); | ||
85 | + | ||
86 | + } else { | ||
87 | + | ||
88 | + sprintf(ncDay, "%d", ncDay); | ||
89 | + | ||
90 | + strcpy(errMsg, "[ERROR] : NC file --> Bad day : "); | ||
91 | + | ||
92 | + strcat(errMsg, ncDay); | ||
93 | + | ||
94 | + getError(errMsg); | ||
95 | + | ||
96 | + exit(EXIT_FAILURE); | ||
97 | + } | ||
98 | + | ||
99 | + if ( hour >= 0 && hour < 10 ) | ||
100 | + { | ||
101 | + sprintf(ncHour, "0%d", hour); | ||
102 | + | ||
103 | + } else if ( hour >= 10 && hour < 24 ) { | ||
104 | + | ||
105 | + sprintf(ncHour, "%d", hour); | ||
106 | + | ||
107 | + } else { | ||
108 | + | ||
109 | + sprintf(ncHour, "%d", hour); | ||
110 | + | ||
111 | + strcpy(errMsg, "[ERROR] : NC file --> Bad hour : "); | ||
112 | + | ||
113 | + strcat(errMsg, ncHour); | ||
114 | + | ||
115 | + getError(errMsg); | ||
116 | + | ||
117 | + exit(EXIT_FAILURE); | ||
118 | + } | ||
119 | + | ||
120 | + if ( minutes >= 0 && minutes < 10 ) | ||
121 | + { | ||
122 | + sprintf(ncMin, "0%d", minutes); | ||
123 | + | ||
124 | + } else if ( minutes >= 10 && minutes < 60 ) { | ||
125 | + | ||
126 | + sprintf(ncMin, "%d", minutes); | ||
127 | + | ||
128 | + } else { | ||
129 | + | ||
130 | + sprintf(ncMin, "%d", minutes); | ||
131 | + | ||
132 | + strcpy(errMsg, "[ERROR] : NC file --> Bad minute : "); | ||
133 | + | ||
134 | + strcat(errMsg, ncMin); | ||
135 | + | ||
136 | + getError(errMsg); | ||
137 | + | ||
138 | + exit(EXIT_FAILURE); | ||
139 | + } | ||
140 | + | ||
141 | + if ( seconds >= 0 && seconds < 10 ) | ||
142 | + { | ||
143 | + sprintf(ncSec, "0%d", seconds); | ||
144 | + | ||
145 | + } else if ( seconds >= 10 && seconds < 60 ) { | ||
146 | + | ||
147 | + sprintf(ncSec, "%d", seconds); | ||
148 | + | ||
149 | + } else { | ||
150 | + | ||
151 | + sprintf(ncSec, "%d", seconds); | ||
152 | + | ||
153 | + strcpy(errMsg, "[ERROR] : NC file --> Bad minute : "); | ||
154 | + | ||
155 | + strcat(errMsg, ncSec); | ||
156 | + | ||
157 | + getError(errMsg); | ||
158 | + | ||
159 | + exit(EXIT_FAILURE); | ||
160 | + } | ||
161 | + | ||
162 | + if ( mls >= 0 && mls < 10 ) | ||
163 | + { | ||
164 | + sprintf(ncMls, "00%d", mls); | ||
165 | + | ||
166 | + } else if ( mls >= 10 && mls < 100 ) { | ||
167 | + | ||
168 | + sprintf(ncMls, "0%d", mls); | ||
169 | + | ||
170 | + } else if ( mls >= 100 && mls < 1000 ) { | ||
171 | + | ||
172 | + sprintf(ncMls, "%d", mls); | ||
173 | + | ||
174 | + } else { | ||
175 | + | ||
176 | + sprintf(ncMls, "%d", mls); | ||
177 | + | ||
178 | + strcpy(errMsg, "[ERROR] : NC file --> Bad minute : "); | ||
179 | + | ||
180 | + strcat(errMsg, ncMls); | ||
181 | + | ||
182 | + getError(errMsg); | ||
183 | + | ||
184 | + exit(EXIT_FAILURE); | ||
185 | + } | ||
186 | + | ||
187 | + // SET DATE | ||
188 | + strcpy(ncDate, ncYear); | ||
189 | + strcat(ncDate, ncDay); | ||
190 | + strcat(ncDate, ncHour); | ||
191 | + strcat(ncDate, ncMin); | ||
192 | + strcat(ncDate, ncSec); | ||
193 | + strcat(ncDate, ncMls); | ||
194 | + | ||
195 | + return 0; | ||
196 | +} | ||
0 | \ No newline at end of file | 197 | \ No newline at end of file |
@@ -0,0 +1,24 @@ | @@ -0,0 +1,24 @@ | ||
1 | +typedef struct Date Date; | ||
2 | + | ||
3 | +struct Date | ||
4 | +{ | ||
5 | + | ||
6 | + char strDate[17]; | ||
7 | +}; | ||
8 | + | ||
9 | +typedef struct Footprint Footprint; | ||
10 | + | ||
11 | +struct Footprint | ||
12 | +{ | ||
13 | + float geoCoordN[2]; | ||
14 | + float geoCoordS[2]; | ||
15 | + | ||
16 | + float distmgnp; | ||
17 | + | ||
18 | + int SWFLAG; | ||
19 | +}; | ||
20 | + | ||
21 | +int getError(char *errorMsg); | ||
22 | +int setDateStr(int year, int doy, int hour, int minutes, int seconds, int mls, | ||
23 | + char ncYear[4], char ncDay[3], char ncHour[2], char ncMin[2], | ||
24 | + char ncSec[2], char ncMls[2], char ncDate[17]); |
@@ -0,0 +1,366 @@ | @@ -0,0 +1,366 @@ | ||
1 | +//////////////// | ||
2 | +// CPP HEADER // | ||
3 | +//////////////// | ||
4 | +#include <iostream> | ||
5 | +#include <string> | ||
6 | +#include <vector> | ||
7 | +#include <fstream> | ||
8 | + | ||
9 | +//////////// | ||
10 | +/// DDLIB // | ||
11 | +//////////// | ||
12 | +#include "DD.h" | ||
13 | + | ||
14 | +using namespace std; | ||
15 | + | ||
16 | +/////////////////////////// | ||
17 | +/// FUNCTIONS PROTOTYPES // | ||
18 | +////////////////////////// | ||
19 | +void getPlasmaData(string plasmaVi, string ddStart, string ddInterval, | ||
20 | + string timeParameter, string vnormParameter, string rampParameter, | ||
21 | + string velParameter, string magParameter, | ||
22 | + vector<string>& plasmaTimeArray, vector<float>& vnormArray, | ||
23 | + vector<float>& rampArray, | ||
24 | + vector<float>& vxArray, vector<float>& vyArray, vector<float>& vzArray, | ||
25 | + vector<float>& bxArray, vector<float>& byArray, vector<float>& bzArray); | ||
26 | + | ||
27 | + | ||
28 | +void getOrbitData(string satelliteVi, string ddStart, string ddInterval, | ||
29 | + string timeParameter, string orbitParameter, | ||
30 | + vector<string>& satelliteTimeArray, | ||
31 | + vector<float>& x, vector<float>& y, vector<float>& z); | ||
32 | + | ||
33 | + | ||
34 | +void getSymIndexData(string symIndexVi, string ddStart, string ddInterval, | ||
35 | + string timeParameter, string symIndexParameter, | ||
36 | + vector<string>& symIndexTimeArray, vector<float>& symIndexArray); | ||
37 | + | ||
38 | + | ||
39 | +/////////// | ||
40 | +/// MAIN // | ||
41 | +/////////// | ||
42 | +int main(int argc, char const *argv[]) | ||
43 | +{ | ||
44 | + // CHECK ARGS | ||
45 | + if (argc != 11) | ||
46 | + { | ||
47 | + cerr << "[ERROR] Usage : GetData (Plasma VI) (Plasma DD Start) (Plasma DD Interval) (Satellite VI) (Satellite Orbit parameter) (Satellite DD Start) (Satellite DD Interval) (Sym Index VI) (Sym Index DD Start) (Sym Index DD Interval)" << endl; | ||
48 | + exit(EXIT_FAILURE); | ||
49 | + } | ||
50 | + | ||
51 | + // CONSTANTS | ||
52 | + string timeParameter = "Time"; | ||
53 | + string plasmaVNorm = "V"; | ||
54 | + string plasmaRamP = "RamP"; | ||
55 | + string plasmaVel = "Vel"; | ||
56 | + string plasmaMag = "B"; | ||
57 | + string indexSym = "SYM"; | ||
58 | + | ||
59 | + // GET PLASMA DATA | ||
60 | + string plasmaVi = argv[1]; | ||
61 | + string pddStart = argv[2]; | ||
62 | + string pddInterval = argv[3]; | ||
63 | + | ||
64 | + vector<string> plasmaTimeArray; | ||
65 | + vector<float> vnormArray,rampArray, vx, vy, vz, bx, by, bz; | ||
66 | + | ||
67 | + getPlasmaData(plasmaVi, pddStart, pddInterval, | ||
68 | + timeParameter, plasmaVNorm, plasmaRamP, plasmaVel, plasmaMag, | ||
69 | + plasmaTimeArray, vnormArray, rampArray, vx, vy, vz, bx, by, bz); | ||
70 | + | ||
71 | + ofstream plasmaData("plasma.txt", ios::out | ios::trunc); | ||
72 | + | ||
73 | + int plasmaSize = plasmaTimeArray.size(); | ||
74 | + | ||
75 | + plasmaData << "Time,V,ramP,vx,vy,vz,bx,by,bz" << endl; | ||
76 | + | ||
77 | + for (int i = 0; i < plasmaSize; ++i) | ||
78 | + { | ||
79 | + plasmaData << plasmaTimeArray[i] << "," | ||
80 | + << vnormArray[i] << "," | ||
81 | + << rampArray[i] << "," | ||
82 | + << vx[i] << "," << vy[i] << "," << vz[i] << "," | ||
83 | + << bx[i] << "," << by[i] << "," << bz[i] << endl; | ||
84 | + } | ||
85 | + | ||
86 | + plasmaData.close(); | ||
87 | + | ||
88 | + cout << "getPlasmaData ok"<< endl; | ||
89 | + | ||
90 | + // GET SATELLITE DATA | ||
91 | + string satelliteVi = argv[4]; | ||
92 | + string satelliteOrbParam = argv[5]; | ||
93 | + string satddStart = argv[6]; | ||
94 | + string satddInterval = argv[7]; | ||
95 | + | ||
96 | + vector<string> satelliteTimeArray; | ||
97 | + vector<float> x, y, z; | ||
98 | + | ||
99 | + getOrbitData(satelliteVi, satddStart, satddInterval, | ||
100 | + timeParameter, satelliteOrbParam, satelliteTimeArray, | ||
101 | + x, y, z); | ||
102 | + | ||
103 | + ofstream satelliteData("orbit.txt", ios::out | ios::trunc); | ||
104 | + | ||
105 | + int satelliteSize = satelliteTimeArray.size(); | ||
106 | + | ||
107 | + satelliteData << "Time,x,y,z" << endl; | ||
108 | + | ||
109 | + for (int i = 0; i < satelliteSize; ++i) | ||
110 | + { | ||
111 | + satelliteData << satelliteTimeArray[i] << "," | ||
112 | + << x[i] << "," << y[i] << "," << z[i] << endl; | ||
113 | + } | ||
114 | + | ||
115 | + satelliteData.close(); | ||
116 | + | ||
117 | + cout << "getOrbitData ok"<< endl; | ||
118 | + | ||
119 | + // GET SYM H DATA | ||
120 | + string indexVi = argv[8]; | ||
121 | + string symddStart = argv[9]; | ||
122 | + string symddInterval = argv[10]; | ||
123 | + | ||
124 | + vector<string> symIndexTimeArray; | ||
125 | + vector<float> symIndexArray; | ||
126 | + | ||
127 | + getSymIndexData(indexVi, symddStart, symddInterval, | ||
128 | + timeParameter, indexSym, | ||
129 | + symIndexTimeArray, symIndexArray); | ||
130 | + | ||
131 | + cout << "getSymIndexData ok"<< endl; | ||
132 | + | ||
133 | + ofstream symData("sym.txt", ios::out | ios::trunc); | ||
134 | + | ||
135 | + int symSize = symIndexTimeArray.size(); | ||
136 | + | ||
137 | + symData << "Time,symIndex" << endl; | ||
138 | + | ||
139 | + for (int i = 0; i < symSize; ++i) | ||
140 | + { | ||
141 | + symData << symIndexTimeArray[i] << "," << symIndexArray[i] << endl; | ||
142 | + } | ||
143 | + | ||
144 | + symData.close(); | ||
145 | + | ||
146 | + return 0; | ||
147 | +} | ||
148 | + | ||
149 | + | ||
150 | + | ||
151 | +//////////////////////////// | ||
152 | +/// FUNCTIONS DEFINITIONS // | ||
153 | +//////////////////////////// | ||
154 | +void getPlasmaData(string plasmaVi, string ddStart, string ddInterval, | ||
155 | + string timeParameter, string vnormParameter, string rampParameter, | ||
156 | + string velParameter, string magParameter, | ||
157 | + vector<string>& plasmaTimeArray, vector<float>& vnormArray, | ||
158 | + vector<float>& rampArray, | ||
159 | + vector<float>& vxArray, vector<float>& vyArray, vector<float>& vzArray, | ||
160 | + vector<float>& bxArray, vector<float>& byArray, vector<float>& bzArray) | ||
161 | +{ | ||
162 | + int id = DD_SetVariable( const_cast<char*>( plasmaVi.c_str() ) ); | ||
163 | + | ||
164 | + cout << "getPlasmaData id : "<< id << endl; | ||
165 | + | ||
166 | + char *st = const_cast<char*>( ddStart.c_str() ); | ||
167 | + | ||
168 | + cout << "getPlasmaData st : "<< st << endl; | ||
169 | + | ||
170 | + int error = DD_SetTime(id, st); | ||
171 | + | ||
172 | + cout << "getPlasmaData error : "<< error << endl; | ||
173 | + | ||
174 | + if (error < 0) | ||
175 | + { | ||
176 | + std::string ddSetTimeErr = "[ERROR] getPlasmaData : bad time pointer init in DD_SetTime -> err value : " + error; | ||
177 | + throw std::string(ddSetTimeErr); | ||
178 | + } | ||
179 | + | ||
180 | + // GET DATA | ||
181 | + char *params[5]; | ||
182 | + | ||
183 | + params[0] = const_cast<char*>( timeParameter.c_str() ); | ||
184 | + params[1] = const_cast<char*>( vnormParameter.c_str() ); | ||
185 | + params[2] = const_cast<char*>( rampParameter.c_str() ); | ||
186 | + params[3] = const_cast<char*>( velParameter.c_str() ); | ||
187 | + params[4] = const_cast<char*>( magParameter.c_str() ); | ||
188 | + | ||
189 | + char *timeIntervall = const_cast<char*>( ddInterval.c_str() ); | ||
190 | + | ||
191 | + DD_data_t *data; | ||
192 | + | ||
193 | + int status = 0; | ||
194 | + | ||
195 | + do { | ||
196 | + | ||
197 | + status = DD_GetMultiData(id, 5, static_cast<char**>(params), timeIntervall, &data, 1); | ||
198 | + | ||
199 | + if (status < 0) | ||
200 | + { | ||
201 | + std::string ddGetDataErr = "[ERROR] getPlasmaData : failed to get data -> status : " + status; | ||
202 | + throw std::string(ddGetDataErr); | ||
203 | + } | ||
204 | + | ||
205 | + for (int j = 0; j < data->VarNumber; ++j) | ||
206 | + { | ||
207 | + plasmaTimeArray.push_back( static_cast<char*>(data[0].Variables[j]) ); | ||
208 | + vnormArray.push_back( *(static_cast<float*>(data[1].Variables[j])) ); | ||
209 | + rampArray.push_back( *(static_cast<float*>(data[2].Variables[j])) ); | ||
210 | + vxArray.push_back( static_cast<float*>(data[3].Variables[j])[0] ); | ||
211 | + vyArray.push_back( static_cast<float*>(data[3].Variables[j])[1] ); | ||
212 | + vzArray.push_back( static_cast<float*>(data[3].Variables[j])[2] ); | ||
213 | + bxArray.push_back( static_cast<float*>(data[4].Variables[j])[0] ); | ||
214 | + byArray.push_back( static_cast<float*>(data[4].Variables[j])[1] ); | ||
215 | + bzArray.push_back( static_cast<float*>(data[4].Variables[j])[2] ); | ||
216 | + | ||
217 | + } | ||
218 | + | ||
219 | + | ||
220 | + } while (status == MOREDATA); | ||
221 | + | ||
222 | + DD_Close(id); | ||
223 | +} | ||
224 | + | ||
225 | +void getOrbitData(string satelliteVi, string ddStart, string ddInterval, | ||
226 | + string timeParameter, string orbitParameter, | ||
227 | + vector<string>& satelliteTimeArray, | ||
228 | + vector<float>& x, vector<float>& y, vector<float>& z) | ||
229 | +{ | ||
230 | + int id = DD_SetVariable( const_cast<char*>( satelliteVi.c_str() ) ); | ||
231 | + | ||
232 | + cout << "getOrbitData id : "<< id << endl; | ||
233 | + | ||
234 | + char *st = const_cast<char*>( ddStart.c_str() ); | ||
235 | + | ||
236 | + cout << "getOrbitData st : " << st<< endl; | ||
237 | + | ||
238 | + int error = DD_SetTime(id, st); | ||
239 | + | ||
240 | + cout << "getOrbitData error: " << error<< endl; | ||
241 | + | ||
242 | + if (error < 0) | ||
243 | + { | ||
244 | + std::string ddSetTimeErr = "[ERROR] getOrbitData : bad time pointer init in DD_SetTime -> err value : " + error; | ||
245 | + throw std::string(ddSetTimeErr); | ||
246 | + } | ||
247 | + | ||
248 | + | ||
249 | + char *params[2]; | ||
250 | + | ||
251 | + params[0] = const_cast<char*>( timeParameter.c_str() ); | ||
252 | + params[1] = const_cast<char*>( orbitParameter.c_str() ); | ||
253 | + | ||
254 | + char *timeIntervall = const_cast<char*>( ddInterval.c_str() ); | ||
255 | + | ||
256 | + DD_data_t *data; | ||
257 | + | ||
258 | + int status = 0; | ||
259 | + | ||
260 | + do { | ||
261 | + | ||
262 | + cout << "getOrbitData DD_GetMultiData ... " << endl; | ||
263 | + cout << "getOrbitData id : "<< id << endl; | ||
264 | + cout << "getOrbitData param 1 : " << params[0] << endl; | ||
265 | + cout << "getOrbitData param 2 : " << params[1] << endl; | ||
266 | + cout << "getOrbitData timeIntervall : " << timeIntervall << endl; | ||
267 | + | ||
268 | + status = DD_GetMultiData(id, 2, static_cast<char**>(params), timeIntervall, &data, 1); | ||
269 | + | ||
270 | + cout << "getOrbitData DD_GetMultiData OK " << endl; | ||
271 | + | ||
272 | + if (status < 0) | ||
273 | + { | ||
274 | + std::string ddGetDataErr = "[ERROR] getOrbitData : failed to get data -> status : " + status; | ||
275 | + throw std::string(ddGetDataErr); | ||
276 | + } | ||
277 | + | ||
278 | + int dataType = data[1].type; | ||
279 | + | ||
280 | + for (int j = 0; j < data->VarNumber; j++) | ||
281 | + { | ||
282 | + satelliteTimeArray.push_back( static_cast<char*>(data[0].Variables[j]) ); | ||
283 | + | ||
284 | + if ( dataType == 3 ) // DOUBLE | ||
285 | + { | ||
286 | + x.push_back( static_cast<float>(static_cast<double*>(data[1].Variables[j])[0]) ); | ||
287 | + y.push_back( static_cast<float>(static_cast<double*>(data[1].Variables[j])[1]) ); | ||
288 | + z.push_back( static_cast<float>(static_cast<double*>(data[1].Variables[j])[2]) ); | ||
289 | + | ||
290 | + } | ||
291 | + else if ( dataType == 2 ) // FLOAT | ||
292 | + { | ||
293 | + | ||
294 | + x.push_back( static_cast<float*>(data[1].Variables[j])[0] ); | ||
295 | + y.push_back( static_cast<float*>(data[1].Variables[j])[1] ); | ||
296 | + z.push_back( static_cast<float*>(data[1].Variables[j])[2] ); | ||
297 | + | ||
298 | + } | ||
299 | + else | ||
300 | + { | ||
301 | + std::string ddGetDataTypeErr = "[ERROR] getOrbitData : failed to get data -> Unknown data type (not double nor float)"; | ||
302 | + throw std::string(ddGetDataTypeErr); | ||
303 | + } | ||
304 | + } | ||
305 | + | ||
306 | + | ||
307 | + } while (status == MOREDATA); | ||
308 | + | ||
309 | + DD_Close(id); | ||
310 | +} | ||
311 | + | ||
312 | +void getSymIndexData(string symIndexVi, string ddStart, string ddInterval, | ||
313 | + string timeParameter, string symIndexParameter, | ||
314 | + vector<string>& symIndexTimeArray, vector<float>& symIndexArray) | ||
315 | +{ | ||
316 | + int id = DD_SetVariable( const_cast<char*>( symIndexVi.c_str() ) ); | ||
317 | + | ||
318 | + cout << "getSymIndexData id : "<< id << endl; | ||
319 | + | ||
320 | + char *st = const_cast<char*>( ddStart.c_str() ); | ||
321 | + | ||
322 | + cout << "getSymIndexData st : "<< st << endl; | ||
323 | + | ||
324 | + int error = DD_SetTime(id, st); | ||
325 | + | ||
326 | + cout << "getSymIndexData error : "<< error << endl; | ||
327 | + | ||
328 | + if (error < 0) | ||
329 | + { | ||
330 | + std::string ddSetTimeErr = "[ERROR] getSymIndexData : bad time pointer init in DD_SetTime -> err value : " + error; | ||
331 | + throw std::string(ddSetTimeErr); | ||
332 | + } | ||
333 | + | ||
334 | + char *params[2]; | ||
335 | + | ||
336 | + params[0] = const_cast<char*>( timeParameter.c_str() ); | ||
337 | + params[1] = const_cast<char*>( symIndexParameter.c_str() ); | ||
338 | + | ||
339 | + char *timeIntervall = const_cast<char*>( ddInterval.c_str() ); | ||
340 | + | ||
341 | + DD_data_t *data; | ||
342 | + | ||
343 | + int status = 0; | ||
344 | + | ||
345 | + do { | ||
346 | + | ||
347 | + status = DD_GetMultiData(id, 2, static_cast<char**>(params), timeIntervall, &data, 1); | ||
348 | + | ||
349 | + if (status < 0) | ||
350 | + { | ||
351 | + std::string ddGetDataErr = "[ERROR] getSymIndexData : failed to get data -> status : " + status; | ||
352 | + throw std::string(ddGetDataErr); | ||
353 | + } | ||
354 | + | ||
355 | + // 5 min to 1 min resampling | ||
356 | + for (int j = 0; j < data->VarNumber; j+=5) | ||
357 | + { | ||
358 | + symIndexTimeArray.push_back( static_cast<char*>(data[0].Variables[j]) ); | ||
359 | + symIndexArray.push_back( static_cast<float*>(data[1].Variables[j])[0] ); | ||
360 | + } | ||
361 | + | ||
362 | + | ||
363 | + } while (status == MOREDATA); | ||
364 | + | ||
365 | + DD_Close(id); | ||
366 | +} | ||
0 | \ No newline at end of file | 367 | \ No newline at end of file |
@@ -0,0 +1,272 @@ | @@ -0,0 +1,272 @@ | ||
1 | +/************************************************************************************************ | ||
2 | + Name : makeFootprints | ||
3 | + Author : Arnaud BIEGUN | ||
4 | + Version : 2.0 (10/07/2015) | ||
5 | + Brief : Computes geomagnetic field lines feet on a | ||
6 | + specified spherical surface | ||
7 | +*************************************************************************************************/ | ||
8 | + | ||
9 | +/**************** HEADERS ******************/ | ||
10 | +// C LIB | ||
11 | +#include <stdio.h> | ||
12 | +#include <stdlib.h> | ||
13 | +#include <string.h> | ||
14 | +#include <math.h> | ||
15 | + | ||
16 | +// CONSTANTS | ||
17 | +// #define PLASMA_FILENAME "inputs.txt" | ||
18 | + | ||
19 | +// FORTRAN BINDING | ||
20 | +extern void recalc_08_(); | ||
21 | +extern void trace_08_(); | ||
22 | +extern void t96_01_(); | ||
23 | +extern void geogsw_08_(); | ||
24 | +extern void gswgse_08_(); | ||
25 | +/**********END OF HEADERS ******************/ | ||
26 | + | ||
27 | +int main(int argc,char *argv[]) | ||
28 | +{ | ||
29 | + /****************** LOCAL VARIABLES ******************/ | ||
30 | + // TIME | ||
31 | + int iyr, iday, ihour, min__, isec; | ||
32 | + | ||
33 | + // SATELLITE | ||
34 | + float sat_pos_GSE_X_i, sat_pos_GSE_Y_i, sat_pos_GSE_Z_i; | ||
35 | + float sat_pos_GSW_X_i, sat_pos_GSW_Y_i, sat_pos_GSW_Z_i; | ||
36 | + | ||
37 | + // PLASMA | ||
38 | + float Pdyn_i, sym_h_i; | ||
39 | + float V_GSE_X_i, V_GSE_Y_i, V_GSE_Z_i; | ||
40 | + float B_GSE_X_i, B_GSE_Y_i, B_GSE_Z_i; | ||
41 | + float B_GSW_X_i, B_GSW_Y_i, B_GSW_Z_i; | ||
42 | + | ||
43 | + // TSYGANENKO MODEL | ||
44 | + float DIR, R0, RLIM, DSMAX; | ||
45 | + float PSI; | ||
46 | + float BT96_GSW_X, BT96_GSW_Y, BT96_GSW_Z; | ||
47 | + float BT96_GSE_X, BT96_GSE_Y, BT96_GSE_Z; | ||
48 | + float x_mgnp, y_mgnp, z_mgnp, vel_mgnp; | ||
49 | + float DIST_MGNP; | ||
50 | + float XX[2000], YY[2000], ZZ[2000], PARMOD[10]; | ||
51 | + float FP_GSW_X_i, FP_GSW_Y_i, FP_GSW_Z_i; | ||
52 | + float FP_GEO_X_i, FP_GEO_Y_i, FP_GEO_Z_i; | ||
53 | + float ERR; | ||
54 | + int L, LMAX, IOPT, IMAP, ISWq = 0; | ||
55 | + int ID_MGNP; // +1 : inside; -1 : outside | ||
56 | + | ||
57 | + // RADIANS TO DEGREES | ||
58 | + float SQ, R, PHI, THETA; | ||
59 | + | ||
60 | + // OTHERS | ||
61 | + FILE *plasma_file = NULL; | ||
62 | + // FILE *fp_file = NULL; | ||
63 | + char line[1000]; | ||
64 | + char errMsg[256]; | ||
65 | + int SWFLAG_i; | ||
66 | + int transform_flag; | ||
67 | + int i; | ||
68 | + /****************** END OF LOCAL VARIABLES ******************/ | ||
69 | + | ||
70 | + /*********************CHECK I/O FILES **********************/ | ||
71 | + char* plasmaFilename = argv[1]; | ||
72 | + plasma_file = fopen(plasmaFilename, "r"); | ||
73 | + | ||
74 | + if(plasma_file == NULL) | ||
75 | + { | ||
76 | + exit(EXIT_FAILURE); | ||
77 | + } | ||
78 | + /****************** END OF CHECK I/O FILES ********************/ | ||
79 | + | ||
80 | + /****************** FOOTPRINTS COMPUTING ******************/ | ||
81 | + // T96 MODEL CONFIGURATION | ||
82 | + R0 = 1.02; // ~ 120 km // Inner boundary radii | ||
83 | + RLIM = 50.0; // Outter boundary radii | ||
84 | + DSMAX = 0.5; // Upper limit of the stepsize | ||
85 | + LMAX = 2000; // Max number of field line points | ||
86 | + ERR = 0.0001; // Permissible step error | ||
87 | + IOPT = 0; // Model index | ||
88 | + PSI = -0.47; | ||
89 | + | ||
90 | + // MAIN LOOP | ||
91 | + while ( fgets(line, 1000, plasma_file) != NULL ) | ||
92 | + { | ||
93 | + // GET VALUES FROM INPUT FILE | ||
94 | + sscanf(line, | ||
95 | + // "%4d %3d %2d %2d %f %f %f %f %f %f %f %f %f %f %f %d", | ||
96 | + "%d %d %d %d %f %f %f %f %f %f %f %f %f %f %f %d", | ||
97 | + &iyr, &iday, &ihour, &min__, | ||
98 | + &sat_pos_GSE_X_i, &sat_pos_GSE_Y_i, &sat_pos_GSE_Z_i, | ||
99 | + &B_GSE_X_i, &B_GSE_Y_i, &B_GSE_Z_i, | ||
100 | + &V_GSE_X_i, &V_GSE_Y_i, &V_GSE_Z_i, | ||
101 | + &Pdyn_i, &sym_h_i, | ||
102 | + &SWFLAG_i | ||
103 | + ); | ||
104 | + | ||
105 | + | ||
106 | + // CONFIGURATION OF GEOMAGNETIC FIELD MODELS | ||
107 | + recalc_08_(&iyr, &iday, &ihour, &min__, &isec, | ||
108 | + &V_GSE_X_i, &V_GSE_Y_i, &V_GSE_Z_i); | ||
109 | + | ||
110 | + printf("%d %d %d %d ", iyr, iday, ihour, min__); | ||
111 | + // fprintf(fp_file, "%d %d %d %d ", iyr, iday, ihour, min__); | ||
112 | + | ||
113 | + | ||
114 | + // GSE --> GSW ( GSM ) | ||
115 | + transform_flag = -1; | ||
116 | + | ||
117 | + gswgse_08_(&sat_pos_GSW_X_i, | ||
118 | + &sat_pos_GSW_Y_i, | ||
119 | + &sat_pos_GSW_Z_i, | ||
120 | + &sat_pos_GSE_X_i, | ||
121 | + &sat_pos_GSE_Y_i, | ||
122 | + &sat_pos_GSE_Z_i, | ||
123 | + &transform_flag); | ||
124 | + | ||
125 | + gswgse_08_(&B_GSW_X_i, &B_GSW_Y_i, &B_GSW_Z_i, | ||
126 | + &B_GSE_X_i, &B_GSE_Y_i, &B_GSE_Z_i, | ||
127 | + &transform_flag); | ||
128 | + | ||
129 | + | ||
130 | + // COMPUTES T96 GEOMAGNETIC FIELD MODEL | ||
131 | + PARMOD[0] = Pdyn_i; | ||
132 | + PARMOD[1] = sym_h_i; | ||
133 | + PARMOD[2] = B_GSW_Y_i; | ||
134 | + PARMOD[3] = B_GSW_Z_i; | ||
135 | + | ||
136 | + t96_01_(&IOPT, PARMOD, &PSI, | ||
137 | + &sat_pos_GSW_X_i, | ||
138 | + &sat_pos_GSW_Y_i, | ||
139 | + &sat_pos_GSW_Z_i, | ||
140 | + &BT96_GSW_X, &BT96_GSW_Y, &BT96_GSW_Z); | ||
141 | + | ||
142 | + | ||
143 | + transform_flag = 1; | ||
144 | + | ||
145 | + gswgse_08_(&BT96_GSW_X, &BT96_GSW_Y, &BT96_GSW_Z, | ||
146 | + &BT96_GSE_X, &BT96_GSE_Y, &BT96_GSE_Z, | ||
147 | + &transform_flag); | ||
148 | + | ||
149 | + // IS IN MAGNETOPAUSE ? | ||
150 | + vel_mgnp = -1.0; // We use dynamic pressure Pdyn | ||
151 | + | ||
152 | + // shuetal_mgnp_08__(&Pdyn_i, &vel_mgnp, | ||
153 | + // &B_GSW_Z_i, | ||
154 | + // &sat_pos_GSW_X_i, | ||
155 | + // &sat_pos_GSW_Y_i, | ||
156 | + // &sat_pos_GSW_Z_i, | ||
157 | + // &x_mgnp, &y_mgnp, &z_mgnp, | ||
158 | + // &DIST_MGNP, &ID_MGNP); | ||
159 | + | ||
160 | + t96_mgnp_08_(&Pdyn_i, &vel_mgnp, | ||
161 | + &sat_pos_GSW_X_i, | ||
162 | + &sat_pos_GSW_Y_i, | ||
163 | + &sat_pos_GSW_Z_i, | ||
164 | + &x_mgnp, &y_mgnp, &z_mgnp, | ||
165 | + &DIST_MGNP, &ID_MGNP); | ||
166 | + | ||
167 | + | ||
168 | + | ||
169 | + // FOOTPRINTS -> STEP 1 : NORTH | ||
170 | + DIR = -1.0; // Parallel to the total field vector | ||
171 | + | ||
172 | + trace_08_(&sat_pos_GSW_X_i, | ||
173 | + &sat_pos_GSW_Y_i, | ||
174 | + &sat_pos_GSW_Z_i, | ||
175 | + &DIR, &DSMAX, &ERR, | ||
176 | + &RLIM, &R0, &IOPT, PARMOD, | ||
177 | + &FP_GSW_X_i, &FP_GSW_Y_i, &FP_GSW_Z_i, | ||
178 | + XX, YY, ZZ, | ||
179 | + &L, &LMAX); | ||
180 | + | ||
181 | + transform_flag = -1; | ||
182 | + geogsw_08_(&FP_GEO_X_i, &FP_GEO_Y_i, &FP_GEO_Z_i, | ||
183 | + &FP_GSW_X_i, &FP_GSW_Y_i, &FP_GSW_Z_i, | ||
184 | + &transform_flag); | ||
185 | + | ||
186 | + SQ=FP_GEO_X_i*FP_GEO_X_i + FP_GEO_Y_i*FP_GEO_Y_i; | ||
187 | + R=sqrt(SQ+FP_GEO_Z_i*FP_GEO_Z_i); | ||
188 | + SQ=sqrt(SQ); | ||
189 | + PHI=atan2(FP_GEO_Y_i, FP_GEO_X_i); | ||
190 | + THETA=atan2(FP_GEO_Z_i, SQ); | ||
191 | + | ||
192 | + if (PHI < 0.) | ||
193 | + { | ||
194 | + PHI += 2.*M_PI; | ||
195 | + } | ||
196 | + | ||
197 | + if ( (ID_MGNP > 0) && (R <= R0) ) { | ||
198 | + | ||
199 | + printf("%.2f %.2f %.2f ", R, THETA*(180./M_PI), PHI*(180./M_PI)); | ||
200 | + // fprintf(fp_file, "%.2f %.2f %.2f ", R, THETA*(180./M_PI), PHI*(180./M_PI)); | ||
201 | + | ||
202 | + } else { | ||
203 | + | ||
204 | + printf("%.2f %.2f %.2f ", nanf(" "), nanf(" "), nanf(" ")); | ||
205 | + } | ||
206 | + | ||
207 | + | ||
208 | + | ||
209 | + // FOOTPRINTS -> STEP 2 : SOUTH | ||
210 | + DIR = 1.0; // Antiparallel to the total field vector | ||
211 | + | ||
212 | + trace_08_(&sat_pos_GSW_X_i, | ||
213 | + &sat_pos_GSW_Y_i, | ||
214 | + &sat_pos_GSW_Z_i, | ||
215 | + &DIR, &DSMAX, &ERR, | ||
216 | + &RLIM, &R0, &IOPT, PARMOD, | ||
217 | + &FP_GSW_X_i, &FP_GSW_Y_i, &FP_GSW_Z_i, | ||
218 | + XX, YY, ZZ, | ||
219 | + &L, &LMAX); | ||
220 | + | ||
221 | + transform_flag = -1; | ||
222 | + geogsw_08_(&FP_GEO_X_i, &FP_GEO_Y_i, &FP_GEO_Z_i, | ||
223 | + &FP_GSW_X_i, &FP_GSW_Y_i, &FP_GSW_Z_i, | ||
224 | + &transform_flag); | ||
225 | + | ||
226 | + | ||
227 | + | ||
228 | + SQ=FP_GEO_X_i*FP_GEO_X_i + FP_GEO_Y_i*FP_GEO_Y_i; | ||
229 | + R=sqrt(SQ+FP_GEO_Z_i*FP_GEO_Z_i); | ||
230 | + SQ=sqrt(SQ); | ||
231 | + PHI=atan2(FP_GEO_Y_i, FP_GEO_X_i); | ||
232 | + THETA=atan2(FP_GEO_Z_i, SQ); | ||
233 | + | ||
234 | + if (PHI < 0.) | ||
235 | + { | ||
236 | + PHI += 2.*M_PI; | ||
237 | + } | ||
238 | + | ||
239 | + if ( (ID_MGNP > 0) && (R <= R0) ) { | ||
240 | + | ||
241 | + printf("%.2f %.2f %.2f ", R, THETA*(180./M_PI), PHI*(180./M_PI)); | ||
242 | + // fprintf(fp_file, "%.2f %.2f %.2f ", R, THETA*(180./M_PI), PHI*(180./M_PI)); | ||
243 | + | ||
244 | + } else { | ||
245 | + | ||
246 | + printf("%.2f %.2f %.2f ", nanf(" "), nanf(" "), nanf(" ")); | ||
247 | + } | ||
248 | + | ||
249 | + // Print SW flag index | ||
250 | + printf("%d ", SWFLAG_i); | ||
251 | + // fprintf(fp_file, "%d ", SWFLAG_i); | ||
252 | + | ||
253 | + // Print distance to magnetopause | ||
254 | + printf("%.2f ", DIST_MGNP*ID_MGNP); | ||
255 | + // fprintf(fp_file, "%f ", DIST_MGNP*ID_MGNP); | ||
256 | + | ||
257 | + // Print positions (GSE) and T96 output (GSE) | ||
258 | + printf("%.2f %.2f %.2f ", sat_pos_GSE_X_i, sat_pos_GSE_Y_i, sat_pos_GSE_Z_i); | ||
259 | + printf("%.2f %.2f %.2f\n", BT96_GSE_X, BT96_GSE_Y, BT96_GSE_Z); | ||
260 | + // fprintf(fp_file, "%.2f %.2f %.2f ", sat_pos_GSE_X_i, sat_pos_GSE_Y_i, sat_pos_GSE_Z_i); | ||
261 | + // fprintf(fp_file, "%.2f %.2f %.2f\n", BT96_GSE_X, BT96_GSE_Y, BT96_GSE_Z); | ||
262 | + } | ||
263 | + /****************** END OF FOOTPRINTS COMPUTING ******************/ | ||
264 | + | ||
265 | + | ||
266 | + /************************** FREE I/O FILES ***********************/ | ||
267 | + // fclose(fp_file); | ||
268 | + fclose(plasma_file); | ||
269 | + /********************* END OF FREE I/O FILES *********************/ | ||
270 | + | ||
271 | + return 0; | ||
272 | +} |
@@ -0,0 +1,116 @@ | @@ -0,0 +1,116 @@ | ||
1 | +///////////////// | ||
2 | +// CPP HEADERS // | ||
3 | +///////////////// | ||
4 | +#include <iostream> | ||
5 | +#include <math.h> // isnan | ||
6 | + | ||
7 | +//////////// | ||
8 | +/// BOOST // | ||
9 | +//////////// | ||
10 | +#include <boost/lexical_cast.hpp> | ||
11 | + | ||
12 | +/////////// | ||
13 | +// DDLIB // | ||
14 | +/////////// | ||
15 | +#include "DD.h" | ||
16 | + | ||
17 | +////////////////////////// | ||
18 | +// FUNCTIONS PROTOTYPES // | ||
19 | +////////////////////////// | ||
20 | +#include "tools.h" | ||
21 | + | ||
22 | + | ||
23 | + | ||
24 | +/////////////// | ||
25 | +// NAMESPACE // | ||
26 | +/////////////// | ||
27 | +using namespace std; | ||
28 | + | ||
29 | + | ||
30 | + | ||
31 | +////////// | ||
32 | +// MAIN // | ||
33 | +////////// | ||
34 | +int main(int argc, char* argv[]) | ||
35 | +{ | ||
36 | + // LOAD MISSION | ||
37 | + if ( argc != 2 ) | ||
38 | + { | ||
39 | + cout << "[ERROR] Usage : ./Inputs (XML_mission_file)" << endl; | ||
40 | + exit(EXIT_FAILURE); | ||
41 | + } | ||
42 | + | ||
43 | + string missionXMLName = argv[1]; | ||
44 | + string missionXML = "../MISSIONS/" + missionXMLName; | ||
45 | + string satelliteName, satelliteVi, satelliteOrbit, satelliteRes; | ||
46 | + string plasmaName, plasmaVi, plasmaVNorm, plasmaRamP, plasmaVel, plasmaMag; | ||
47 | + string indexName, indexVi, indexSym; | ||
48 | + string start, stop; | ||
49 | + | ||
50 | + loadMission(missionXML, satelliteName, satelliteVi, | ||
51 | + satelliteOrbit, satelliteRes, | ||
52 | + plasmaName, plasmaVi, plasmaVNorm, | ||
53 | + plasmaRamP, plasmaVel, plasmaMag, | ||
54 | + indexName, indexVi, indexSym, | ||
55 | + start, stop); | ||
56 | + | ||
57 | + string ddStart = toDDTime(start); | ||
58 | + string ddStop = toDDTime(stop); | ||
59 | + string ddInterval = makeDDIntervall(start, stop); | ||
60 | + | ||
61 | + printMission(satelliteName, satelliteVi, | ||
62 | + satelliteOrbit, satelliteRes, | ||
63 | + plasmaName, plasmaVi, plasmaVNorm, | ||
64 | + plasmaRamP, plasmaVel, plasmaMag, | ||
65 | + indexName, indexVi, indexSym, | ||
66 | + start, stop, ddStart, ddStop, ddInterval); | ||
67 | + | ||
68 | + | ||
69 | + // GET DATA | ||
70 | + string timeParameter = "Time"; | ||
71 | + | ||
72 | + vector<string> plasmaTimeArray; | ||
73 | + vector<float> vnormArray,rampArray, vx, vy, vz, bx, by, bz; | ||
74 | + | ||
75 | + getPlasmaData(plasmaVi, ddStart, ddInterval, | ||
76 | + timeParameter, plasmaVNorm, plasmaRamP, plasmaVel, plasmaMag, | ||
77 | + plasmaTimeArray, vnormArray, rampArray, vx, vy, vz, bx, by, bz); | ||
78 | + | ||
79 | + | ||
80 | + vector<string> satelliteTimeArray; | ||
81 | + vector<float> x, y, z; | ||
82 | + | ||
83 | + getOrbitData(satelliteVi, satelliteRes, | ||
84 | + ddStart, ddInterval, timeParameter, satelliteOrbit, satelliteTimeArray, | ||
85 | + x, y, z); | ||
86 | + | ||
87 | + vector<string> symIndexTimeArray; | ||
88 | + vector<float> symIndexArray; | ||
89 | + | ||
90 | + getSymIndexData(indexVi, ddStart, ddInterval, timeParameter, indexSym, | ||
91 | + symIndexTimeArray, symIndexArray); | ||
92 | + | ||
93 | + int res = boost::lexical_cast<int>(satelliteRes); | ||
94 | + | ||
95 | + if ( res > 300 ) | ||
96 | + { | ||
97 | + // linearInterpolation(satelliteTimeArray, x, y, z); | ||
98 | + | ||
99 | + GTL_interp(x, y, z); | ||
100 | + } | ||
101 | + | ||
102 | + int n = plasmaTimeArray.size(); | ||
103 | + | ||
104 | + vector<int> flag(n, 1); | ||
105 | + | ||
106 | + cleanData(n, rampArray, vnormArray, vx, vy, vz, bx, by, bz, | ||
107 | + symIndexArray, flag); | ||
108 | + | ||
109 | + string filename = missionXMLName.erase( missionXMLName.rfind('.') ); | ||
110 | + filename = filename + ".txt"; | ||
111 | + | ||
112 | + writeInputsFile(filename, n, plasmaTimeArray, rampArray, vnormArray, | ||
113 | + vx, vy, vz, bx, by, bz, x, y, z, symIndexArray, flag); | ||
114 | + | ||
115 | + return 0; | ||
116 | +} |
@@ -0,0 +1,443 @@ | @@ -0,0 +1,443 @@ | ||
1 | +/********************************************************************************************** | ||
2 | + Name : makeNcFile.c | ||
3 | + Author : Arnaud BIEGUN | ||
4 | + Version : 1.0 (30/06/2015) | ||
5 | + Brief : Creates the NetCDF file from ASCII footprints file | ||
6 | +*************************************************************************************************/ | ||
7 | +/**************** HEADERS ******************/ | ||
8 | +// C lib | ||
9 | +#include <stdio.h> | ||
10 | +#include <stdlib.h> | ||
11 | +#include <string.h> | ||
12 | +#include <math.h> | ||
13 | +#include <time.h> | ||
14 | + | ||
15 | +// NETCDF lib | ||
16 | +#include <netcdf.h> | ||
17 | + | ||
18 | +// Other functions | ||
19 | +#include "ctools.h" | ||
20 | + | ||
21 | +// CONSTANTS | ||
22 | +#define SAT_TIME_RES 60 | ||
23 | +#define PLASMA_TIME_RES 300 | ||
24 | +#define INDEX_TIME_RES 60 | ||
25 | +#define RE 6371.0 | ||
26 | +#define TIME_DIM "Time" | ||
27 | +#define TIMELEN_DIM "TimeLength" | ||
28 | +#define VECTOR_DIM "Vector" | ||
29 | +#define TIME_VAR "Time" | ||
30 | +#define FPN_VAR "North" | ||
31 | +#define FPS_VAR "South" | ||
32 | +#define DISTMGNP_VAR "Dist_mgnp" | ||
33 | +#define SWFLAG_VAR "SW_flag" | ||
34 | +#define START_VAR "StartTime" | ||
35 | +#define STOP_VAR "StopTime" | ||
36 | +#define SOURCE "T96 geomagnetic field model" | ||
37 | +#define FOOTPRINT_UNIT "degrees, degrees" | ||
38 | +#define DISTMGNP_UNIT "Re" | ||
39 | +#define TIME_STR_LEN 17 | ||
40 | +/**********END OF HEADERS ******************/ | ||
41 | + | ||
42 | +int main(int argc, char const *argv[]) | ||
43 | +{ | ||
44 | + /**************** LOCAL VARIABLES ***********************/ | ||
45 | + // DATA | ||
46 | + int iyr, iday, ihour, min__, isec; | ||
47 | + float fpn_r_i, fpn_lat_i, fpn_lon_i; | ||
48 | + float fps_r_i, fps_lat_i, fps_lon_i; | ||
49 | + float satX_i, satY_i, satZ_i; | ||
50 | + float BT96X_i, BT96Y_i, BT96Z_i; | ||
51 | + float dist_mgnp_i; | ||
52 | + int SWFLAG_i; | ||
53 | + Footprint *fpArray = NULL; | ||
54 | + | ||
55 | + // ID | ||
56 | + int ncid, time_dimid, timelength_dimid, dataId; | ||
57 | + int time_varid, fpn_varid, fps_varid, SWFLAG_varid, startTime_varid, stopTime_varid; | ||
58 | + // int distmgnp_varid; | ||
59 | + int time_tab_dimid[2], data_tab_dimid[2]; | ||
60 | + | ||
61 | + // BUFFERS | ||
62 | + size_t start[2], timeCount[2], dataCount[2]; | ||
63 | + | ||
64 | + // TIME | ||
65 | + char ncYear[4], ncDay[3], ncHour[2], ncMin[2], ncSec[2], ncMls[2]; | ||
66 | + char ncDate[17]; | ||
67 | + Date *dateArray = NULL; | ||
68 | + time_t p; | ||
69 | + char *s; | ||
70 | + | ||
71 | + // OTHERS | ||
72 | + FILE *dataFile = NULL; | ||
73 | + char line[1000], errMsg[256]; | ||
74 | + int retval, off, count, i; | ||
75 | + /*************** END OF LOCAL VARIABLES ********************/ | ||
76 | + | ||
77 | + | ||
78 | + /*************** GET DATA FROM OUTPUT FILE *****************/ | ||
79 | + // Date dateArray[500]; | ||
80 | + count = 0; | ||
81 | + | ||
82 | + const char* footprintsFilename = argv[1]; | ||
83 | + | ||
84 | + dataFile = fopen(footprintsFilename, "r"); | ||
85 | + | ||
86 | + if ( dataFile != NULL ) | ||
87 | + { | ||
88 | + | ||
89 | + while ( fgets(line, 1000, dataFile) != NULL ) | ||
90 | + { | ||
91 | + // GET VALUES FROM INPUT FILE | ||
92 | + sscanf(line, | ||
93 | + "%4d %3d %2d %2d %f %f %f %f %f %f %d %f %f %f %f %f %f %f", | ||
94 | + &iyr, &iday, &ihour, &min__, | ||
95 | + &fpn_r_i, &fpn_lat_i, &fpn_lon_i, | ||
96 | + &fps_r_i, &fps_lat_i, &fps_lon_i, | ||
97 | + &SWFLAG_i, | ||
98 | + &dist_mgnp_i, | ||
99 | + &satX_i, &satY_i, &satZ_i, | ||
100 | + &BT96X_i, &BT96Y_i, &BT96Z_i | ||
101 | + ); | ||
102 | + | ||
103 | + fpArray = (Footprint*) realloc( fpArray, (count+1)*sizeof(Footprint) ); | ||
104 | + | ||
105 | + fpArray[count].geoCoordN[0] = fpn_lat_i; | ||
106 | + fpArray[count].geoCoordN[1] = fpn_lon_i; | ||
107 | + fpArray[count].geoCoordS[0] = fps_lat_i; | ||
108 | + fpArray[count].geoCoordS[1] = fps_lon_i; | ||
109 | + | ||
110 | + fpArray[count].distmgnp = dist_mgnp_i; | ||
111 | + | ||
112 | + fpArray[count].SWFLAG = SWFLAG_i; | ||
113 | + | ||
114 | + // DATE : INT --> STR | ||
115 | + setDateStr(iyr, iday-1, ihour, min__, 0, 0, | ||
116 | + ncYear, ncDay, ncHour, ncMin, ncSec, ncMls, ncDate); | ||
117 | + | ||
118 | + dateArray = (Date*) realloc( dateArray, (count+1)*sizeof(Date) ); | ||
119 | + | ||
120 | + strcpy(dateArray[count].strDate, ncDate); | ||
121 | + | ||
122 | + | ||
123 | + // printf("%s %f %f %f %f %f %f %f %f %f %f %f %f %f %d\n", | ||
124 | + // dateArray[count].strDate, | ||
125 | + // fpn_r_i, fpArray[count].geoCoordN[0], fpArray[count].geoCoordN[1], | ||
126 | + // fps_r_i, fpArray[count].geoCoordS[0], fpArray[count].geoCoordS[1], | ||
127 | + // fpArray[count].distmgnp, | ||
128 | + // satX_i, satY_i, satZ_i, | ||
129 | + // BT96X_i, BT96Y_i, BT96Z_i, | ||
130 | + // SWFLAG_i); | ||
131 | + | ||
132 | + count++; | ||
133 | + | ||
134 | + } | ||
135 | + | ||
136 | + } else { | ||
137 | + | ||
138 | + strcpy(errMsg, "[ERROR] : NC file --> Unable to read "); | ||
139 | + | ||
140 | + strcat(errMsg, footprintsFilename); | ||
141 | + | ||
142 | + getError(errMsg); | ||
143 | + | ||
144 | + exit(EXIT_FAILURE); | ||
145 | + } | ||
146 | + /*************** END OF GET DATA FROM OUTPUT FILE *****************/ | ||
147 | + | ||
148 | + | ||
149 | + /************************ OPEN NC FILE *****************************/ | ||
150 | + const char* ncFilename = argv[2]; | ||
151 | + | ||
152 | + retval = nc_create(ncFilename, NC_CLOBBER, &ncid); | ||
153 | + | ||
154 | + if (retval != NC_NOERR) | ||
155 | + { | ||
156 | + strcpy(errMsg, "[ERROR] : NC file --> Failed to create NC file "); | ||
157 | + | ||
158 | + strcat(errMsg, ncFilename); | ||
159 | + | ||
160 | + getError(errMsg); | ||
161 | + | ||
162 | + exit(EXIT_FAILURE); | ||
163 | + } | ||
164 | + /************************** END OF OPEN NC FILE *********************/ | ||
165 | + | ||
166 | + | ||
167 | + | ||
168 | + /*************************** DEFINE MODE ****************************/ | ||
169 | + // DIMENSIONS | ||
170 | + retval = nc_def_dim(ncid, TIME_DIM, NC_UNLIMITED, &time_dimid); | ||
171 | + | ||
172 | + if (retval != NC_NOERR) | ||
173 | + { | ||
174 | + getError("[ERROR] : NC file --> Failed to define time dimension"); | ||
175 | + | ||
176 | + exit(EXIT_FAILURE); | ||
177 | + } | ||
178 | + | ||
179 | + retval = nc_def_dim(ncid, TIMELEN_DIM, TIME_STR_LEN, &timelength_dimid); | ||
180 | + | ||
181 | + if (retval != NC_NOERR) | ||
182 | + { | ||
183 | + getError("[ERROR] : NC file --> Failed to define time length dimension"); | ||
184 | + | ||
185 | + exit(EXIT_FAILURE); | ||
186 | + } | ||
187 | + | ||
188 | + retval = nc_def_dim(ncid, VECTOR_DIM, 2, &dataId); | ||
189 | + | ||
190 | + if (retval != NC_NOERR) | ||
191 | + { | ||
192 | + getError("[ERROR] : NC file --> Failed to define vector dimension"); | ||
193 | + | ||
194 | + exit(EXIT_FAILURE); | ||
195 | + } | ||
196 | + | ||
197 | + | ||
198 | + // NETCDF VARIABLES | ||
199 | + time_tab_dimid[0] = time_dimid; | ||
200 | + time_tab_dimid[1] = timelength_dimid; | ||
201 | + data_tab_dimid[0] = time_dimid; | ||
202 | + data_tab_dimid[1] = dataId; | ||
203 | + | ||
204 | + retval = nc_def_var(ncid, TIME_VAR, NC_CHAR, 2, time_tab_dimid, &time_varid); | ||
205 | + | ||
206 | + if (retval != NC_NOERR) | ||
207 | + { | ||
208 | + getError("[ERROR] : NC file --> Failed to define time variable"); | ||
209 | + | ||
210 | + exit(EXIT_FAILURE); | ||
211 | + } | ||
212 | + | ||
213 | + retval = nc_def_var(ncid, FPN_VAR, NC_FLOAT, 2, data_tab_dimid, &fpn_varid); | ||
214 | + | ||
215 | + if (retval != NC_NOERR) | ||
216 | + { | ||
217 | + getError("[ERROR] : NC file --> Failed to define north footprints variable"); | ||
218 | + | ||
219 | + exit(EXIT_FAILURE); | ||
220 | + } | ||
221 | + | ||
222 | + retval = nc_def_var(ncid, FPS_VAR, NC_FLOAT, 2, data_tab_dimid, &fps_varid); | ||
223 | + | ||
224 | + if (retval != NC_NOERR) | ||
225 | + { | ||
226 | + getError("[ERROR] : NC file --> Failed to define south footprints variable"); | ||
227 | + | ||
228 | + exit(EXIT_FAILURE); | ||
229 | + } | ||
230 | + | ||
231 | + // retval = nc_def_var(ncid, DISTMGNP_VAR, NC_FLOAT, 1, &time_dimid, &distmgnp_varid); | ||
232 | + | ||
233 | + // if (retval != NC_NOERR) | ||
234 | + // { | ||
235 | + // getError("[ERROR] : NC file --> Failed to define dist mgnp variable"); | ||
236 | + | ||
237 | + // exit(EXIT_FAILURE); | ||
238 | + // } | ||
239 | + | ||
240 | + retval = nc_def_var(ncid, SWFLAG_VAR, NC_INT, 1, &time_dimid, &SWFLAG_varid); | ||
241 | + | ||
242 | + if (retval != NC_NOERR) | ||
243 | + { | ||
244 | + getError("[ERROR] : NC file --> Failed to define solar wind quality flag variable"); | ||
245 | + | ||
246 | + exit(EXIT_FAILURE); | ||
247 | + } | ||
248 | + | ||
249 | + retval = nc_def_var(ncid, START_VAR, NC_CHAR, 1, &timelength_dimid, &startTime_varid); | ||
250 | + | ||
251 | + if (retval != NC_NOERR) | ||
252 | + { | ||
253 | + getError("[ERROR] : NC file --> Failed to define start time variable"); | ||
254 | + | ||
255 | + exit(EXIT_FAILURE); | ||
256 | + } | ||
257 | + | ||
258 | + retval = nc_def_var(ncid, STOP_VAR, NC_CHAR, 1, &timelength_dimid, &stopTime_varid); | ||
259 | + | ||
260 | + if (retval != NC_NOERR) | ||
261 | + { | ||
262 | + getError("[ERROR] : NC file --> Failed to define stop time variable"); | ||
263 | + | ||
264 | + exit(EXIT_FAILURE); | ||
265 | + } | ||
266 | + | ||
267 | + // UNITS | ||
268 | + retval = nc_put_att_text(ncid, fpn_varid, "units", strlen(FOOTPRINT_UNIT), FOOTPRINT_UNIT); | ||
269 | + | ||
270 | + if (retval != NC_NOERR) | ||
271 | + { | ||
272 | + getError("[ERROR] : NC file --> Failed to define footprints unit attribute"); | ||
273 | + | ||
274 | + exit(EXIT_FAILURE); | ||
275 | + } | ||
276 | + | ||
277 | + retval = nc_put_att_text(ncid, fps_varid, "units", strlen(FOOTPRINT_UNIT), FOOTPRINT_UNIT); | ||
278 | + | ||
279 | + if (retval != NC_NOERR) | ||
280 | + { | ||
281 | + getError("[ERROR] : NC file --> Failed to define footprints unit attribute"); | ||
282 | + | ||
283 | + exit(EXIT_FAILURE); | ||
284 | + } | ||
285 | + | ||
286 | + // retval = nc_put_att_text(ncid, distmgnp_varid, "units", strlen(DISTMGNP_UNIT), DISTMGNP_UNIT); | ||
287 | + | ||
288 | + // if (retval != NC_NOERR) | ||
289 | + // { | ||
290 | + // getError("[ERROR] : NC file --> Failed to define dist mgnp unit attribute"); | ||
291 | + | ||
292 | + // exit(EXIT_FAILURE); | ||
293 | + // } | ||
294 | + | ||
295 | + // DATA SOURCE | ||
296 | + retval = nc_put_att_text(ncid, NC_GLOBAL, "Source", strlen(SOURCE), SOURCE); | ||
297 | + | ||
298 | + if (retval != NC_NOERR) | ||
299 | + { | ||
300 | + getError("[ERROR] : NC file --> Failed to define data source attribute"); | ||
301 | + | ||
302 | + exit(EXIT_FAILURE); | ||
303 | + } | ||
304 | + | ||
305 | + // NC FILE CREATION DATE | ||
306 | + time(&p); | ||
307 | + s = ctime(&p); | ||
308 | + | ||
309 | + retval = nc_put_att_text(ncid, NC_GLOBAL, "Created", 24, s); | ||
310 | + | ||
311 | + if (retval != NC_NOERR) | ||
312 | + { | ||
313 | + getError("[ERROR] : NC file --> Failed to define NC file creation date attribute"); | ||
314 | + | ||
315 | + exit(EXIT_FAILURE); | ||
316 | + } | ||
317 | + | ||
318 | + | ||
319 | + // CLOSE DEFINE MODE | ||
320 | + retval = nc_enddef(ncid); | ||
321 | + | ||
322 | + if (retval != NC_NOERR) | ||
323 | + { | ||
324 | + getError("[ERROR] : NC file --> Failed to close define mode"); | ||
325 | + | ||
326 | + exit(EXIT_FAILURE); | ||
327 | + } | ||
328 | + /*********************** END OF DEFINE MODE ****************************/ | ||
329 | + | ||
330 | + | ||
331 | + /************************ WRITING MODE ********************************/ | ||
332 | + // BUFFERS CONFIGURATION | ||
333 | + timeCount[0] = 1; | ||
334 | + timeCount[1] = TIME_STR_LEN; | ||
335 | + dataCount[0] = 1; | ||
336 | + dataCount[1] = 2; | ||
337 | + start[1] = 0; | ||
338 | + | ||
339 | + | ||
340 | + // WRITE DATA | ||
341 | + off = 0; | ||
342 | + | ||
343 | + for (i = 0; i < count; i++) | ||
344 | + { | ||
345 | + | ||
346 | + start[0] = off; | ||
347 | + | ||
348 | + retval = nc_put_vara_text(ncid, time_varid, | ||
349 | + start, timeCount, dateArray[i].strDate); | ||
350 | + | ||
351 | + if (retval != NC_NOERR) | ||
352 | + { | ||
353 | + getError("[ERROR] : NC file --> Failed to write time variable"); | ||
354 | + | ||
355 | + exit(EXIT_FAILURE); | ||
356 | + } | ||
357 | + | ||
358 | + retval = nc_put_vara_float(ncid, fpn_varid, | ||
359 | + start, dataCount, fpArray[i].geoCoordN); | ||
360 | + | ||
361 | + if (retval != NC_NOERR) | ||
362 | + { | ||
363 | + getError("[ERROR] : NC file --> Failed to write north footprints variable"); | ||
364 | + | ||
365 | + exit(EXIT_FAILURE); | ||
366 | + } | ||
367 | + | ||
368 | + retval = nc_put_vara_float(ncid, fps_varid, | ||
369 | + start, dataCount, fpArray[i].geoCoordS); | ||
370 | + | ||
371 | + if (retval != NC_NOERR) | ||
372 | + { | ||
373 | + getError("[ERROR] : NC file --> Failed to write south footprints variable"); | ||
374 | + | ||
375 | + exit(EXIT_FAILURE); | ||
376 | + } | ||
377 | + | ||
378 | + // retval = nc_put_vara_float(ncid, distmgnp_varid, &start[0], | ||
379 | + // &dataCount[0], &fpArray[i].distmgnp); | ||
380 | + | ||
381 | + // if (retval != NC_NOERR) | ||
382 | + // { | ||
383 | + // getError("[ERROR] : NC file --> Failed to write dist mgnp variable"); | ||
384 | + | ||
385 | + // exit(EXIT_FAILURE); | ||
386 | + // } | ||
387 | + | ||
388 | + retval = nc_put_vara_int(ncid, SWFLAG_varid, &start[0], | ||
389 | + &dataCount[0], &fpArray[i].SWFLAG); | ||
390 | + | ||
391 | + if (retval != NC_NOERR) | ||
392 | + { | ||
393 | + getError("[ERROR] : NC file --> Failed to write solar wind variable"); | ||
394 | + | ||
395 | + exit(EXIT_FAILURE); | ||
396 | + } | ||
397 | + | ||
398 | + off++; | ||
399 | + } | ||
400 | + | ||
401 | + | ||
402 | + retval = nc_put_var_text(ncid, startTime_varid, dateArray[0].strDate); | ||
403 | + | ||
404 | + if (retval != NC_NOERR) | ||
405 | + { | ||
406 | + getError("[ERROR] : NC file --> Failed to write start time variable"); | ||
407 | + | ||
408 | + exit(EXIT_FAILURE); | ||
409 | + } | ||
410 | + | ||
411 | + retval = nc_put_var_text(ncid, stopTime_varid, dateArray[count-1].strDate); | ||
412 | + | ||
413 | + if (retval != NC_NOERR) | ||
414 | + { | ||
415 | + getError("[ERROR] : NC file --> Failed to write stop time variable"); | ||
416 | + | ||
417 | + exit(EXIT_FAILURE); | ||
418 | + } | ||
419 | + /************************ END OF WRITING MODE ********************************/ | ||
420 | + | ||
421 | + /**************** CLOSE NC FILE ***********************/ | ||
422 | + //Close the file | ||
423 | + retval = nc_close(ncid); | ||
424 | + | ||
425 | + if (retval != NC_NOERR) | ||
426 | + { | ||
427 | + getError("[ERROR] : NC file --> Failed to close NC file"); | ||
428 | + | ||
429 | + exit(EXIT_FAILURE); | ||
430 | + } | ||
431 | + /**************** END OF CLOSE NC FILE ***********************/ | ||
432 | + | ||
433 | + /*********************** FREE MEMORY ********************************/ | ||
434 | + free(dateArray); | ||
435 | + free(fpArray); | ||
436 | + /******************** END OF FREE MEMORY ****************************/ | ||
437 | + | ||
438 | + return 0; | ||
439 | +} | ||
440 | + | ||
441 | + | ||
442 | + | ||
443 | + |
@@ -0,0 +1,819 @@ | @@ -0,0 +1,819 @@ | ||
1 | +///////////////// | ||
2 | +// CPP HEADERS // | ||
3 | +///////////////// | ||
4 | +#include <iostream> | ||
5 | +#include <fstream> | ||
6 | + | ||
7 | +#include "math.h" | ||
8 | + | ||
9 | +//////////// | ||
10 | +/// BOOST // | ||
11 | +//////////// | ||
12 | +#include <boost/tokenizer.hpp> | ||
13 | +#include <boost/lexical_cast.hpp> | ||
14 | +#include <boost/regex.hpp> | ||
15 | +#include "boost/date_time/gregorian/gregorian.hpp" | ||
16 | +#include "boost/date_time/posix_time/posix_time.hpp" | ||
17 | + | ||
18 | +/////////////// | ||
19 | +// TINYXML-2 // | ||
20 | +/////////////// | ||
21 | +#include "tinyxml2.h" | ||
22 | + | ||
23 | +//////////// | ||
24 | +/// DDLIB // | ||
25 | +//////////// | ||
26 | +#include "DD.h" | ||
27 | + | ||
28 | +////////// | ||
29 | +/// GSL // | ||
30 | +////////// | ||
31 | +#include "gsl_spline.h" | ||
32 | + | ||
33 | +////////////////////////// | ||
34 | +// FUNCTIONS PROTOTYPES // | ||
35 | +////////////////////////// | ||
36 | +#include "tools.h" | ||
37 | + | ||
38 | +//////////////// | ||
39 | +// NAMESPACES // | ||
40 | +/////////////// | ||
41 | +using namespace std; | ||
42 | +using namespace tinyxml2; | ||
43 | + | ||
44 | + | ||
45 | + | ||
46 | +//////////////////////////// | ||
47 | +/// FUNCTIONS DEFINITIONS // | ||
48 | +//////////////////////////// | ||
49 | + | ||
50 | +/** | ||
51 | + * @brief Print mission features | ||
52 | + * | ||
53 | + * @param satelliteName Name of the satellite | ||
54 | + * @param satelliteVi Virtual instrument's name of the satellite | ||
55 | + * @param satelliteOrbit Name of the netCDF variable for the satellite | ||
56 | + * @param satelliteRes Satellite orbit resolution | ||
57 | + * @param plasmaName Name of the plasma dataset | ||
58 | + * @param plasmaVi Virtual instrument's name of the plasma dataset | ||
59 | + * @param plasmaVNorm Name of the netCDF variable for plasma dataset | ||
60 | + * @param plasmaRamP Ram pressure parameter into the netCDF file | ||
61 | + * @param plasmaVel Velocity parameter into the netCDF file | ||
62 | + * @param plasmaMag Magnetic field parameter into the netCDF file | ||
63 | + * @param indexName Name of the Sym H index | ||
64 | + * @param indexVi Virtual instrument's name of the Sym H index | ||
65 | + * @param indexSym Sym H index parameter into the netCDF file | ||
66 | + * @param start Start time | ||
67 | + * @param stop Stop time | ||
68 | + * @param ddStart DD style start time | ||
69 | + * @param ddStop DD style stop time | ||
70 | + * @param ddInterval DD style time interval | ||
71 | + */ | ||
72 | +void printMission(string& satelliteName, string& satelliteVi, | ||
73 | + string& satelliteOrbit, string& satelliteRes, | ||
74 | + string& plasmaName, string& plasmaVi, string& plasmaVNorm, | ||
75 | + string& plasmaRamP, string& plasmaVel, string& plasmaMag, | ||
76 | + string& indexName, string& indexVi, string& indexSym, | ||
77 | + string& start, string& stop, string& ddStart, string& ddStop, string& ddInterval) | ||
78 | +{ | ||
79 | + cout << "********** MISSION ************" << endl; | ||
80 | + cout << "Satellite name : " << satelliteName << endl; | ||
81 | + cout << "Satellite VI : " << satelliteVi << endl; | ||
82 | + cout << "Satellite orbit param : " << satelliteOrbit << endl; | ||
83 | + cout << "Satellite orbit resolution : " << satelliteRes << endl; | ||
84 | + cout << "Plasma name : " << plasmaName << endl; | ||
85 | + cout << "Plasma VI : " << plasmaVi << endl; | ||
86 | + cout << "Plasma V norm param : " << plasmaVNorm << endl; | ||
87 | + cout << "Plasma ram pressure : " << plasmaRamP << endl; | ||
88 | + cout << "Plasma velocity : " << plasmaVel << endl; | ||
89 | + cout << "Plasma B mag : " << plasmaMag << endl; | ||
90 | + cout << "Sym index name : " << indexName << endl; | ||
91 | + cout << "Sym index VI : " << indexVi << endl; | ||
92 | + cout << "Sym index param : " << indexSym << endl; | ||
93 | + cout << "Start time : " << start << endl; | ||
94 | + cout << "Stop time : " << stop << endl; | ||
95 | + cout << "DD Start time : " << ddStart << endl; | ||
96 | + cout << "DD Stop time : " << ddStop << endl; | ||
97 | + cout << "DD Interval : " << ddInterval << endl; | ||
98 | + cout << "********** END OF MISSION ************" << endl << endl; | ||
99 | +} | ||
100 | + | ||
101 | + | ||
102 | + | ||
103 | +/** | ||
104 | + * @brief Get mission parameters | ||
105 | + * | ||
106 | + * @param filename Name of the XML config file | ||
107 | + * @param satelliteName Name of the satellite | ||
108 | + * @param satelliteVi Virtual instrument's name for the satellite | ||
109 | + * @param satelliteOrbit Name of the parameter into the netCDF file for the satellite's orbit | ||
110 | + * @param satelliteRes Time resolution for satellite orbit | ||
111 | + * @param plasmaName Name of plasma dataset | ||
112 | + * @param plasmaVi Virtual instrument's name for the plasma dataset | ||
113 | + * @param plasmaVNorm Name of the parameter into the netCDF file for the plasma's velocity norm | ||
114 | + * @param plasmaRamP Name of the parameter into the netCDF file for the plasma's ram pressure | ||
115 | + * @param plasmaVel Name of the parameter into the netCDF file for the plasma's velocity vector | ||
116 | + * @param plasmaMag Name of the parameter into the netCDF file for the plasma's magnetic field vector | ||
117 | + * @param indexName Name of the Sym index | ||
118 | + * @param indexVi Virtual instrument's name for the Sym index | ||
119 | + * @param indexSym Name of the parameter into the netCDF file for the Sym index | ||
120 | + * @param start Start time | ||
121 | + * @param stop Stop time | ||
122 | + */ | ||
123 | +void loadMission(string& filename, | ||
124 | + string& satelliteName, string& satelliteVi, | ||
125 | + string& satelliteOrbit, string& satelliteRes, | ||
126 | + string& plasmaName, string& plasmaVi, string& plasmaVNorm, | ||
127 | + string& plasmaRamP, string& plasmaVel, string& plasmaMag, | ||
128 | + string& indexName, string& indexVi, string& indexSym, | ||
129 | + string& start, string& stop) | ||
130 | +{ | ||
131 | + | ||
132 | + // LOADING THE DOM | ||
133 | + XMLDocument doc; | ||
134 | + | ||
135 | + if ( doc.LoadFile( filename.c_str() ) != 0 ) | ||
136 | + { | ||
137 | + cout << "[ERROR] loadMission : Failed to load " << filename << endl; | ||
138 | + | ||
139 | + exit(EXIT_FAILURE); | ||
140 | + } | ||
141 | + | ||
142 | + | ||
143 | + // GET PARAMETERS VALUES | ||
144 | + XMLElement* pRoot = doc.FirstChildElement( "MISSION" ); | ||
145 | + | ||
146 | + satelliteName = pRoot->FirstChildElement( "SATELLITE" )->FirstChildElement( "NAME" )->GetText(); | ||
147 | + satelliteVi = pRoot->FirstChildElement( "SATELLITE" )->FirstChildElement( "VI" )->GetText(); | ||
148 | + satelliteOrbit = pRoot->FirstChildElement( "SATELLITE" )->FirstChildElement( "ORBIT" )->GetText(); | ||
149 | + satelliteRes = pRoot->FirstChildElement( "SATELLITE" )->FirstChildElement( "RES" )->GetText(); | ||
150 | + | ||
151 | + plasmaName = pRoot->FirstChildElement( "PLASMA" )->FirstChildElement( "NAME" )->GetText(); | ||
152 | + plasmaVi = pRoot->FirstChildElement( "PLASMA" )->FirstChildElement( "VI" )->GetText(); | ||
153 | + plasmaVNorm = pRoot->FirstChildElement( "PLASMA" )->FirstChildElement( "VNORM" )->GetText(); | ||
154 | + plasmaRamP = pRoot->FirstChildElement( "PLASMA" )->FirstChildElement( "RAMP" )->GetText(); | ||
155 | + plasmaVel = pRoot->FirstChildElement( "PLASMA" )->FirstChildElement( "VEL" )->GetText(); | ||
156 | + plasmaMag = pRoot->FirstChildElement( "PLASMA" )->FirstChildElement( "MAG" )->GetText(); | ||
157 | + | ||
158 | + indexName = pRoot->FirstChildElement( "INDEX" )->FirstChildElement( "NAME" )->GetText(); | ||
159 | + indexVi = pRoot->FirstChildElement( "INDEX" )->FirstChildElement( "VI" )->GetText(); | ||
160 | + indexSym = pRoot->FirstChildElement( "INDEX" )->FirstChildElement( "SYM" )->GetText(); | ||
161 | + | ||
162 | + start = pRoot->FirstChildElement( "START" )->GetText(); | ||
163 | + stop = pRoot->FirstChildElement( "STOP" )->GetText(); | ||
164 | + | ||
165 | +} | ||
166 | + | ||
167 | + | ||
168 | +/** | ||
169 | + * @brief Split date time into 2 parts : date and clock time | ||
170 | + * | ||
171 | + * @param[in] strDate Date to split | ||
172 | + * | ||
173 | + * @return Array containing the splitted date : [0] -> Date / [1] -> Clock time | ||
174 | + */ | ||
175 | +vector<string> tokenizedDate(string strDate) | ||
176 | +{ | ||
177 | + vector<string> tDate; | ||
178 | + | ||
179 | + boost::char_separator<char> sep("T"); | ||
180 | + boost::tokenizer< boost::char_separator<char> > tok(strDate, sep); | ||
181 | + boost::tokenizer< boost::char_separator<char> >::iterator tok_iter; | ||
182 | + | ||
183 | + for(tok_iter = tok.begin(); tok_iter != tok.end(); ++tok_iter) | ||
184 | + { | ||
185 | + tDate.push_back(*tok_iter); | ||
186 | + } | ||
187 | + | ||
188 | + // tDate[1] = tDate[1].substr(0, tDate[1].size()-1); | ||
189 | + | ||
190 | + return tDate; | ||
191 | +} | ||
192 | + | ||
193 | + | ||
194 | +/** | ||
195 | + * @brief Check if date is in good format : yyyy-mm-ddThh:mm:ss.mls (modify if a pattern is matched) | ||
196 | + * | ||
197 | + * @param date Date to check for | ||
198 | + * | ||
199 | + * @return True : good format / False : bad format | ||
200 | + */ | ||
201 | +bool checkDateFormat(string& date) | ||
202 | +{ | ||
203 | + boost::regex regex0("^[0-9]{4}[-]{1}[0-9]{2}[-]{1}[0-9]{2}[T]{1}[0-9]{2}[:]{1}[0-9]{2}[:]{1}[0-9]{2}$"); | ||
204 | + boost::regex regex1("^[0-9]{4}[-]{1}[0-9]{2}[-]{1}[0-9]{2}[T]{1}[0-9]{2}[:]{1}[0-9]{2}[:]{1}[0-9]{2}[.]{1}[0-9]{3}$"); | ||
205 | + boost::regex regex2("^[0-9]{4}[-]{1}[0-9]{2}[-]{1}[0-9]{2}[T]{1}[0-9]{2}[:]{1}[0-9]{2}[:]{1}[0-9]{2}[.]{1}[0-9]{6}$"); | ||
206 | + boost::regex regex3("^[0-9]{4}[-]{1}[0-9]{2}[-]{1}[0-9]{2}[T]{1}[0-9]{2}[:]{1}[0-9]{2}[:]{1}[0-9]{2}[.]{1}[0-9]{9}$"); | ||
207 | + boost::regex regex4("^[0-9]{4}[-]{1}[0-9]{2}[-]{1}[0-9]{2}[T]{1}[0-9]{2}[:]{1}[0-9]{2}[:]{1}[0-9]{2}[.]{1}[0-9]{3}[Z]{1}$"); | ||
208 | + | ||
209 | + bool match0 = boost::regex_match(date, regex0); | ||
210 | + bool match1 = boost::regex_match(date, regex1); | ||
211 | + bool match2 = boost::regex_match(date, regex2); | ||
212 | + bool match3 = boost::regex_match(date, regex3); | ||
213 | + bool match4 = boost::regex_match(date, regex4); | ||
214 | + | ||
215 | + if (match0) | ||
216 | + { | ||
217 | + date += ".000"; | ||
218 | + return true; | ||
219 | + } | ||
220 | + else if (match1) | ||
221 | + { | ||
222 | + return true; | ||
223 | + } | ||
224 | + else if (match2 || match3) | ||
225 | + { | ||
226 | + date = date.substr(0, 23); | ||
227 | + return true; | ||
228 | + } | ||
229 | + else if (match4) | ||
230 | + { | ||
231 | + date = date.substr(0, date.size()-1); | ||
232 | + return true; | ||
233 | + } | ||
234 | + else | ||
235 | + { | ||
236 | + return false; | ||
237 | + } | ||
238 | + | ||
239 | +} | ||
240 | + | ||
241 | + | ||
242 | +/** | ||
243 | + * @brief Date to DD date : yyyydoyhhmmssmls | ||
244 | + * | ||
245 | + * @param[in] date Date to transform | ||
246 | + * | ||
247 | + * @return DD date | ||
248 | + */ | ||
249 | +string toDDTime(string date) | ||
250 | +{ | ||
251 | + if ( !checkDateFormat(date) ) | ||
252 | + { | ||
253 | + string badDateFormatErr = "[ERROR] Tools::toDDTime -> Start time is bad formatted " + date + " : cannot build DD start time"; | ||
254 | + throw std::string(badDateFormatErr); | ||
255 | + } | ||
256 | + | ||
257 | + string dateTime = (tokenizedDate(date))[0]; | ||
258 | + string clockTime = (tokenizedDate(date))[1]; | ||
259 | + string gregoDate = dateTime.substr(0, 4)+dateTime.substr(5, 2)+dateTime.substr(8, 2); | ||
260 | + | ||
261 | + boost::gregorian::date d( boost::gregorian::from_undelimited_string(gregoDate) ); | ||
262 | + string doy = boost::lexical_cast<std::string>( d.day_of_year()-1 ); | ||
263 | + | ||
264 | + if ( doy.size() < 1 ) | ||
265 | + { | ||
266 | + throw std::string("[ERROR] Tools::toDDTime -> Failed to build day of year"); | ||
267 | + } | ||
268 | + else if ( doy.size() < 2 ) | ||
269 | + { | ||
270 | + doy = "00" + doy; | ||
271 | + } | ||
272 | + else if( doy.size() < 3 ) | ||
273 | + { | ||
274 | + doy = "0" + doy; | ||
275 | + } | ||
276 | + else if ( doy.size() > 3 ) | ||
277 | + { | ||
278 | + throw std::string("[ERROR] Tools::toDDTime -> Failed to build day of year"); | ||
279 | + } | ||
280 | + | ||
281 | + string ddTime = dateTime.substr(0, 4)+doy+clockTime.substr(0, 2)+clockTime.substr(3, 2)+clockTime.substr(6, 2)+clockTime.substr(9); | ||
282 | + | ||
283 | + return ddTime; | ||
284 | +} | ||
285 | + | ||
286 | + | ||
287 | +/** | ||
288 | + * @brief Make DD style time intervall : yyyydoyhhmmssmls | ||
289 | + * | ||
290 | + * @param[in] startTime Start time | ||
291 | + * @param[in] stopTime Stop time | ||
292 | + * | ||
293 | + * @return DD style time intervall | ||
294 | + */ | ||
295 | +string makeDDIntervall(string startTime, string stopTime) | ||
296 | +{ | ||
297 | + if ( !(checkDateFormat(startTime) && checkDateFormat(stopTime)) ) | ||
298 | + { | ||
299 | + string badBoundsErr = "[ERROR] makeDDIntervall -> Failed to build DD intervall because of bad time boundaries : " + startTime + " / " + stopTime; | ||
300 | + throw std::string(badBoundsErr); | ||
301 | + } | ||
302 | + | ||
303 | + vector<string> startVec = tokenizedDate(startTime); | ||
304 | + vector<string> stopVec = tokenizedDate(stopTime); | ||
305 | + | ||
306 | + boost::posix_time::ptime ptStart(boost::posix_time::time_from_string(startVec[0]+ " " + startVec[1])); | ||
307 | + boost::posix_time::ptime ptStop(boost::posix_time::time_from_string(stopVec[0]+ " " + stopVec[1])); | ||
308 | + boost::posix_time::time_period tp(ptStart, ptStop); | ||
309 | + | ||
310 | + if ( tp.is_null() ) | ||
311 | + { | ||
312 | + string badBoundsErr = "[ERROR] makeDDIntervall -> Failed to build DD intervall because of bad time boundaries : " + startTime + " / " + stopTime; | ||
313 | + throw std::string(badBoundsErr); | ||
314 | + } | ||
315 | + | ||
316 | + string days = boost::lexical_cast<string>( tp.length().hours()/24 ); | ||
317 | + string hours = boost::lexical_cast<string>( tp.length().hours()%24 ); | ||
318 | + string minutes = boost::lexical_cast<string>( tp.length().minutes() ); | ||
319 | + string seconds = boost::lexical_cast<string>( tp.length().seconds() ); | ||
320 | + string mls = boost::lexical_cast<string>( tp.length().fractional_seconds() ); | ||
321 | + | ||
322 | + if ( days.size() < 2 ) | ||
323 | + { | ||
324 | + days = "00" + days; | ||
325 | + } | ||
326 | + else if ( days.size() < 3 ) | ||
327 | + { | ||
328 | + days = "0" + days; | ||
329 | + } | ||
330 | + | ||
331 | + if ( hours.size() < 2 ) | ||
332 | + { | ||
333 | + hours = "0" + hours; | ||
334 | + } | ||
335 | + | ||
336 | + if ( minutes.size() < 2 ) | ||
337 | + { | ||
338 | + minutes = "0" + minutes; | ||
339 | + } | ||
340 | + | ||
341 | + if ( seconds.size() < 2 ) | ||
342 | + { | ||
343 | + seconds = "0" + seconds; | ||
344 | + } | ||
345 | + | ||
346 | + if ( mls.size() < 2 ) | ||
347 | + { | ||
348 | + mls = "00" + mls; | ||
349 | + } | ||
350 | + else if ( mls.size() == 6 ) | ||
351 | + { | ||
352 | + mls = mls.substr(0, 3); | ||
353 | + } | ||
354 | + else if ( mls.size() == 9 ) | ||
355 | + { | ||
356 | + mls = mls.substr(0, 3); | ||
357 | + } | ||
358 | + | ||
359 | + string ddIntervall = "0000" + days + hours + minutes + seconds + mls; | ||
360 | + | ||
361 | + return ddIntervall; | ||
362 | +} | ||
363 | + | ||
364 | + | ||
365 | + | ||
366 | +/** | ||
367 | + * @brief Get plasma data from DD base | ||
368 | + * | ||
369 | + * @param[in] plasmaVi Name of the VI into DD base | ||
370 | + * @param[in] ddStart Start time DD time format | ||
371 | + * @param[in] ddInterval Time interval DD time format | ||
372 | + * @param[in] timeParameter Name of time parameter into NC file | ||
373 | + * @param[in] vnormParameter Name of velocity norm parameter into NC file | ||
374 | + * @param[in] rampParameter Name of ram pressure parameter into NC file | ||
375 | + * @param[in] velParameter Name of velocity parameter into NC file | ||
376 | + * @param[in] magParameter Name of magnetic field parameter into NC file | ||
377 | + * @param plasmaTimeArray Time array | ||
378 | + * @param vnormArray Velocity norm array | ||
379 | + * @param rampArray Ram pressure array | ||
380 | + * @param vxArray Vx component of the velocity array | ||
381 | + * @param vyArray Vy component of the velocity array | ||
382 | + * @param vzArray Vz component of the velocity array | ||
383 | + * @param bxArray Bx component of the velocity array | ||
384 | + * @param byArray By component of the velocity array | ||
385 | + * @param bzArray Bz component of the velocity array | ||
386 | + */ | ||
387 | +void getPlasmaData(string plasmaVi, string ddStart, string ddInterval, | ||
388 | + string timeParameter, string vnormParameter, string rampParameter, | ||
389 | + string velParameter, string magParameter, | ||
390 | + vector<string>& plasmaTimeArray, vector<float>& vnormArray, | ||
391 | + vector<float>& rampArray, | ||
392 | + vector<float>& vxArray, vector<float>& vyArray, vector<float>& vzArray, | ||
393 | + vector<float>& bxArray, vector<float>& byArray, vector<float>& bzArray) | ||
394 | +{ | ||
395 | + int id = DD_SetVariable( const_cast<char*>( plasmaVi.c_str() ) ); | ||
396 | + | ||
397 | + char *st = const_cast<char*>( ddStart.c_str() ); | ||
398 | + | ||
399 | + int error = DD_SetTime(id, st); | ||
400 | + | ||
401 | + if (error < 0) | ||
402 | + { | ||
403 | + std::string ddSetTimeErr = "[ERROR] getPlasmaData : bad time pointer init in DD_SetTime -> err value : " + error; | ||
404 | + throw std::string(ddSetTimeErr); | ||
405 | + } | ||
406 | + | ||
407 | + // GET DATA | ||
408 | + char *params[5]; | ||
409 | + | ||
410 | + params[0] = const_cast<char*>( timeParameter.c_str() ); | ||
411 | + params[1] = const_cast<char*>( vnormParameter.c_str() ); | ||
412 | + params[2] = const_cast<char*>( rampParameter.c_str() ); | ||
413 | + params[3] = const_cast<char*>( velParameter.c_str() ); | ||
414 | + params[4] = const_cast<char*>( magParameter.c_str() ); | ||
415 | + | ||
416 | + char *timeIntervall = const_cast<char*>( ddInterval.c_str() ); | ||
417 | + | ||
418 | + DD_data_t *data; | ||
419 | + | ||
420 | + int status = 0; | ||
421 | + | ||
422 | + do { | ||
423 | + | ||
424 | + status = DD_GetMultiData(id, 5, static_cast<char**>(params), timeIntervall, &data, 1); | ||
425 | + | ||
426 | + if (status < 0) | ||
427 | + { | ||
428 | + std::string ddGetDataErr = "[ERROR] getPlasmaData : failed to get data -> status : " + status; | ||
429 | + throw std::string(ddGetDataErr); | ||
430 | + } | ||
431 | + | ||
432 | + for (int j = 0; j < data->VarNumber; ++j) | ||
433 | + { | ||
434 | + plasmaTimeArray.push_back( static_cast<char*>(data[0].Variables[j]) ); | ||
435 | + vnormArray.push_back( *(static_cast<float*>(data[1].Variables[j])) ); | ||
436 | + rampArray.push_back( *(static_cast<float*>(data[2].Variables[j])) ); | ||
437 | + vxArray.push_back( static_cast<float*>(data[3].Variables[j])[0] ); | ||
438 | + vyArray.push_back( static_cast<float*>(data[3].Variables[j])[1] ); | ||
439 | + vzArray.push_back( static_cast<float*>(data[3].Variables[j])[2] ); | ||
440 | + bxArray.push_back( static_cast<float*>(data[4].Variables[j])[0] ); | ||
441 | + byArray.push_back( static_cast<float*>(data[4].Variables[j])[1] ); | ||
442 | + bzArray.push_back( static_cast<float*>(data[4].Variables[j])[2] ); | ||
443 | + | ||
444 | + } | ||
445 | + | ||
446 | + | ||
447 | + } while (status == MOREDATA); | ||
448 | + | ||
449 | + DD_Close(id); | ||
450 | +} | ||
451 | + | ||
452 | + | ||
453 | +/** | ||
454 | + * @brief Get orbit data from DD base | ||
455 | + * | ||
456 | + * @param[in] orbitVi Name of the VI into DD base | ||
457 | + * @param[in] orbitRes Resolution of the satellite orbit | ||
458 | + * @param[in] ddStart Start time DD time format | ||
459 | + * @param[in] ddInterval Time interval DD time format | ||
460 | + * @param[in] timeParameter Name of time parameter into NC file | ||
461 | + * @param[in] orbitParameter Name of orbit parameter into NC file | ||
462 | + * @param orbitArray Time array | ||
463 | + * @param x X component array | ||
464 | + * @param y Y component array | ||
465 | + * @param z Z component array | ||
466 | + */ | ||
467 | +void getOrbitData(string satelliteVi, string orbitRes, | ||
468 | + string ddStart, string ddInterval, | ||
469 | + string timeParameter, string orbitParameter, | ||
470 | + vector<string>& satelliteTimeArray, | ||
471 | + vector<float>& x, vector<float>& y, vector<float>& z) | ||
472 | +{ | ||
473 | + int id = DD_SetVariable( const_cast<char*>( satelliteVi.c_str() ) ); | ||
474 | + | ||
475 | + char *st = const_cast<char*>( ddStart.c_str() ); | ||
476 | + | ||
477 | + int error = DD_SetTime(id, st); | ||
478 | + | ||
479 | + if (error < 0) | ||
480 | + { | ||
481 | + std::string ddSetTimeErr = "[ERROR] getOrbitData : bad time pointer init in DD_SetTime -> err value : " + error; | ||
482 | + throw std::string(ddSetTimeErr); | ||
483 | + } | ||
484 | + | ||
485 | + char *params[2]; | ||
486 | + | ||
487 | + params[0] = const_cast<char*>( timeParameter.c_str() ); | ||
488 | + params[1] = const_cast<char*>( orbitParameter.c_str() ); | ||
489 | + | ||
490 | + char *timeIntervall = const_cast<char*>( ddInterval.c_str() ); | ||
491 | + | ||
492 | + DD_data_t *data; | ||
493 | + | ||
494 | + int status = 0; | ||
495 | + | ||
496 | + do { | ||
497 | + | ||
498 | + status = DD_GetMultiData(id, 2, static_cast<char**>(params), timeIntervall, &data, 1); | ||
499 | + | ||
500 | + if (status < 0) | ||
501 | + { | ||
502 | + std::string ddGetDataErr = "[ERROR] getOrbitData : failed to get data -> status : " + status; | ||
503 | + throw std::string(ddGetDataErr); | ||
504 | + } | ||
505 | + | ||
506 | + int intOrbitStep = static_cast<int>( boost::lexical_cast<int>(orbitRes) ); | ||
507 | + int samplingStep; | ||
508 | + | ||
509 | + if ( intOrbitStep < 300 ) | ||
510 | + { | ||
511 | + // Resampling to 5min resolution for resolution strictly superior to 300s | ||
512 | + // samplingStep = intOrbitStep/60; | ||
513 | + samplingStep = static_cast<int>( 300/intOrbitStep ); | ||
514 | + | ||
515 | + } else { | ||
516 | + // Else : interpolation to do ... | ||
517 | + samplingStep = 1; | ||
518 | + } | ||
519 | + | ||
520 | + int dataType = data[1].type; | ||
521 | + | ||
522 | + for (int j = 0; j < data->VarNumber; j+=samplingStep) | ||
523 | + { | ||
524 | + satelliteTimeArray.push_back( static_cast<char*>(data[0].Variables[j]) ); | ||
525 | + | ||
526 | + if ( dataType == 3 ) // DOUBLE | ||
527 | + { | ||
528 | + x.push_back( static_cast<float>(static_cast<double*>(data[1].Variables[j])[0]) ); | ||
529 | + y.push_back( static_cast<float>(static_cast<double*>(data[1].Variables[j])[1]) ); | ||
530 | + z.push_back( static_cast<float>(static_cast<double*>(data[1].Variables[j])[2]) ); | ||
531 | + | ||
532 | + } | ||
533 | + else if ( dataType == 2 ) // FLOAT | ||
534 | + { | ||
535 | + | ||
536 | + x.push_back( static_cast<float*>(data[1].Variables[j])[0] ); | ||
537 | + y.push_back( static_cast<float*>(data[1].Variables[j])[1] ); | ||
538 | + z.push_back( static_cast<float*>(data[1].Variables[j])[2] ); | ||
539 | + | ||
540 | + } | ||
541 | + else | ||
542 | + { | ||
543 | + std::string ddGetDataTypeErr = "[ERROR] getOrbitData : failed to get data -> Unknown data type (not double nor float)"; | ||
544 | + throw std::string(ddGetDataTypeErr); | ||
545 | + } | ||
546 | + } | ||
547 | + | ||
548 | + | ||
549 | + } while (status == MOREDATA); | ||
550 | + | ||
551 | + DD_Close(id); | ||
552 | +} | ||
553 | + | ||
554 | + | ||
555 | +/** | ||
556 | + * @brief Get Sym H index data from DD base | ||
557 | + * | ||
558 | + * @param[in] symIndexVi Name of the VI into DD base | ||
559 | + * @param[in] ddStart Start time DD time format | ||
560 | + * @param[in] ddInterval Time interval DD time format | ||
561 | + * @param[in] timeParameter Name of time parameter into NC file | ||
562 | + * @param[in] symIndexParameter Name of Sym H index parameter into NC file | ||
563 | + * @param symIndexTimeArray Time array | ||
564 | + * @param symIndexArray Sym H index array | ||
565 | + */ | ||
566 | +void getSymIndexData(string symIndexVi, string ddStart, string ddInterval, | ||
567 | + string timeParameter, string symIndexParameter, | ||
568 | + vector<string>& symIndexTimeArray, vector<float>& symIndexArray) | ||
569 | +{ | ||
570 | + int id = DD_SetVariable( const_cast<char*>( symIndexVi.c_str() ) ); | ||
571 | + | ||
572 | + char *st = const_cast<char*>( ddStart.c_str() ); | ||
573 | + | ||
574 | + int error = DD_SetTime(id, st); | ||
575 | + | ||
576 | + if (error < 0) | ||
577 | + { | ||
578 | + std::string ddSetTimeErr = "[ERROR] getSymIndexData : bad time pointer init in DD_SetTime -> err value : " + error; | ||
579 | + throw std::string(ddSetTimeErr); | ||
580 | + } | ||
581 | + | ||
582 | + char *params[2]; | ||
583 | + | ||
584 | + params[0] = const_cast<char*>( timeParameter.c_str() ); | ||
585 | + params[1] = const_cast<char*>( symIndexParameter.c_str() ); | ||
586 | + | ||
587 | + char *timeIntervall = const_cast<char*>( ddInterval.c_str() ); | ||
588 | + | ||
589 | + DD_data_t *data; | ||
590 | + | ||
591 | + int status = 0; | ||
592 | + | ||
593 | + do { | ||
594 | + | ||
595 | + status = DD_GetMultiData(id, 2, static_cast<char**>(params), timeIntervall, &data, 1); | ||
596 | + | ||
597 | + if (status < 0) | ||
598 | + { | ||
599 | + std::string ddGetDataErr = "[ERROR] getSymIndexData : failed to get data -> status : " + status; | ||
600 | + throw std::string(ddGetDataErr); | ||
601 | + } | ||
602 | + | ||
603 | + // 5 min to 1 min resampling | ||
604 | + for (int j = 0; j < data->VarNumber; j+=5) | ||
605 | + { | ||
606 | + symIndexTimeArray.push_back( static_cast<char*>(data[0].Variables[j]) ); | ||
607 | + symIndexArray.push_back( static_cast<float*>(data[1].Variables[j])[0] ); | ||
608 | + } | ||
609 | + | ||
610 | + | ||
611 | + } while (status == MOREDATA); | ||
612 | + | ||
613 | + DD_Close(id); | ||
614 | +} | ||
615 | + | ||
616 | + | ||
617 | +/** | ||
618 | + * @brief Remove NAN values | ||
619 | + * | ||
620 | + * @param n Size of arrays | ||
621 | + * @param rampArray Ram pressure array | ||
622 | + * @param vnormArray Velocity norm array | ||
623 | + * @param vx Vx component array | ||
624 | + * @param vy Vy component array | ||
625 | + * @param vz Vz component array | ||
626 | + * @param bx Bx component array | ||
627 | + * @param by By component array | ||
628 | + * @param bz Bz component array | ||
629 | + * @param symIndexArray Sym H index array | ||
630 | + * @param flag Quality flag array | ||
631 | + */ | ||
632 | +void cleanData(int& n, vector<float>& rampArray, vector<float>& vnormArray, | ||
633 | + vector<float>& vx, vector<float>& vy, vector<float>& vz, | ||
634 | + vector<float>& bx, vector<float>& by, vector<float>& bz, | ||
635 | + vector<float>& symIndexArray, vector<int>& flag) | ||
636 | +{ | ||
637 | + for (int i = 0; i < n; ++i) | ||
638 | + { | ||
639 | + // No value for plasma data | ||
640 | + if ( isnan(rampArray[i]) ) | ||
641 | + { | ||
642 | + rampArray[i] = 1.5; | ||
643 | + vnormArray[i] = 500.0; | ||
644 | + vx[i] = -500.0; | ||
645 | + vy[i] = -29.78; | ||
646 | + // vy[i] = 0.0; | ||
647 | + vz[i] = 0.0; | ||
648 | + | ||
649 | + flag[i] = -1; | ||
650 | + } | ||
651 | + | ||
652 | + // No value for magnetic field data | ||
653 | + if ( isnan(by[i]) ) | ||
654 | + { | ||
655 | + bx[i] = 0.0; | ||
656 | + by[i] = 0.0; | ||
657 | + bz[i] = 1.0; | ||
658 | + | ||
659 | + flag[i] = -1; | ||
660 | + } | ||
661 | + | ||
662 | + // No value for Sym H index data | ||
663 | + if ( isnan(symIndexArray[i]) ) | ||
664 | + { | ||
665 | + symIndexArray[i] = -10.0; | ||
666 | + | ||
667 | + flag[i] = -1; | ||
668 | + } | ||
669 | + } | ||
670 | +} | ||
671 | + | ||
672 | +void writeInputsFile(string& filename, int& size, | ||
673 | + vector<string>& plasmaTimeArray, | ||
674 | + vector<float>& rampArray, vector<float>& vnormArray, | ||
675 | + vector<float>& vx, vector<float>& vy, vector<float>& vz, | ||
676 | + vector<float>& bx, vector<float>& by, vector<float>& bz, | ||
677 | + vector<float>& x, vector<float>& y, vector<float>& z, | ||
678 | + vector<float>& symIndexArray, | ||
679 | + vector<int>& flag) | ||
680 | +{ | ||
681 | + ofstream inFile(filename.c_str(), ios::out | ios::trunc); | ||
682 | + | ||
683 | + string year, day, hour, minute; | ||
684 | + | ||
685 | + if(inFile) | ||
686 | + { | ||
687 | + for (int i = 0; i < size; ++i) | ||
688 | + { | ||
689 | + year = plasmaTimeArray[i].substr(0, 4); | ||
690 | + day = plasmaTimeArray[i].substr(4, 3); | ||
691 | + hour = plasmaTimeArray[i].substr(7, 2); | ||
692 | + minute = plasmaTimeArray[i].substr(9, 2); | ||
693 | + | ||
694 | + int yearInt = boost::lexical_cast<int>(year); | ||
695 | + | ||
696 | + int dayInt = boost::lexical_cast<int>(day); | ||
697 | + dayInt++; | ||
698 | + | ||
699 | + int hourInt = boost::lexical_cast<int>(hour); | ||
700 | + | ||
701 | + int minuteInt = boost::lexical_cast<int>(minute); | ||
702 | + | ||
703 | + inFile << yearInt << " " | ||
704 | + << dayInt << " " | ||
705 | + << hourInt << " " | ||
706 | + << minuteInt << " " | ||
707 | + << x[i] << " " << y[i] << " " << z[i] << " " | ||
708 | + << bx[i] << " " << by[i] << " " << bz[i] << " " | ||
709 | + << vx[i] << " " << vy[i] + 29.78 << " " << vz[i] << " " | ||
710 | + // << vx[i] << " " << vy[i] << " " << vz[i] << " " | ||
711 | + << rampArray[i] << " " | ||
712 | + << symIndexArray[i] << " " | ||
713 | + << flag[i] | ||
714 | + << endl; | ||
715 | + } | ||
716 | + | ||
717 | + inFile.close(); | ||
718 | + } | ||
719 | +} | ||
720 | + | ||
721 | + | ||
722 | + | ||
723 | + | ||
724 | +/** | ||
725 | + * @brief Linear interpolation for satellite orbit data | ||
726 | + * | ||
727 | + * @param[in] satTime Time array of the satellite | ||
728 | + * @param x X component array | ||
729 | + * @param y Y component array | ||
730 | + * @param z Z component array | ||
731 | + */ | ||
732 | +void linearInterpolation(vector<string> satTime, | ||
733 | + vector<float>& x, vector<float>& y, vector<float>& z) | ||
734 | +{ | ||
735 | + int n = satTime.size(); | ||
736 | + double *p_t = new double[n]; | ||
737 | + double *p_x = new double[n]; | ||
738 | + double *p_y = new double[n]; | ||
739 | + double *p_z = new double[n]; | ||
740 | + | ||
741 | + for (int i = 0; i < n; ++i) | ||
742 | + { | ||
743 | + p_t[i] = boost::lexical_cast<double>(satTime[i]); | ||
744 | + p_x[i] = boost::lexical_cast<double>(x[i]); | ||
745 | + p_y[i] = boost::lexical_cast<double>(y[i]); | ||
746 | + p_z[i] = boost::lexical_cast<double>(z[i]); | ||
747 | + } | ||
748 | + | ||
749 | + gsl_interp *interpolation = gsl_interp_alloc (gsl_interp_linear, n); | ||
750 | + | ||
751 | + gsl_interp_init(interpolation, p_t, p_x, n); | ||
752 | + gsl_interp_init(interpolation, p_t, p_y, n); | ||
753 | + gsl_interp_init(interpolation, p_t, p_z, n); | ||
754 | + | ||
755 | + gsl_interp_accel * accelerator = gsl_interp_accel_alloc(); | ||
756 | + | ||
757 | + double startTime = p_t[0]; | ||
758 | + double stopTime = p_t[n-1]; | ||
759 | + double step = 300; | ||
760 | + | ||
761 | + vector<float> newX; | ||
762 | + vector<float> newY; | ||
763 | + vector<float> newZ; | ||
764 | + double nx, ny, nz; | ||
765 | + | ||
766 | + for (double ti = startTime; ti < stopTime; ti += step) | ||
767 | + { | ||
768 | + nx = gsl_interp_eval(interpolation, p_t, p_x, ti, accelerator); | ||
769 | + ny = gsl_interp_eval(interpolation, p_t, p_y, ti, accelerator); | ||
770 | + nz = gsl_interp_eval(interpolation, p_t, p_z, ti, accelerator); | ||
771 | + | ||
772 | + newX.push_back( boost::lexical_cast<float>(nx) ); | ||
773 | + newY.push_back( boost::lexical_cast<float>(ny) ); | ||
774 | + newZ.push_back( boost::lexical_cast<float>(nz) ); | ||
775 | + } | ||
776 | + | ||
777 | + x = newX; | ||
778 | + y = newY; | ||
779 | + z = newZ; | ||
780 | + | ||
781 | + delete p_t; | ||
782 | + delete p_x; | ||
783 | + delete p_y; | ||
784 | + delete p_z; | ||
785 | + | ||
786 | + gsl_interp_free(interpolation); | ||
787 | + gsl_interp_accel_free(accelerator); | ||
788 | + | ||
789 | +} | ||
790 | + | ||
791 | + | ||
792 | +/** | ||
793 | + * @brief Bad looking tool to add new data for Geotail orbit (x2) | ||
794 | + * | ||
795 | + * @param x X component array | ||
796 | + * @param y Y component array | ||
797 | + * @param z Z component array | ||
798 | + */ | ||
799 | +void GTL_interp(vector<float>& x, vector<float>& y, vector<float>& z) | ||
800 | +{ | ||
801 | + vector<float> newX, newY, newZ; | ||
802 | + int size = x.size(); | ||
803 | + | ||
804 | + for (int i = 0; i < size; ++i) | ||
805 | + { | ||
806 | + newX.push_back(x[i]); | ||
807 | + newX.push_back(x[i]); | ||
808 | + | ||
809 | + newY.push_back(y[i]); | ||
810 | + newY.push_back(y[i]); | ||
811 | + | ||
812 | + newZ.push_back(z[i]); | ||
813 | + newZ.push_back(z[i]); | ||
814 | + } | ||
815 | + | ||
816 | + x = newX; | ||
817 | + y = newY; | ||
818 | + z = newZ; | ||
819 | +} | ||
0 | \ No newline at end of file | 820 | \ No newline at end of file |
@@ -0,0 +1,66 @@ | @@ -0,0 +1,66 @@ | ||
1 | +#ifndef TOOLS_H_INCLUDED | ||
2 | +#define TOOLS_H_INCLUDED | ||
3 | + | ||
4 | +//////////////// | ||
5 | +// CPP HEADER // | ||
6 | +//////////////// | ||
7 | +#include <string> | ||
8 | +#include <vector> | ||
9 | + | ||
10 | + | ||
11 | +///////////////////////// | ||
12 | +// FUNCTION PROTOTYPES // | ||
13 | +///////////////////////// | ||
14 | +void printMission(std::string& satelliteName, std::string& satelliteVi, | ||
15 | +std::string& satelliteOrbit, std::string& satelliteRes, | ||
16 | +std::string& plasmaName, std::string& plasmaVi, std::string& plasmaVNorm, | ||
17 | +std::string& plasmaRamP, std::string& plasmaVel, std::string& plasmaMag, | ||
18 | +std::string& indexName, std::string& indexVi, std::string& indexSym, | ||
19 | +std::string& start, std::string& stop, | ||
20 | +std::string& ddStart, std::string& ddStop, std::string& ddInterval); | ||
21 | +void loadMission(std::string& filename, | ||
22 | + std::string& satelliteName, std::string& satelliteVi, | ||
23 | + std::string& satelliteOrbit, std::string& satelliteRes, | ||
24 | + std::string& plasmaName, std::string& plasmaVi, std::string& plasmaVNorm, | ||
25 | + std::string& plasmaRamP, std::string& plasmaVel, std::string& plasmaMag, | ||
26 | + std::string& indexName, std::string& indexVi, std::string& indexSym, | ||
27 | + std::string& start, std::string& stop); | ||
28 | +std::vector<std::string> tokenizedDate(std::string strDate); | ||
29 | +bool checkDateFormat(std::string& date); | ||
30 | +std::string toDDTime(std::string date); | ||
31 | +std::string makeDDIntervall(std::string startTime, std::string stopTime); | ||
32 | +void getPlasmaData(std::string plasmaVi, std::string ddStart, std::string ddInterval, | ||
33 | + std::string timeParameter, std::string vnormParameter, std::string rampParameter, | ||
34 | + std::string velParameter, std::string magParameter, | ||
35 | + std::vector<std::string>& plasmaTimeArray, std::vector<float>& vnormArray, std::vector<float>& rampArray, | ||
36 | + std::vector<float>& vxArray, std::vector<float>& vyArray, std::vector<float>& vzArray, | ||
37 | + std::vector<float>& bxArray, std::vector<float>& byArray, std::vector<float>& bzArray); | ||
38 | +void getOrbitData(std::string satelliteVi, std::string orbitRes, | ||
39 | + std::string ddStart, std::string ddInterval, | ||
40 | + std::string timeParameter, std::string orbitParameter, | ||
41 | + std::vector<std::string>& satelliteTimeArray, | ||
42 | + std::vector<float>& x, std::vector<float>& y, std::vector<float>& z); | ||
43 | +void getSymIndexData(std::string symIndexVi, std::string ddStart, std::string ddInterval, | ||
44 | + std::string timeParameter, std::string symIndexParameter, | ||
45 | + std::vector<std::string>& symIndexTimeArray, std::vector<float>& symIndexArray); | ||
46 | +void cleanData(int& n, std::vector<float>& rampArray, std::vector<float>& vnormArray, | ||
47 | + std::vector<float>& vx, std::vector<float>& vy, std::vector<float>& vz, | ||
48 | + std::vector<float>& bx, std::vector<float>& by, std::vector<float>& bz, | ||
49 | + std::vector<float>& symIndexArray, std::vector<int>& flag); | ||
50 | + | ||
51 | +void writeInputsFile(std::string& filename, int& size, | ||
52 | + std::vector<std::string>& plasmaTimeArray, | ||
53 | + std::vector<float>& rampArray, std::vector<float>& vnormArray, | ||
54 | + std::vector<float>& vx, std::vector<float>& vy, std::vector<float>& vz, | ||
55 | + std::vector<float>& bx, std::vector<float>& by, std::vector<float>& bz, | ||
56 | + std::vector<float>& x, std::vector<float>& y, std::vector<float>& z, | ||
57 | + std::vector<float>& symIndexArray, | ||
58 | + std::vector<int>& flag); | ||
59 | + | ||
60 | + | ||
61 | +void linearInterpolation(std::vector<std::string> satTime, | ||
62 | + std::vector<float>& x, std::vector<float>& y, std::vector<float>& z); | ||
63 | + | ||
64 | +void GTL_interp(std::vector<float>& x, std::vector<float>& y, std::vector<float>& z); | ||
65 | + | ||
66 | +#endif | ||
0 | \ No newline at end of file | 67 | \ No newline at end of file |