function out = read_timesig_fb(intfg_timesigfile,long_in, spec_tz)

% test value
% intfg_timesigfile = 'cnspectra_sigtime20140613.dat'; %14.8186, 14.8543
% %810.2876
% intfg_timesigfile = 'muintf_sigtime20160403.dat'; 
% long_in = 18;
% spec_tz = 0;
% intfg_timesigfile = 'plintf_sigtime20150811.dat'; 
% long_in = -118;
% spec_tz = 0;


%% read in interferogram time + sig files
%% this function reads in time plus signal files and outputs an SIA result in arbitrary units
%% it also outputs other values needed for creating .in files

% V - 13-Feb-2017 - JKH

fileID = fopen(intfg_timesigfile);
intfg_time_int = textscan(fileID, '%s', 'delimiter', '\n');
intfg_time_int = intfg_time_int{1,1};
hdr_lines = cellfun(@(x) length(x)>=2 && isequal(x(1:2), 'H '), intfg_time_int); %pulls out header lines

space_length = sum(hdr_lines); %number of fields
hdrs_list = intfg_time_int(hdr_lines)'; 
intfg_time_int = reshape(intfg_time_int(~hdr_lines)', space_length, [])'; %reshapes with fields in columns, specific files in rows

date_col = find(cellfun(@(x) isequal(x, 'H DAT:') || isequal(x, 'H DAT1:'), hdrs_list)); %find the date column
time_col = find(cellfun(@(x) isequal(x, 'H TIM:') || isequal(x, 'H TIM1:'), hdrs_list)); %find the time column
filedates =  cellfun(@(x, y) datenum([x, ' ', y], 'dd/mm/yyyy HH:MM:SS.FFF'),  intfg_time_int(:,date_col), intfg_time_int(:,time_col));
intfg_times = datevec(filedates); %vector of dates

name_col = find(cellfun(@(x) isequal(x, 'H NAME'), hdrs_list)); %find the name column (should be the first)
ndirlgth = textscan(intfg_time_int{1, name_col}, '%s', 'delimiter', '/'); % 

name_splt = cell(size(intfg_time_int,1), length(ndirlgth{1})); %pre-allocate cell
for i1 = 1:size(intfg_time_int,1); %for every file/spectra/interferogram
    i1_splt = textscan(intfg_time_int{i1, name_col}, '%s', 'delimiter', '/');
    name_splt(i1,:) = i1_splt{1};
end
intfg_names = name_splt(:,end); %just the interferogram names

other_cols = 1:length(hdrs_list); %other headers, not name or time/date
other_cols = other_cols(~ismember(other_cols, [name_col, date_col, time_col]));
for i1=1:length(other_cols);
    fld_i1_name = hdrs_list{other_cols(i1)}(3:end-1); %pull the fieldname (e.g. MXY)
    if all(ismember(intfg_time_int{1, other_cols(i1)}, '0123456789+-.eE')); %determines if first value in field is a number
        ofields.(fld_i1_name) = cellfun(@str2num, intfg_time_int(:, other_cols(i1))); %saves all other fields as numbers in a structure
    end
end
numbwd_col2 = find(cellfun(@(x) isequal(x, 'H GBW:'), hdrs_list)); %find the GBW column



[zd,zltr,zone] = timezone(long_in,'degrees'); %approximate the timezone based on longitude
filedates_loc_day = filedates+spec_tz/24-zd/24; %changes filesdates so that all files will have local date (though not local time)
%WARNING: this does NOT allow global usage!  This assumes measurements are
%started no earlier than 3am PDT (4am PST), 5am CDT.
filedates_loc_day_vec = datevec(filedates_loc_day);


outtext = intfg_names;
outtext = [outtext, cellstr(num2str(filedates_loc_day_vec(:,1)))]; %year
outtext = [outtext, cellstr(num2str(filedates_loc_day_vec(:,2), '%02d'))]; %month
outtext = [outtext, cellstr(num2str(filedates_loc_day_vec(:,3), '%02d'))]; %day

run_num = NaN*zeros(length(intfg_names),1);
[~, ~, dates_ind] = unique((filedates_loc_day_vec(:,1)*10000+filedates_loc_day_vec(:,2)*100+filedates_loc_day_vec(:,3)),'stable');
j1 = 1;
for i1 = 1:max(dates_ind);
    dates_ind2 = dates_ind==i1;
    j2 = j1+sum(dates_ind2)-1;
    
    run_num(j1:j2) = (1:2:(sum(dates_ind2)*2))';
    
    j1 = j2+1;
end

outtext = [outtext, cellstr(num2str(run_num, '%04d'))]; %run number

out.intfg_name = outtext(:,1);
out.year = intfg_times(:,1);
out.month = intfg_times(:,2);
out.day = intfg_times(:,3);
out.hour = intfg_times(:,4);
out.minute = intfg_times(:,5);
out.second = intfg_times(:,6);
out.fdate = datenum(out.year, out.month, out.day, out.hour, out.minute, out.second); %internal file times
out.runnum  = outtext(:,5);
out.outtext = outtext;
% from here on will not work with I2S generated spectra, need new ifg signals
if isfield(ofields, 'MXY1'); %sometimes names are different for spectral headers
    mxy_col = find(cellfun(@(x) isequal(x, 'H MXY1:'), hdrs_list)); %find the MXY column
    mny_col = find(cellfun(@(x) isequal(x, 'H MNY1:'), hdrs_list));
    out.infymax = ofields.MXY1;
    out.infymin = ofields.MNY1;
else
    mxy_col = find(cellfun(@(x) isequal(x, 'H MXY:'), hdrs_list)); %find the MXY column
    mny_col = find(cellfun(@(x) isequal(x, 'H MNY:'), hdrs_list));
    out.infymax = ofields.MXY;
    out.infymin = ofields.MNY;
end



% approx_sia changed Apr 2015 to destructive inf points instead (more likely linear) was -out.infymin/.155*1000
% change again in Nov 2017 to difference between max & min. This was to solve the + or - ifg problem. 
% Was -1*out.infymax/0.041*1000 for ifgs thought to be negative
%%%indx_inf_neg = out.infymax<0 & out.infymin<0; %determines where the interferograms are negative
%approx_sia_1 = -1*out.infymax/0.041*1000; %SIA value, using arbitrary units, with 1000 set as approximate maximum for instrument
%approx_sia_2 = out.infymin/0.041*1000; %SIA value, using arbitrary units, with 1000 set as approximate maximum for instrument
%if sum(approx_sia_1)>sum(approx_sia_2); %likely more negative igms
%  approx_sia = approx_sia_1;
%else %likely more positive igms
%  approx_sia = approx_sia_2; 
%end
%if sum(approx_sia)<0; %if there are more negative values
%  approx_sia = approx_sia*-1; 
%end
approx_sia = (out.infymax - out.infymin)*6300; %new SIA calcuation


% if ~isempty(strfind(intfg_timesigfile, 'spectra')); 
% if date_col > numbwd_col2; %this type of ordering may be indicative of I2S spectra
%     approx_sia  = 54.55*approx_sia; %This fudge factor was meant for the SIA from OPUS or I2S spectra to be similar, but now is an overestimate.
% end
approx_sia(approx_sia<0) = -99; %null SIA value
approx_sia(approx_sia>9999) = 9999; %max length of 4 in *.gop files


%approx_sia = round(approx_sia*10)/10; %rounds to one decimal places
out.arb_sia = approx_sia;
%out.arb_sia = cellstr(num2str(approx_sia, '% 6.1f'));
out.headers = hdrs_list;

hdrs_oth_ind = true(length(hdrs_list),1); 
hdrs_oth_ind([name_col, date_col, time_col, mxy_col, mny_col]) = false; %these fields have already been used
hdrs_oth = hdrs_list(hdrs_oth_ind);
for i1=1:length(hdrs_oth); %list out the other fields
   fld_i1_name = hdrs_oth{i1}(3:end-1);
   out.(fld_i1_name) = ofields.(fld_i1_name);
end

