-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpoint_Functions_Utils.h
More file actions
186 lines (171 loc) · 5.98 KB
/
Copy pathNpoint_Functions_Utils.h
File metadata and controls
186 lines (171 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#ifndef NPOINT_FUNCTIONS_UTILS_H
#define NPOINT_FUNCTIONS_UTILS_H
#include <vector>
#include <string>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <Twopt_Table.h>
#include <healpix_base.h>
#include <vec3.h>
namespace {
/// @cond IDTAG
const std::string NPOINT_FUNCTIONS_UTILS_RCSID
("$Id: Npoint_Functions_Utils.h,v 1.10 2016/02/09 20:31:43 copi Exp $");
/// @endcond
}
/** Namespace for containing all Npoint function related objects.
*/
namespace Npoint_Functions {
/** Make a numbered filename from a prefix and suffix.
* The number is zero padded to a fixed size. No additional characters
* are added to the file name so the suffix MUST include the dot such as
* ".dat", if desired. Similarly the prefix MUST include a separator
* character such as an underscore, "prefix_", if desired.
*/
std::string make_filename (const std::string& prefix, int filenum,
int digits=5,
const std::string& suffix=".dat")
{
std::ostringstream sstr;
sstr << prefix << std::setw(digits) << std::setfill('0') << filenum
<< suffix;
return sstr.str();
}
/** Find sequentially numbered files.
* A list of existing, sequentially numbered files is generated from the
* prefix, digits, and suffix provided. The filenames are generated by
* make_filename() and numbered from \a start in increments of \a
* increment. Existence here means that the file can be opened
* for reading.
*/
std::vector<std::string>
get_sequential_file_list (const std::string& prefix,
int start, int increment,
int digits=5,
const std::string& suffix=".dat")
{
std::vector<std::string> files;
std::ifstream in;
int Nfile = start;
while (true) {
std::string fname = make_filename (prefix, Nfile, digits, suffix);
in.open(fname.c_str());
if (! in) break;
in.close();
files.push_back (fname);
Nfile += increment;
}
return files;
}
/** Find sequentially numbered files.
* A short hand version for the common case when the file numbers start
* at 0 and are incremented by 1.
*/
std::vector<std::string>
get_sequential_file_list (const std::string& prefix,
int digits=5,
const std::string& suffix=".dat")
{ return get_sequential_file_list (prefix, 0, 1, digits, suffix); }
/** Find numbered files in a range.
* A list of existing, numbered files is generated from the prefix,
* digits, and suffix provided. The filenames are generated by
* make_filename() and numbered from \a start to \a end. All filenames
* in this range are checked. Existence here means that the file can be
* opened for reading.
*/
std::vector<std::string>
get_range_file_list (const std::string& prefix,
int start, int end,
int digits=5,
const std::string& suffix=".dat")
{
std::vector<std::string> files;
std::ifstream in;
for (int Nfile = start; Nfile <= end; ++Nfile) {
std::string fname = make_filename (prefix, Nfile, digits, suffix);
in.open(fname.c_str());
if (in) {
in.close();
files.push_back (fname);
}
}
return files;
}
/** Convert a string to any (valid) type.
* The conversion is done using a stringstream and the usual c++ io
* mechanism. This is not the most robust way to do things and it
* doesn't allow for complete error checking, however, it is simple
* which is what we want here.
*/
template<typename T>
bool from_string (const std::string& instr, T& val)
{
std::istringstream iss(instr);
return !(iss >> val).fail();
}
/** Fill a list with HEALpix vectors.
* Helper function to create a list of vectors pointing to HEALPix pixel
* centers. The vectors are labelled by the pixel INDEX in the two
* point table, not the actual pixel number.
*/
template<typename T>
void fill_vector_list (const Npoint_Functions::Twopt_Table<T>& t,
std::vector<vec3>& veclist)
{
Healpix_Base HBase (t.Nside(), t.Scheme(), SET_NSIDE);
veclist.resize (t.Npix());
for (size_t i=0; i < t.Npix(); ++i) {
veclist[i] = HBase.pix2vec (t.pixel_list(i));
}
}
/** Fill a list with HEALpix vectors.
* Helper function to create a list of vectors pointing to HEALPix pixel
* centers. All vectors for the provided \a Nside are calculated in the
* \a scheme HEALPix ordering scheme.
*/
void fill_vector_list (size_t Nside, Healpix_Ordering_Scheme scheme,
std::vector<vec3>& veclist)
{
Healpix_Base HBase (Nside, scheme, SET_NSIDE);
veclist.resize (HBase.Npix());
for (size_t i=0; i < veclist.size(); ++i) {
veclist[i] = HBase.pix2vec (i);
}
}
/** Generate a range of values.
* Sequentially generate a range of values from a starting value in
* increments of delta. This can be used with STL algorithms such as
* std::generate().
*
* A sequential list of real numbers from 1 to 2 (inclusive) in steps of
* 0.1 may be generated as
* \code
* std::vector<double> rseq(11);
* std::generate (rseq.begin(), rseq.end(), myRange<double>(1.0, 0.1));
* \endcode
*/
template<typename T>
class myRange {
private :
T start, delta, curr, next;
public :
/** Construct a range.
* By default the range starts at zero and increases in steps of one.
*/
myRange (T start_=0, T delta_=1)
: start(start_), delta(delta_), curr(start_), next(start_) {}
/// Get the next value in the range.
inline T operator() () { curr=next; next += delta; return curr; }
/** Reset the range.
* The range is reset to its initial value starting value.
*/
inline void reset() { curr = next = start; }
};
}
#endif
/* For emacs, this is a c++ header
* Local Variables:
* mode: c++
* End:
*/