Skip to content

Commit

Permalink
Adding processing to MetOceanData to create netCDF CRMS database
Browse files Browse the repository at this point in the history
  • Loading branch information
zcobell committed Jun 29, 2019
1 parent a69241e commit ca72abd
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 48 deletions.
2 changes: 2 additions & 0 deletions MetOceanData/MetOceanData.pro
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ CONFIG += c++11 console
CONFIG -= app_bundle
TARGET = MetOceanData

INCLUDEPATH += $$PWD/../thirdparty/boost_1_67_0

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
Expand Down
11 changes: 8 additions & 3 deletions MetOceanData/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@ int main(int argc, char *argv[]) {

option->processOptions();
Options::CommandLineOptions opt = option->getCommandLineOptions();
MetOceanData *d =
new MetOceanData(opt.service, opt.station, opt.product, opt.datum,
opt.startDate, opt.endDate, opt.outputFile, &a);
MetOceanData *d;
if (opt.doCrms) {
d = new MetOceanData(opt.crms, opt.outputFile, &a);
} else {
d = new MetOceanData(opt.service, opt.station, opt.product, opt.datum,
opt.startDate, opt.endDate, opt.outputFile, &a);
}
d->setLoggingActive();
QObject::connect(d, SIGNAL(finished()), &a, SLOT(quit()));
QTimer::singleShot(0, d, SLOT(run()));

return a.exec();
}
85 changes: 60 additions & 25 deletions MetOceanData/metoceandata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <algorithm>
#include <iostream>
#include "constants.h"
#include "crmsdatabase.h"
#include "generic.h"
#include "hmdf.h"
#include "ndbcdata.h"
Expand All @@ -34,16 +35,17 @@ static const QHash<int, QString> noaaProducts = {
{4, "air_temperature"}, {5, "water_temperature"}, {6, "wind:speed"},
{7, "humidity"}, {8, "air_pressure"}, {9, "wind:direction"},
{10, "wind:gusts"}};
static const QHash<int, QString> noaaProductLongName = {{1, "6 minute water level"},
{2, "Hourly water level"},
{3, "Tide predictoins"},
{4, "Air temperature"},
{5, "Water temperature"},
{6, "Wind speed"},
{7, "Humidity"},
{8, "Air pressure"},
{9, "Wind direction"},
{10, "Wind gusts"}};
static const QHash<int, QString> noaaProductLongName = {
{1, "6 minute water level"},
{2, "Hourly water level"},
{3, "Tide predictoins"},
{4, "Air temperature"},
{5, "Water temperature"},
{6, "Wind speed"},
{7, "Humidity"},
{8, "Air pressure"},
{9, "Wind direction"},
{10, "Wind gusts"}};
static const QHash<int, QString> noaaUnits = {
{1, "m"}, {2, "m"}, {3, "m"}, {4, "C"}, {5, "C"},
{6, "m/s"}, {7, "%"}, {8, "mb"}, {9, "deg"}, {10, "m/s"}};
Expand All @@ -59,6 +61,8 @@ MetOceanData::MetOceanData(QObject *parent) : QObject(parent) {
this->m_startDate = QDateTime();
this->m_endDate = QDateTime();
this->m_outputFile = QString();
this->m_doCrms = false;
this->m_crmsFile = QString();
}

MetOceanData::MetOceanData(serviceTypes service, QStringList station,
Expand All @@ -73,6 +77,22 @@ MetOceanData::MetOceanData(serviceTypes service, QStringList station,
this->m_startDate = startDate;
this->m_endDate = endDate;
this->m_outputFile = outputFile;
this->m_doCrms = false;
this->m_crmsFile = QString();
}

MetOceanData::MetOceanData(QString crmsFile, QString outputFile,
QObject *parent)
: QObject(parent) {
this->m_service = 0;
this->m_product = 0;
this->m_datum = 0;
this->m_station = QStringList();
this->m_startDate = QDateTime();
this->m_endDate = QDateTime();
this->m_outputFile = outputFile;
this->m_doCrms = true;
this->m_crmsFile = crmsFile;
}

int MetOceanData::service() const { return m_service; }
Expand Down Expand Up @@ -137,23 +157,27 @@ void MetOceanData::showStatus(QString message, int pct) {
}

void MetOceanData::run() {
if (this->service() == USGS && this->m_station.length() > 1) {
emit error(
"Beacuase each station has different characteristics, only one "
"USGS station may be selected at a time.");
emit finished();
if (this->m_doCrms) {
this->processCrmsData();
return;
}

if (this->service() == NOAA)
this->getNoaaData();
else if (this->service() == USGS)
this->getUsgsData();
else if (this->service() == NDBC)
this->getNdbcData();
else if (this->service() == XTIDE)
this->getXtideData();
} else {
if (this->service() == USGS && this->m_station.length() > 1) {
emit error(
"Beacuase each station has different characteristics, only one "
"USGS station may be selected at a time.");
emit finished();
return;
}

if (this->service() == NOAA)
this->getNoaaData();
else if (this->service() == USGS)
this->getUsgsData();
else if (this->service() == NDBC)
this->getNdbcData();
else if (this->service() == XTIDE)
this->getXtideData();
}
emit finished();
return;
}
Expand Down Expand Up @@ -502,3 +526,14 @@ QString MetOceanData::noaaIndexToUnits() { return noaaUnits[this->m_product]; }
int MetOceanData::getDatum() const { return m_datum; }

void MetOceanData::setDatum(int datum) { m_datum = datum; }

void MetOceanData::processCrmsData() {
CrmsDatabase *d = new CrmsDatabase(this->m_crmsFile.toStdString(),
this->m_outputFile.toStdString(), this);
connect(d, SIGNAL(error(QString)), this, SLOT(showError(QString)));
d->setShowProgressBar(true);
d->parse();
emit finished();
delete d;
return;
}
10 changes: 8 additions & 2 deletions MetOceanData/metoceandata.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class MetOceanData : public QObject {
explicit MetOceanData(serviceTypes service, QStringList station, int product,
int datum, QDateTime startDate, QDateTime endDate,
QString outputFile, QObject *parent = nullptr);
explicit MetOceanData(QString crmsFile, QString outputFile,
QObject *parent = nullptr);

static QStringList selectStations(serviceTypes service, double x1, double y1,
double x2, double y2);
Expand Down Expand Up @@ -62,9 +64,10 @@ class MetOceanData : public QObject {
int getDatum() const;
void setDatum(int datum);

static StationLocations::MarkerType serviceToMarkerType(MetOceanData::serviceTypes type);
static StationLocations::MarkerType serviceToMarkerType(
MetOceanData::serviceTypes type);
static bool findStation(QStringList name, StationLocations::MarkerType type,
QVector<Station> &s);
QVector<Station> &s);

signals:
void finished();
Expand All @@ -83,20 +86,23 @@ class MetOceanData : public QObject {
void getUsgsData();
void getNdbcData();
void getXtideData();
void processCrmsData();

QString noaaIndexToProduct();
QString noaaIndexToDatum();
QString noaaIndexToUnits();

int printAvailableProducts(Hmdf *data);

bool m_doCrms;
int m_service;
QStringList m_station;
int m_product;
int m_datum;
QDateTime m_startDate;
QDateTime m_endDate;
QString m_outputFile;
QString m_crmsFile;
};

#endif // DRIVER_H
23 changes: 20 additions & 3 deletions MetOceanData/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,25 @@ void Options::addOptions() {
<< m_serviceType << m_stationId << m_boundingBox
<< m_nearest << m_startDate << m_endDate
<< m_product << m_outputFile << m_datum << m_list
<< m_show);
<< m_show << m_crmsSourceFile);
}

Options::CommandLineOptions Options::getCommandLineOptions() {
Options::CommandLineOptions opt;

opt.doCrms = false;

if (this->parser()->isSet(m_crmsSourceFile)) {
if (!this->parser()->isSet(m_outputFile)) {
std::cerr << "No output file set." << std::endl;
this->parser()->showHelp(1);
}
opt.crms = this->parser()->value(m_crmsSourceFile);
opt.outputFile = this->parser()->value(m_outputFile);
opt.doCrms = true;
return opt;
}

std::vector<bool> inputOptions;
inputOptions.push_back(this->parser()->isSet(m_stationId));
inputOptions.push_back(this->parser()->isSet(m_boundingBox));
Expand Down Expand Up @@ -238,8 +251,12 @@ void Options::printStationList(QStringList station,
MetOceanData::serviceToMarkerType(markerType), s);
for (size_t i = 0; i < station.size(); ++i) {
std::cout << s[i].id().toStdString() << ",'"
<< s[i].name().replace(" ", "_").replace(",","_").replace("__","_").toStdString() << "',"
<< s[i].coordinate().longitude() << ","
<< s[i].name()
.replace(" ", "_")
.replace(",", "_")
.replace("__", "_")
.toStdString()
<< "'," << s[i].coordinate().longitude() << ","
<< s[i].coordinate().latitude() << std::endl;
std::cout.flush();
}
Expand Down
2 changes: 2 additions & 0 deletions MetOceanData/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class Options : public QObject {
QDateTime endDate;
QString outputFile;
QStringList station;
bool doCrms;
QString crms;
};

void processOptions();
Expand Down
7 changes: 6 additions & 1 deletion MetOceanData/optionslist.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ static const QCommandLineOption m_outputFile =
QCommandLineOption(QStringList() << "o"
<< "output",
"Name of the output file. Format will be guessed from "
"extension (.imeds or .nc)",
"extension (.imeds or .nc). Note if performing crms "
"processing, the output extension will always be *.nc",
"filename");

static const QCommandLineOption m_boundingBox =
Expand All @@ -96,4 +97,8 @@ static const QCommandLineOption m_show =
"Show the stations that would be selected given the "
"provided criteria and exit.");

static const QCommandLineOption m_crmsSourceFile = QCommandLineOption(
QStringList() << "crms", "Converts a CRMS database into netCDF format",
"filename");

#endif // OPTIONSLIST_H
18 changes: 9 additions & 9 deletions libraries/libmetocean/crmsdatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ CrmsDatabase::CrmsDatabase(const std::string &datafile,
m_hasError(false),
m_showProgressBar(false),
m_progressbar(nullptr),
m_previousPercentComplete(0),
QObject(parent) {}

float CrmsDatabase::fillValue() const { return -9999.0f; }

double CrmsDatabase::getPercentComplete() {
size_t fileposition = static_cast<size_t>(this->m_file.tellg());
double percent =
Expand All @@ -55,18 +54,19 @@ double CrmsDatabase::getPercentComplete() {
100.0;
emit this->percentComplete(static_cast<int>(std::floor(percent)));
if (this->m_showProgressBar) {
unsigned long dt =
static_cast<unsigned long>(std::floor(std::round(percent))) -
this->m_previousPercentComplete;
*(this->m_progressbar) += dt;
this->m_previousPercentComplete += dt;
unsigned long dt = static_cast<unsigned long>(std::floor(percent)) -
this->m_previousPercentComplete;
if (dt > 0) {
*(this->m_progressbar) += dt;
this->m_previousPercentComplete += dt;
}
}
return percent;
}

void CrmsDatabase::parse() {
if (!this->fileExists(this->m_databaseFile)) {
emit error();
emit error("File does not exist");
emit complete();
return;
}
Expand Down Expand Up @@ -103,7 +103,7 @@ void CrmsDatabase::parse() {
this->closeCrmsFile();

if (this->m_hasError) {
emit error();
emit error("Error during CRMS processing");
emit this->percentComplete(0);
} else {
emit success();
Expand Down
11 changes: 6 additions & 5 deletions libraries/libmetocean/crmsdatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------*/
#ifndef READCRMSDATABASE_H
#define READCRMSDATABASE_H
#ifndef CRMSDATABASE_H
#define CRMSDATABASE_H

#include <QDateTime>
#include <QObject>
Expand All @@ -39,14 +39,16 @@ class CrmsDatabase : public QObject {
bool showProgressBar() const;
void setShowProgressBar(bool showProgressBar);

static constexpr float fillValue() { return -9999.0f; }

public slots:
void parse();

signals:
void percentComplete(int);
void complete();
void success();
void error();
void error(QString);

private:
struct Position {
Expand Down Expand Up @@ -74,7 +76,6 @@ class CrmsDatabase : public QObject {
void initializeOutputFile();
void closeOutputFile(size_t numStations);
bool fileExists(const std::string &filename);
float fillValue() const;
void exitCleanly();

std::string m_databaseFile;
Expand All @@ -93,4 +94,4 @@ class CrmsDatabase : public QObject {
size_t m_fileLength;
};

#endif // READCRMSDATABASE_H
#endif // CRMSDATABASE_H

0 comments on commit ca72abd

Please sign in to comment.