Blame view

src/SpiceKernel/SpiceKernelMgr.cc 5.65 KB
cebd3f0e   Benjamin Renard   First implementat...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
 * SpiceKernelMgr.cc
 *
 *  Created on: 08 jul. 2016
 *      Author: AKKA IS
 */

#include "SpiceKernelMgr.hh"
#include "SpiceKernelConfigParser.hh"
#include "Helper.hh"
#include "Properties.hh"
#include "SpiceKernelStatus.hh"
#include "TimeUtil.hh"

#include "SpiceUsr.h"

namespace AMDA {
namespace SpiceKernel {

413a6873   Benjamin Renard   Add coordinates t...
20
SpiceKernelMgr::SpiceKernelMgr() : ::Singleton<SpiceKernelMgr>(), _genericDataFilesLoaded(false) {
cebd3f0e   Benjamin Renard   First implementat...
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
	SpiceKernelStatus::CheckErrors();
	loadConfig();
}

SpiceKernelMgr::~SpiceKernelMgr() {

}

int SpiceKernelMgr::getBodyCenterFromFrameName(const char *frameName) {
	if (_bodyCenterFromFrameNameMap.find(frameName) != _bodyCenterFromFrameNameMap.end())
		return _bodyCenterFromFrameNameMap[frameName];

	SpiceInt frcode = SpiceKernelMgr::getInstance()->getFrameIdFromFrameName(frameName);

	if (frcode == -1)
		return -1;

	SpiceInt     cent;
	SpiceInt     frclss;
	SpiceInt     clssid;
	SpiceBoolean found;

	frinfo_c (frcode, &cent, &frclss, &clssid, &found);

	//THE SPICE convention uses negative integers as spacecraft ID codes.
	if (SpiceKernelStatus::CheckErrors() || (cent < 0))
		return -1;

	_bodyCenterFromFrameNameMap[frameName] = cent;

	return cent;
}

int SpiceKernelMgr::getFrameIdFromFrameName(const char *frameName) {
	if (_frameIdFromFrameNameMap.find(frameName) != _frameIdFromFrameNameMap.end())
		return _frameIdFromFrameNameMap[frameName];

	SpiceInt frcode;

	namfrm_c (frameName, &frcode);

	//If the name input through frname is not recognized,
    //frcode will be returned with a value of zero.
	if (SpiceKernelStatus::CheckErrors() || (frcode == 0))
		return -1;

	_frameIdFromFrameNameMap[frameName] = frcode;

	return frcode;
}

bool SpiceKernelMgr::loadDataFilesForFrameName(const char* frameName) {
	if (std::find(_DataFilesLoadedForFrameName.begin(), _DataFilesLoadedForFrameName.end(), frameName) != _DataFilesLoadedForFrameName.end())
		return true;

413a6873   Benjamin Renard   Add coordinates t...
76
	if (!loadGenericDataFiles())
cebd3f0e   Benjamin Renard   First implementat...
77
78
79
80
81
82
83
84
85
86
87
88
89
90
		return false;

	int bodyCenter = SpiceKernelMgr::getInstance()->getBodyCenterFromFrameName(frameName);
	if (bodyCenter == -1)
		return false;

	std::list<std::string> dataFilesForCrtBody = _config->getDataFilesForBodyId(bodyCenter);
	bool loaded = loadListOfDataFiles(dataFilesForCrtBody);

	if (loaded)
		_DataFilesLoadedForFrameName.push_back(frameName);
	return loaded;
}

413a6873   Benjamin Renard   Add coordinates t...
91
92
93
94
95
96
97
98
bool SpiceKernelMgr::loadGenericDataFiles() {
	if (_genericDataFilesLoaded)
		return true;
	std::list<std::string> dataFilesForAllBodies = _config->getDataFilesForBodyId(-1);
	_genericDataFilesLoaded =  loadListOfDataFiles(dataFilesForAllBodies);
	return _genericDataFilesLoaded;
}

cebd3f0e   Benjamin Renard   First implementat...
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
bool SpiceKernelMgr::isDataFileLoaded(const char* filePath) {
	return (std::find(_loadedDataFiles.begin(), _loadedDataFiles.end(), filePath) != _loadedDataFiles.end());
}

bool SpiceKernelMgr::loadListOfDataFiles(std::list<std::string> dataFilesList) {
	for (std::list<std::string>::iterator it = dataFilesList.begin(); it != dataFilesList.end(); ++it) {
		std::string path = *it;
		if (isDataFileLoaded(path.c_str()))
			continue;
		std::string fullPath = _config->getFullPath(path.c_str());

		furnsh_c (fullPath.c_str());

		if (SpiceKernelStatus::CheckErrors())
			return false;

		LOG4CXX_INFO(gLogger, "SpiceKernelMgr::loadDataFilesForFrameName - " << fullPath);

		_loadedDataFiles.push_back(path);
	}

	return true;
}

void SpiceKernelMgr::loadConfig() {
	AMDA::helpers::Properties lProperties("app.properties");

	// Retrieve XSD file for DataSet and build a parser
	SpiceKernelConfigParser parser(lProperties["app.spicekernel.configxsd"].c_str());

	// Parse XML file
	std::string configFile = lProperties["app.spicekernel.configfile"];
	_config = parser.parse (configFile);
}

double SpiceKernelMgr::timeStampToEt(double timeStamp) {
413a6873   Benjamin Renard   Add coordinates t...
135
136
137
	if (!loadGenericDataFiles())
		return 0;

cebd3f0e   Benjamin Renard   First implementat...
138
139
140
141
142
143
144
145
	std::string isoTime = TimeUtil::formatTimeDateInIso(timeStamp);

	double et = 0;
	str2et_c ( isoTime.c_str(), &et);

	if (SpiceKernelStatus::CheckErrors())
		return 0.;

413a6873   Benjamin Renard   Add coordinates t...
146
147
148
149
150
151
152
153
	return et;
}

double SpiceKernelMgr::timeStampToJulianDate(double timeStamp) {
	if (!loadGenericDataFiles())
		return 0;

	double et = timeStampToEt(timeStamp);
cebd3f0e   Benjamin Renard   First implementat...
154
	SpiceInt           lenout = 35;
d6c7a344   RENARD Benjamin   Fix some warning ...
155
156
	//ConstSpiceChar * format = "J";
	//SpiceInt prec  =  6;
413a6873   Benjamin Renard   Add coordinates t...
157
158
159
160
161
162
	SpiceChar      utcstr[lenout+1];
	memset(utcstr,0,sizeof(SpiceChar)*(lenout+1));

	et2utc_c ( et, "J", 7, lenout, utcstr );
	if (SpiceKernelStatus::CheckErrors())
		return 0.;
cebd3f0e   Benjamin Renard   First implementat...
163

413a6873   Benjamin Renard   Add coordinates t...
164
	double jd;
c6a67968   Benjamin Renard   Fix some violatio...
165
	sscanf(utcstr, "JD %32lf", &jd);
cebd3f0e   Benjamin Renard   First implementat...
166

413a6873   Benjamin Renard   Add coordinates t...
167
	return jd;
cebd3f0e   Benjamin Renard   First implementat...
168
169
}

413a6873   Benjamin Renard   Add coordinates t...
170

cebd3f0e   Benjamin Renard   First implementat...
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
bool SpiceKernelMgr::getRotationMatrix(const char* fromFrame, const char* toFrame, double et, double rotate[3][3]) {
	pxform_c (fromFrame, toFrame, et, rotate) ;

	if (SpiceKernelStatus::CheckErrors())
		return false;

	return true;
}

bool SpiceKernelMgr::getTranslation(const char* fromFrame, const char* toFrame, bool isPosition, double et, double translation[3]) {
	translation[0] = 0;
	translation[1] = 0;
	translation[2] = 0;

	if (isPosition) {
		int fromBodyId = getBodyCenterFromFrameName(fromFrame);
		int toBodyId = getBodyCenterFromFrameName(toFrame);
c6a67968   Benjamin Renard   Fix some violatio...
188
		if (fromBodyId != toBodyId) {
cebd3f0e   Benjamin Renard   First implementat...
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
			SpiceDouble state[6];
			SpiceDouble lt;
			spkgeo_c(fromBodyId, et, toFrame, toBodyId, state, &lt);

			if (SpiceKernelStatus::CheckErrors())
				return false;

			translation[0] = state[0];
			translation[1] = state[1];
			translation[2] = state[2];

		}
	}
	return true;
}

bool SpiceKernelMgr::computeTransformation(double input[3], double translation[3], double rotation[3][3], double output[3]) {
75c853a2   Benjamin Renard   Fix bug in frame ...
206
	SpiceDouble vin[3] = {input[0], input[1], input[2]};
cebd3f0e   Benjamin Renard   First implementat...
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
	SpiceDouble vout[3];

	mxv_c (rotation, vin, vout);
	if (SpiceKernelStatus::CheckErrors())
		return false;

	output[0] = vout[0] + translation[0];
	output[1] = vout[1] + translation[1];
	output[2] = vout[2] + translation[2];

	return true;
}

} /* SpiceKernel */
} /* AMDA */