read-spectra.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * @file read-spectra.cc
  3. * @author Konstantin Ladutenko <kostyfisik at gmail (.) com>
  4. * @date Wed Mar 11 11:51:26 2015
  5. *
  6. * @copyright 2015 Konstantin Ladutenko
  7. *
  8. * @brief Read complex spectra from file in format 'WL real imag'
  9. *
  10. * read-spectra is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * read-spectra is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with read-spectra. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. #include <algorithm>
  25. #include <complex>
  26. #include <cstdio>
  27. #include <fstream>
  28. #include <sstream>
  29. #include <string>
  30. #include <stdexcept>
  31. #include <iostream>
  32. #include <vector>
  33. #include "read-spectra.h"
  34. namespace read_spectra {
  35. template<class T> inline T pow2(const T value) {return value*value;}
  36. // ********************************************************************** //
  37. // ********************************************************************** //
  38. // ********************************************************************** //
  39. ReadSpectra& ReadSpectra::ReadFromFile(std::string fname) {
  40. //std::cout<<"Reading file: "<< fname << std::endl;
  41. std::ifstream infile(fname.c_str());
  42. data_.clear();
  43. std::string line;
  44. while (std::getline(infile, line))
  45. {
  46. if (line.front() == '#') continue; //do not read comments
  47. if (line.find('#') != std::string::npos)
  48. throw std::invalid_argument("Error! Comments should be marked with # in the begining of the line!\n");
  49. std::istringstream iss(line);
  50. double wl, re, im;
  51. if (!(iss >> wl >> re >> im)) throw std::invalid_argument("Error! Unexpected format of the line!\n");
  52. data_.push_back(std::vector<double>({wl,re,im}));
  53. //std::cout<<wl<<' '<<re<<' '<<im<<std::endl;
  54. } // end of wile reading file
  55. std::sort(data_.begin(), data_.end(),
  56. [](const std::vector<double>& a, const std::vector<double>& b) {
  57. return a.front() < b.front();
  58. });
  59. return *this;
  60. } // end of void ReadSpectra::ReadFromFile(std::string fname)
  61. // ********************************************************************** //
  62. // ********************************************************************** //
  63. // ********************************************************************** //
  64. /// Cut the spectra to the range and convert it to std::complex<double>
  65. ReadSpectra& ReadSpectra::ResizeToComplex(double from_wl, double to_wl, int samples) {
  66. if (data_.size() < 2) throw std::invalid_argument("Nothing to resize!/n");
  67. if (data_.front()[0] > from_wl || data_.front()[0] > to_wl ||
  68. data_.back()[0] < from_wl || data_.back()[0] < to_wl ||
  69. from_wl > to_wl)
  70. throw std::invalid_argument("Invalid range to resize spectra!/n");
  71. if (samples < 1) throw std::invalid_argument("Not enough samples!/n");
  72. std::vector<double> wl_sampled(samples, 0.0);
  73. if (samples == 1) {
  74. wl_sampled[0] = (from_wl + to_wl)/2.0;
  75. } else {
  76. for (int i =0; i<samples; ++i)
  77. wl_sampled[i] = from_wl
  78. + (to_wl-from_wl)*static_cast<double>(i)/static_cast<double>(samples-1);
  79. } // end of setting wl_sampled
  80. data_complex_.clear();
  81. int j = 0;
  82. for (int i = 0; i < data_.size(); ++i) {
  83. const double& wl_i = data_[i][0];
  84. const double& wl_s = wl_sampled[j];
  85. if (wl_i < wl_s) continue;
  86. else {
  87. const double& wl_prev = data_[i-1][0];
  88. const double& re_prev = data_[i-1][1];
  89. const double& im_prev = data_[i-1][2];
  90. const double& re_i = data_[i][1];
  91. const double& im_i = data_[i][2];
  92. // Linear approximation
  93. double re_s = re_i - (re_i-re_prev)*(wl_i-wl_s)/(wl_i-wl_prev);
  94. double im_s = im_i - (im_i-im_prev)*(wl_i-wl_s)/(wl_i-wl_prev);
  95. auto tmp = std::make_pair(wl_s, std::complex<double>(re_s,im_s));
  96. data_complex_.push_back(tmp);
  97. ++j;
  98. --i; // Next sampled point(j) can be in the same i .. i-1 region
  99. // All sampled wavelengths has a value
  100. if (j >= wl_sampled.size()) break;
  101. }
  102. }
  103. if (data_complex_.size() == 0)
  104. throw std::invalid_argument("No points in spectra for the defined range!/n");
  105. if (data_complex_.size() != samples)
  106. throw std::invalid_argument("Was not able to get all samples!/n");
  107. return *this;
  108. }
  109. // ********************************************************************** //
  110. // ********************************************************************** //
  111. // ********************************************************************** //
  112. /// from relative permittivity to refractive index
  113. ReadSpectra& ReadSpectra::ToIndex() {
  114. data_complex_index_.clear();
  115. for (auto row : data_complex_) {
  116. const double wl = row.first;
  117. const double e1 = row.second.real();
  118. const double e2 = row.second.imag();
  119. const double n = std::sqrt( (std::sqrt(pow2(e1)+pow2(e2)) + e1) /2.0 );
  120. const double k = std::sqrt( (std::sqrt(pow2(e1)+pow2(e2)) - e1) /2.0 );
  121. auto tmp = std::make_pair(wl, std::complex<double>(n,k));
  122. data_complex_index_.push_back(tmp);
  123. }
  124. return *this;
  125. }
  126. // ********************************************************************** //
  127. // ********************************************************************** //
  128. // ********************************************************************** //
  129. void ReadSpectra::PrintData() {
  130. if (data_complex_.size() == 0)
  131. throw std::invalid_argument("Nothing to print!");
  132. for (auto row : data_complex_) {
  133. printf("wl:%g\tre:%g\tim:%g\n", row.first, row.second.real(),
  134. row.second.imag());
  135. } // end of for each row
  136. }
  137. // ********************************************************************** //
  138. // ********************************************************************** //
  139. // ********************************************************************** //
  140. } // end of namespace read_spectra