% based on a script by A. Cheville posted on the LASER CULT web-site
%
% This program is used to load a spectrum file from 
% the Ocean Optics spectrometer. The Ocean Optics OOIBASE 
% program asks you to save a sample as one of several types: 
% Sample,Absorbance,Transmission,Irradiance,Reference,Dark,Scope. 
% For example to load a file you saved in OOIBASE as a sample, 
% i.e., flame.master.sample, run getspectrum.m and 
% at the filename prompt enter 'flame'. Then, select "Sample" 
% from the popup menu. The data is loaded into two variables: 
% 'lamda' (wavelength in nm) and the filename. In the example 
% above the intensity is returned as the variable 'flame'. 

% The program will plot your data for you in the current figure window. 
disp('Make sure your file is in the Matlab Path') 
disp('Type "pwd" to see current path, or use ''File/Set Path''') 
file=input('Input file name (no extension): ','s'); 
% This command asks the user to input the kind of data taken 
selct=menu('How was this file saved? As a...','Sample','Absorbance','Transmission','Irradiance','Reference','Dark','Scope'); 
extarray = strvcat('sample','absorbance','transmission','irradiance','reference','dark','scope'); 
fid=-1; n=1; 
%this statement adds on the correct file extension used by the Ocean Optics spectrometer 
filename= deblank([file '.Master.' extarray(selct,:)]); 
fid=fopen(filename); 
%assigns file ID 

%this line finds the first period in the filename, and gets rid of all the text after that period 
%this just allows the user to type only the filename, without file extensions 
pointpos = min(findstr(filename,'.')); 
varname = filename(1:(pointpos(1)-1)); 

%this FOR loop gets rid of the character lines that don't contain any useful info 
bogus2 = '*'; 
while bogus2 ~= '>' 
bogus = fgetl(fid); 
bogus2 = bogus(1); 
end

clear lamda
%this FOR loop gets the useful data (I, lambda), then stores the data into matrices 
bogus = 1; 
m = 0; 
while ~isempty(bogus) m = m+1; 
aa=fscanf(fid,'%f',[1,2]); 
bogus = aa; 
if ~isempty(bogus) lamda(m)=aa(1); 
    waveno_nm(m) = (1/785)-(1/lamda(m));
    waveno(m)= waveno_nm(m)*(10^7);
    eval([varname '(m) = aa(2);']); 
end
end
fclose(fid); 

eval(['plot(waveno,' varname ');']);
xlabel('Wave Number (1/cm)') 
%this SWITCH statement determines the correct yaxis label by file extension 
switch selct 
case {1,5,6}, ylabel('Intensity [counts]') 
case 2, ylabel('Absorbance') 
case 3, ylabel('Percent Transmission') 
case 4, ylabel('Relative Irradiance') 
end; 

disp(['Data returned in 1/cm, ' varname]) 