Commit 53f22492 authored by Norby, Tom's avatar Norby, Tom

Using (S)OLID reduce utility methods to a single responsibility.

parent b78013e1
Pipeline #112019 failed with stages
in 6 minutes and 26 seconds
......@@ -17,15 +17,35 @@ int main(int argc, char **argv)
return 1;
}
const char *fi[2] = {argv[1], argv[2]};
double time = atof(argv[3]);
const char *inputFile = argv[1];
const char *outputFile = argv[2];
double time = atof(argv[3]);
bool success = haem::cnormal(fi[0], fi[1], float(time));
if (!success)
// read input cdump
HysplitCDump::SP cdump = std::make_shared<HysplitCDump>();
HysplitCDumpStream<HysplitCDump::SP> stream(cdump);
if (!stream.read_from(inputFile))
{
std::cerr << "ERROR occured in cnormal()" << std::endl;
std::cerr << "ERROR: Could not open file: " << inputFile << std::endl;
return 1;
}
if (time <= 0.f)
{
std::cerr << "ERROR: time to normalize to should be positive" << std::endl;
return 1;
}
// normalize cdump
haem::cnormal(cdump, float(time));
// write cdump to output
if (!stream.write_to(outputFile))
{
std::cerr << "ERROR: Could not write file: " << outputFile << std::endl;
return 1;
}
std::cout << "cdump normalized" << std::endl;
return 0;
}
......@@ -18,15 +18,37 @@ int main(int argc, char **argv)
return 1;
}
const char *fi[2] = {argv[1], argv[2]};
int species = atoi(argv[3]);
const char *inputfile = argv[1];
const char *outputfile = argv[2];
int species = atoi(argv[3]);
HysplitCDump::SP cdumpin, cdumpout;
bool success = haem::speciesSelect(fi[0], fi[1], species);
if (!success)
// read in source dump
cdumpin = std::make_shared<HysplitCDump>();
HysplitCDumpStream<HysplitCDump::SP> streamin(cdumpin);
if (!streamin.read_from(inputfile))
{
std::cerr << "ERROR occured in speciesSelect()" << std::endl;
std::cerr << "ERROR: Could not open file: " << inputfile << std::endl;
return 1;
}
if (species < 0)
{
std::cerr << "ERROR: selected species should be non-negative" << std::endl;
return 1;
}
// obtain the user selected species
cdumpout = haem::speciesSelect(cdumpin, species);
// write output dump
HysplitCDumpStream<HysplitCDump::SP> streamout(cdumpout);
if (!streamout.write_to(outputfile))
{
std::cerr << "ERROR: Could not write file: " << outputfile << std::endl;
return 1;
}
std::cout << "Species written to output" << std::endl;
return 0;
}
#include "radixio/hysplitcdump.hh"
#include "haemmodel/concutil.hh"
static time_t hourDiff(struct tm *laterTime, struct tm *earlierTime)
......@@ -9,26 +7,8 @@ static time_t hourDiff(struct tm *laterTime, struct tm *earlierTime)
return (later - earlier) / 60 / 60;
}
bool haem::cnormal(const char *inputfile, const char *outputfile,
float normalTime)
void haem::cnormal(HysplitCDump::SP cdump, float normalTime)
{
radix::HysplitCDump::SP cdump;
cdump = std::make_shared<radix::HysplitCDump>();
radix::HysplitCDumpStream<radix::HysplitCDump::SP> stream(cdump);
if (!stream.read_from(inputfile))
{
std::cerr << "ERROR: Could not open file: " << inputfile << std::endl;
return false;
}
if (normalTime <= 0.f)
{
std::cerr << "ERROR: time to normalize to should be positive" << std::endl;
return false;
}
struct tm st = {};
int forecast;
cdump->startTime(0, st.tm_year, st.tm_mon, st.tm_mday, st.tm_hour, st.tm_min,
......@@ -71,36 +51,11 @@ bool haem::cnormal(const char *inputfile, const char *outputfile,
} // for each altitude
} // for each pollutant
} // for each time
if (!stream.write_to(outputfile))
{
std::cerr << "ERROR: Could not write file: " << outputfile << std::endl;
return false;
}
return true;
}
bool haem::speciesSelect(const char *inputfile, const char *outputfile,
int speciesSelect)
HysplitCDump::SP haem::speciesSelect(HysplitCDump::SP cdumpin, int species)
{
radix::HysplitCDump::SP cdumpin, cdumpout;
cdumpin = std::make_shared<radix::HysplitCDump>();
radix::HysplitCDumpStream<radix::HysplitCDump::SP> streamin(cdumpin);
if (!streamin.read_from(inputfile))
{
std::cerr << "ERROR: Could not open file: " << inputfile << std::endl;
return false;
}
if (speciesSelect < 0)
{
std::cerr << "ERROR: selected species should be non-negative" << std::endl;
return false;
}
cdumpout = std::make_shared<radix::HysplitCDump>();
HysplitCDump::SP cdumpout = std::make_shared<HysplitCDump>();
{
cdumpout->setId(cdumpin->id());
cdumpout->setNumLat(cdumpin->numLat());
......@@ -129,31 +84,24 @@ bool haem::speciesSelect(const char *inputfile, const char *outputfile,
{
cdumpout->addLevel(cdumpin->level(li));
}
cdumpout->addPollutant(cdumpin->pollutant(speciesSelect));
cdumpout->addPollutant(cdumpin->pollutant(species));
} // prepare cdumpout - especially for setNumNonZeroPoints()
for (int ti = 0; ti < cdumpin->numEndTimes(); ++ti)
{
for (int li = 0; li < cdumpin->numLevels(); ++li)
{
int nnzp = cdumpin->numNonZeroPoints(ti, speciesSelect, li);
int nnzp = cdumpin->numNonZeroPoints(ti, species, li);
cdumpout->setNumNonZeroPoints(ti, 0, li, nnzp);
for (int nzi = 0; nzi < nnzp; nzi++)
{
float conc;
short xi, yi;
cdumpin->point(ti, speciesSelect, li, nzi, xi, yi, conc);
cdumpin->point(ti, species, li, nzi, xi, yi, conc);
cdumpout->setPoint(ti, 0, li, nzi, xi, yi, conc);
} // for each non zero point
} // for each altitude
} // for each time
radix::HysplitCDumpStream<radix::HysplitCDump::SP> streamout(cdumpout);
if (!streamout.write_to(outputfile))
{
std::cerr << "ERROR: Could not write file: " << outputfile << std::endl;
return false;
}
return true;
return cdumpout;
}
......@@ -2,14 +2,15 @@
#define HAEM_HAEMMODEL_CNORMAL_HH
#include "radixcore/visibility.hh"
#include "radixio/hysplitcdump.hh"
using namespace radix;
namespace haem
{
RADIX_PUBLIC bool cnormal(const char *inputfile, const char *outputfile,
float normalTime);
RADIX_PUBLIC void cnormal(HysplitCDump::SP cdump, float normalTime);
RADIX_PUBLIC bool speciesSelect(const char *inputfile, const char *outputfile,
int speciesSelect);
RADIX_PUBLIC HysplitCDump::SP speciesSelect(HysplitCDump::SP cdumpin,
int species);
}; // namespace haem
#endif // HAEM_HAEMMODEL_CNORMAL_HH
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