Skip to content

Commit

Permalink
Fix incorrect ASCII geoid grid file handling across Linux/Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerfraser committed Apr 30, 2021
1 parent 16b58b9 commit 1475cba
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
20 changes: 8 additions & 12 deletions dynadjust/dynadjust/dnageoid/dnageoid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ dna_geoid_interpolation::dna_geoid_interpolation()
, m_inputCoordinates("")
, m_isRadians(false)
{

// Set the actual ASCII line length based on the system
m_lineLength = real_line_length_ascii<UINT32>(ASCII_LINE_LENGTH);
}

dna_geoid_interpolation::~dna_geoid_interpolation()
Expand Down Expand Up @@ -922,7 +923,7 @@ void dna_geoid_interpolation::CreateGridIndex(const char* fileName, const char*
double dNum;
char cBuf[DATA_RECORD];
char ident_rec[9];

int gridType = DetermineFileType(m_pGridfile->filetype);

try {
Expand All @@ -933,7 +934,7 @@ void dna_geoid_interpolation::CreateGridIndex(const char* fileName, const char*
{
if (gridType == TYPE_ASC) // ascii
{
filePos = m_pGridfile->ptrIndex[i].iGridPos + m_pGridfile->ptrIndex[i].lGscount * 42;
filePos = m_pGridfile->ptrIndex[i].iGridPos + m_pGridfile->ptrIndex[i].lGscount * m_lineLength;
m_pGfileptr.seekg(filePos);
m_pGfileptr.getline(cBuf, DATA_RECORD);
}
Expand Down Expand Up @@ -2314,7 +2315,7 @@ bool dna_geoid_interpolation::ReadAsciiShifts(geoid_values *pNShifts[], int iNod

try {
// calculate file position and set stream pointer
filePos = m_pGridfile->ptrIndex[m_pGridfile->iTheGrid].iGridPos + ((lNode-1) * 42);
filePos = m_pGridfile->ptrIndex[m_pGridfile->iTheGrid].iGridPos + ((lNode-1) * m_lineLength);
m_pGfileptr.seekg(filePos);

// Retrieve the whole line for node A
Expand Down Expand Up @@ -2555,16 +2556,12 @@ int dna_geoid_interpolation::OpenGridFile(const char *filename, const char *file
// fills the new elements with default values
ptheGrid->ptrIndex = new n_gridfileindex[ptheGrid->iNumsubgrids];

bool isRadians(false);
string shiftType(ptheGrid->chGs_type);
if (iequals(trimstr(shiftType), "radians"))
{
isRadians = true;
m_isRadians = true;
}
else
m_isRadians = false;

for (int i=0; i<ptheGrid->iNumsubgrids; i++)
{
if (gridType == TYPE_ASC) // ascii
Expand Down Expand Up @@ -2606,11 +2603,10 @@ int dna_geoid_interpolation::OpenGridFile(const char *filename, const char *file
ptheGrid->ptrIndex[i].iGridPos = (int)pgrid_ifs->tellg();

// Set the file position after the GS_COUNT number...which is the start of the next sub file.
pgrid_ifs->seekg(ptheGrid->ptrIndex[i].iGridPos + (ptheGrid->ptrIndex[i].lGscount * 42));
pgrid_ifs->seekg(ptheGrid->ptrIndex[i].iGridPos + (ptheGrid->ptrIndex[i].lGscount * m_lineLength));
}
else // binary
{

{
pgrid_ifs->ignore(IDENT_BUF);
pgrid_ifs->read(reinterpret_cast<char *>(ptheGrid->ptrIndex[i].chSubname), IDENT_BUF);

Expand Down
6 changes: 5 additions & 1 deletion dynadjust/dynadjust/dnageoid/dnageoid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ using namespace dynadjust::measurements;
using namespace dynadjust::exception;
using namespace dynadjust::iostreams;

/* format identifiers for NTv2 grid file */
// grid node format identifiers for ASCII NTv2 grid file
const char* const SHIFTS = "%f%f%f%f";
// grid node line length for ASCII NTv2 grid file
const UINT32 ASCII_LINE_LENGTH = 40;

#define GSB "gsb"
#define ASC "asc"
Expand Down Expand Up @@ -266,6 +268,8 @@ class dna_geoid_interpolation {
string m_inputCoordinates;

bool m_isRadians;

UINT32 m_lineLength;
};

} // namespace geoidinterpolation
Expand Down
21 changes: 21 additions & 0 deletions dynadjust/include/functions/dnaiostreamfuncs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ using namespace boost::gregorian;
// return input;
//}

template <typename T>
T real_line_length_ascii(const T& line_length_ascii)
{
// return the real length of a line based on the system definition
// of a "new line". On Windows, a new line is comprised of two characters
// namely, carriage return (CR, '\r') and line feed (LF, '\n'). On Unix
// and Unix-like systems (Linux, mac OS, BSD, etc.), a new line is
// comprised of a line feed (LF, '\n') only.
//

#if defined(__linux) || defined(sun) || defined(__unix__) || defined(__APPLE__)
// "real" line length = line_length_ascii + "\n"
return line_length_ascii + 1;

#else // #if defined(_WIN32) || defined(__WIN32__)
// "real" line length = line_length_ascii + "\r\n"
return line_length_ascii + 2;

#endif
}


template <typename T>
void file_opener(
Expand Down

0 comments on commit 1475cba

Please sign in to comment.