123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- #include <algorithm>
- #include <complex>
- #include <cstdio>
- #include <fstream>
- #include <sstream>
- #include <string>
- #include <stdexcept>
- #include <iostream>
- #include <vector>
- #include "read-spectra.h"
- namespace read_spectra {
- template<class T> inline T pow2(const T value) {return value*value;}
-
-
-
- ReadSpectra& ReadSpectra::ReadFromFile(std::string fname) {
-
- std::ifstream infile(fname.c_str());
- data_.clear();
- std::string line;
- while (std::getline(infile, line))
- {
- if (line.front() == '#') continue;
- if (line.find('#') != std::string::npos)
- throw std::invalid_argument("Error! Comments should be marked with # in the begining of the line!\n");
- std::istringstream iss(line);
- double wl, re, im;
- if (!(iss >> wl >> re >> im)) throw std::invalid_argument("Error! Unexpected format of the line!\n");
- data_.push_back(std::vector<double>({wl,re,im}));
-
- }
- std::sort(data_.begin(), data_.end(),
- [](const std::vector<double>& a, const std::vector<double>& b) {
- return a.front() < b.front();
- });
- return *this;
- }
-
-
-
-
- ReadSpectra& ReadSpectra::ResizeToComplex(double from_wl, double to_wl, int samples) {
- if (data_.size() < 2) throw std::invalid_argument("Nothing to resize!/n");
- if (data_.front()[0] > from_wl || data_.front()[0] > to_wl ||
- data_.back()[0] < from_wl || data_.back()[0] < to_wl ||
- from_wl > to_wl)
- throw std::invalid_argument("Invalid range to resize spectra!/n");
- if (samples < 1) throw std::invalid_argument("Not enough samples!/n");
- std::vector<double> wl_sampled(samples, 0.0);
- if (samples == 1) {
- wl_sampled[0] = (from_wl + to_wl)/2.0;
- } else {
- for (int i =0; i<samples; ++i)
- wl_sampled[i] = from_wl
- + (to_wl-from_wl)*static_cast<double>(i)/static_cast<double>(samples-1);
- }
- data_complex_.clear();
- int j = 0;
- for (int i = 0; i < data_.size(); ++i) {
- const double& wl_i = data_[i][0];
- const double& wl_s = wl_sampled[j];
- if (wl_i < wl_s) continue;
- else {
- const double& wl_prev = data_[i-1][0];
- const double& re_prev = data_[i-1][1];
- const double& im_prev = data_[i-1][2];
- const double& re_i = data_[i][1];
- const double& im_i = data_[i][2];
-
- double re_s = re_i - (re_i-re_prev)*(wl_i-wl_s)/(wl_i-wl_prev);
- double im_s = im_i - (im_i-im_prev)*(wl_i-wl_s)/(wl_i-wl_prev);
- auto tmp = std::make_pair(wl_s, std::complex<double>(re_s,im_s));
- data_complex_.push_back(tmp);
-
- ++j;
- --i;
-
- if (j >= wl_sampled.size()) break;
- }
- }
- if (data_complex_.size() == 0)
- throw std::invalid_argument("No points in spectra for the defined range!/n");
- if (data_complex_.size() != samples)
- throw std::invalid_argument("Was not able to get all samples!/n");
- return *this;
- }
-
-
-
-
- ReadSpectra& ReadSpectra::ToIndex() {
- data_complex_index_.clear();
- for (auto row : data_complex_) {
- const double wl = row.first;
- const double e1 = row.second.real();
- const double e2 = row.second.imag();
- const double n = std::sqrt( (std::sqrt(pow2(e1)+pow2(e2)) + e1) /2.0 );
- const double k = std::sqrt( (std::sqrt(pow2(e1)+pow2(e2)) - e1) /2.0 );
- auto tmp = std::make_pair(wl, std::complex<double>(n,k));
- data_complex_index_.push_back(tmp);
- }
- return *this;
- }
-
-
-
- void ReadSpectra::PrintData() {
- if (data_complex_.size() == 0)
- throw std::invalid_argument("Nothing to print!");
- for (auto row : data_complex_) {
- printf("wl:%g\tre:%g\tim:%g\n", row.first, row.second.real(),
- row.second.imag());
- }
- }
-
-
-
- }
|