function FOQA_normalisation(LoadFile,minmax,savename,flightconfig,aircraftconfig,faultconfig,altimeseries,altimehours,simcountertxt)
%FOQA_normalisation
% J.G. Pelham Cranfield University 2014
% % This function evaluates a flight data file to find events within the
% file and do some preprocessing.  When a fault condition occurs this will
% create a time entry and ID for it so that the AIS can  easily find it and
% use it. the number of variables
% Two  main sources.
%   the faultconfig file
%   the data itself
intermediate=load(LoadFile);
load(minmax);
FOQA_data=intermediate.fdm;
names = fieldnames(FOQA_data);
no_of_channels = size(names);
min_max_channel_names = fieldnames(new_flight_data_min_max);
new_normalised_data = flight_data_object;

tf = logical(0);
pattern1 = '.mat';
replacement = '';
num_text1 = regexprep(LoadFile,pattern1,replacement);
pattern2 = 'sim';
replacement = '';
num_text = regexprep(num_text1,pattern2,replacement);


% name_of_channel_tmp = char('null','null','null','null','null','null','null','null','null','null','null','null','null','null','null','null','null','null','null','null','null','null','null','null','null','null','null','null','null','null','null','null');
% name_of_channel = cellstr(name_of_channel_tmp);
% for n =1:29
%     tmp_channel_no = num2str(n);
%     tmp_channel_name = strcat('channel_',(tmp_channel_no));
%     value_from_file = new_flight_data_min_max.(tmp_channel_name).channel_name;
%     name_of_channel(n) = {value_from_file};
% end
channel_map(29) = 0  ; %e.g. entry 1 gives the channel number of channel 1 in the min_max data file
%get channel name and length

%the search for a crash time and the trimming of the file so that when Normalised_Altitude_h_metres reaches zero is
%the last value channel_15 is the most likely to be altitude
try
    check_data = FOQA_data.Altitude_h_metres;
    %reverse normalisation
    big_array=check_data.data;
    endindex = length(FOQA_data.Altitude_h_metres.data);
    time_is = endindex;
    for m=1:size(big_array)
        if big_array(m) <0
            time_is = m;
            break
        end
    end
    before_end = (time_is-1);
    A=[before_end:endindex]; %#ok<NBRAK>
    trimmed_data.elevator_out = delsample(FOQA_data.elevator_out,'Index',A);
    trimmed_data.aileron_out = delsample(FOQA_data.aileron_out,'Index',A);
    trimmed_data.rudder_out = delsample(FOQA_data.rudder_out,'Index',A);
    trimmed_data.throttle_out = delsample(FOQA_data.throttle_out,'Index',A);
    trimmed_data.airspeed__knots_ = delsample(FOQA_data.airspeed__knots_,'Index',A);
    trimmed_data.dec_sideslip = delsample(FOQA_data.dec_sideslip,'Index',A);
    trimmed_data.dec_AOA = delsample(FOQA_data.dec_AOA,'Index',A);
    trimmed_data.dec_Pitch_angle = delsample(FOQA_data.dec_Pitch_angle,'Index',A);
    trimmed_data.dec_Bank_angle = delsample(FOQA_data.dec_Bank_angle,'Index',A);
    trimmed_data.dec_Heading = delsample(FOQA_data.dec_Heading,'Index',A);
    trimmed_data.Altitude_h_metres = delsample(FOQA_data.Altitude_h_metres,'Index',A);
    trimmed_data.Fuel_mass_out = delsample(FOQA_data.Fuel_mass_out,'Index',A);
    trimmed_data.Thrust_in_x_axis = delsample(FOQA_data.Thrust_in_x_axis,'Index',A);
    trimmed_data.Fuel_Flow = delsample(FOQA_data.Fuel_Flow,'Index',A);
    trimmed_data.X_E_out = delsample(FOQA_data.X_E_out,'Index',A);
    trimmed_data.Y_E_out = delsample(FOQA_data.Y_E_out,'Index',A);
    trimmed_data.Z_E_out = delsample(FOQA_data.Z_E_out,'Index',A);

catch sim_err
    disp([sim_err.identifier]);
    disp([sim_err.message]);
    for e=1:length(sim_err.stack)
       disp(['Error in ' sim_err.stack(e).file ' at line ' num2str(sim_err.stack(e).line)]);

    end
    disp(getReport(sim_err,'extended'));
    extended_report = getReport(sim_err,'extended');
    message_txt = strcat(extended_report, sim_err.message,' Error in ', sim_err.stack(e).file, ' at line ', num2str(sim_err.stack(e).line), ' sim no. ', simcountertxt);
    sendolmail('jonititan@gmail.com','Sim Error',message_txt);
end

    assignin('base','trimmed_data',trimmed_data);
%save('test.mat','trimmed_data');


size_of_min_Max = size(min_max_channel_names);
for y_not = 1:no_of_channels
    %get channel name
    this_channel_name_tmp = names(y_not);
    this_channel_name = char(this_channel_name_tmp);
    for y_not_indeed=1:size_of_min_Max
        tmp_channel_no_here_num = y_not_indeed;
        tmp_channel_no_here_str = num2str(tmp_channel_no_here_num);
        tmp_channel_name_here = strcat('channel_',(tmp_channel_no_here_str));

        comparison_name_channel = char(tmp_channel_name_here);
        %comparison_name = min_max_channel_data.new_flight_data_min_max.(comparison_name_channel).channel_name;
        %comparison_name_char = char(comparison_name);
        tf = strcmp(this_channel_name,new_flight_data_min_max.(comparison_name_channel).channel_name);
        if tf == 1
            %correct channel found
            channel_map(y_not) = y_not_indeed;
        elseif tf == 0
            % do nothing

        end
    end
end

%
for m = 1:no_of_channels



    %get channel name and length
    this_channel_name_tmp = names(m);
    this_channel_name = char(this_channel_name_tmp);
    this_channel_data = trimmed_data.(this_channel_name);

    tmp_channel_no_here_num = channel_map(m);
    tmp_channel_no_here_str = num2str(tmp_channel_no_here_num);
    tmp_channel_name_here = strcat('channel_',(tmp_channel_no_here_str));
    name_channel = char(tmp_channel_name_here);
    filein_biggest = new_flight_data_min_max.(name_channel).biggest;
    filein_smallest = new_flight_data_min_max.(name_channel).smallest;
    %find min&max from channel
    biggest = max(this_channel_data);
    smallest = min(this_channel_data);
    %compare and update if necessary
    file_change_flag = 0;
    if biggest >  filein_biggest
        new_flight_data_min_max.(tmp_channel_name_here).biggest = biggest;
        file_change_flag = 1;
    end

    if smallest < filein_smallest
        new_flight_data_min_max.(tmp_channel_name_here).smallest = smallest;
        file_change_flag = 1;
    end

    current_biggest = new_flight_data_min_max.(tmp_channel_name_here).biggest;
    current_smallest = new_flight_data_min_max.(tmp_channel_name_here).smallest;
    %determine range
    channel_range = new_flight_data_min_max.(tmp_channel_name_here).biggest-new_flight_data_min_max.(tmp_channel_name_here).smallest;
    %Channel must be normalised using an extended range to prevent out of
    %range errors during processing. 25% additional range is usually used
    %normalisation_range =  channel_range*1.25;
    normal_name = strcat('Normalised_',this_channel_name);
    normalised_data.(tmp_channel_name_here) = (this_channel_data-current_smallest)/(current_biggest-current_smallest);
    normalised_data.(tmp_channel_name_here).name = normal_name;

end

new_normalised_data.channel_min_max = new_flight_data_min_max;
if file_change_flag == 1
    save((minmax),'new_flight_data_min_max');
end

c = csvread('C:\Users\s140146\Documents\MATLAB\aerosondeBatchSim\counter.txt',0,0);
simcounter = c + 1;
altimeseries = FOQA_data.Altitude_h_metres.TimeInfo.End;
altimehours = (altimeseries/10)/3600;
simcountertxt = num2str(simcounter);
%outputfilename = strcat((savelocation),'\Aerosonde_Normalised_FOQA_data_v2_sim_no_',(simcountertxt),'_.mat');
outputtsname = strcat('Aerosonde_Normalised_FOQA_data_v2_',(LoadFile),'Flight_length',(altimehours),datestr(now));
outputtsname = strrep(outputtsname,' ','_');
%outputfilename = strrep(outputfilename,' ','_');

flight_data = aircraft_sim_save;
flight_data.name = outputtsname;
flight_data.flight_data = normalised_data;
flight_data.faultdata = faultconfig;
flight_data.aircraftdata = aircraftconfig;
flight_data.siminitialdata = flightconfig;
flight_data.flight_length = altimeseries;
flight_data.flight_length_hours = altimehours;
flight_data.data_channels_min_max = new_flight_data_min_max;
save((savename),'flight_data');
clear;


end
Error using FOQA_normalisation (line 11)
Not enough input arguments.