Commit 134cad9b authored by Achim Morschhauser's avatar Achim Morschhauser
Browse files

File output buffer

parent 9ae99267
/********************************************************************************
* WRITTEN BY ACHIM MORSCHHAUSER, GFZ POTSDAM, July 2019 *
* mors//gfz-potsdam.de *
* *
* This class provides a buffer of observatory data that uses file input and *
* output. It can be used in combination with the SHELL file operator (|). *
* *
* This class implements the abstract class buffer_obs. *
* *
********************************************************************************/
#ifndef _BUFFER_OBS_file_H
#define _BUFFER_OBS_file_H
#include <buffer_obs.hpp>
#include <data_obs.hpp>
#include <fstream>
class buffer_obs_file : public buffer_obs
{
//
// Private class variables
//
private:
// Precision
int precision;
// Path to output files
std::string path;
// Format of output file (strftime)
std::string format;
// Currently opened filename (no path)
std::string filename;
// File stream
std::fstream file;
//
// Constructors
//
public:
//buffer_obs_file(int precision=2);
//buffer_obs_file(int precision,std::string path);
buffer_obs_file(int precision=2,std::string path="", std::string format="TEST_%Y%m%d.txt");
//
// Public Methods
//
/** Add to buffer */
public: int put(data_obs* data);
/** Get last element and delete */
public: int pop(data_obs* data);
/** Get last element and delete */
public: data_obs pop();
//
// Private Methods
//
/** Initialize the buffer */
private: int init();
};
#endif /* _BUFFER_OBS_file_H */
/********************************************************************************
* WRITTEN BY ACHIM MORSCHHAUSER, GFZ POTSDAM, April 2016 *
* mors//gfz-potsdam.de *
* *
* This class provides a buffer of observatory data that uses standard input and *
* output. It can be used in combination with the SHELL pipe operator (|). *
* *
* This class implements the abstract class buffer_obs. *
* *
********************************************************************************/
#ifndef _BUFFER_OBS_pipe_H
#define _BUFFER_OBS_pipe_H
#include <buffer_obs.hpp>
#include <data_obs.hpp>
class buffer_obs_pipe : public buffer_obs
{
//
// Private class variables
//
private:
// Precision
int precision;
//
// Constructors
//
public:
buffer_obs_pipe(int precision=2);
//
// Public Methods
//
/** Add to buffer */
public: int put(data_obs* data);
/** Get last element and delete */
public: int pop(data_obs* data);
/** Get last element and delete */
public: data_obs pop();
//
// Private Methods
//
/** Initialize the buffer */
private: int init();
};
#endif /* _BUFFER_OBS_pipe_H */
......@@ -69,6 +69,8 @@ public: int set_time(data_obs* data);
/** Get the time of the data */
public: int get_time(int* year, int* mon, int* day, int* hour, int* min, int* sec,
long* msec);
/** Get time as tm */
public: int get_time(struct tm* time) const;
/** Get time as timespec */
public: int get_time(struct timespec* time) const;
......
/*******************************************************************************
* WRITTEN BY ACHIM MORSCHHAUSER, GFZ POTSDAM, July 2019 *
* mors//gfz-potsdam.de *
* *
* This class provides a buffer of observatory data that uses file input *
* and output. *
* *
* This class implements the abstract class buffer_obs. *
* *
********************************************************************************/
// C++ Headers
#include <iostream> // Standard I/O
#include <fstream>
// C headers
#include <time.h>
// Custom C++ Headers
#include <buffer_obs_file.hpp> // Buffer using File I/O
/********************************************************************************
*********************************************************************************
* *
* Constructors. *
* *
*********************************************************************************
********************************************************************************/
buffer_obs_file::buffer_obs_file(int precision, std::string path,
std::string format) {
// Set precision
this->precision = precision;
// Set path
this->path = path;
std::cout << "PATH: " << path << std::endl;
// Set format
this->format = format;
}
/********************************************************************************
*********************************************************************************
* *
* Public Methods. *
* *
*********************************************************************************
********************************************************************************/
/********************************************************************************
* *
* Add data of type data_obs to the buffer. This is done by converting the *
* data to its string representation and writing to File Output. *
* Take care to name files according to date *
* *
********************************************************************************/
int buffer_obs_file::put(data_obs* data) {
std::string datastr;
struct tm time;
size_t smax=40;
char filename[smax];
std::string filename_full;
data->string(&datastr,precision);
// Create filename
data->get_time(&time);
strftime(filename,smax,format.c_str(),&time);
filename_full = path+filename;
// Check if file is already opened
if (file.is_open()){
if (this->filename.compare(filename)) {
file.close();
file.open(filename_full,std::ios::out | std::ios::app);
std::cerr << "OPEN FILE: " << filename << std::endl;
this->filename = filename;
} else {
file << datastr << std::endl;
}
} else {
// Open file
file.open(filename_full,std::ios::out | std::ios::app);
}
if (file.bad()) {
std::cerr << "Opening or writing to file " << filename
<< " failed!" << std::endl;
}
return(0);
}
/********************************************************************************
* *
* Read data from the buffer. This is done by reading from Standard Input and *
* parsing the string. *
* *
********************************************************************************/
int buffer_obs_file::pop(data_obs* data) {
std::string input;
std::getline (std::cin,input);
/*std::cout << "STDIN: "
<< input
<< std::endl;
*/
data->parse(&input);
return(0);
}
data_obs buffer_obs_file::pop(){
data_obs data;
pop(&data);
return(data);
}
/********************************************************************************
*********************************************************************************
* *
* Private Methods. *
* *
*********************************************************************************
********************************************************************************/
/********************************************************************************
* *
* Method dummy for initialization: No initialization is needed. *
* *
********************************************************************************/
int buffer_obs_file::init() {
return(0);
}
/********************************************************************************
* WRITTEN BY ACHIM MORSCHHAUSER, GFZ POTSDAM, April 2016 *
* mors//gfz-potsdam.de *
* *
* This class provides a buffer of observatory data that uses standard input and *
* output. It can be used in combination with the SHELL pipe operator (|). *
* *
* This class implements the abstract class buffer_obs. *
* *
********************************************************************************/
// C++ Headers
#include <iostream> // Standard I/O
// Custom C++ Headers
#include <buffer_obs_pipe.hpp> // Buffer using Standard I/O and the SHELL Pipe
/********************************************************************************
*********************************************************************************
* *
* Constructors. *
* *
*********************************************************************************
********************************************************************************/
buffer_obs_pipe::buffer_obs_pipe(int precision) {
this-> precision=precision;
}
/********************************************************************************
*********************************************************************************
* *
* Public Methods. *
* *
*********************************************************************************
********************************************************************************/
/********************************************************************************
* *
* Add data of type data_obs to the buffer. This is done by converting the *
* data to its string representation and writing to Standard Output. *
* *
********************************************************************************/
int buffer_obs_pipe::put(data_obs* data) {
std::string datastr;
data->string(&datastr,precision);
std::cout << datastr << std::endl;
return(0);
}
/********************************************************************************
* *
* Read data from the buffer. This is done by reading from Standard Input and *
* parsing the string. *
* *
********************************************************************************/
int buffer_obs_pipe::pop(data_obs* data) {
std::string input;
std::getline (std::cin,input);
/*std::cout << "STDIN: "
<< input
<< std::endl;
*/
data->parse(&input);
return(0);
}
data_obs buffer_obs_pipe::pop(){
data_obs data;
pop(&data);
return(data);
}
/********************************************************************************
*********************************************************************************
* *
* Private Methods. *
* *
*********************************************************************************
********************************************************************************/
/********************************************************************************
* *
* Method dummy for initialization: No initialization is needed. *
* *
********************************************************************************/
int buffer_obs_pipe::init() {
return(0);
}
/********************************************************************************
* WRITTEN BY ACHIM MORSCHHAUSER, GFZ POTSDAM, April 2016 *
* mors//gfz-potsdam.de *
* *
* This class provides a buffer of observatory data that uses standard input and *
* output. It can be used in combination with the SHELL pipe operator (|). *
* *
/*******************************************************************************
* WRITTEN BY ACHIM MORSCHHAUSER, GFZ POTSDAM, April 2016 *
* mors//gfz-potsdam.de *
* *
* This class provides a buffer of observatory data that uses standard input *
* and output. It can be used in combination with the SHELL pipe operator (|). *
* *
* This class implements the abstract class buffer_obs. *
* *
********************************************************************************/
......
......@@ -159,6 +159,24 @@ int data_obs::get_time(int* year, int* mon, int* day, int* hour, int* min,
}
/********************************************************************************
* *
* Get the time from tm. *
* *
********************************************************************************/
int data_obs::get_time(struct tm* tm) const{
tm->tm_year = year - 1900;
tm->tm_mon = mon - 1;
tm->tm_mday = day;
tm->tm_hour = hour;
tm->tm_min = min;
tm->tm_sec = sec;
return (0);
}
/********************************************************************************
* *
* Get the time from timespec. *
......@@ -168,12 +186,7 @@ int data_obs::get_time(struct timespec* time) const{
struct tm tm;
tm.tm_year = year - 1900;
tm.tm_mon = mon - 1;
tm.tm_mday = day;
tm.tm_hour = hour;
tm.tm_min = min;
tm.tm_sec = sec;
get_time(&tm);
time->tv_sec = timegm(&tm);
time->tv_nsec = msec;
......
......@@ -219,7 +219,20 @@ int driver_serial::receive(char *buf, int buflen,
strcpy(buf,"!01<obsDAQ V5.5.1>");
} else if (strncmp(sent, "$0FM",4)==0) {
strcpy(buf,"!01<obsDAQ V5.5.1>");
// Serial number
} else if (strncmp(sent, "$01SN",4)==0) {
strcpy(buf,"EMULATION");
// Calibration
} else if (strncmp(sent, "$01RF",4)==0) {
strcpy(buf,"!0145195D");
} else if (strncmp(sent, "$01RO",4)==0) {
strcpy(buf,"!01FFFB4A");
// Write calibration
// Continuous run
} else if (strncmp(sent, "#01CS",4)==0) {
strcpy(buf,">FF4AF1010B9500414736810DE05C1D5BF5000A");
......
......@@ -9,6 +9,7 @@
#include <data_obs_vector.hpp>
#include <driver_obs_obsdaq.hpp>
#include <buffer_obs_pipe.hpp>
#include <buffer_obs_file.hpp>
#include <buffer_obs_queue.hpp>
#include <Obs_Calibration_Vector.hpp>
#include <Parameters_Reader.hpp>
......@@ -43,7 +44,8 @@ int main(int argc, char* argv[]) {
double Z_s = par.get_dbl("Z_s");
// Initialize new buffer for STDOUT
buffer_obs_pipe pipe = buffer_obs_pipe(5);
buffer_obs_file pipe = buffer_obs_file(5,par.get_str("PATH"),
par.get_str("FORMAT"));
// Initialize the calibration constants for FGE
Obs_Calibration_Vector cal = Obs_Calibration_Vector(X_s/R,Y_s/R,Z_s/R);
......
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