%% Script that saves corrected annotations in a proper format

%%  Cleaning
clc
clear 
close all

%%  Settings
data_source_path='C:\Users\vgliner\Desktop\Private\Master\data Physionet\MITDB\mitdb';
listing= dir([data_source_path '\*.mat']);
Database_name='mitdb/';
Records_name='RECORDS';
url = 'https://www.physionet.org/physiobank/database/mitdb/RECORDS';
filename = 'C:\Users\vgliner\Desktop\Private\Master\data Physionet\MITDB\mitdb/records.txt';
outfilename = websave(filename,url);
records_list = textread(filename);
num_of_records = numel(records_list);

%%  Routine

for listing_counter=1:num_of_records
      %Loading my data
            test=listing(listing_counter).name(1:3);
            test_ann=[test(1:3) '_ann_qrs_cleaned.txt'];    %mitdb _cleaned       
            test_ann_spare=[test(1:3) '_ann_qrs.txt'];    %mitdb 
            delimiter = '';
            formatSpec = '%f%[^\n\r]';
            fileID = fopen([data_source_path '\' test_ann],'r');
            if (fileID<0)
            fileID = fopen([data_source_path '\' test_ann_spare],'r');                
            end
            dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'EmptyValue' ,NaN, 'ReturnOnError', false);
            fclose(fileID);
            ann_vec = dataArray{:, 1};
    try
              %Loading Physionet data
             [ann,type,subtype,chan,num] = rdann([Database_name num2str(records_list(listing_counter))],'atr');
              %Saving 
              if numel(num2str(listing_counter))==2
              writing_path=['challenge/2013/set-a/a' num2str(listing_counter)] ;      
              else
              writing_path=['challenge/2013/set-a/a0' num2str(listing_counter)] ;                
              end
              if numel(ann_vec)>numel(ann)
              ann=ann_vec;
              type(end:numel(ann_vec))=type(end);
              subtype(end:numel(ann_vec))=subtype(end);
              chan(end:numel(ann_vec))=chan(end);
              num(end:numel(ann_vec))=num(end);      
              else
              type=type(1:numel(ann_vec));    
              subtype=subtype(1:numel(ann_vec));    
              chan=chan(1:numel(ann_vec));    
              num=num(1:numel(ann_vec));          
              end
              wrann(writing_path,'atr',ann_vec,type,subtype,chan,num);
        disp(['Successfuly downloaded...' num2str(records_list(listing_counter))]);              
    catch
        disp(['Failed to download...' num2str(records_list(listing_counter))]);
    end
end


%%  Now rename the files
writing_path=['challenge/2013/set-a/'] 
for listing_counter=1:num_of_records
    try
        if listing_counter<10
        movefile([writing_path 'a0' num2str(listing_counter) '.atr'], [writing_path 'new/' 'a' listing(listing_counter).name(1:3) '.atr']);
        else
        movefile([writing_path 'a' num2str(listing_counter) '.atr'], [writing_path 'new/' 'a' listing(listing_counter).name(1:3) '.atr']);        
        end
    catch
        disp(['File not found...' num2str(listing_counter)]);
    end
end