%% Plot glitch estimate
%
%The start and stop indices, the input data filename etc., are stored in a
%JSON file inside the folder below. It is expected that the output for each
%segment is stored in the folder below as 'segment_*.mat'.
shapesRunOutDir = 'BLIP_SHAPESRun_20221229T125928';

%-----------DO NOT CHANGE BELOW------------------------
%JSON file containing information about the segments processed by SHAPES.
glitchTimesFile = [shapesRunOutDir,filesep,'glitchTimes.json'];

infoStrct = loadjson(glitchTimesFile);

nSegs = length(infoStrct.glitchStrtIndx);

%Get sampling interval from the input data file
inFileData = fullfile(shapesRunOutDir,infoStrct.fileName);
XSpacing = h5readatt(inFileData, '/strain/Strain','Xspacing');

estFig = figure;
hold on;
%For each segment...
for lp = 1:nSegs
    %There should be a file called 'segment_<lp>.mat' containing the
    %estimate for this segment
    inFileEst = fullfile(shapesRunOutDir,['segment_',num2str(lp)]);
    outStrct = load(inFileEst,'bestModel','glitchStrtIndx','glitchEndIndx');
    %plot the estimate
    plot((glitchStrtIndx:glitchEndIndx)*XSpacing, outStrct.bestModel.bestModelSig);
end