Commit cb5d4971 authored by Purves, Murray's avatar Purves, Murray
Browse files

Added basic interpolation to radixarl2snd;

parent 00d01b25
Pipeline #16719 passed with stages
in 17 minutes and 14 seconds
......@@ -16,6 +16,117 @@
using namespace radix;
/**
* @brief interpolateValues Interpolate/extrapolate missing values from a data
* vector Basic capability for now - linear interpolation, extrapolation assumes
* constant from ends.
* @param pressureValues Pressure vector for meteorology - requires all values
* to be present. Used for determining 'interpolation bounds' (there's
* definitely a better phrase for this)
* @param valuesToInterpolate
*/
bool interpolateValues(const std::vector<float> &pressureValues,
std::vector<float> &valuesToInterpolate)
{
float missingValue = -9999.f;
// Ensure pressure and other vector are same length for interpolation
if (pressureValues.size() != valuesToInterpolate.size())
{
std::cout << "Error! Pressure vector is not same size ("
<< pressureValues.size() << ") as vector to interpolate ("
<< valuesToInterpolate.size() << ") - can't use to interpolate"
<< std::endl;
return false;
}
// Ensure we have no missing values in the pressure dataset
for (float pressure : pressureValues)
{
if (pressure == -9999.f)
{
std::cout << "Error! Pressure vector contains " << missingValue
<< " values - can't use to interpolate" << std::endl;
return false;
}
}
// Write out initial values
std::cout << "Interpolating " << valuesToInterpolate.size()
<< " values; initial:" << std::endl
<< " ";
for (float f : valuesToInterpolate)
{
std::cout << f << " ";
}
std::cout << std::endl;
// Loop through vector first to find first/last non-missing indices
size_t firstIndex = valuesToInterpolate.size(), lastIndex = 0;
bool foundFirst = false;
for (size_t i = 0; i < valuesToInterpolate.size(); ++i)
{
if (valuesToInterpolate[i] != missingValue)
{
if (!foundFirst)
{
firstIndex = i;
foundFirst = true;
}
lastIndex = i;
}
}
// Fill in values before first and after last indices
std::cout << " Found first (" << firstIndex << ") and last (" << lastIndex
<< ") non-missing indices; filling in values before & after..."
<< std::endl;
for (size_t i = 0; i < firstIndex; ++i)
{
valuesToInterpolate[i] = valuesToInterpolate[firstIndex];
}
for (size_t i = lastIndex + 1; i < valuesToInterpolate.size(); ++i)
{
valuesToInterpolate[i] = valuesToInterpolate[lastIndex];
}
// Fill in missing values in central parts of vector
std::cout << " Filling in missing data in central part of vector..."
<< std::endl;
for (size_t i = firstIndex + 1; i < lastIndex; ++i)
{
// Search for a missing value
size_t lastGood = i - 1;
if (valuesToInterpolate[i] == missingValue)
{
// Get the next good value
while (valuesToInterpolate[i] == missingValue)
{
i++;
}
size_t nextGood = i;
// Interpolate between the two good values
for (size_t j = lastGood + 1; j < nextGood; ++j)
{
valuesToInterpolate[j] =
valuesToInterpolate[lastGood] +
((valuesToInterpolate[nextGood] - valuesToInterpolate[lastGood]) *
((pressureValues[j] - pressureValues[lastGood]) /
(pressureValues[nextGood] - pressureValues[lastGood])));
}
}
}
// Write out final values
std::cout << "Interpolation complete; final:" << std::endl << " ";
for (float f : valuesToInterpolate)
{
std::cout << f << " ";
}
std::cout << std::endl;
return true;
}
int main(int argc, char **argv)
{
std::cout << "************************" << std::endl;
......@@ -281,6 +392,20 @@ int main(int argc, char **argv)
std::cout << "Data read complete: " << inputPressures.size()
<< " entries read." << std::endl;
// Interpolate the data to remove missing values
std::cout << "Interpolating values to remove missing data..." << std::endl;
std::cout << "Height:" << std::endl;
interpolateValues(inputPressures, inputHeights);
std::cout << "Temperature:" << std::endl;
interpolateValues(inputPressures, inputTemps);
std::cout << "Relative humidity:" << std::endl;
interpolateValues(inputPressures, inputRelHums);
std::cout << "Wind speed:" << std::endl;
interpolateValues(inputPressures, inputWSpds);
std::cout << "Wind direction:" << std::endl;
interpolateValues(inputPressures, inputWDirs);
std::cout << "Interpolation complete." << std::endl;
// Convert the data into ARL format
std::cout << "Converting data to ARL format..." << std::endl;
ARLDataStream outputStream(outputArlPath, std::ios::out);
......
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