-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSkySelection.cc
executable file
·87 lines (80 loc) · 2.46 KB
/
SkySelection.cc
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
#include "SkySelection.h"
#include "Region.h"
#include "HealpixBaseExtended.h"
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <functional>
#include <cctype>
SkySelection::SkySelection(const std::string & filename) {
addFromFile(filename);
}
void SkySelection::addFromFile(const std::string & filename) {
//Open the file
std::ifstream is(filename.c_str());
SkyRegion region;
std::string incl, buffer;
//Read the file and add to the vector
while ( is.good() ) {
//Read one line at a time
std::getline(is, buffer);
//Continue if the buffer is empty
if (buffer == "")
continue;
//Create a input stream from the buffer
std::istringstream iss(buffer);
//Read the ADD or REMOVE keywords
iss >> std::skipws >> incl;
//Read the region
iss >> region;
//Skip the line if something went wrong
if (iss.fail() || iss.bad() ){
std::cerr<<"Failed parsing line \""<<buffer<<"\""<<std::endl;
continue;
}
//Check the inclusion keyword
std::transform(incl.begin(), incl.end(), incl.begin(), toupper);
if (incl == "ADD" ) {
addRegion(region);
}else if (incl == "REMOVE" ) {
removeRegion(region);
} else {
std::cerr<<"First word in the line has to be either ADD or REMOVE"<<std::endl;
std::cerr<<"Skipping line \""<<buffer<<"\""<<std::endl;
continue;
}
}
if (fselection.size() == 0) {
std::cerr<<"No region added after reading file "<<filename<<std::endl;
}
}
void SkySelection::addRegion(const SkyRegion & region){
fselection.push_back(std::pair<Inclusion,SkyRegion>(ADD,region));
}
void SkySelection::removeRegion(const SkyRegion & region){
fselection.push_back(std::pair<Inclusion,SkyRegion>(REMOVE,region));
}
std::set<int> SkySelection::pixelsForHealpix(const HealpixBaseExtended &hp) const{
std::set<int> pixels;
//Loop through the selection and either add or remove the selection
for (size_t i = 0; i < fselection.size(); ++i){
if (fselection[i].first == ADD) {
std::set<int> pix = hp.regionToPixels(fselection[i].second);
std::set<int>::iterator it = pix.begin();
for ( ; it != pix.end(); ++it) {
pixels.insert(*it);
}
} else {
std::set<int> pix = hp.regionToPixels(fselection[i].second);
std::set<int>::iterator it = pix.begin();
for ( ; it != pix.end(); ++it) {
pixels.erase(*it);
}
}
}
return pixels;
}