Line data Source code
1 : /** 2 : Bojan Nikolic <b.nikolic@mrao.cam.ac.uk>, <bojan@bnikolic.co.uk> 3 : Initial version August 2010. 4 : Maintained by ESO since 2013. 5 : 6 : This file is part of LibAIR and is licensed under GNU Public 7 : License Version 2 8 : 9 : \file dispersion.cpp 10 : Renamed dispersion.cc 2023 11 : 12 : */ 13 : #include <iostream> 14 : #include <fstream> 15 : #include <sstream> 16 : 17 : #include <stdio.h> 18 : #include <stdlib.h> 19 : #include <string> 20 : 21 : #include "dispersion.h" 22 : 23 : namespace LibAIR2 { 24 : 25 918 : double DispersionTab::operator() (double fnu) 26 : { 27 918 : std::pair<double, double> b= *lower_bound(fnu); 28 918 : if (b.first==fnu) 29 0 : return b.second; 30 : 31 918 : std::pair<double, double> l= *(--lower_bound(fnu)); 32 918 : std::pair<double, double> u= *upper_bound(fnu); 33 : 34 918 : const double f=(fnu-l.first)/(u.first-l.first); 35 918 : return l.second+ f*(u.second-l.second); 36 : 37 : } 38 : 39 1 : void loadCSV(const char *fname, 40 : DispersionTab &dt) 41 : { 42 1 : std::ifstream ifs(fname); 43 1 : if (not ifs.good()){ 44 0 : throw std::runtime_error(std::string("Could not open dispersion table ")+fname); 45 : } 46 1 : std::string scratch; 47 : 48 1742 : while(ifs.good()){ 49 1741 : std::getline(ifs, scratch); 50 : //std::cerr << scratch << " - interpreted as:"; 51 1741 : if (scratch.size() < 5){ 52 : //std::cerr << "(nothing)" << std:: endl; 53 1 : continue; 54 : } 55 : 56 1740 : std::stringstream ss(scratch); 57 : double first, second; 58 1740 : std::string sep; 59 1740 : if(ss >> first){ 60 1740 : if(ss >> sep){ 61 1740 : if(ss >> second){ 62 1740 : dt.insert(dt.end(), 63 3480 : std::pair<double, double>(first, second)); 64 : //std::cerr << "(double) " << first << " , " << second << " " << std::endl; 65 : } 66 : else{ 67 0 : std::cerr<<" Reading " << fname << ": could not interpret third part of " << scratch <<std::endl; 68 : } 69 : } 70 : else{ 71 0 : std::cerr<<" Reading " << fname << ": could not interpret separator in " << scratch <<std::endl; 72 : } 73 : } 74 : else{ 75 0 : std::cerr<<" Reading " << fname << ": could not interpret first part of " << scratch <<std::endl; 76 : } 77 1740 : } 78 : 79 2 : return; 80 1 : } 81 : 82 : }