
%% this script is meant to plot an assortment of different spectral windows and fits
% where the directories are in the locations expected by EGI. Example
% output/daily/20160622/spt/o2_7885/
% you would specify 'output/daily/' as the directory, and this script will
% search in the specified day folders and plot all windows avaliable for
% the runs specified for that day

% 3 inputs
% list the output directories from EGI
spt_dir = '/oco2-data/tccon/analysis/jaxa_em27/mid_ir/daily/';

% list the days and spectral numbers to plot
days_runs = [20160622 20;
  20160622 524];


% list directory where to save pdf files
out_dir = '/oco2-data/tccon/analysis/jaxa_em27/mid_ir/spt_fits/'; 

%% find all species/windows avaliable
u_days = unique(days_runs(:,1)); %list all unique days

gas_wins = {};
for i1=1:length(u_days);
  if isdir([spt_dir, num2str(u_days(i1)), '/spt/']);
    dir_lst = dir([spt_dir, num2str(u_days(i1)), '/spt/']);
    gas_wins_i1 = {dir_lst.name};
    dir_ind = [dir_lst.isdir]; %logical, isdir index
    gas_wins_i1 = gas_wins_i1(dir_ind);
    gas_wins_i1 = gas_wins_i1(3:end); %skipping the '.' and '..'
    gas_wins = union(gas_wins, gas_wins_i1);
  end
end

%% grouping by species/window

spt_names = cell(2,1);
nm_row = 1;
for i2=1:length(gas_wins); %species/windows first
  pdf_num = 1; %pdf_number
  
  for i1=1:size(days_runs,1); %for every day and run number combo
    file_i2i1 = dir([spt_dir, num2str(days_runs(i1,1)), '/spt/', gas_wins{i2}, '/*.', sprintf('%04d', days_runs(i1,2))]); %spt file being looked for
    if ~isempty(file_i2i1)
      full_fn_i2i1 = [spt_dir, num2str(days_runs(i1,1)), '/spt/', gas_wins{i2}, '/', file_i2i1.name];
      spt_names{nm_row} = full_fn_i2i1;
      nm_row = nm_row+1;
    end
    
    if nm_row==3 || (nm_row~=1 && i1==size(days_runs,1)); %full cell or last run
      fnameout = [out_dir, gas_wins{i2}, '_', sprintf('%02d', pdf_num), '.pdf'];
      plot_2spt_function_pdf(spt_names, fnameout) %call in subprogram to plot spt files and save as pdf
      pdf_num = pdf_num+1; %advance pdf number
      spt_names = cell(2,1);  %reset
      nm_row = 1; %reset
    end
  end
end







