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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
function conj = OCMDB_convert_conj(DB,params)
%==========================================================================
%
% Convert DB object to conj object (can be used to aid in writing test case
% CDMs)
%
% All output in ECI reference frame and MKS units unless otherwise noted.
%
%==========================================================================

% Initializations and defaults

if (nargin == 1); params = []; end

if ~isfield(params,'mat_file'); mat_file = []; else ...
        mat_file = params.mat_file; end
if isempty(mat_file); mat_file = 'OCMDB.mat'; end

if ~isfield(params,'save_UTlimited_mat_file')
    params.save_UTlimited_mat_file = false;
end

if ~isfield(params,'sort_UTlimited_mat_file') || ...
   ~params.save_UTlimited_mat_file
    params.sort_UTlimited_mat_file = false;
end

if ~isfield(params,'UTlimited_created_before_TCA')
    params.UTlimited_created_before_TCA = false;
end

if ~isfield(params,'UTlimited_on_TCA')
    params.UTlimited_on_TCA = true;
end

if ~isfield(params,'UTbegin'); UTbegin = []; else ...
        UTbegin = params.UTbegin; end

if ~isfield(params,'UTend'); UTend = []; else ...
        UTend = params.UTend; end

if ~isfield(params,'primary_set'); primary_set = []; else ...
        primary_set = params.primary_set; end

not_empty_primary_set = ~isempty(primary_set);

if ~isfield(params,'secondary_set'); secondary_set = []; else ...
        secondary_set = params.secondary_set; end

not_empty_secondary_set = ~isempty(secondary_set);

if ~isfield(params,'Pc_20m_cutoff'); Pc_20m_cutoff = []; else ...
        Pc_20m_cutoff = params.Pc_20m_cutoff; end
if isempty(Pc_20m_cutoff); Pc_20m_cutoff = -Inf; end

if ~isfield(params,'DCA_km_cutoff'); DCA_km_cutoff = []; else ...
        DCA_km_cutoff = params.DCA_km_cutoff; end
if isempty(DCA_km_cutoff); DCA_km_cutoff = Inf; end

if ~isfield(params,'Nconj_cutoff'); Nconj_cutoff = []; else ...
        Nconj_cutoff = params.Nconj_cutoff; end
if isempty(Nconj_cutoff); Nconj_cutoff = Inf; end

if ~isfield(params,'full_data_set'); full_data_set = []; else ...
        full_data_set = params.full_data_set; end
if isempty(full_data_set); full_data_set = true; end

if ~isfield(params,'remove_corrupt_data'); remove_corrupt_data = []; else ...
        remove_corrupt_data = params.remove_corrupt_data; end
if isempty(remove_corrupt_data); remove_corrupt_data = false; end

if ~isfield(params,'logfid'); logfid = []; else ...
        logfid = params.logfid; end

if ~isfield(params,'verbose'); verbose = []; else ...
        verbose = params.verbose; end
if isempty(verbose); verbose = 1; end

% Initializations

deg2rad = pi/180;

% Ensure proper number of columns

[~,Ncol] = size(DB); %#ok<NODEF>

if Ncol ~= 218
    error('Expecting 218 columns in OCMDB mat file.');
end

% Remove corrupt data if required

ind = DB(:,218) == 1;

if remove_corrupt_data

    DB(ind,:)  = [];
    
    if any(ind)
        logstr = ['  Found and eliminated ' num2str(sum(ind)) ...
            ' corrupt conjunctions of ' num2str(numel(ind)) '.'];
        log_string(logfid,logstr,[],verbose);    
    end
    
else
    
    if any(ind)
        logstr = ['  Found and retained ' num2str(sum(ind)) ...
            ' corrupt conjunctions of ' num2str(numel(ind)) '.'];
        log_string(logfid,logstr,[],verbose);    
    end
    
end

% Filter data out of specified date range

% if JDfilter
%     
%     [NN,~] = size(DB);
%     
%     ind = false(NN,1);
%     
%     if params.sort_UTlimited_mat_file
%         JDfilt = zeros(NN,1);
%     end
% 
%     for nn=1:NN
%         
%         if params.UTlimited_on_TCA
%             % OCM TCA date
%             JD = date2jd(DB(nn,12), DB(nn,13), DB(nn,14), ...
%                          DB(nn,15), DB(nn,16), DB(nn,17));
%         else
%             % OCM creation date
%             JD = date2jd(DB(nn, 3), DB(nn, 4), DB(nn, 5), ...
%                          DB(nn, 6), DB(nn, 7), DB(nn, 8));
%         end
%         
%         if params.sort_UTlimited_mat_file
%             JDfilt(nn) = JD;
%         end
%         
%         % Mark those out of specified date range, or those not matching
%         % specified primary or secondary
%         
%         if (JD < JDbegin) || (JD > JDend)
%             ind(nn) = true;
%         elseif not_empty_primary_set && ~ismember(DB(nn,1),primary_set)
%             ind(nn) = true;
%         elseif not_empty_secondary_set && ~ismember(DB(nn,2),secondary_set)
%             ind(nn) = true;
%         end
%         
%     end
%     
%     % Eliminate those out of date range, and save the raw DB mat file if
%     % requested
%     
%     if any(ind)
%     
%         % DB(ind,:) = [];
%         % JDfilt(ind) = [];
%         
%         DB = DB(~ind,:);
%         JDfilt = JDfilt(~ind,:);
%         
%         if params.save_UTlimited_mat_file
%             
%             if params.sort_UTlimited_mat_file
%                 [~,ind] = sort(JDfilt);
%                 DB = DB(ind,:);
%             end
%             
%             if params.UTlimited_created_before_TCA
%                 ind = DB(:,169) > 0;
%                 DB = DB(ind,:);
%             end
%         
%             filtered_file = fullfile(ppp,[fff '_UTlimited' eee]);
%             save(filtered_file,'DB');
%             OCMDB_rename_matfile(filtered_file,verbose);
%             
%         end
%         
%     end
%     
% end

% Return now when saving UTlimited files

if params.save_UTlimited_mat_file
    return;
end

% remove data that has Pc (w/ HBR=20m) less than cutoff
ind = DB(:,158) < Pc_20m_cutoff;
DB(ind,:)  = [];

% remove data that has nominal DCA greater than cutoff
ind = DB(:,19) > DCA_km_cutoff;
DB(ind,:)  = [];

% Number of conjunctions

[Nconj,~] = size(DB);

% Display results

logstr = ['  Number of loaded conjunctions = ' num2str(Nconj)];
log_string(logfid,logstr,[],verbose);

% Sort by TCA

logstr = '  Sorting by TCA...';
log_string(logfid,logstr,[],verbose);
[~,ndx] = sort(DB(:,166));
DB = DB(ndx,:);

% logstr = '  Sorting by conjunction creation time...';
% log_string(logfid,logstr,[],verbose);
% [~,ndx] = sort(DB(:,165));
% DB = DB(ndx,:);

% Build the covariance matrix blocks

CovBuild6 = triu(ones(6,6))';

CovBuild6(CovBuild6~=0)  = 1:21;

CovBuild6 = CovBuild6 + triu(CovBuild6',1);

% CovBuild3 = triu(ones(3,3))';
% CovBuild3(CovBuild3~=0)  = 1:6;
% CovBuild3 = CovBuild3 + triu(CovBuild3',1);

% Apply the user-specified maximum number of conjunctions

% Nconj_orig = Nconj;
Nconj = min(Nconj,Nconj_cutoff);

% Allocate the conjunction arrays

logstr = '  Allocating buffers...';
log_string(logfid,logstr,[],verbose);

conj.OCMDB_mat_file = mat_file;

conj.UTC            = cell(1,Nconj);  % UTC at nominal TCA
conj.UTCcreation    = cell(1,Nconj);  % UTC at nominal TCA
conj.id_string      = cell(1,Nconj);  % ID tag of this conjunction

conj.ob1            = NaN(1,Nconj);   % SCN for primary
conj.x1             = NaN(6,Nconj);   % ECI state
conj.C1             = NaN(6,6,Nconj); % ECI covariance

conj.ob2            = NaN(1,Nconj);   % SCN for secondary
conj.x2             = NaN(6,Nconj);   % ECI state
conj.C2             = NaN(6,6,Nconj); % ECI covariance

conj.HBR            = NaN(1,Nconj);   % Combined hard-body radii

if full_data_set
    
    conj.C1_UVW         = NaN(6,6,Nconj); % UVW covariance (e.g., RIC, RSW)
    conj.UTC_ObsLast1   = cell(1,Nconj);  % UTC of last obs
    conj.ObsAgeDays1    = NaN(1,Nconj);   % TCA-T(last obs.)
    conj.apo1           = NaN(1,Nconj);   % Apogee radius
    conj.per1           = NaN(1,Nconj);   % Perigee radius
    conj.inc1           = NaN(1,Nconj);   % Inclination

    conj.C2_UVW         = NaN(6,6,Nconj); % UVW covariance (e.g., RIC, RSW)
    conj.UTC_ObsLast2   = cell(1,Nconj);  % UTC of last obs.
    conj.ObsAgeDays2    = NaN(1,Nconj);   % TCA-T(last obs.)
    conj.apo2           = NaN(1,Nconj);   % Apogee radius
    conj.per2           = NaN(1,Nconj);   % Perigee radius
    conj.inc2           = NaN(1,Nconj);   % Inclination

    conj.Pc_JSPOC       = NaN(1,Nconj);
    conj.Pc_CAS         = NaN(1,Nconj);

    conj.Pc_Foster_20m  = NaN(1,Nconj);
    conj.Pc_Foster      = NaN(1,Nconj);
    conj.Pc_Foster_flag = NaN(1,Nconj);

    conj.Pc_Chan_20m    = NaN(1,Nconj);
    conj.Pc_Chan        = NaN(1,Nconj);
    conj.Pc_Chan_flag   = NaN(1,Nconj);
    
end
   
conj.Corrupt_flag   = NaN(1,Nconj);   % OCMBD corrupt (use/no-use) flag
 
% Loop through conjunctions 

logstr = '  Processing conjunctions...';
log_string(logfid,logstr,[],verbose);

progress_inc  = 10000;
progress_disp = (Nconj > progress_inc);

for nc = 1:Nconj
    
    % Get primary and secondary object numbers
    conj.ob1(nc) = DB(nc,1);
    conj.ob2(nc) = DB(nc,2);
    
    % TCA in UTC, in matlab datenum format
    TCA = DB(nc,166);    
    % TCA in a date string yyyy-mm-dd HH:MM:SS.FFF
    conj.UTC{nc} = datestr(TCA,'yyyy-mm-dd HH:MM:SS.FFF');

    % Tcreation in UTC, in matlab datenum format
    Tcr = DB(nc,165);    
    % TCA in a date string yyyy-mm-dd HH:MM:SS.FFF
    conj.UTCcreation{nc} = datestr(Tcr,'yyyy-mm-dd HH:MM:SS.FFF');
    
    % Combined hard-body radii
    conj.HBR(nc) = DB(nc,157);
    
    if full_data_set
        
        % Primary and secondary last obs.

        conj.UTC_ObsLast1{nc} = datestr(DB(nc,167),'yyyy-mm-dd HH:MM:SS.FFF');
        conj.UTC_ObsLast2{nc} = datestr(DB(nc,168),'yyyy-mm-dd HH:MM:SS.FFF');

        conj.ObsAgeDays1(nc) = DB(nc,170);
        conj.ObsAgeDays2(nc) = DB(nc,171);

        % Primary and secondary orbital params

        conj.apo1(nc) = DB(nc,51) * 1e3;
        conj.per1(nc) = DB(nc,52) * 1e3;
        conj.inc1(nc) = DB(nc,53) * deg2rad;

        conj.apo2(nc) = DB(nc,111) * 1e3;
        conj.per2(nc) = DB(nc,112) * 1e3;
        conj.inc2(nc) = DB(nc,113) * deg2rad;
        
    end
    
    % Primary object ECI state
    X1 = DB(nc,172:177); % [km km/s]
    
    % Primary object covariance 
    TriEls = DB(nc,73:93);
    C1UVW = TriEls(CovBuild6)/1e6; % [km^2 km^2/s km^2/s^2]
    
    % Convert UVW (same as the RIC) to ECI
    C1ECI = RIC2ECI(C1UVW,X1(1:3),X1(4:6));  % [km^2 km^2/s km^2/s^2]
    C1ECI = cov_make_symmetric(C1ECI);
    
    % Secondary object ECI state
    X2 = DB(nc,178:183); % [km km/s]
    
    % Secondary object covariance 
    TriEls = DB(nc,133:153);
    C2UVW = TriEls(CovBuild6)/1e6;  % [km^2 km^2/s km^2/s^2]
    
    % Convert UVW (same as the RIC) to ECI
    C2ECI = RIC2ECI(C2UVW,X2(1:3),X2(4:6));  % [km^2 km^2/s km^2/s^2]
    C2ECI = cov_make_symmetric(C2ECI);
    
    % % Combined covariance in ECI
    % CombCov = C1ECI(1:3,1:3) + C2ECI(1:3,1:3);
    % 
    % % Check the combined covariance against the database covariance
    % TriEls = DB(nc,196:201);
    % CombCovCheck = TriEls(CovBuild3);
    
    % Define the outputs in MKS units
    
    conj.x1(:,nc) = 1e3 * X1;
    conj.x2(:,nc) = 1e3 * X2;
    
    conj.C1(:,:,nc) = 1e6 * C1ECI;    
    conj.C2(:,:,nc) = 1e6 * C2ECI;
    
    if full_data_set
    
        conj.C1_UVW(:,:,nc) = 1e6 * C1UVW;
        conj.C2_UVW(:,:,nc) = 1e6 * C2UVW;

        conj.Pc_JSPOC(nc) = DB(nc,27);

        conj.Pc_CAS(nc) = DB(nc,156);

        conj.Pc_Foster_20m(nc) = DB(nc,158);
        conj.Pc_Foster(nc) = DB(nc,159);
        conj.Pc_Foster_flag(nc) = DB(nc,160);

        conj.Pc_Chan_20m(nc) = DB(nc,161);
        conj.Pc_Chan(nc) = DB(nc,163);
        conj.Pc_Chan_flag(nc) = DB(nc,164);
        
    end
    
    conj.Corrupt_flag(nc) = DB(nc,218);
    
    conj.id_string{nc} = ...
        sprintf('%05i%s%05i%s%04i%02i%02i%s%02i%02i%02i%s%04i%02i%02i%s%02i%02i%02i', ...
        DB(nc,1),'_conj_',DB(nc,2),'_',DB(nc,12:14),'_',DB(nc,15:17),'_',DB(nc,3:5),'_',DB(nc,6:8));

    if progress_disp && (mod(nc,progress_inc) == 0)
        logstr = ['    ' current_timestring() ' processed ' ...
                  num2str(nc) ' of ' num2str(Nconj)];
        log_string(logfid,logstr,[],verbose);        
    end
    
end

if progress_disp
    logstr = ['    ' current_timestring() ' processing complete'];
    log_string(logfid,logstr,[],verbose);
end 

% Save the conj structure for later runs

conj.UTbegin       = UTbegin; 
conj.UTend         = UTend;
conj.Pc_20m_cutoff = Pc_20m_cutoff;
conj.DCA_km_cutoff = DCA_km_cutoff;

% logstr = ['Writing file ' save_file];
% log_string(logfid,logstr,[],verbose);    
% 
% save(save_file,'conj','-v7.3');
% 
% logstr = 'Finished writing file';
% log_string(logfid,logstr,[],verbose);    

return;
end

function Csym = cov_make_symmetric(C)

% Make a covariance matrix symmetric.

szC = size(C);

if (numel(szC) ~= 2)
    error('Array needs to be a 2D matrix to make symmetric.');
end

if (szC(1) ~= szC(2))
    error('Matrix needs to be square to make symmetric.');
end

if ~issymmetric(C)
    Csym = (C+C')/2;

else

    Csym = C;

end



return;

end