function dataout = read_merge(campaign,merge_file,coin_file,intype)

% Read Data for .mat files
if contains(campaign, 'DAQ_California') || contains(campaign, 'DAQ_Texas') 
    merge = load(merge_file);
    header = merge.P3Bmerge_header;
    data = merge.P3Bmerge;
    coincident = load(coin_file);
    profile_times = coincident.P3B_HSRL_idx(:,3);
    time_merge=data(:,1)/3600; %UTC
    %match the merge times to the profiles
    start_idx = zeros(length(profile_times(1)));
    end_idx = zeros(length(profile_times(1)));
    for prof = 1:length(profile_times)
        time_prof = profile_times{prof};
        start_prof = time_prof(1);
        end_prof = time_prof(2);
        %Find the indices of the closest times
        [~,idx_start] = min(abs(time_merge - start_prof));
        start_idx(prof,:) = idx_start;
        [~,idx_end] = min(abs(time_merge - end_prof));
        end_idx(prof,:) = idx_end ; 
    end

    dataout.idx_start = start_idx;
    dataout.idx_end = end_idx;
    dataout.time_merge = time_merge;
    dataout.time_start = time_merge(start_idx);
    dataout.time_end = time_merge(end_idx);

    dataout.Press=data(:,9); % pressure; [mb]
    dataout.Temp=data(:,10); % temperature [K]
    dataout.RH=data(:,44); % relative humidity [%]
    dataout.Speed=data(:,14);
    dataout.Dir=data(:,15); 
    dataout.Latitude=data(:,6); 
    dataout.Longitude=data(:,7);
    dataout.Altitude=data(:,8) * 1000; %[m]
    dataout.GPS_Alt=data(:,17) * 1000; %[m]
    dataout.Dewpoint=data(:,30); %[K]
    %dataout.Uwnd=data.Uwnd;
    %dataout.Vwnd=data.Vwnd;
    dataout.qv = data(:,40)/1000; % specific humidity [kg/kg]  
    dataout.Theta = data(:,11); %[K]
end 


fldn = fieldnames(dataout);

% Cleanup Data

% if strcmp(intype, 'stripnan')
%     %% Remove NaN Altitudes and Pressures
%     ix= ~isnan(dataout.Altitude) & ~isnan(dataout.Press);
% 
%     for i = 1:length(fldn)
%         if isnumeric(dataout.(fldn{i}))
%             dataout.(fldn{i}) = dataout.(fldn{i})(ix); % remove any repeated time steps
%         end
%     end
% end


% Calculate Derived Values
%virtual potential temperature
dataout.Thetav = dataout.Theta .*(1+0.608 .* dataout.qv); %[K]



end




