ASCIIObservation.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 2007 by Willem van Straten
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 // dspsr/Kernel/Classes/dsp/ASCIIObservation.h
10 
11 #ifndef __ASCIIObservation_h
12 #define __ASCIIObservation_h
13 
14 #include "dsp/Observation.h"
15 #include "ascii_header.h"
16 
17 namespace dsp {
18 
20 
24  class ASCIIObservation : public Observation
25  {
26 
27  public:
28 
30  ASCIIObservation (const char* header=0);
31 
34 
36  ASCIIObservation* clone() const override;
37 
39  void load (const char* header);
40 
42  void unload (char* header);
43 
45  uint64_t get_offset_bytes () const { return offset_bytes; }
46 
48  void set_required (std::string key, bool required=true);
49 
51  bool is_required (std::string key);
52 
53  template <typename T>
54  int custom_header_get (std::string key, const char *format, T result) const;
55 
57  const char* get_header () const { return loaded_header.c_str(); }
58 
60  char* get_header () { return const_cast<char*>(loaded_header.c_str()); }
61 
62  protected:
63 
64  std::string hdr_version;
65 
67  std::vector< std::string > required_keys;
68 
70  template <typename T>
71  int ascii_header_check (const char *header, std::string key, const char *format, T result);
72 
73  template <typename T>
74  void load_str_into_array (std::string from, T* buffer, unsigned bufsize);
75 
77  uint64_t offset_bytes;
78 
79  std::string loaded_header;
80  };
81 
82 }
83 
84 template <typename T>
85 void dsp::ASCIIObservation::load_str_into_array ( std::string from, T* buffer, unsigned bufsize )
86 {
87  std::string val;
88  std::string delimiter = ",";
89  size_t pos=0;
90  for (unsigned i=0; i<bufsize; i++) {
91  pos = from.find(delimiter);
92  val = from.substr(0, pos);
93  buffer[i] = fromstring<T>(val);
94  from.erase(0, pos + delimiter.length());
95  }
96 }
97 
98 
99 template <typename T>
100 int dsp::ASCIIObservation::custom_header_get ( std::string key, const char *format, T result) const
101 {
102  int rv = ascii_header_get (loaded_header.c_str(), key.c_str(), format, result);
103  if ( rv > 0)
104  return rv;
105  throw Error (InvalidState, "ASCIIObservation::custom_header_get", "failed to find " + key);
106 }
107 
108 
109 template <typename T>
111  std::string key, const char *format, T result)
112 {
113  int rv = ascii_header_get(header, key.c_str(), format, result);
114 
115  if ( rv>0 || !is_required(key) )
116  return rv;
117 
118  throw Error (InvalidState, "ASCIIObservation", "failed load " + key);
119 }
120 
121 #endif
std::vector< std::string > required_keys
The list of ASCII keywords that must be present.
Definition: ASCIIObservation.h:77
Contains all Baseband Data Reduction Library classes.
Definition: ASCIIObservation.h:17
std::string format
Data format used to record signal.
Definition: Observation.h:359
Represents a rational number.
Definition: Rational.h:15
Stores information about digital, band-limited, time-varying signals.
Definition: Observation.h:33
void set_required(std::string key, bool required=true)
Set/unset a required keyword.
Definition: ASCIIObservation.C:53
ASCIIObservation * clone() const override
Cloner (calls new)
Definition: ASCIIObservation.C:79
uint64_t get_offset_bytes() const
Get the number of bytes offset from the beginning of acquisition.
Definition: ASCIIObservation.h:55
int ascii_header_check(const char *header, std::string key, const char *format, T result)
Load a keyword, only throw an error if it's required and doesn't exist.
Definition: ASCIIObservation.h:110
Sizes of header, data, and tailer of data that are divided in blocks / frames / heaps.
Definition: BlockSize.h:24
void unload(char *header)
Write an ASCII header block.
Definition: ASCIIObservation.C:563
uint64_t offset_bytes
Number of bytes offset from the beginning of acquisition.
Definition: ASCIIObservation.h:87
const char * get_header() const
Return the immutable ASCII header from which this was constructed.
Definition: ASCIIObservation.h:67
const float version
The baseband/dsp version number.
Definition: dsp.C:19
const std::string get_message() const
void load(const char *header)
Read the ASCII header block.
Definition: ASCIIObservation.C:88
bool is_required(std::string key)
Check if a certain keyword is required.
Definition: ASCIIObservation.C:45
Parses Observation attributes from an ASCII header.
Definition: ASCIIObservation.h:29
ASCIIObservation(const char *header=0)
Construct from an ASCII header block.
Definition: ASCIIObservation.C:23

Generated using doxygen 1.8.17