function x = get_cs(f_ring, w_amf, do_ss)
% get_cs (w_amf, do_ss)
%   This function reads in laboratory absorption cross-sections. It can also
%   read in the wavelength-dependent AMFs (w_amf = 1 -> read them in, 
%   w_amf = 0 -> don't read them in) and shift n' stretch (i.e. rescale) the 
%   wavelength range for each individual gas species (do_ss = 1 -> rescale,
%   do_ss = 0 -> don't rescale).

% BUGS: 	- 	file read error when reading full array lengths, len_crs(i)
%			  	temp fix is to use len_crs - 1 when reading in cross sections (June 15)
%			- 	O3 cross section doesn't seem to look right (compare with general
%				appearance of cross sec. on pg. 84 of Deb Fish's thesis). All the 
%				others look okay (except Ring, I don't know what it supposed to look
%				like). (June 15)

% ARGUMENTS:
%				f_ring	whether to use NO3 or Ring cross section
%				w_amf		0 -> don't use wavelength-dependent AMF's; 1 -> use them
%				do_ss			0 -> don't shift/stretch wavelengths; 1 -> shift/stretch them
%
% INPUT FILES:
%		o3___crs.ext(1)	O3 cross section vs wavelength
%		no2__crs.ext(2)	NO2 cross section vs wavelength
%		oclo_crs.ext(3) 	OClO cross section vs wavelength
%		o4___crs.ext(4) 	O4 cross section vs wavelength
%		h2o__crs.ext(5)	H2O cross section vs wavelength
%		bro__crs.ext(6)	BrO cross section vs wavelength
%   	ring_crs.ext(7)	Ring effect cross section vs wavelength
%		     OR
%		no3__crs.ext(7)   NO3 cross section vs wavelength (use either ring OR NO3)
%				( NOTE: ext(i) is a string array given the 3-character extension of these
%				filenames. Different extensions denote varying array lengths for the
%				cross sections.)
%		ss.use				contains shift/stretch parameters for wavelength rescaling
%
% CALLS:			
%
% CALLED BY: 	input.m

% COMMENTS:
%	July 17	-	determines length of cross section arrays during program execution

% Globals from INIT.M:
global n_abs n_pix abs_gas dir filenames

global ext			% cross section file extensions
global len_crs		% length of cross section arrays
global col			% unit vertical column for scaling the cross sections
% wavelength-dependent AMFs:
global wv_amf				% wavelength
global o3_amf no2_amf	% AMF for O3 and NO2
% wavlength (wv) and cross sections (sm):
global wv_o3 wv_no2 wv_oclo wv_o4 wv_h2o wv_bro wv_ring wv_no3
global sm_o3 sm_no2 sm_oclo sm_o4 sm_h2o sm_bro sm_ring sm_no3

%global wv_o3
%global sm_o3

%dir_cs = '..\input_files\';

% Check input arguments:
if (~(w_amf == 0 | w_amf == 1) | ~(do_ss == 0 | do_ss == 1))	
   disp('GET_CS.M: Error, invalid arguments')
   x = 0;
	return
end

file(1,1:8) = 'o3___crs';
file(2,1:8) = 'no2__crs';
file(3,1:8) = 'oclo_crs';
file(4,1:8) = 'o4___crs';
file(5,1:8) = 'h2o__crs';
file(6,1:8) = 'bro__crs';
if f_ring > 0
   file(7,1:8) = 'ring_crs';
else
   file(7,1:8) = 'no3__crs';
end
for i = 1:1:n_abs
   file(i,9:12) = strcat('.',ext(i,1:3));
end

% Store filenames in global struct, just in case we want to use them elsewhere:
filenames.cs_data = file;

% Read in cross sections. For the full arrays, this takes a while (about a minute
% or two).
for i = 1:1:n_abs
   fid = fopen(strcat(dir.ip,file(i,1:12)),'r');
   if fid == -1 	% file was not opened
   	disp(strcat('GET_CS.M: Error opening ',strcat(dir.ip,file(i,1:12)),' - file not opened.'))
   	x = 0;
   	return
   end
   len_crs(i) = 1;
   j = 0;
   continue = 1;
   switch i
   case 1  
      disp('here we are')
      %clear fid wv_o3 sm_o3		% IMPORTANT: DO NOT USE THIS LINE!
      %fid = fopen(strcat(dir.ip,'o3___crs.530'),'r')
      while continue 
         j = j + 1;
       	wv_o3(j) = fscanf(fid,'%f11',1);
         sm_o3(j) = fscanf(fid,'%f11',1);
         sm_o3(j) = sm_o3(j)*col(i);
         z = fgetl(fid);
         if feof(fid) 
            continue = 0;
            len_crs(i) = j;
         end
      end
      figure(4)
      plot(wv_o3, sm_o3)
      i = n_abs + 10;		% temp; exit for loop
   case 2
      while continue
         j = j + 1;
         wv_no2(j) = fscanf(fid,'%f11',1);
         sm_no2(j) = fscanf(fid,'%f11',1);
         sm_no2(j) = sm_no2(j)*col(i);
         z = fgetl(fid);
         if feof(fid) 
            continue = 0;
            len_crs(i) = j;
         end
      end
   %   figure(5)
   %   plot(wv_no2, sm_no2)
   case 3
      while continue
         j = j + 1;
         wv_oclo(j) = fscanf(fid,'%f11',1);
         sm_oclo(j) = fscanf(fid,'%f11',1);
         sm_oclo(j) = sm_oclo(j)*col(i);
         z = fgetl(fid);
         if feof(fid) 
    	    	continue = 0;
            len_crs(i) = j;
         end
      end
   case 4
      while continue
         j = j + 1;
         wv_o4(j) = fscanf(fid,'%f11',1);
         sm_o4(j) = fscanf(fid,'%f11',1);
         sm_o4(j) = sm_o4(j)*col(i);
         z = fgetl(fid);
         if feof(fid) 
            continue = 0;
            len_crs(i) = j;
         end
      end
   case 5
		while continue
         j = j + 1;
         wv_h2o(j) = fscanf(fid,'%f11',1);
         sm_h2o(j) = fscanf(fid,'%f11',1);
         sm_h2o(j) = sm_h2o(j)*col(i);
         z = fgetl(fid);
         if feof(fid) 
            continue = 0;
            len_crs(i) = j;
         end
      end
   case 6
      while continue
         j = j + 1;
         wv_bro(j) = fscanf(fid,'%f11',1);
         sm_bro(j) = fscanf(fid,'%f11',1);
         sm_bro(j) = sm_bro(j)*col(i);
         z = fgetl(fid);
         if feof(fid) 
            continue = 0;
            len_crs(i) = j;
         end
      end
   case 7
      if f_ring > 0
         while continue
         	j = j + 1;
         	wv_ring(j) = fscanf(fid,'%f11',1);
          	sm_ring(j) = fscanf(fid,'%f11',1);
            sm_ring(j) = sm_ring(j)*col(i);
            z = fgetl(fid);
            if feof(fid) 
             	continue = 0;
             	len_crs(i) = j;
           	end
         end
      else
         while continue
         	j = j + 1;
          	wv_no3(j) = fscanf(fid,'%f11',1);
           	sm_no3(j) = fscanf(fid,'%f11',1);
            sm_no3(j) = sm_no3(j)*col(i);
            z = fgetl(fid);
            if feof(fid) 
             	continue = 0;
              	len_crs(i) = j;
           	end
         end
      end	% end of "if f_ring > 0"
   end	% end of "switch i"
   fclose(fid);
   if i == n_abs + 10
      break
   end
end	% end of "for" loop

% Need something here to handle wavelenth-dependent AMFs. I will worry about this 
% later, since it involves re-binning the AMF arrays, which requires writing
% a linear and/or polynomial interpolation routine. (June 12)
if w_amf == 1
   disp(strcat('GET_CS.M: Sorry...this code does not yet handle w',... 
      'avelength dependentAMFs. The management apologies for any inconvenience.'))
end

% If do_ss == 1, we shift and stretch the wavelengths for each species by the amounts
% given in the file SS.USE:
if do_ss == 1
   fid = fopen(strcat(dir.ip,'ss.use'),'r');
   if fid == -1 	% file was not opened
   	disp(strcat('GET_CS.M: Error opening SS.USE - file not opened.'))
   	x = 0;
   	return
   end
   for i = 1:1:n_abs
      % Read in shift and stretch parameters for species i:
      z = fscanf(fid,'%1d',1);
      shift = fscanf(fid,'%10f',1);
      z = fscanf(fid,'%s4',1);
      stretch = fscanf(fid,'%10f',1);
      z = fgetl(fid);
      switch i
      	case 1
            for j = 1:1:len_crs(i)
               wv_o3(j) = wv_o3(j).*stretch + shift;
            end
         case 2
            for j = 1:1:len_crs(i)
               wv_no2(j) = wv_no2(j).*stretch + shift;
            end
         case 3
            for j = 1:1:len_crs(i)
               wv_oclo(j) = wv_oclo(j).*stretch + shift;
            end
   		case 4
            for j = 1:1:len_crs(i)
               wv_o4(j) = wv_o4(j).*stretch + shift;
            end
         case 5
            for j = 1:1:len_crs(i)
               wv_h2o(j) = wv_h2o(j).*stretch + shift;
            end
         case 6
            for j = 1:1:len_crs(i)
               wv_bro(j) = wv_bro(j).*stretch + shift;
            end
         case 7
            if f_ring > 0
               for j = 1:1:len_crs(i)
               	wv_ring(j) = wv_ring(j).*stretch + shift;
               end
            else
               for j = 1:1:len_crs(i)
               	wv_no3(j) = wv_no3(j).*stretch + shift;
               end 
            end	% end of "if f_ring > 0"
      end	% end of "switch i"
   end	% end "for i = 1:1:7"
	fclose(fid);   
end	% end "if do_ss == 1"

x = 1;
