FileIO.cpp
4.07 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "globals.h"
#include <sstream>
#include <fstream>
#include <iostream>
using namespace std;
vector<SpecPair> LoadSpectrum(string filename)
{
//load a spectrum from a file and resample to 2wn intervals
//create the spectrum
vector<SpecPair> S;
//open the file
ifstream inFile(filename.c_str());
if(!inFile)
{
cout<<"Error loading spectrum: "<<inFile<<endl;
return S;
}
//read all elements of the file
SpecPair temp;
while(!inFile.eof()) {
inFile>>temp.nu;
inFile>>temp.A;
S.push_back(temp);
}
//compute the minimum and maximum input wavenumbers
double inMin = S.front().nu;
double inMax = S.back().nu;
int nuMin = (int)ceil(inMin);
int nuMax = (int)floor(inMax);
//make sure both are either even or odd
if(nuMin % 2 != nuMax % 2)
nuMax--;
//compute the number of values in the resampled spectrum
int nVals = (nuMax - nuMin)/2 + 1;
//allocate space for the spectrum
vector<SpecPair> outSpec;
double nu, highVal, lowVal, a;
int j=1;
for(int i=0; i<nVals; i++)
{
nu = nuMin + i * 2;
temp.nu = nu;
//handle the boundary cases
if(nu < inMin || nu > inMax)
temp.A = 0.0;
else
{
//move to the correct position in the input array
while(j < (int)S.size()-1 && S[j].nu <= nu)
j++;
lowVal = S[j-1].nu;
highVal = S[j].nu;
a = (nu - lowVal)/(highVal - lowVal);
temp.A = S[j-1].A * (1.0 - a) + S[j].A * a;
}
outSpec.push_back(temp);
}
return outSpec;
}
vector<SpecPair> SetReferenceSpectrum(char* text)
{
stringstream inString(text);
//create the spectrum
vector<SpecPair> S;
SpecPair temp;
while(!inString.eof()) {
inString>>temp.nu;
inString>>temp.A;
S.push_back(temp);
}
return S;
}
/*void SaveK(string fileName)
{
ofstream outFile(fileName.c_str());
for(unsigned int i=0; i<EtaK.size(); i++)
{
outFile<<EtaK[i].nu<<" ";
outFile<<EtaK[i].A<<endl;
}
outFile.close();
}*/
void SaveMaterial(string fileName)
{
ofstream outFile(fileName.c_str());
outFile<<"nu"<<'\t'<<"n"<<'\t'<<"k"<<endl;
for(unsigned int i=0; i<EtaN.size(); i++)
{
outFile<<EtaN[i].nu<<'\t';
outFile<<EtaN[i].A<<'\t';
outFile<<EtaK[i].A<<endl;
}
outFile.close();
}
void SaveSimulation(string fileName)
{
ofstream outFile(fileName.c_str());
outFile.precision(10);
for(unsigned int i=0; i<SimSpectrum.size(); i++)
{
outFile<<SimSpectrum[i].nu<<" ";
outFile<<SimSpectrum[i].A<<endl;
}
outFile.close();
}
void SaveState()
{
ofstream outFile("main.prj");
//Window Parameters
outFile<<nuMin<<endl;
outFile<<nuMax<<endl;
outFile<<aMin<<endl;
outFile<<aMax<<endl;
outFile<<dNu<<endl;
//material parameters
outFile<<radius<<endl;
outFile<<baseIR<<endl;
outFile<<cA<<endl;
//optical parameters
outFile<<cNAi<<endl;
outFile<<cNAo<<endl;
outFile<<oNAi<<endl;
outFile<<oNAo<<endl;
outFile.close();
}
void LoadState()
{
ifstream inFile("main.prj");
//Window Parameters
inFile>>nuMin;
inFile>>nuMax;
inFile>>aMin;
inFile>>aMax;
inFile>>dNu;
//material parameters
inFile>>radius;
inFile>>baseIR;
inFile>>cA;
//optical parameters
inFile>>cNAi;
inFile>>cNAo;
inFile>>oNAi;
inFile>>oNAo;
inFile.close();
}
void SetDefaults()
{
nuMin = 800;
nuMax = 4000;
dNu = 2;
aMin = 0;
aMax = 1;
//material parameters
radius = 4.0f;
baseIR = 1.49f;
cA = 1.0;
vector<SpecPair> KMaterial;
vector<SpecPair> NMaterial;
//optical parameters
cNAi = 0.0;
cNAo = 0.6;
oNAi = 0.0;
oNAo = 0.6;
}