Commits
Dirk Petry authored a0d5a040bc5
5 5 | |
6 6 | This file is part of LibAIR and is licensed under GNU Public |
7 7 | License Version 2 |
8 8 | |
9 9 | \file dispersion.cpp |
10 10 | Renamed dispersion.cc 2023 |
11 11 | |
12 12 | */ |
13 13 | |
14 14 | |
15 + | |
15 16 | |
16 17 | |
17 18 | |
18 19 | |
19 - | |
20 - | |
21 20 | |
22 21 | |
23 22 | |
24 23 | namespace LibAIR2 { |
25 24 | |
26 25 | double DispersionTab::operator() (double fnu) |
27 26 | { |
28 27 | std::pair<double, double> b= *lower_bound(fnu); |
29 28 | if (b.first==fnu) |
30 29 | return b.second; |
34 33 | |
35 34 | const double f=(fnu-l.first)/(u.first-l.first); |
36 35 | return l.second+ f*(u.second-l.second); |
37 36 | |
38 37 | } |
39 38 | |
40 39 | void loadCSV(const char *fname, |
41 40 | DispersionTab &dt) |
42 41 | { |
43 42 | std::ifstream ifs(fname); |
44 - | if (not ifs.good()) |
45 - | { |
43 + | if (not ifs.good()){ |
46 44 | throw std::runtime_error(std::string("Could not open dispersion table ")+fname); |
47 45 | } |
48 46 | std::string scratch; |
49 47 | |
50 - | while(ifs.good()) |
51 - | { |
48 + | while(ifs.good()){ |
52 49 | std::getline(ifs, scratch); |
53 - | if (scratch.size() < 5) |
50 + | //std::cerr << scratch << " - interpreted as:"; |
51 + | if (scratch.size() < 5){ |
52 + | //std::cerr << "(nothing)" << std:: endl; |
54 53 | continue; |
55 - | |
56 - | char * pch; |
57 - | char *s = new char[scratch.size()+1]; |
58 - | strcpy( s, scratch.c_str() ); |
59 - | pch = strtok(s, ",;\""); |
60 - | casacore::String first(*pch); |
61 - | pch = strtok (NULL, ",;\""); |
62 - | casacore::String second(*pch); |
63 - | |
64 - | casacore::trim(first); |
65 - | casacore::trim(second); |
66 - | try { |
67 - | dt.insert(dt.end(), |
68 - | std::pair<double, double>(casacore::String::toDouble(first), |
69 - | casacore::String::toDouble(second) |
70 - | )); |
71 54 | } |
72 - | catch (const std::bad_cast &bc) |
73 - | { |
74 - | std::cerr<<"Could not interpret " << first << " and " << second |
75 - | <<std::endl; |
55 + | |
56 + | std::stringstream ss(scratch); |
57 + | double first, second; |
58 + | std::string sep; |
59 + | if(ss >> first){ |
60 + | if(ss >> sep){ |
61 + | if(ss >> second){ |
62 + | dt.insert(dt.end(), |
63 + | std::pair<double, double>(first, second)); |
64 + | //std::cerr << "(double) " << first << " , " << second << " " << std::endl; |
65 + | } |
66 + | else{ |
67 + | std::cerr<<" Reading " << fname << ": could not interpret third part of " << scratch <<std::endl; |
68 + | } |
69 + | } |
70 + | else{ |
71 + | std::cerr<<" Reading " << fname << ": could not interpret separator in " << scratch <<std::endl; |
72 + | } |
76 73 | } |
77 - | delete [] s; |
74 + | else{ |
75 + | std::cerr<<" Reading " << fname << ": could not interpret first part of " << scratch <<std::endl; |
76 + | } |
78 77 | } |
79 - | } |
80 78 | |
79 + | return; |
80 + | } |
81 81 | |
82 82 | } |
83 - | |
84 - | |
85 - | |