Commit d40ecf06 authored by Lefebvre, Jordan's avatar Lefebvre, Jordan
Browse files

Minor changes to arldatastream.

parent 916442f9
Pipeline #16839 passed with stages
in 17 minutes and 47 seconds
......@@ -85,7 +85,7 @@ ARLDataStream::ARLDataStream(const std::string& file,
mode | std::ios::binary);
if (!p->stream->is_open())
{
radix_line("Failed to open ARL file: " << file);
throw std::runtime_error("Error opening file '" + p->file + "'");
}
}
......@@ -171,25 +171,25 @@ bool ARLDataStream::read_index_header(const ARLRecordHeader& rheader,
// Read variable description part of index header
radix_line(" Reading variable part of index header... " << iheader.nz
<< " levels");
for (size_t level = 0; level < iheader.nz; ++level)
for (size_t level = 0; level < size_t(iheader.nz); ++level)
{
// Read levels information
std::string levelString = p->stream->readString(6);
iheader.levels.push_back(std::stof(levelString));
std::string nVarsString = p->stream->readString(2);
iheader.nVarsAtLevels.push_back(std::stoi(nVarsString));
iheader.num_vars_at_levels.push_back(std::stoi(nVarsString));
// Read names and checksums
iheader.varNames.push_back(std::vector<std::string>());
iheader.checkSums.push_back(std::vector<int>());
radix_line(" Level " << level << "... " << iheader.nVarsAtLevels[level]
<< " vars");
for (size_t var = 0; var < iheader.nVarsAtLevels[level]; ++var)
iheader.var_names.push_back(std::vector<std::string>());
iheader.check_sums.push_back(std::vector<int>());
radix_line(" Level " << level << "... "
<< iheader.num_vars_at_levels[level] << " vars");
for (int var = 0; var < iheader.num_vars_at_levels[level]; ++var)
{
std::string nameString = p->stream->readString(4);
iheader.varNames[level].push_back(nameString);
iheader.var_names[level].push_back(nameString);
std::string sumString = p->stream->readString(3);
iheader.checkSums[level].push_back(std::stoi(sumString));
iheader.check_sums[level].push_back(std::stoi(sumString));
p->stream->skipBytes(1);
}
}
......@@ -226,16 +226,17 @@ bool ARLDataStream::write_index_header(const ARLRecordHeader& rheader,
int pos = 0;
// Write the variable description part of the header
for (size_t level = 0; level < iheader.nz; ++level)
for (size_t level = 0; level < size_t(iheader.nz); ++level)
{
pos += sprintf(indexHeaderVars + pos, "%6.1f", iheader.levels[level]);
pos += sprintf(indexHeaderVars + pos, "%2d", iheader.nVarsAtLevels[level]);
for (size_t var = 0; var < iheader.nVarsAtLevels[level]; ++var)
pos += sprintf(indexHeaderVars + pos, "%2d",
iheader.num_vars_at_levels[level]);
for (size_t var = 0; var < size_t(iheader.num_vars_at_levels[level]); ++var)
{
pos += sprintf(indexHeaderVars + pos, "%4s",
iheader.varNames[level][var].c_str());
pos +=
sprintf(indexHeaderVars + pos, "%3d ", iheader.checkSums[level][var]);
iheader.var_names[level][var].c_str());
pos += sprintf(indexHeaderVars + pos, "%3d ",
iheader.check_sums[level][var]);
}
}
......@@ -253,6 +254,7 @@ bool ARLDataStream::write_index_header(const ARLRecordHeader& rheader,
// Write the two elements of the header
p->stream->writeString(std::string(indexHeaderMain),
ARLDataStream::PImpl::indexHeaderLength);
p->stream->writeString(std::string(indexHeaderVars), pos);
// Skip enough bytes to get to the next index header
......@@ -261,7 +263,7 @@ bool ARLDataStream::write_index_header(const ARLRecordHeader& rheader,
radix_line(" Write complete: skipping "
<< bytesToSkip << " bytes to the next index header");
p->stream->writeString("", bytesToSkip);
p->stream->writeString("", size_t(bytesToSkip));
radix_line(" header as string:\n " << indexHeaderMain
<< indexHeaderVars);
......@@ -281,11 +283,12 @@ bool ARLDataStream::read_record(const ARLRecordHeader& rheader,
record.resize(iheader.nx);
for (std::vector<float>& vec : record)
{
vec.resize(iheader.ny);
vec.resize(size_t(iheader.ny));
}
// Calculate the scaling factor
float scaleFactor = pow(2.0, 7.0 - (float)rheader.nexp), lastValue = 0.0;
float scaleFactor = std::pow(2.0f, 7.0f - float(rheader.nexp)),
lastValue = 0.f;
radix_line(" Reading record data:");
for (int y = 0; y < iheader.ny; ++y)
......@@ -293,7 +296,7 @@ bool ARLDataStream::read_record(const ARLRecordHeader& rheader,
for (int x = 0; x < iheader.nx; ++x)
{
// Read the raw value
unsigned char ch = (unsigned char)p->stream->readChar();
unsigned char ch = (unsigned char)(p->stream->readChar());
int packedValue = ch - 127;
// Calculate the unpacked value
......@@ -340,7 +343,8 @@ bool ARLDataStream::write_record(const ARLRecordHeader& rheader,
}
// Calculate the scaling factor
float scaleFactor = pow(2.0, 7.0 - (float)rheader.nexp), lastValue = 0.0;
float scaleFactor = std::pow(2.0f, 7.0f - float(rheader.nexp)),
lastValue = 0.f;
radix_line(" Writing record data:");
for (int y = 0; y < iheader.ny; ++y)
......@@ -356,13 +360,13 @@ bool ARLDataStream::write_record(const ARLRecordHeader& rheader,
}
else
{
lastValue = record[x][y - 1];
lastValue = record[size_t(x)][size_t(y - 1)];
}
}
// Calculate the packed value
float unpackedValue = record[x][y];
int packedInt = (int)((unpackedValue - lastValue) * scaleFactor);
int packedInt = int((unpackedValue - lastValue) * scaleFactor);
unsigned char packedValue = (unsigned char)(packedInt + 127);
......
......@@ -110,9 +110,9 @@ struct RADIX_PUBLIC ARLIndexHeader
int z_flag;
int lenh;
std::vector<float> levels;
std::vector<int> nVarsAtLevels;
std::vector<std::vector<std::string>> varNames;
std::vector<std::vector<int>> checkSums;
std::vector<int> num_vars_at_levels;
std::vector<std::vector<std::string>> var_names;
std::vector<std::vector<int>> check_sums;
bool prime;
bool latlon;
......
......@@ -471,41 +471,42 @@ int main(int argc, char **argv)
thisRecordHeader.prec = 0.f;
thisRecordHeader.var1 = 0.f;
ARLIndexHeader thisIndexHeader;
thisIndexHeader.model_id = "NFDB";
thisIndexHeader.icx = 0;
thisIndexHeader.mn = 0;
thisIndexHeader.pole_lat = centreLat;
thisIndexHeader.pole_lon = centreLon;
thisIndexHeader.ref_lat = centreLat;
thisIndexHeader.ref_lon = centreLon;
thisIndexHeader.size = resolution;
thisIndexHeader.orient = 0.f;
thisIndexHeader.tang_lat = centreLat;
thisIndexHeader.sync_xp = (numberGridCells + 1) / 2;
thisIndexHeader.sync_yp = (numberGridCells + 1) / 2;
thisIndexHeader.sync_lat = centreLat;
thisIndexHeader.sync_lon = centreLon;
thisIndexHeader.dummy = 0.f;
thisIndexHeader.nx = numberGridCells;
thisIndexHeader.ny = numberGridCells;
thisIndexHeader.nz = inputPressures.size();
thisIndexHeader.z_flag = 2;
thisIndexHeader.lenh = numberGridCells * numberGridCells;
thisIndexHeader.levels = inputPressures;
thisIndexHeader.nVarsAtLevels = std::vector<int>(inputPressures.size(), 5);
thisIndexHeader.model_id = "NFDB";
thisIndexHeader.icx = 0;
thisIndexHeader.mn = 0;
thisIndexHeader.pole_lat = centreLat;
thisIndexHeader.pole_lon = centreLon;
thisIndexHeader.ref_lat = centreLat;
thisIndexHeader.ref_lon = centreLon;
thisIndexHeader.size = resolution;
thisIndexHeader.orient = 0.f;
thisIndexHeader.tang_lat = centreLat;
thisIndexHeader.sync_xp = (numberGridCells + 1) / 2;
thisIndexHeader.sync_yp = (numberGridCells + 1) / 2;
thisIndexHeader.sync_lat = centreLat;
thisIndexHeader.sync_lon = centreLon;
thisIndexHeader.dummy = 0.f;
thisIndexHeader.nx = numberGridCells;
thisIndexHeader.ny = numberGridCells;
thisIndexHeader.nz = inputPressures.size();
thisIndexHeader.z_flag = 2;
thisIndexHeader.lenh = numberGridCells * numberGridCells;
thisIndexHeader.levels = inputPressures;
thisIndexHeader.num_vars_at_levels =
std::vector<int>(inputPressures.size(), 5);
std::vector<std::string> surfaceVarNames = {"PRSS", "TEMP", "RELH", "UWND",
"VWND"};
std::vector<std::string> varNames = {"HGTS", "TEMP", "RELH", "UWND",
"VWND"};
thisIndexHeader.varNames.push_back(surfaceVarNames);
thisIndexHeader.var_names.push_back(surfaceVarNames);
for (size_t level = 1; level < inputPressures.size(); ++level)
{
thisIndexHeader.varNames.push_back(varNames);
thisIndexHeader.var_names.push_back(varNames);
}
std::vector<int> checkSums = std::vector<int>(5, 0);
for (size_t level = 0; level < inputPressures.size(); ++level)
{
thisIndexHeader.checkSums.push_back(checkSums);
thisIndexHeader.check_sums.push_back(checkSums);
}
// Write the headers
......
......@@ -261,6 +261,12 @@ double RADIX_PUBLIC gammaRayAttenuationInAir(double energy,
*/
double RADIX_PUBLIC exponentialIntegral(double d);
/**
* @brief roundUpInt Round value up to nearest multiple
* @param value
* @param multiple
* @return int
*/
inline int RADIX_PUBLIC roundUpInt(int value, int multiple)
{
if (multiple == 0)
......@@ -272,6 +278,12 @@ inline int RADIX_PUBLIC roundUpInt(int value, int multiple)
return (remainder == 0 ? value : value + multiple - remainder);
}
/**
* @brief roundDownInt Round value down to the nearest multiple
* @param value
* @param multiple
* @return int
*/
inline int RADIX_PUBLIC roundDownInt(int value, int multiple)
{
if (multiple == 0)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment