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

#13. Starting on hysplit cdump stream interface.

parent bf03f0cd
Pipeline #10614 passed with stage
in 3 minutes and 43 seconds
......@@ -18,6 +18,8 @@ SET(HEADERS
endian.hh
gfsfile.hh
hcdump.hh
hysplitcdump.hh
hysplitcdump.i.hh
)
#
......
......@@ -320,27 +320,10 @@ void HCDump::reload()
mConcList[mConcList.size()-1][i][j].resize(nxyp);
for(int np = 0; np < nxyp; ++np)
{
// rstream >> mXList[mXList.size()-1][i][j][np]
// >> mYList[mYList.size()-1][i][j][np]
// >> mConcList[mConcList.size()-1][i][j][np];
mXList[mXList.size()-1][i][j][np] = rstream.readShort();
mYList[mYList.size()-1][i][j][np] = rstream.readShort();
mConcList[mConcList.size()-1][i][j][np] = rstream.readFloat();
}
// if(nxyp > 0)
// {
// float sum = std::accumulate(mConcList[mConcList.size()-1][i][j].begin()
// , mConcList[mConcList.size()-1][i][j].end()
// , 0.f);
// radix_line(time1.year << " " << time1.month << " " << time1.day << " "
// << time1.hour << " " << time1.minutes << " " << time1.forecast
// << " " << pollutant << " " << z << " " << nxyp << " "
// << mXList[mXList.size()-1][i][j][nxyp-1] << " "
// << mYList[mXList.size()-1][i][j][nxyp-1] << " "
// << mConcList[mConcList.size()-1][i][j][nxyp-1]
// << " sum:" << sum);
// total += sum;
// }
} // if packing is enabled
rstream.readFooter();
rstream.peek(); // peek ahead to check file integrity
......
#ifndef RADIX_RADIXIO_HH_
#define RADIX_RADIXIO_HH_
#ifndef RADIX_RADIXIO_HCDUMP_HH_
#define RADIX_RADIXIO_HCDUMP_HH_
#include <string>
#include <sstream>
......@@ -144,4 +144,4 @@ public:
} // namespace radix
#endif /** RADIX_RADIXIO_HH_ */
#endif /** RADIX_RADIXIO_HCDUMP_HH_ */
#ifndef RADIX_RADIXIO_HYSPLITCDUMP_HH_
#define RADIX_RADIXIO_HYSPLITCDUMP_HH_
#include <string>
#include <sstream>
#include <vector>
#include "radixdl/visibility.hh"
namespace radix
{
/**
* @class HysplitCDump
* @brief HYSPLIT Concentration file interface
*/
template<typename data_type>
class RADIX_PUBLIC HysplitCDump
{
private:
data_type mData;
public:
HysplitCDump(data_type container);
/**
* @brief read_from a hysplit concentration file
* @param file Path to hysplit concentration file
*
* Requires data_type to have the following methods:
* setId(const std::string&)
* setDateTime(int year, int month, int day, int hour, int minute)
* setForcastHour(int)
* addLocation(int year, int month, int day, int hour, float lat, float lon, float z, int minutes)
* setNumLat(float)
* setNumLon(float)
* setDeltaLat(float)
* setDeltaLon(float)
* setCornerLat(float)
* setCornerLon(float)
* addLevel(int)
* addPollutant(const std::string&)
* addStartTime(int year, int month, int day, int hour, int minute, int forecast)
* addEndTime(int year, int month, int day, int hour, int minute, int forecast)
* setNumNonZeroPoints(int pollutantIndex, int levelIndex, int numPoints)
* addPoint(int pollutantIndex, int levelIndex, int nonZeroIndex, short xIndex, short yIndex, float concentration)
* @return true on success, false otherwise
*/
bool read_from(const std::string& file);
/**
* @brief write_to create a hysplit concentration file
* @param file Path to the file
*
* Requires data_type to have the following methods:
* id() const
* int year() const
* int month() const
* int day() const
* int hour() const
* int minute() const
* int forecastHour() const
*
* @return true on success, false otherwise
*/
bool write_to(const std::string& file);
}; // class
} // namespace radix
#include "radixio/hysplitcdump.i.hh"
#endif /** RADIX_RADIXIO_HYSPLITCDUMP_HH_ */
#include <string>
#include "radixbug/bug.hh"
#include "radixio/eafstream.hh"
#include "radixio/hysplitcdump.hh"
namespace radix
{
template<typename data_type>
HysplitCDump<data_type>::HysplitCDump(data_type container)
{
mData = container;
}
template<typename data_type>
bool HysplitCDump<data_type>::read_from(const std::string& file)
{
bool result = false;
eafstream fstr(file.c_str()
, std::ios::in | std::ios::binary);
if(fstr.is_open() == false)
{
return result;
}
// big endian
fstr.setReverseBytes(true);
// read fortran header
fstr.readHeader();
std::string id = fstr.readString(4);
mData->setId(id);
int packing, year, month, day, hour, forecastHour, numLocations;
fstr >> year
>> month
>> day
>> hour
>> forecastHour
>> numLocations
>> packing;
fstr.readFooter();
mData->setDateTime(year, month, day, hour, forecastHour);
float lat, lon, z;
int minutes;
// for the number of locations
for( int i = 0; i < numLocations; ++i)
{
fstr.readHeader();
fstr >> year
>> month
>> day
>> hour
>> lat
>> lon
>> z
>> minutes;
mData->addLocation(year, month, day, hour, lat, lon, z, minutes);
fstr.readFooter();
}
int numLat, numLon;
float deltaLat, deltaLon, cLat, cLon;
fstr.readHeader();
fstr >> numLat
>> numLon
>> deltaLat
>> deltaLon
>> cLat
>> cLon;
fstr.readFooter();
mData->setNumLat(numLat);
mData->setNumLon(numLon);
mData->setDeltaLat(deltaLat);
mData->setDeltaLon(deltaLon);
mData->setCornerLat(cLat);
mData->setCornerLon(cLon);
fstr.readHeader();
int numLevels;
fstr >> numLevels;
for(int i = 0; i < numLevels; ++i)
{
int level;
fstr >> level;
mData->addLevel(level);
}
fstr.readFooter();
fstr.readHeader();
int numPollutants;
fstr >> numPollutants;
for(int i = 0; i < numPollutants; ++i)
{
std::string pol;
pol = fstr.readString(4);
mData->addPollutant(pol);
}
fstr.readFooter();
// infinite loop over observation times
for(; fstr.good();)
{
fstr.readHeader();
fstr >> year
>> month
>> day
>> hour
>> minutes
>> forecastHour;
fstr.readFooter();
mData->addStartTime(year, month, day, hour, minutes, forecastHour);
fstr.readHeader();
fstr >> year
>> month
>> day
>> hour
>> minutes
>> forecastHour;
fstr.readFooter();
mData->addEndTime(year, month, day, hour, minutes, forecastHour);
for(int i = 0; i < numPollutants; ++i)
{
for(int j = 0; j < numLevels; ++j)
{
fstr.readHeader();
std::string pollutant;
int z;
int nxyp;
if(packing)
{
pollutant = fstr.readString(4);
fstr >> z >> nxyp;
mData->setNumNonZeroPoints(i,j,nxyp);
for(int np = 0; np < nxyp; ++np)
{
short xi = fstr.readShort();
short yi = fstr.readShort();
float c = fstr.readFloat();
mData->addPoint(i,j,np,xi,yi,c);
}
} // if packing is enabled
fstr.readFooter();
fstr.peek(); // peek ahead to check file integrity
} // loop over num levels
} // loop num pollutants
} // infinite loop over output times
return result;
} // HysplitCDump::read_from
} // namespace radix
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