function DataArray = applyLogTransformation(DataArray, FieldNames)
    % APPLYLOGTRANSFORMATION Applies a log transformation to specified fields in an array of structures.
    %
    % USAGE:
    % DataArray = applyLogTransformation(DataArray, FieldNames);
    %
    % INPUTS:
    % DataArray - An array of structures.
    % FieldNames - A cell array of field names as character vectors.
    %   Default is {
    %      'Volume','lengthPCA1','lengthPCA2','lengthPCA3','Area', 
    %      'Compactness','MaxLength','RatioPCA21','RatioPCA31'
    %   }.
    %
    % OUTPUTS:
    % DataArray - The modified array of structures with the specified fields log-transformed.
    %
    % NOTES:
    % - This function assumes that the values in the specified fields are positive.
    % - The log transformation is applied using the natural logarithm (base e).
    % - If a field name does not exist in any of the structures, a warning is issued.
    % - If a field contains non-positive values, an error is raised.
    % - Zero elements in the fields are replaced with half the minimum value of the positive values.
    % Copyright (c) 2024, Odense University Hospital - Laboratory of Radiation Physics
    
    % Set default value for FieldNames if not provided
    if nargin <  2
        FieldNames = {'Volume','lengthPCA1','lengthPCA2','lengthPCA3','Area', 'Compactness','MaxLength','RatioPCA21','RatioPCA31','Solidity'};
    end

    % Iterate over each structure in the array
    for i = 1:numel(DataArray)
        % Iterate over each field name
        for j = 1:numel(FieldNames)
            fieldName = FieldNames{j};
            % Check if the field exists in the current structure
            if isfield(DataArray(i), fieldName)
                % Check if all values in the field are positive
                if all(DataArray(i).(fieldName) > 0)
                    % % Apply log transformation
                    % DataArray(i).(fieldName) = log(DataArray(i).(fieldName));
                    
                    % Alternative if we want to apply log(-log())
                    % normalization to IslandsLikelihood
                    if ~strcmp(fieldName, 'IslandsLikelihood')
                        DataArray(i).(fieldName) = log(DataArray(i).(fieldName));
                    else
                        DataArray(i).(fieldName) = log(-log(DataArray(i).(fieldName)));
                    end
                else
                    % Check for zero elements and handle them
                    fieldValues = DataArray(i).(fieldName);
                    zeroCheck = fieldValues <=  0;
                    if sum(zeroCheck) >  0
                        if sum(~zeroCheck) >  0
                            minval = min(temp(~zeroCheck),[],'omitnan');
                            minval = minval /  2;
                        else
                            minval = .1;
                        end
                        fieldValues(zeroCheck) = minval;
                        % Apply log transformation after handling zero elements
                        DataArray(i).(fieldName) = log(fieldValues);
                    else
                        error('Error: Non-positive values found in field ''%s''. Log transformation cannot be applied.', fieldName);
                    end
                end
            else
                warning('Warning: Field ''%s'' not found in structure %d.', fieldName, i);
            end
        end
    end
end
