addpath(genpath('W:\Cassini\Titan\valerio\Anthony_backscatter'));
addpath(genpath('W:\Anthony\Plains Backscatter'));
%% Define inc angle search range

range = [27 33];

%% Create list of swaths with low inc angles

%addpath(genpath('W:\Anthony\Plains Backscatter'));
rootdir = 'W:\Cassini\Titan\valerio\Anthony_backscatter';
filelist = dir(fullfile(rootdir, '**'));  %get list of files and folders in any subfolder

inc_mask = contains({filelist.name}, 'BIEQH') ;
filelist = filelist(inc_mask);
mat_mask = contains({filelist.name}, '.mat') ;
filelist = filelist(mat_mask);

filelist = {filelist.name};


output = {};

% loop over the files
for i = 1:numel(filelist)
    disp(filelist{i})
    % import data from each file
    data = importdata(filelist{i});
    ind = find(data > range(1) & data < range(2));
    num_pixels = numel(ind);

    pos = strfind(filelist{i}, 'T'); %Extract the name of the swath from the filename
    part = strcat(filelist{i}(pos), filelist{i}(pos+1:pos+6));


    if num_pixels > 0
        output = [output; {part, num_pixels, filelist{i}}];
    end
   
end

clear data

% convert the second column of output to a numeric array
num_pixels_array = cell2mat(output(:, 2));

% sort the num_pixels_array in descending order and get the indices
[~, ind] = sort(num_pixels_array, 'descend');

% reorder the rows of output using the indices
output = output(ind, :);

%% After output is created, plot a selected site
% Run only this section after generating "output"

ind = 50; %Select which swath to print
ind_filename = output{ind, 3};
sar_filename = strrep (ind_filename, 'BIE', 'BIS');

Plotsite(importdata(sar_filename), importdata(ind_filename))
title(output{ind,1})
clim(range)
