FileIO.cpp 4.07 KB
#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;
}