VieSched++
VieVS VLBI Scheduling Software
util.h
Go to the documentation of this file.
1 /*
2  * VieSched++ Very Long Baseline Interferometry (VLBI) Scheduling Software
3  * Copyright (C) 2018 Matthias Schartner
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
27 #ifndef GIT_COMMIT_HASH
28 #define GIT_COMMIT_HASH "unknown"
29 #endif
30 
31 #ifndef UTILITY_H
32 #define UTILITY_H
33 
34 
35 #include <boost/date_time.hpp>
36 #include <boost/format.hpp>
37 #include <boost/property_tree/ptree.hpp>
38 #include <cmath>
39 #include <numeric>
40 #include <vector>
41 
42 #include "Constants.h"
43 
44 
45 namespace VieVS {
50 enum class Timestamp {
51  start,
52  end
53 };
54 } // namespace VieVS
55 
56 namespace VieVS {
62 namespace util {
63 
71 unsigned long getNumberOfStations( const boost::property_tree::ptree &xml );
72 
73 
81 std::vector<std::string> getStationNames( const boost::property_tree::ptree &xml );
82 
83 
93 std::string ra2dms( double angle );
94 
95 
105 std::string ra2dms_astFormat( double angle );
106 
107 
117 std::string dc2hms( double angle );
118 
119 
129 std::string dc2hms_astFormat( double angle );
130 
131 
141 double freqency2wavelenth( double frequency );
142 
143 
153 double wavelength2frequency( double wavelength );
154 
155 
163 double wrap2twoPi( double angle );
164 
165 
173 double wrap2pi( double angle );
174 
175 
184 int duration( const boost::posix_time::ptime &start, const boost::posix_time::ptime &end );
185 
186 
193 std::string version();
194 
195 
207 template <typename T>
208 T absDiff( const T &a, const T &b ) {
209  return ( a > b ) ? ( a - b ) : ( b - a );
210 }
211 
212 
223 template <typename T>
224 std::vector<int> sortIndexes( const std::vector<T> &v ) {
225  // initialize original index locations
226  std::vector<int> idx( v.size() );
227  std::iota( idx.begin(), idx.end(), 0 );
228 
229  // sort indexes based on comparing values in v
230  std::sort( idx.begin(), idx.end(), [&v]( int i1, int i2 ) { return v[i1] < v[i2]; } );
231 
232  return idx;
233 }
234 
235 
246 template <typename K, typename V>
247 bool valueExists( std::map<K, V> mapOfElemen, V value ) {
248  auto it = mapOfElemen.begin();
249  // Iterate through the map
250  while ( it != mapOfElemen.end() ) {
251  // Check if value of this entry matches with given value
252  if ( it->second == value ) {
253  return true;
254  }
255  // Go to next entry in map
256  it++;
257  }
258  return false;
259 }
260 
261 
271 void outputObjectList( const std::string &title, const std::vector<std::string> &names, std::ofstream &of,
272  unsigned long indents = 4 );
273 
274 
286 std::string weekDay2string( int weekday );
287 
288 
299 std::string month2string( int month );
300 
308 template <typename A, typename B>
309 std::pair<B, A> flip_pair( const std::pair<A, B> &p ) {
310  return std::pair<B, A>( p.second, p.first );
311 }
312 
320 template <typename A, typename B>
321 std::map<B, A> flip_map( const std::map<A, B> &src ) {
322  std::map<B, A> dst;
323  for ( const auto &any : src ) dst[any.second] = any.first;
324  return dst;
325 }
326 
334 char numberOfScans2char( long n );
335 
342 std::string numberOfScans2char_header();
343 
344 
353 void simplify_inline( std::string &str );
354 
364 std::string simplify( const std::string &str );
373 std::string milliseconds2string( long long int usec, bool forceSeconds = false );
374 
382 std::string version2prefix(int version);
383 
384 
385 } // namespace util
386 } // namespace VieVS
387 
388 #endif // UTILITY_H
T absDiff(const T &a, const T &b)
absolute difference between two points
Definition: util.h:208
void simplify_inline(std::string &str)
simplifies the string inline
namespace util for utility functions.
std::string dc2hms_astFormat(double angle)
convert declination to string
Definition: util.cpp:67
std::vector< int > sortIndexes(const std::vector< T > &v)
indices of sorted vector elements
Definition: util.h:224
void outputObjectList(const std::string &title, const std::vector< std::string > &names, std::ofstream &of, unsigned long indents=4)
outputs list of objects
Definition: util.cpp:107
double freqency2wavelenth(double frequency)
transforms frequency to wavelength
Definition: util.cpp:145
double wrap2pi(double angle)
wrap angle to interval [-pi, pi)
Definition: util.cpp:95
std::string milliseconds2string(long long int usec, bool forceSeconds=false)
converts microseconds to string
Definition: util.cpp:326
unsigned long getNumberOfStations(const boost::property_tree::ptree &xml)
get number of stations from xml file
Definition: util.cpp:151
constants used in this program
bool valueExists(std::map< K, V > mapOfElemen, V value)
checks if a value already exists in a map
Definition: util.h:247
std::string ra2dms(double angle)
convert right ascension to string
Definition: util.cpp:26
std::string numberOfScans2char_header()
translation table for number of scans (within 900 sec) to character for output tables ...
Definition: util.cpp:291
std::vector< std::string > getStationNames(const boost::property_tree::ptree &xml)
get station names from xml file
Definition: util.cpp:163
char numberOfScans2char(long n)
converts number of scans (within 900 sec) to character for output tables
Definition: util.cpp:276
double wavelength2frequency(double wavelength)
transforms wavelength to frequency
Definition: util.cpp:148
std::string weekDay2string(int weekday)
convert weekday integer to string
Definition: util.cpp:178
std::string version2prefix(int version)
version number to prefix
Definition: util.cpp:351
std::string dc2hms(double angle)
convert declination to string
Definition: util.cpp:48
double wrap2twoPi(double angle)
wrap angle to interval [0, 2*pi)
Definition: util.cpp:86
std::string month2string(int month)
convert month integer to string
Definition: util.cpp:218
std::string ra2dms_astFormat(double angle)
convert right ascension to string
Definition: util.cpp:37
int duration(const boost::posix_time::ptime &start, const boost::posix_time::ptime &end)
calculate duration between two time points
Definition: util.cpp:101
std::map< B, A > flip_map(const std::map< A, B > &src)
flip map
Definition: util.h:321
namespace VieVS is used for all defined classes.
Definition: FocusCorners.h:32
std::pair< B, A > flip_pair(const std::pair< A, B > &p)
flip pair
Definition: util.h:309
std::string version()
software version number
Definition: util.cpp:137
Timestamp
Definition: util.h:50
std::string simplify(const std::string &str)
generates simplified version of string