Commit 5f76c673 authored by Purves, Murray's avatar Purves, Murray
Browse files

Changing precision of ARL met data to float

parent e61d19df
Pipeline #16670 passed with stages
in 16 minutes and 40 seconds
......@@ -90,6 +90,8 @@ ARLDataStream::ARLDataStream(const std::string& file,
bool ARLDataStream::read_index_header(ARLIndexHeader& iheader)
{
bool result = false;
std::string headerString =
p->stream->readString(ARLDataStream::PImpl::indexHeaderLength);
expand(headerString, iheader);
......@@ -102,11 +104,15 @@ bool ARLDataStream::read_index_header(ARLIndexHeader& iheader)
radix_line(" scaling exponent = " << iheader.nexp);
radix_line(" initial value = " << iheader.var1);
radix_line(" header as string:\n " << headerString);
return true;
result = true;
return result;
}
bool ARLDataStream::write_index_header(const ARLIndexHeader& iheader)
{
bool result = false;
radix_line("Write index header: year = " << iheader.year);
radix_line(" month = " << iheader.month);
radix_line(" day = " << iheader.day);
......@@ -126,7 +132,8 @@ bool ARLDataStream::write_index_header(const ARLIndexHeader& iheader)
radix_line(" header as string:\n " << indexHeader);
return true;
result = true;
return result;
}
bool ARLDataStream::read_next_index_header(ARLIndexHeader& iheader)
......@@ -143,6 +150,8 @@ bool ARLDataStream::read_next_index_header(ARLIndexHeader& iheader)
bool ARLDataStream::read_record_header(const ARLIndexHeader& iheader,
ARLRecordHeader& rheader)
{
bool result = false;
std::string headerString =
p->stream->readString(ARLDataStream::PImpl::recordHeaderLength);
expand(headerString, iheader, rheader);
......@@ -157,12 +166,16 @@ bool ARLDataStream::read_record_header(const ARLIndexHeader& iheader,
radix_line(" nz = " << rheader.nz);
radix_line(" Size of each record = " << p->recordSize);
radix_line(" header as string:\n " << headerString);
return true;
result = true;
return result;
}
bool ARLDataStream::write_record_header(const ARLIndexHeader& iheader,
const ARLRecordHeader& rheader)
{
bool result = false;
// Calculate and save record size
p->recordSize =
(rheader.nx * rheader.ny) + ARLDataStream::PImpl::indexHeaderLength;
......@@ -195,23 +208,26 @@ bool ARLDataStream::write_record_header(const ARLIndexHeader& iheader,
radix_line(" header as string:\n " << recordHeader);
return true;
result = true;
return result;
}
bool ARLDataStream::read_record(const ARLIndexHeader& iheader,
const ARLRecordHeader& rheader,
std::vector<std::vector<double> >& record)
std::vector<std::vector<float> >& record)
{
bool result = false;
// Set up the vector size
record.clear();
record.resize(rheader.nx);
for (std::vector<double>& vec : record)
for (std::vector<float>& vec : record)
{
vec.resize(rheader.ny);
}
// Calculate the scaling factor
double scaleFactor = pow(2.0, 7.0 - (double)iheader.nexp), lastValue = 0.0;
float scaleFactor = pow(2.0, 7.0 - (float)iheader.nexp), lastValue = 0.0;
radix_line(" Reading record data:");
for (int y = 0; y < rheader.ny; ++y)
......@@ -223,7 +239,7 @@ bool ARLDataStream::read_record(const ARLIndexHeader& iheader,
int packedValue = ch - 127;
// Calculate the unpacked value
double unpackedValue = 0.0;
float unpackedValue = 0.0;
// Get the correct 'last value' if we are at a 0 index
if (x == 0)
{
......@@ -249,13 +265,16 @@ bool ARLDataStream::read_record(const ARLIndexHeader& iheader,
}
}
return true;
result = true;
return result;
}
bool ARLDataStream::write_record(
const ARLIndexHeader& iheader, const ARLRecordHeader& rheader,
const std::vector<std::vector<double> >& record)
bool ARLDataStream::write_record(const ARLIndexHeader& iheader,
const ARLRecordHeader& rheader,
const std::vector<std::vector<float> >& record)
{
bool result = false;
if (record.size() == 0)
{
radix_line("No data here - returning false");
......@@ -263,7 +282,7 @@ bool ARLDataStream::write_record(
}
// Calculate the scaling factor
double scaleFactor = pow(2.0, 7.0 - (double)iheader.nexp), lastValue = 0.0;
float scaleFactor = pow(2.0, 7.0 - (float)iheader.nexp), lastValue = 0.0;
radix_line(" Writing record data:");
for (int y = 0; y < rheader.ny; ++y)
......@@ -284,8 +303,8 @@ bool ARLDataStream::write_record(
}
// Calculate the packed value
double unpackedValue = record[x][y];
int packedInt = (int)((unpackedValue - lastValue) * scaleFactor);
float unpackedValue = record[x][y];
int packedInt = (int)((unpackedValue - lastValue) * scaleFactor);
unsigned char packedValue = (unsigned char)(packedInt + 127);
......@@ -301,7 +320,8 @@ bool ARLDataStream::write_record(
}
}
return true;
result = true;
return result;
}
void ARLDataStream::close_stream() { p->stream->close(); }
......
......@@ -75,12 +75,12 @@ class RADIX_PUBLIC ARLDataStream
*/
bool read_record(const ARLIndexHeader& iheader,
const ARLRecordHeader& rheader,
std::vector<std::vector<double> >& record);
std::vector<std::vector<float> >& record);
// TODO
bool write_record(const ARLIndexHeader& iheader,
const ARLRecordHeader& rheader,
const std::vector<std::vector<double> >& record);
const std::vector<std::vector<float> >& record);
void close_stream();
......
......@@ -9,7 +9,7 @@ using namespace radix;
TEST(RadixIO, ReadArlElements)
{
double tolerance = 0.001;
float tolerance = 0.001;
std::string testFile = "RP195111.gbl";
ARLDataStream testStream(testFile, std::ios::in);
......@@ -44,7 +44,7 @@ TEST(RadixIO, ReadArlElements)
EXPECT_EQ(0, testIndexHeader.hour);
EXPECT_STREQ("PRSS", testIndexHeader.kvar.c_str());
// Read data
std::vector<std::vector<double>> testRecord;
std::vector<std::vector<float>> testRecord;
success =
testStream.read_record(testIndexHeader, testRecordHeader, testRecord);
// Test content
......@@ -90,7 +90,7 @@ TEST(RadixIO, ReadArlElements)
TEST(RadixIO, WriteArlElements)
{
double tolerance = 0.001;
float tolerance = 0.001;
std::string testReadFile = "RP195111.gbl";
ARLDataStream setupStream(testReadFile, std::ios::in);
......@@ -105,7 +105,7 @@ TEST(RadixIO, WriteArlElements)
ARLIndexHeader setupIndexHeader;
success = setupStream.read_next_index_header(setupIndexHeader);
ASSERT_TRUE(success);
std::vector<std::vector<double>> setupRecord;
std::vector<std::vector<float>> setupRecord;
success =
setupStream.read_record(setupIndexHeader, setupRecordHeader, setupRecord);
ASSERT_TRUE(success);
......@@ -138,7 +138,7 @@ TEST(RadixIO, WriteArlElements)
ARLIndexHeader testIndexHeader;
success = testReadStream.read_next_index_header(testIndexHeader);
ASSERT_TRUE(success);
std::vector<std::vector<double>> testRecord;
std::vector<std::vector<float>> testRecord;
success =
testReadStream.read_record(testIndexHeader, testRecordHeader, testRecord);
ASSERT_TRUE(success);
......
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