function write_running_vals(UTC_time, lcl_date, fnameout, AMrun_range, ics_raw, run_poly, run_mbr)


%% This function was written to write running I0 values to file
% so that the main I0 script is not so cluttered

% lcl_date = atf_raw.apx_lcl_date;
% UTC_time = atf_raw.UTC_time;
% fnameout = [out_dir, [atf_fname(out_dir_sf(end)+1:out_name_df(end)), 'runi0']];

%% version
v_num = 'write_running_vals   V: 2017-02-27   - JKH';  

%% check inputs
if ~isequal(size(ics_raw), size(run_poly)) && ~isequal(size(ics_raw), size(run_mbr));
  error('Inputs are of different sizes.')
end

%% prep writing
ncols = 3; %year, day, hour
if ~isempty(run_poly{1});
  fldn_rp = fieldnames(run_poly{1}); 
  ncols = ncols + 1 + (length(fldn_rp)-2)*2; %1 for flag, 2 for each of the orders
end
if ~isempty(run_mbr{1});
  fldn_rmbr = fieldnames(run_mbr{1}); 
  ncols = ncols + (length(run_mbr)-1); %UTC_time elsewhere
end

lcl_year = year(lcl_date);
lcl_doy = lcl_date - datenum(lcl_year,1,1) + 1;
utc_hod = (UTC_time - datenum(lcl_date))*24;


%% write to cell array
outcols = cell(length(UTC_time), ncols); %pre-allocate outgoing text
out_header = cell(1, ncols);
out_header(1:4) = {'Year', 'doy', 'hour', 'delAM'};
for i1=1:length(UTC_time);
  outcols{i1,1} = sprintf('%4d', lcl_year(i1)); %Year
  outcols{i1,2} = sprintf('%3d', lcl_doy(i1)); %doy
  outcols{i1,3} = sprintf('%7.4f', utc_hod(i1)); %UTC hour
  outcols{i1,4} = sprintf('%7.5f', AMrun_range(i1)); %delta AM
  
  col_i1 = 5;
  if ~isempty(run_poly{1});
    for i3=1:length(run_poly); %for each window
      ctr_i3 = ics_raw{i3}.ctr_str; %center wavenumber, string
      for i2=1:length(fldn_rp); %for each fieldname
        if isequal(fldn_rp{i2}, 'flag');
          outcols{i1,col_i1} = sprintf('%2d', run_poly{i3}.(fldn_rp{i2})(i1)); %write if flagged
          if i1==1; out_header{col_i1}=['pflg_', ctr_i3]; end
          col_i1 = col_i1+1;
        elseif ~isequal(fldn_rp{i2}, 'UTC_time'); %all other fields (except UTC time) are ordered fits
          outcols{i1,col_i1} = sprintf('%11.9f', run_poly{i3}.(fldn_rp{i2})(i1,1)); %ordered fit
          outcols{i1,col_i1+1} = sprintf('%6.4f', run_poly{i3}.(fldn_rp{i2})(i1,2)); %write gof indicator
          if i1==1; 
            odr_num = regexp(fldn_rp{i2},'\d*','Match'); %pulls the number out
            odr_num = odr_num{1};
            out_header(col_i1:col_i1+1)={['I0p', odr_num, '_', ctr_i3], ['R2a', odr_num, '_', ctr_i3]}; 
          end
          col_i1 = col_i1+2; 
        end
        
      end
    end
  end
  
  
  if ~isempty(run_mbr{1}); %for running windows with linear-only slopes
    for i3=1:length(run_mbr); %for each window
      ctr_i3 = ics_raw{i3}.ctr_str; %center wavenumber, string
      for i2=1:length(fldn_rmbr); %for each fieldname
        if isequal(fldn_rmbr{i2}, 'n');
          outcols{i1,col_i1} = sprintf('%4d', run_mbr{i3}.(fldn_rmbr{i2})(i1)); %write number of points used
          if i1==1; out_header{col_i1}=['n_', ctr_i3]; end
          col_i1 = col_i1+1; 
        elseif isequal(fldn_rmbr{i2}, 'r'); %
          outcols{i1,col_i1} = sprintf('%7.4f', run_mbr{i3}.(fldn_rmbr{i2})(i1,1)); %
          if i1==1; out_header{col_i1}=['r_', ctr_i3]; end
          col_i1 = col_i1+1;
        elseif isequal(fldn_rmbr{i2}, 'lineb'); %
          outcols{i1,col_i1} = sprintf('%11.9f', run_mbr{i3}.(fldn_rmbr{i2})(i1,1)); %
          if i1==1; out_header{col_i1}=['lineb_', ctr_i3]; end
          col_i1 = col_i1+1; 
        elseif isequal(fldn_rmbr{i2}, 'tsreb'); %
          outcols{i1,col_i1} = sprintf('%11.9f', run_mbr{i3}.(fldn_rmbr{i2})(i1,1)); %
          if i1==1; out_header{col_i1}=['tsreb_', ctr_i3]; end
          col_i1 = col_i1+1; 
        end
        
      end
    end
  end
end

%% actual file writing
fid = fopen(fnameout, 'w'); %open file for writing

fprintf(fid, '%s\r\n', sprintf('%4d %4d', [3, size(outcols,2)]));
fprintf(fid, '%s\r\n', v_num);
fprintf(fid, '%s\r\n', strjoin(out_header));


for row=1:size(outcols,1);
    fprintf(fid, '%s\r\n', strjoin(outcols(row, :)));
end
fclose(fid);





