function dataout = read_dropsonde(campaign,filename, intype)

%% Read Data for .ict files
if contains(campaign, 'CPEX_CV') || contains(campaign, 'CPEX_AW') || contains(campaign, '2020_ACTIVATE') || contains(campaign, '2021_ACTIVATE') || contains(campaign, '2022_ACTIVATE')
    data = ICARTTreader(filename);
    dataout.header=data.header;
    dataout.Time=data.Time_Start;
    dataout.Press=data.Pressure; % pressure; [mb]
    dataout.Temp=data.Temperature + 273.15; % temperature [K]
    dataout.RH=data.RH; % relative humidity [%]
    dataout.Speed=data.Speed;
    dataout.Dir=data.Direction; 
    dataout.Latitude=data.Latitude; 
    dataout.Longitude=data.Longitude;
    dataout.Altitude1=data.Altitude;
    dataout.Altitude=data.GPSAltitude;
    dataout.Dewpoint=data.Dewpoint + 273.15;
    dataout.Uwnd=data.Uwnd;
    dataout.Vwnd=data.Vwnd;
end 

%Read Data for .dat files
if contains(campaign,'AEOLUS')
    data = readtable(filename, 'Delimiter', '\t');
    time_string = table2array(data(:,2));
    time_string = cellstr(time_string);
    %convert strings to UTC seconds after midnight
    time_seconds = zeros(size(time_string));
    for i = 1:length(time_string)
        timeValue = datetime(time_string{i}, 'InputFormat', 'HH:mm:ss.SSS');    
        time_seconds(i) = posixtime(timeValue);
    end
    dataout.Time = time_seconds;
    dataout.Press = table2array(data(:,3)); %hpa
    dataout.Temp = table2array(data(:,4)) + 273.15;
    dataout.RH = table2array(data(:,5)); %percent
    dataout.Speed = table2array(data(:,25));
    dataout.Dir = table2array(data(:,26));
    dataout.Latitude = table2array(data(:,29));
    dataout.Longitude = table2array(data(:,30));
    dataout.Altitude1 = table2array(data(:,27)); %m
    dataout.Altitude = table2array(data(:,9)); %m
    dataout.Dewpoint = table2array(data(:,22)) + 273.15;
    dataout.Uwnd = table2array(data(:,7));
    dataout.Vwnd = table2array(data(:,6));
end 

%Read Data for .nc files
if contains(campaign,'CAMP2Ex')
    dataout.Time = ncread(filename,'time'); %time since launch time;
    dataout.Press = ncread(filename,'pres'); %hpa;
    dataout.Temp = ncread(filename,'tdry') + 273.15; %celsius;
    dataout.RH = ncread(filename,'rh');%percent;
    dataout.Speed = ncread(filename,'wspd'); %m/s;
    dataout.Dir = ncread(filename,'wdir'); % degree;
    dataout.Latitude = ncread(filename,'lat'); %degree;
    dataout.Longitude = ncread(filename,'lon'); %degree;
    dataout.Altitude1 = ncread(filename,'alt'); %altitude above MSL [m];
    dataout.Altitude = ncread(filename,'gpsalt'); %[m]
    dataout.Dewpoint = ncread(filename,'dp') + 273.15; %celsius
    dataout.Uwnd = ncread(filename,'u_wind'); %m/s
    dataout.Vwnd = ncread(filename, 'v_wind'); %m/s
end 
fldn = fieldnames(dataout);

%% Cleanup Data
[U, iU] = unique(dataout.Time);
% Remove any repeated time steps and replace -999.0 with NaN
for i = 1:length(fldn)
    if isnumeric(dataout.(fldn{i}))
        dataout.(fldn{i}) = dataout.(fldn{i})(iU); % remove any repeated time steps
        dataout.(fldn{i})(dataout.(fldn{i}) == -999.0) = NaN; % replace -999 with NaN
    end
end

% If GPS altitude is bad, replace it with geometric altitude
if (all(dataout.Altitude < 0) || all(isnan(dataout.Altitude)))    % all negative or all nan  wat  c
    dataout.Altitude= dataout.Altitude1;
    fprintf('\n\nNo positive GPS altitude.  Replaced it with geopotenital to geometric altitude.\n')
end

% Remove data below last good GPS altitude 
bottom_ix= length(dataout.Altitude);
while (isnan(dataout.Altitude(bottom_ix))||(dataout.Altitude(bottom_ix)<= 10))
    bottom_ix= bottom_ix-1; % find the last good altitude
    if bottom_ix == 0 %added by Julie for CAMP2Ex
        dataout = NaN;
        return
    end 
end

if bottom_ix < 10
    fprintf(' Read_avaps_dropsonde failed because fewer than 10 valid GPS altitudes were found. \n')
    fprintf(' Consider using geopotential altitude for this sonde.  Returning.\n')
    dataout= NaN;
    return
end

for i = 1:length(fldn)
    if isnumeric(dataout.(fldn{i}))
        dataout.(fldn{i}) = dataout.(fldn{i})(1:bottom_ix); % remove any repeated time steps
    end
end

% Replace huge Altitude1 values and anomalous Press values with NaN 
bd= find(dataout.Altitude > 14000);
if ~isempty(bd), dataout.Altitude(bd)= NaN; end
bd= find(dataout.Press < 100);
if ~isempty(bd), dataout.Press(bd)= NaN; end

% Remove data above the first good GPS altitude
top_ix= 1;
while isnan(dataout.Altitude(top_ix)), top_ix= top_ix+1; end
% while (isnan(data.Altitude(top_ix)) || data.Altitude(top_ix)>4000)
%     top_ix= top_ix+1;
% end

for i = 1:length(fldn)
    if isnumeric(dataout.(fldn{i}))
        dataout.(fldn{i}) = dataout.(fldn{i})(top_ix:end); % remove any repeated time steps
    end
end

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

%dataout.h2o_gkg = rh_2_gkg(dataout.RH, dataout.Temp, dataout.Press); % 
dataout.Theta=dataout.Temp.*(1013.25./dataout.Press).^0.286; % potential temperature [K]

%mixing ratio
rh_ratio = dataout.RH/100; %[ratio]
temp_C = dataout.Temp - 273.15; %[C]
if temp_C >= 0
        es = 6.112 .* exp((17.67 .* temp_C) ./ (temp_C + 243.5)); %[hPa]
else
        es = 6.112 .* exp((22.46 .* temp_C) ./ (temp_C + 272.6)); %[hPa]
end
e = rh_ratio .* es; %[hPa]
dataout.qv = (0.622 .* es) ./ (dataout.Press - es); %[kg/kg]
%virtual potential temperature
dataout.Thetav = dataout.Theta .*(1+0.608 .* dataout.qv); %[K]



end




