Skip to content

Commit

Permalink
Write comment & replaced std::optional
Browse files Browse the repository at this point in the history
  • Loading branch information
chkim-usgs committed May 31, 2024
1 parent 8089792 commit 5361809
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 42 deletions.
42 changes: 22 additions & 20 deletions SpiceQL/include/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace SpiceQL {
* @param centerOfMotion Naif body code of an object which is the center of motion for
* bodyCode
* @param referenceFrame Naif name of the reference system relative to which the state is
* @param id SPK segment identifier (max size 40)
* @param segmentId SPK segment identifier (max size 40)
* @param degree Degree of the Hermite polynomials used to interpolate the states
* @param stateVelocities Time ordered vector of state velocities dX, dY, dZ
* @param segmentComment The comment string for the new segment
Expand All @@ -45,20 +45,20 @@ namespace SpiceQL {
int bodyCode,
int centerOfMotion,
std::string referenceFrame,
std::string id, int degree,
std::optional<std::vector<std::vector<double>>> stateVelocities,
std::optional<std::string> segmentComment);
std::string segmentId, int degree,
std::vector<std::vector<double>> stateVelocities = {},
std::string segmentComment = "");

//! @cond Doxygen_Suppress
std::vector<double> stateTimes;
int bodyCode;
int centerOfMotion;
std::string referenceFrame;
std::string id;
std::string segmentId;
int polyDegree;
std::vector<std::vector<double>> statePositions;
std::optional<std::vector<std::vector<double>>> stateVelocities;
std::optional<std::string> comment;
std::vector<std::vector<double>> stateVelocities;
std::string comment;
//! @endcond
};

Expand All @@ -82,23 +82,23 @@ namespace SpiceQL {
* @param times times for the CK segment in ascending order
* @param bodyCode Naif body code of an object whose state is described by the segments
* @param referenceFrame Naif name of the reference system relative to which the state is
* @param id SPK segment identifier (max size 40)
* @param anglularVelocities Time ordered vector of state velocities dX, dY, dZ
* @param segmentId SPK segment identifier (max size 40)
* @param angularVelocities Time ordered vector of state velocities dX, dY, dZ
* @param comment The comment string for the new segment
*/
CkSegment(std::vector<std::vector<double>> quats, std::vector<double> times, int bodyCode,
std::string referenceFrame, std::string id,
std::optional<std::vector<std::vector<double>>> anglularVelocities = std::nullopt,
std::optional<std::string> comment = std::nullopt);
std::string referenceFrame, std::string segmentId,
std::vector<std::vector<double>> angularVelocities = {},
std::string comment = "");

//! @cond Doxygen_Suppress
std::vector<double> times;
std::vector<std::vector<double>> quats;
int bodyCode;
std::string referenceFrame;
std::string id;
std::optional<std::vector<std::vector<double>>> angularVelocities = std::nullopt;
std::optional<std::string> comment = std::nullopt;
std::string segmentId;
std::vector<std::vector<double>> angularVelocities;
std::string comment;
//! @endcond
};

Expand Down Expand Up @@ -146,8 +146,8 @@ namespace SpiceQL {
std::string referenceFrame,
std::string segmentId,
int polyDegree,
std::optional<std::vector<std::vector<double>>> stateVelocities = std::nullopt,
std::optional<std::string> segmentComment = std::nullopt);
std::vector<std::vector<double>> stateVelocities = {},
std::string segmentComment = "");


/**
Expand All @@ -174,8 +174,8 @@ namespace SpiceQL {
std::string segmentId,
std::string sclk,
std::string lsk,
std::optional<std::vector<std::vector<double>>> angularVelocity = std::nullopt,
std::optional<std::string> comment= std::nullopt);
std::vector<std::vector<double>> angularVelocity = {},
std::string comment = "");


/**
Expand All @@ -193,6 +193,8 @@ namespace SpiceQL {
std::string lsk,
std::vector<CkSegment> segments);

void writeComment(std::string fileName,
std::string comment);

/**
* @brief Write json key value pairs into a NAIF text kernel
Expand All @@ -202,6 +204,6 @@ namespace SpiceQL {
* @param comment the comment to add to the top of the kernel
* @param keywords json object containing key/value pairs to write to the text kernel
*/
void writeTextKernel(std::string fileName, std::string type, nlohmann::json &keywords, std::optional<std::string> comment = std::nullopt);
void writeTextKernel(std::string fileName, std::string type, nlohmann::json &keywords, std::string comment = "");

}
88 changes: 66 additions & 22 deletions SpiceQL/src/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ namespace SpiceQL {
int bodyCode,
int centerOfMotion,
string referenceFrame,
string id, int degree,
optional<vector<vector<double>>> stateVelocities,
optional<string> comment) {
string segmentId, int degree,
vector<vector<double>> stateVelocities,
string comment) {

this->comment = comment;
this->bodyCode = bodyCode;
this->centerOfMotion = centerOfMotion;
this->referenceFrame = referenceFrame;
this->id = id;
this->segmentId = segmentId;
this->polyDegree = degree;
this->statePositions = statePositions;
this->stateVelocities = stateVelocities;
Expand Down Expand Up @@ -61,14 +61,15 @@ namespace SpiceQL {
int bodyCode,
string referenceFrame,
string segmentId,
optional<vector<vector<double>>> anglularVelocities,
optional<string> comment) {
vector<vector<double>> angularVelocities,
string comment) {

this->comment = comment;
this->quats = quats;
this->times = times;
this->bodyCode = bodyCode;
this->referenceFrame = referenceFrame;
this->id = segmentId;
this->angularVelocities = anglularVelocities;
this->segmentId = segmentId;
this->angularVelocities = angularVelocities;
this->comment = comment;
}

Expand All @@ -81,8 +82,8 @@ namespace SpiceQL {
string segmentId,
string sclk,
string lsk,
optional<vector<vector<double>>> angularVelocities,
optional<string> comment) {
vector<vector<double>> angularVelocities,
string comment) {

SpiceInt handle;

Expand All @@ -98,24 +99,32 @@ namespace SpiceQL {
et = sclkdp;
}
checkNaifErrors();
ckopn_c(path.c_str(), "CK", comment.value_or("CK Kernel").size(), &handle);

if(comment.empty()) {
comment = "CK Kernel";
}

ckopn_c(path.c_str(), "CK", comment.size(), &handle);
checkNaifErrors();

ckw03_c (handle,
times.at(0),
times.at(times.size()-1),
bodyCode,
referenceFrame.c_str(),
(bool)angularVelocities,
!angularVelocities.empty(),
segmentId.c_str(),
times.size(),
times.data(),
quats.data(),
(angularVelocities) ? angularVelocities->data() : nullptr,
(!angularVelocities.empty()) ? angularVelocities.data() : nullptr,
times.size(),
times.data());
checkNaifErrors();
ckcls_c(handle);
checkNaifErrors();

writeComment(path, comment);
}


Expand All @@ -127,12 +136,12 @@ namespace SpiceQL {
string referenceFrame,
string segmentId,
int polyDegree,
optional<vector<vector<double>>> stateVelocities,
optional<string> segmentComment) {
vector<vector<double>> stateVelocities,
string segmentComment) {

vector<vector<double>> states;

if (!stateVelocities) {
if (stateVelocities.empty()) {
// init a 0 velocity array
vector<vector<double>> velocities;
for (int i = 0; i < statePositions.size(); i++) {
Expand All @@ -141,7 +150,7 @@ namespace SpiceQL {
stateVelocities = velocities;
}

states = concatStates(statePositions, *stateVelocities);
states = concatStates(statePositions, stateVelocities);

SpiceInt handle;
checkNaifErrors();
Expand All @@ -163,6 +172,9 @@ namespace SpiceQL {
spkcls_c(handle);
checkNaifErrors();

// Write comment to header
writeComment(fileName, segmentComment);

return;
}

Expand All @@ -183,7 +195,7 @@ namespace SpiceQL {
segments[0].bodyCode,
segments[0].centerOfMotion,
segments[0].referenceFrame,
segments[0].id,
segments[0].segmentId,
segments[0].polyDegree,
segments[0].stateVelocities,
segments[0].comment);
Expand All @@ -206,16 +218,48 @@ namespace SpiceQL {
segments[0].times,
segments[0].bodyCode,
segments[0].referenceFrame,
segments[0].id,
segments[0].segmentId,
sclk,
lsk,
segments[0].angularVelocities,
segments[0].comment);

}

void writeComment(string fileName, string comment) {
SpiceInt handle;
dafopw_c(fileName.c_str(), &handle);
checkNaifErrors();

// Trap errors so they are not fatal if the comment section fills up.
// Calling environments can decide how to handle it.

string commOut;
checkNaifErrors();
for ( int i = 0 ; i < comment.size() ; i++ ) {
if ( comment[i] == '\n' ) {
while ( commOut.size() < 2 ) { commOut.append(" "); }
dafac_c(handle, 1, commOut.size(), commOut.data());
checkNaifErrors();
commOut.clear();
}
else {
commOut.push_back(comment[i]);
}
}

// See if there is residual to write
if ( commOut.size() > 0 ) {
while ( commOut.size() < 2 ) { commOut.append(" "); }
dafac_c(handle, 1, commOut.size(), commOut.data());
checkNaifErrors();
}

// Close file handle
dafcls_c(handle);
}

void writeTextKernel(string fileName, string type, json &keywords, optional<string> comment) {
void writeTextKernel(string fileName, string type, json &keywords, string comment) {

/**
* @brief Return an adjusted string such that it multi-lined if longer than some max length.
Expand Down Expand Up @@ -286,7 +330,7 @@ namespace SpiceQL {

textKernel << "KPL/" + toUpper(type) << endl << endl;
textKernel << "\\begintext" << endl << endl;
textKernel << comment.value_or("") << endl << endl;
textKernel << comment << endl << endl;
textKernel << "\\begindata" << endl << endl;

for(auto it = keywords.begin(); it != keywords.end(); it++) {
Expand Down

0 comments on commit 5361809

Please sign in to comment.