File size: 21,419 Bytes
5ab75a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
function out = AnalyzeMMTData(datapath,Rnormkm,ExtinctionCoef,UTbegin,UTend,params)
% Analyze MMT Data - Generate CDFs from MMT photometric data.
% Syntax: out = AnalyzeMMTData(datapath,Rnormkm,ExtinctionCoef);
%         out = AnalyzeMMTData(datapath,Rnormkm,ExtinctionCoef,UTbegin);
%         out = AnalyzeMMTData(datapath,Rnormkm,ExtinctionCoef,UTbegin,UTend);
%         out = AnalyzeMMTData(datapath,Rnormkm,ExtinctionCoef,UTbegin,UTend,params);
%
% =========================================================================
%
% Copyright (c)�2024�United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% =========================================================================
%
% Description: Fit a three-component model to MMT photometric data, and 
% generate associated CDFs.
%
% =========================================================================
%
% Input:
%
%��� datapath����   -�[String] Path to directory containing MMT data files.
%������������������ 
%��� Rnormkm����    -�[Scalar] Range normalization distance (km).
%
%��� ExtinctionCoef -�[Scalar] Atmospheric extinction coefficient.
%
%    UTbegin        - [String] (Optional) Start of observational period in 
%                     Universal Time. Defaults to all available data if not
%                     provided.
%
%    UTend          - [String] (Optional) End of observational period in 
%                     Universal Time. Defaults to all available data if not
%                     provided.
%
%    params         - [Structure] Auxiliary parameter structure with
%                     optional settings fields.
%
% =========================================================================
%
% Output:
%
%�� out        ���� -�[Structure] Output structure containing analysis
%                      results.
%
% =========================================================================
%
% Initial version: Nov�2024;� Latest update: Nov�2024
%
% ----------------- BEGIN CODE -----------------

% Initializations
Nargin = nargin;
if Nargin < 6; params  = []; end
if Nargin < 5; UTend   = ''; end
if Nargin < 4; UTbegin = ''; end

% Default parameters

% Check if previous data has been input
if ~isfield(params,'previous_output'); params.previous_output = []; end
previous_output_exists = ~isempty(params.previous_output);

% Flux scaling factor, usually to applt to previously analyzed data
if ~isfield(params,'Fscale') || isempty(params.Fscale)
    Fscale = 1;
else
    Fscale = params.Fscale;
end

% Statistical analysis flag to estimate quantiles and CDF parameters
if ~isfield(params,'StatAnalysis') || isempty(params.StatAnalysis)
    StatAnalysis = true;
else
    StatAnalysis = params.StatAnalysis;
end

% Number of aA model components; zero for no model
if ~isfield(params,'Ncomp') || isempty(params.Ncomp)
    Ncomp = 3;
else
    Ncomp = params.Ncomp;
    if ~(Ncomp == 0 || Ncomp == 1 || Ncomp == 2 || Ncomp == 3)
        error('Invalid number of model aA fitting components');
    end
end

% Output path
if ~isfield(params,'outpath') || isempty(params.outpath)
    outpath = datapath;
else
    outpath = params.outpath;
end

% Mat file saving flag
if ~isfield(params,'UseMMTSaveFiles') || isempty(params.UseMMTSaveFiles)
    UseMMTSaveFiles = ~previous_output_exists;
else
    UseMMTSaveFiles = params.UseMMTSaveFiles;
end

% Plotting flag
if ~isfield(params,'Plotting') || isempty(params.Plotting)
    Plotting = ~previous_output_exists;
else
    Plotting = params.Plotting;
end
if Plotting
    StatAnalysis = true;
end

% Verbose flag
if ~isfield(params,'verbose') || isempty(params.verbose)
    verbose = ~previous_output_exists;
else
    verbose = params.verbose;
end

% Model components (up to three)
if (Ncomp > 0)
    comp = {'SpecSphere' 'DiffSphere' 'DiffPABNormalFacet'};
    comp = comp(1:Ncomp);
end

% CDF reporting levels
CDFrep = [0.50 0.90 0.95];
ndxstar = 2; % Index for reference zentith magniture 

% Normalization range squared
Rnormm2 = (Rnormkm*1e3)^2;

% Clear filter solar magnitude
Msun1AU = -26.91;
AUkm = 149597870.700; % 1.49598e8;

% JD minus Matlab datenum
JDminusDN = 1721058.5;

% Number of digits for fractions
Ndig = 4;

% Initialized data structure
dat = [];

% Generate output file name root
[~,datpth,epth] = fileparts(datapath);
if ~isempty(epth)
    datpth = [datpth '.' epth];
end
outroot = [datpth ...
           '_' num2str(Rnormkm) 'km' ...
           '_Sc' smart_exp_format(Fscale,Ndig)];

% Check if a previous run has been provided
if previous_output_exists
    
    % Extract MMT output from the previous run
    out = params.previous_output;
    
else
    
    % Search for a previously saved .mat file
    need_to_read_data = true;
    if UseMMTSaveFiles
        dl = dir(fullfile(outpath,[outroot '*_AnalyzeMMTData.mat']));
        Nfile = numel(dl);
        if Nfile == 1
            out = []; load(fullfile(outpath,dl.name));
            if isequal(out.datapath , datapath ) && ...
               isequal(out.Rnormkm  , Rnormkm  ) && ...
               isequal(out.Fscale   , Fscale   ) && ...
               isequal(out.UTbegin  , UTbegin  ) && ...
               isequal(out.UTend    , UTend    ) && ...
               isequal(out.Ncomp    , Ncomp    )
                need_to_read_data = false;
            end
        elseif Nfile ~= 0
            error('More than one *_AnalyzeMMTData.mat file found');
        end
    end

    % Use the mat file if appropriate
    if verbose
        if ~need_to_read_data
            disp(['Using save file ' dl.name]);
        else
            disp(['Processing MMT data in ' datapath]);
        end
    end
    
    if need_to_read_data
        
        % Get all text files in data directory
        dl = dir(fullfile(fullfile(datapath),'satellite*.txt'));
        Nfile = numel(dl);
        % Issue an error if no data files are found
        if Nfile == 0
            error('No photometric data files found (they may need to be unzipped)');
        elseif verbose
            disp(['Reading ' num2str(Nfile) ' data files']);
        end
        
        % Date numbers
        if isempty(UTbegin)
            DNbegin = -Inf;
        else
            DNbegin = datenum(UTbegin,'yyyy-mm-dd HH:MM:SS.FFF');
        end
        if isempty(UTend)
            DNend = Inf;
        else
            DNend   = datenum(UTend,'yyyy-mm-dd HH:MM:SS.FFF');
        end

        % Get all of the data, eliminating out-of-range dates and penumbral/umbral
        % data points
        dat = [];
        for n=1:Nfile
            % Read the MMT data table
            [dt,~,~] = ReadMMTData(fullfile(datapath,dl(n).name));
            % MMT satellite number
            [~,fff,~] = fileparts(dl(n).name);
            [prt,~] = string_parts(fff,'_');
            MMTSat = str2double(prt{2});
            % Only keep desired data
            ndx = (dt.Penumbra == 0)      & ...
                  (dt.DateNum >= DNbegin) & ...
                  (dt.DateNum <= DNend);
            dt = dt(ndx,:);
            Ndt = size(dt,1);
            Ntr = numel(unique(dt.Track));
            if verbose
                disp([' MMTSat = ' num2str(MMTSat) ...
                    ', Ntrk = ' num2str(Ntr) ', Ndat = ' num2str(Ndt)]);
            end
            if Ndt == 0
                break;
            end
            % Add the MMT satellite number to the table   
            dt.MMTSat = repmat(MMTSat,[Ndt 1]);
            % Concatenate the data
            if isempty(dat)
                dat = dt;
            else
                dat = vertcat(dat, dt); %#ok<AGROW>
            end
        end

        % Add solar distances to the data
        Ndat = size(dat,1);
        dat.SunDistance = NaN(Ndat,1);
        for n=1:Ndat
            JD = dat.DateNum(n)+JDminusDN;
            rsun = SunPos(JD);
            dat.SunDistance(n) = norm(rsun);
        end
        
        % Set data Rnormkm and Fscale to placeholder values to indicate
        % that further analysis will be required for this data set
        out.Rnormkm = -1;
        out.Fscale  = -1;
        
    end

end

% Check if any further analysis is required
if ~params.Plotting && (Rnormkm == out.Rnormkm) && (Fscale == out.Fscale)
    % Return the current out structure
    return;
else
    % Extract data for further analysis, if required
    if isempty(dat)
        dat = out.dat;
    end
end

% Calculate the range-normalized mag and OCS, including the flux scale
% factor if required
if verbose
    disp([' Normalizing to a range of ' num2str(Rnormkm) ' km']);
    if Fscale ~= 1
        disp([' Adjusting by a flux scaling factor of ' num2str(Fscale)]);
    end
end
dat.MagNorm = dat.Mag ...
    - 5.0 * log10(dat.Distance/Rnormkm) ...
    - 5.0 * log10(dat.SunDistance/AUkm) ...
    - 2.5 * log10(Fscale);

% Total number of data points, tracks and satellites
Ndat = size(dat,1);
Ntrk = numel(unique(dat.Track));
Nsat = numel(unique(dat.MMTSat));

if verbose
    disp('---- TOTAL DATA SET ----');
    disp(['Nsat = ' num2str(Nsat) ', Ntrk = ' num2str(Ntrk) ', Ndat = ' num2str(Ndat)]);
end

% Define output structure
out.datapath = datapath;
out.Rnormkm = Rnormkm;
out.Fscale = Fscale;
out.UTbegin = UTbegin;
out.UTend = UTend;
out.Ncomp = Ncomp;
out.Nsat = Nsat;
out.Ntrk = Ntrk;
out.Ndat = Ndat;
out.dat = dat;
out.CDFfrac = CDFrep;

% Calculate the magnitude CDF and quantiles
if StatAnalysis
    [F,X] = ecdf(dat.MagNorm);
    F = flipud(1-F);
    NF = numel(F);
    X = flipud(X);
    mrep = NaN(size(CDFrep));
    Nrep = numel(CDFrep);
    for nrep=1:Nrep
        ndx = find(F < CDFrep(nrep));
        ndx = ndx(end);
        if (ndx == NF)
            idx = [ndx-1 ndx];
        else
            idx = [ndx ndx+1];
        end
        mrep(nrep) = interp1(F(idx),X(idx),CDFrep(nrep),'linear','extrap');
    end
    out.CDFMagNorm = mrep;
    out.MagNormStar = mrep(ndxstar);
    % Calculate the +1sigma and -1sigma equivalent deviations to match
    % central 90%
    mmd = mrep(1);
    mlo = mrep(3);
    ndx = find(F < 0.05);
    ndx = ndx(end);
    if (ndx == NF)
        idx = [ndx-1 ndx];
    else
        idx = [ndx ndx+1];
    end
    mhi = interp1(F(idx),X(idx),0.05,'linear','extrap');
    siglo = (mlo-mmd)/sqrt(2)/erfinv(-1+2*0.05);
    sighi = (mhi-mmd)/sqrt(2)/erfinv(-1+2*0.95);
    out.StatsMagNorm = [mmd siglo sighi];
end

% Calculate SATCON-1 cutoff
mcut = SatCon1Limit(Rnormkm);
out.MagnitudeSatCon1Limit = mcut;

% Calculate fraction of zenith mags brighter than cutoff
fcut = sum(dat.MagNorm < mcut)/numel(dat.MagNorm);
out.FracBrighterThanSatCon1Limit = fcut;

% Calculate fraction of off-zenith mags bright than cutoff
Nzag = numel(params.ZenAngGrid);
Rekm = 6378.137;
akm = Rekm+Rnormkm; akm2 = akm^2;
out.ZenAngGrid = params.ZenAngGrid;
out.FracBrightGrid0 = NaN(size(params.ZenAngGrid));
out.FracBrightGrid  = NaN(size(params.ZenAngGrid));
AirMass = AirMassRozenberg1966(params.ZenAngGrid);
for nzag=1:Nzag
    czag = cos(params.ZenAngGrid(nzag)); szag = sin(params.ZenAngGrid(nzag));
    rho = sqrt(akm2-(Rekm*szag)^2) - Rekm*czag;
    % AirMass = 1/czag;
    dM = 5*log10(rho/Rnormkm);
    out.FracBrightGrid0(nzag) = sum(dat.MagNorm + dM < mcut);    
    dM = dM + ExtinctionCoef*AirMass(nzag);
    out.FracBrightGrid(nzag) = sum(dat.MagNorm + dM < mcut);
end
out.FracBrightGrid0 = out.FracBrightGrid0/numel(dat.MagNorm);
out.FracBrightGrid  = out.FracBrightGrid /numel(dat.MagNorm);

% Calculate the multicomponent aA model

if Ncomp > 0

    if verbose
        disp(['Fitting the data using ' num2str(Ncomp) ' component aA model']);
    end
    
    % OCS values
    dat.OCSm2 = Rnormm2 * 10.^(-0.4*(dat.MagNorm-Msun1AU));
    
    % Generate the design matrix aA products
    Dmat = NaN(Ndat,Ncomp);
    for n=1:Ncomp
        Dmat(:,n) = ThreeCompComponent(comp{n},dat);
    end

    % Estimate nonnegative aA products
    aAvec = lsqnonneg(Dmat,dat.OCSm2);

    % Analyze to find lambda = log(aA) values for Least Absolute deviation
    logy = log(dat.OCSm2);
    errfcn = @(lambda)sum(abs(logy-log(Dmat*exp(lambda)))); % L1 error-function 

    fmsopts = optimset('fminsearch');
    fmsopts.MaxIter     = 2000*Ncomp;
    fmsopts.MaxFunEvals = 2*fmsopts.MaxIter;

    aAscale = 1.5*sum(aAvec); aAmin = 1e-3*aAscale;
    Nsearch = 30;
    lsearch = NaN(Ncomp,Nsearch);
    esearch = NaN(1,Nsearch);

    for ns=1:Nsearch
        aA0 = aAscale*rand(Ncomp,1);
        aA0(aA0 < aAmin) = aAmin;
        lam0 = log(aA0);
        [lsearch(:,ns),esearch(ns)] = fminsearch(errfcn,lam0,fmsopts); % L1-optimization
    end
    [~,ns] = min(esearch);
    aAvec = exp(lsearch(:,ns));

    sumaAvec = sum(aAvec);
    ndx = aAvec < 1e-6*sumaAvec;
    if any(ndx)
        aAvec(ndx) = 0;
        ndx = ~ndx;
        errfcn = @(lambda)sum(abs(logy-log(Dmat(:,ndx)*exp(lambda))));
        lam1 = fminsearch(errfcn,lsearch(ndx,ns),fmsopts); % L1-optimization
        aAvec(ndx) = exp(lam1);
    end
    OCSmod = Dmat * aAvec;
    
    out.aA = aAvec;
    
    % Analyze the distibution of OCS value above the model as a function of aA
    % scaling factor

    f1 = 0;
    f2 = 2.5;
    Nf = 1000;
    CDF = NaN(1,Nf);
    maxCDFrep = max(CDFrep);
    iterating = true;
    while iterating
        f = linspace(f1,f2,Nf);
        for n=1:Nf
            ndx = dat.OCSm2 <= f(n)*OCSmod;
            CDF(n) = sum(ndx)/Ndat;
        end
        if CDF(end) < maxCDFrep
            f2 = 2*f2;
        else
            iterating = false;
        end
    end

    frep = NaN(size(CDFrep));
    for nrep=1:Nrep
        ndx = find(CDF < CDFrep(nrep));
        ndx = ndx(end);
        if (ndx == Nf)
            idx = [ndx-1 ndx];
        else
            idx = [ndx ndx+1];
        end
        frep(nrep) = interp1(CDF(idx),f(idx),CDFrep(nrep),'linear','extrap');
    end
    out.CDFaAScaleFact = frep;
    
end

% Generate output file name
outfile = [outroot                                  ...
           '_' datestr(min(dat.DateNum),'yyyymmdd') ...
           '_' datestr(max(dat.DateNum),'yyyymmdd') ...
           '_Ns' num2str(Nsat) '_Nt' num2str(Ntrk)  ...
           '_Nd' num2str(Ndat) '_Nc' num2str(Ncomp)];

% Set up for plotting

if Plotting
    
    figure(1); clf;

    xfsz = 11;
    yfsz = xfsz;

    xfwt = 'bold';
    yfwt = xfwt;

    tfsz = xfsz;
    tfwt = 'bold';
    tfan = 'italic';

    afsz = xfsz;
    afwt = 'bold';
    alwd = 0.5;

    msiz = 2;
    
    % Generate title
    titl = cell(1,1);
    nt = 1;  titl{nt} = [strrep(datpth,'_','\_') ...
        ' (Nsat = ' num2str(Nsat) ', Ntrk = ' num2str(Ntrk) ...
        ', Ndat = ' num2str(Ndat) ')'];
    UTearliest = datestr(min(dat.DateNum),'yyyy-mm-dd HH:MM:SS');
    UTlatest   = datestr(max(dat.DateNum),'yyyy-mm-dd HH:MM:SS');
    nt=nt+1; titl{nt} = ['Earliest obs. date = ' UTearliest];
    nt=nt+1; titl{nt} = ['Latest   obs. date = ' UTlatest];
    if Fscale ~= 1
        nt=nt+1; titl{nt} = ['Flux scale factor = ' ...
            smart_exp_format(Fscale,Ndig)];
    end
    nt0 = numel(titl);
    
    % Set x-axis range
    xrng = plot_range(dat.Phase,0.05);
    xrng(xrng < 0) = 0;
    xrng(xrng > 180) = 180;

    % Plot the multicomponent OCS model, if required    
    if Ncomp > 0
    
        clf;
        mrkr = 'o';
        lwid = 0.5;
        semilogy(dat.Phase,dat.OCSm2, ...
                'LineStyle','none', ...
                'LineWidth',lwid, ...
                'Marker',mrkr, ...
                'MarkerSize',msiz, ...
                'MarkerFaceColor','k', ...
                'MarkerEdgeColor','k');
        hold on;
        [~,srt] = sort(dat.Phase);
        plot(dat.Phase(srt),OCSmod(srt),'-r','LineWidth',1);
        hold off;
        xlim(xrng);
        xlabl = 'Phase Angle (deg)';
        ylabl = 'OCS (m^2)';
        xlabel(xlabl,'FontSize',xfsz,'FontWeight',xfwt);
        ylabel(ylabl,'FontSize',yfsz,'FontWeight',yfwt);

        nt=nt+1; titl{nt} = ['Least absolute magnitude deviation ' ...
            num2str(Ncomp) '-component solution'];
        for n=1:Ncomp
            titl{nt+n} = [comp{n} ': aA = ' ...
                smart_exp_format(aAvec(n),3) ' m^2'];
        end
        nt=numel(titl)+1; titl{nt} = ' ';        
        title(titl,'FontSize',tfsz,'FontWeight',tfwt,'FontAngle',tfan);
        
        set(gca,'FontSize',afsz,'FontWeight',afwt,'LineWidth',alwd);

        saveas(gcf,[fullfile(outpath,outfile) '_OCSPhase.png']);

        nt=numel(titl); titl{nt} = ['Scaling factors: ' ...
            smart_exp_format(frep(1),3) ' (' smart_exp_format(100*CDFrep(1),3) '%) ' ...
            smart_exp_format(frep(2),3) ' (' smart_exp_format(100*CDFrep(2),3) '%) ' ...
            smart_exp_format(frep(3),3) ' (' smart_exp_format(100*CDFrep(3),3) '%)'];
        nt=nt+1; titl{nt} = ' ';

        clf;
        plot(f,CDF,'-k','LineWidth',1);
        grid on;
        xlabl = 'aA Scaling Factor';
        ylabl = 'CDF';
        xlabel(xlabl,'FontSize',xfsz,'FontWeight',xfwt);
        ylabel(ylabl,'FontSize',yfsz,'FontWeight',yfwt);
        title(titl,'FontSize',tfsz,'FontWeight',tfwt,'FontAngle',tfan);
        set(gca,'FontSize',afsz,'FontWeight',afwt,'LineWidth',alwd);

        saveas(gcf,[fullfile(outpath,outfile) '_aACDF.png']);
        
    end

    % Plot a CDF of range-normalized magnitudes
    % Put quantiles in title
    titl = titl(1:nt0); nt = numel(titl);
    nt=nt+1; titl{nt} = ['Magnitude quantiles: ' ...
        smart_exp_format(mrep(1),3) ' (' num2str(100*CDFrep(1)) '%) ' ...
        smart_exp_format(mrep(2),3) ' (' num2str(100*CDFrep(2)) '%) ' ...
        smart_exp_format(mrep(3),3) ' (' num2str(100*CDFrep(3)) '%)'];
    nt=nt+1; titl{nt} = ['Magnitude statistics: ' ...
        smart_exp_format(mhi,3) ' (05%) ' ...
        smart_exp_format(mmd,3) ' (50%) ' ...
        smart_exp_format(mlo,3) ' (95%)'];
    nt=nt+1; titl{nt} = ' ';

    clf;
    ndx = X <= mmd;
    xx = (X(ndx)-mmd)/siglo/sqrt(2);
    Fmod = NaN(size(F));
    Fmod(ndx) = 1 - 0.5*(1+erf(xx));
    ndx = ~ndx;
    xx = (X(ndx)-mmd)/sighi/sqrt(2);
    Fmod(ndx) = 1 - 0.5*(1+erf(xx));
    plot([mlo mmd mhi],[0.95 0.50 0.05],'o', ...
        'MarkerFaceColor','r','MarkerEdgeColor','r');
    hold on;
    plot(X,Fmod,'--m','LineWidth',1);
    plot(X,F,'-k','LineWidth',1);
    hold off;
    
    set(gca, 'XDir', 'reverse');
    grid on;
    xlabl = ['Magnitude (Range = ' num2str(Rnormkm) ' km)'];
    ylabl = 'CDF';
    xlabel(xlabl,'FontSize',xfsz,'FontWeight',xfwt);
    ylabel(ylabl,'FontSize',yfsz,'FontWeight',yfwt);
    title(titl,'FontSize',tfsz,'FontWeight',tfwt,'FontAngle',tfan);
    set(gca,'FontSize',afsz,'FontWeight',afwt,'LineWidth',alwd);
    grid on;

    saveas(gcf,[fullfile(outpath,outfile) '_MagRNormCDF.png']);

    % Plot the range-norm magnitude
    clf;
    mrkr = 'o';
    lwid = 0.5;
    plot(dat.Phase,dat.MagNorm, ...
            'LineStyle','none', ...
            'LineWidth',lwid, ...
            'Marker',mrkr, ...
            'MarkerSize',msiz, ...
            'MarkerFaceColor','k', ...
            'MarkerEdgeColor','k');
    hold on;
    plot(xrng,[mrep(1) mrep(1)],'-.b','LineWidth',1);
    plot(xrng,[mrep(2) mrep(2)],'--b','LineWidth',1);
    plot(xrng,[mrep(3) mrep(3)],':b','LineWidth',1);
    plot(xrng,[mcut mcut],'-r','LineWidth',1);
    hold off;
    xlim(xrng);
    set(gca, 'YDir', 'reverse');
    yrng = plot_range([dat.MagNorm; mcut],0.05);
    ylim(yrng);
    xlabl ='Phase Angle (deg)';
    ylabl = ['Magnitude (Range = ' num2str(Rnormkm) ' km)'];
    xlabel(xlabl,'FontSize',xfsz,'FontWeight',xfwt);
    ylabel(ylabl,'FontSize',yfsz,'FontWeight',yfwt);
    
    % Put SATCON-1 cutoff in title
    nt=numel(titl)-1;
    titl{nt} = ['Fraction brighter than SATCON-1 limit: ' ...
                smart_exp_format(100*fcut,Ndig) '%'];
    title(titl,'FontSize',tfsz,'FontWeight',tfwt,'FontAngle',tfan);
    set(gca,'FontSize',afsz,'FontWeight',afwt,'LineWidth',alwd);

    saveas(gcf,[fullfile(outpath,outfile) '_MagPhase.png']);
    
end

% Save output structure
if UseMMTSaveFiles
    save([fullfile(outpath,outfile) '_AnalyzeMMTData.mat'],'out');
end

return
end

% ----------------- END OF CODE ------------------
%
% Please record any changes to the software in the change history
% shown below:
%
% ----------------- CHANGE HISTORY ------------------
% Developer |���� Date��� | Description
% ---------------------------------------------------
% D. Hall��� |� 2024-Nov �| Initial version.
% =========================================================================
%
% Copyright (c)�2024�United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% =========================================================================