-
Notifications
You must be signed in to change notification settings - Fork 5
/
orbit.m
1826 lines (1816 loc) · 68.1 KB
/
orbit.m
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
classdef orbit
%static
properties(Constant,GetAccess=private)
%list of data fields
%NOTICE: needs updated when adding a new data type
data_type_list=struct(...
'pos',struct(...
'label','position',...
'size',3,...
'xyz',struct('units',{{'m', 'm', 'm'}},'names',{{'x', 'y', 'z'}}),...
'sph',struct('units',{{'deg','deg','m'}},'names',{{'lon','lat','rad'}})),...
'vel',struct(...
'label','velocity',...
'size',3,...
'xyz',struct('units',{{'m/s', 'm/s', 'm/s'}},'names',{{'x', 'y', 'z'}}),...
'sph',struct('units',{{'deg/s','deg/s','m/s'}},'names',{{'azim','elev','rad'}})),...
'acc',struct(...
'label','acceleration',...
'size',3,...
'xyz',struct('units',{{'m/s^2', 'm/s^2', 'm/s^2'}},'names',{{'x', 'y', 'z'}}),...
'sph',struct('units',{{'deg/s^2','deg/s^2','m/s^2'}},'names',{{'azim','elev','rad'}})),...
'pos_cor',struct(...
'label','pos. correlation',...
'size',6,...
'xyz',struct('units',{{'m^2', 'm^2', 'm^2', 'm^2', 'm^2', 'm^2'}}, 'names',{{'xx','yy','zz','xy','xz','yz'}}),...
'sph',struct('units',{{'deg^2','deg^2','m^2','deg^2','deg*m','deg*m'}},'names',{{'xx','yy','zz','xy','xz','yz'}})),...
'clk',struct(...
'label','clock correction',...
'size',1,...
'xyz',struct('units',{{'s'}}, 'names',{{'t'}}),...
'sph',struct('units',{{'1/s'}},'names',{{'f'}})),...
'clk_cor',struct(...
'label','clk. corr. correlation',...
'size',4,....
'xyz',struct('units',{{'s^2','m.s', 'm.s', 'm.s'}},'names',{{'tt','xt','yt','zt'}}),...
'sph',struct('units',{{'s^2','deg.s','deg.s','m.s'}},'names',{{'tt','xt','yt','zt'}}))...
);
%default value of parameters
%NOTICE: needs updated when adding a new parameter
parameter_list={...
'satname', 'unknown',@ischar;...
'frame', 'crs', @(i) orbit.isframe(i);...
'geodatum' , 'grs80', @ischar;...
'sp3id', 'Xnn', @ischar;...
'ctype', 'xyz', @ischar;...
'orbit_type','unknown',@ischar;...
'data_used', 'unknown',@ischar;...
'agency', 'unknown',@ischar;...
'data_dir', file.orbdir('data'),@ischar;...
};
%These parameter are considered when checking if two data sets are
%compatible (and only these).
%NOTICE: needs updated when adding a new data type (if relevant)
compatible_parameter_list={'satname','frame'};
%These are parameters relevant to the SP3 format; the _f fields defined the convertion from SP3 to SI units
%ftp://igs.org/pub/data/format/sp3c.txt
%TODO: add vel_cor and ckr_cor
sp3_parameter_list=struct(...
'pos_f', 1e3,... %km
'pos_gap', 0,... %value to patch "Bad or absent positional values"
'clk_f', 1e-6,... %microseconds
'clk_gap', 999999.999999,... %value to patch "Bad or absent clock values"
'pos_cor_f', 1e-3,... %mm (used in the auto-correlation fields, represented by STDs)
'pos_cor_gap', 0,... %value to patch non-existing correlations (SP3 standard says it should be blank but that's dum)
'clk_cor_f',1e-12,... %picoseconds
'clk_cor_gap', 0,... %value to patch non-existing correlations (SP3 standard says it should be blank but that's dum)
'xcor_f', 1e-7,... %correlation coefficient factor (used in the cross-correlation fields for pos_cor and clk_cor)
'vel_f', 1e1,... %dm/s (go figure...)
'vel_gap', 0,... %value to patch "Bad or absent velocity values"
'ckr_f', 1e1,... %10**-4 microseconds/second (I kid you not)
'ckr_gap', 999999.999999,... %value to patch "Bad or absent clock-rate values"
'version_list',{{'k','c'}}...
);
end
%NOTICE: needs updated when adding a new parameter
properties
%parameters
satname
frame
geodatum
sp3id
ctype
orbit_type
data_used
agency
data_dir
end
%NOTICE: needs updated when adding a new data type
properties(SetAccess=private)
%data types
pos
vel
acc
pos_cor
clk
clk_cor
end
%private (visible only to this object)
properties(GetAccess=private)
localframei
end
%calculated only when asked for
properties(Dependent)
time
localframe
tsys
end
methods(Static)
%interface methods to object constants
function out=data_types
out=fieldnames(orbit.data_type_list);
end
function out=data_type_unit(dt,ctype)
if ~exist('ctype','var') || isempty(ctype)
ctype=orbit.parameters('ctype');
end
out=orbit.data_type_list.(dt).(ctype).units;
end
function out=data_type_name(dt,ctype)
if ~exist('ctype','var') || isempty(ctype)
ctype=orbit.parameters('ctype');
end
out=orbit.data_type_list.(dt).(ctype).names;
end
function v=data_type_parameters(n)
%build varargs cell array for data_type_list
dtl_fn=fieldnames(orbit.data_type_list);
v=cell(numel(dtl_fn),3);
for i=1:numel(dtl_fn)
s=orbit.data_type_list.(dtl_fn{i}).size;
v(i,:)={dtl_fn{i}, zeros(n,s), @(i) isnumeric(i) && all(size(i)==[n,s])};
end
end
function out=parameters(varargin)
persistent v
if isempty(v); v=varargs(orbit.parameter_list); end
out=v.picker(varargin{:});
end
%translation methods
function out=translateframe(in)
if ischar(in)
switch lower(in)
case {'crs','crf','eci','icrf','gcrf','j2000','eme2000','celestial','inertial'}
out='crf';
case {'m50'}
out='m50';
case {'teme'}
out='teme';
case {'trs','trf','ecf','ecef','itrf','terrestrial','rotating','co-rotating',}
out='trf';
otherwise
out='';
end
else
out='';
end
end
function out=isframe(in)
out=~isempty(orbit.translateframe(in));
end
%NOTICE: satnames are: {satellite name}-{orbit type}-{processing center}
function out=translatesat(in)
in=strsplit(in,'-');
in=in{1};
%search for satellite name
switch lower(in)
case {'champ','ch'}
out='ch';
case {'grace_a','gracea','grace a','ga'}
out='ga';
case {'grace_b','graceb','grace b','gb'}
out='gb';
case {'swarm_a','swarma','swarm a','swma','sa','l47'}
out='sa';
case {'swarm_b','swarmb','swarm b','swmb','sb','l48'}
out='sb';
case {'swarm_c','swarmc','swarm c','swmc','sc','l49'}
out='sc';
case {'goce','go'}
out='go';
case {'unknown','test'}
out=in;
otherwise
error(['cannot handle satellite ''',in,'''.'])
end
end
function out=translatesatname(in)
%search for satellite name
switch orbit.translatesat(in)
case 'ch'; out='CHAMP';
case 'ga'; out='GRACE-A';
case 'gb'; out='GRACE-B';
case 'sa'; out='Swarm-A';
case 'sb'; out='Swarm-B';
case 'sc'; out='Swarm-C';
case 'go'; out='GOCE';
case {'unknown','test'}; out=in;
otherwise
error(['cannot handle satellite ''',in,'''.'])
end
end
function out=translatesp3id(in)
switch orbit.translatesat(in)
% case 'ch'
% out='TODO!';
% case 'ga'
% out='TODO!';
% case 'gb'
% out='TODO!';
case 'sa'; out='L47';
case 'sb'; out='L48';
case 'sc'; out='L49';
% case 'go'
% out='TODO!';
otherwise
out='unknown';
warning(['Could not resolve the SP3 id for satellite ''',in,'''.'])
end
end
function out=translate_orbit_type(in)
in=strsplit(in,'-');
in=in{2};
%add more aliases/orbit types here if needed
switch lower(in)
case {'l1b','l1'} ; out='l1b';
case {'l2','sp3xcom'} ; out='l2';
case {'kin','sp3xkin'} ; out='kin';
otherwise
error(['cannot handle satellite ''',in,'''.'])
end
end
function out=translate_processing_center(in)
in=strsplit(in,'-');
in=in{3};
%add more aliases/orbit types here if needed
switch lower(in)
case {'tud','tudelft'}; out='tud';
case {'ifg','tug'} ; out='ifg';
case 'aiub' ; out='aiub';
otherwise
error(['cannot handle satellite ''',in,'''.'])
end
end
%data source definitions
function out=nrtdm_product(in)
switch orbit.translatesat(in)
case 'ch'
out='CH_Basic/Orbit_CH-OG-3-RSO';
case 'ga'
out='GA_Basic/Orbit_NAVSOL';
case 'gb'
out='GB_Basic/Orbit_NAVSOL';
case 'sa-l1b'
out='SA_Basic/Orbit_L1B';
case 'sb-l1b'
out='SB_Basic/Orbit_L1B';
case 'sc-l1b'
out='SC_Basic/Orbit_L1B';
case 'sa-kin'
out='SA_Basic/Orbit_KIN';
case 'sb-kin'
out='SB_Basic/Orbit_KIN';
case 'sc-kin'
out='SC_Basic/Orbit_KIN';
case 'sa-l2'
out='SA_Basic/Orbit_L2';
case 'sb-l2'
out='SB_Basic/Orbit_L2';
case 'sc-l2'
out='SC_Basic/Orbit_L2';
otherwise
error(['BUG TRAP: unknown NRTDM product for satellite ''',in,''', debug needed!'])
end
end
%the function <format>_filename below define the filenames given sat, date and dir
%for the data of different formats
function [filename,dirname]=aiub_filename(satname,start,data_dir)
if ~exist('data_dir','var') || isempty(data_dir)
data_dir=orbit.parameters('data_dir');
end
switch orbit.translatesat(satname)
case 'sa'; prefix='SWMA';
case 'sb'; prefix='SWMB';
case 'sc'; prefix='SWMC';
end
assert(strcmp(orbit.translate_orbit_type(satname),'kin'),'Can only handle kinematic orbits for ''aiub'' format')
doy=time.FromDateTime(start,'yeardoysec');
filename=[prefix,num2str(doy(1)-round(doy(1)/1e3)*1e3),num2str(doy(2),'%03d'),'_S20.KIN.gz'];
dirname=fullfile(data_dir,'gswarm','aiub','orbit',num2str(year(start)));
end
function [filename,dirname]=ifg_filename(satname,start,data_dir)
if ~exist('data_dir','var') || isempty(data_dir)
data_dir=orbit.parameters('data_dir');
end
switch orbit.translatesat(satname)
case 'sa'; prefix='SwarmA-kinematicOrbit';
case 'sb'; prefix='SwarmB-kinematicOrbit';
case 'sc'; prefix='SwarmC-kinematicOrbit';
end
assert(strcmp(orbit.translate_orbit_type(satname),'kin'),'Can only handle kinematic orbits for ''ifg'' format')
filename=[prefix,'-',num2str(year(start)),'-',num2str(month(start),'%02d'),'-',num2str(day(start),'%02d'),'.txt.gz'];
dirname=fullfile(data_dir,'gswarm','ifg','orbit','ascii',num2str(year(start)));
end
function [filename,dirname]=tudelft_filename(satname,start,data_dir)
if ~exist('data_dir','var') || isempty(data_dir)
data_dir=orbit.parameters('data_dir');
end
switch orbit.translatesat(satname)
case 'sa'; prefix='SWARMA';
case 'sb'; prefix='SWARMB';
case 'sc'; prefix='SWARMC';
end
assert(strcmp(orbit.translate_orbit_type(satname),'kin'),'Can only handle kinematic orbits for ''tudelft'' format')
doy=time.FromDateTime(start,'yeardoysec');
filename=[prefix,'.',num2str(doy(1)-round(doy(1)/1e3)*1e3),'.',num2str(doy(2),'%03d'),'_KIPP.sigma.gz'];
dirname=fullfile(data_dir,'gswarm','tudelft','orbit',num2str(year(start)));
end
function [filename,dirname]=swarm_filename(satname,start,data_dir)
if ~exist('data_dir','var') || isempty(data_dir)
data_dir=orbit.parameters('data_dir');
end
switch orbit.translatesat(satname)
case 'sa'; sat='A';
case 'sb'; sat='B';
case 'sc'; sat='C';
end
prefix='SW_OPER_SP3';
switch orbit.translate_orbit_type(satname)
case 'l2'; prefix=[prefix,sat,'COM_2__'];
case 'kin'; prefix=[prefix,sat,'KIN_2__'];
end
s=num2str(60-sum(time.leap_seconds<start));
filename=[prefix,...
datestr(start-days(1),'yyyymmdd'),'T2359',s,'_',...
datestr(start, 'yyyymmdd'),'T2359',s,'_'....
'0101.ZIP'];
dirname=fullfile(data_dir,'swarm','dissemination');
end
function [filename,dirname]=gswarm_filename(satname,start,data_dir)
if ~exist('data_dir','var') || isempty(data_dir)
data_dir=orbit.parameters('data_dir');
end
%build the filename
prefix='GSWARM_KO_S';
%add satellite
switch orbit.translatesat(satname)
case 'sa'; prefix=[prefix,'A'];
case 'sb'; prefix=[prefix,'B'];
case 'sc'; prefix=[prefix,'C'];
end
%this must be a kinematic orbit
assert(strcmp(orbit.translate_orbit_type(satname),'kin'),'Can only handle kinematic orbits for ''gswarm'' format')
%add processing center
prefix=[prefix,'_',upper(orbit.translate_processing_center(satname)),'_'];
%add time stamp
filename=[prefix,...
datestr(start,'yyyy-mm-dd'),'_',num2str(time.date2doy(datenum(start)),'%03d'),'_'....
];
%append suffix
switch orbit.translate_processing_center(satname)
case 'tud' ; filename=[filename,'01.sigma.gz']; subdir='tudelft';
case 'ifg' ; filename=[filename,'06.txt.gz' ]; subdir='ifg';
case 'aiub'; filename=[filename,'01.KIN.gz' ]; subdir='aiub';
end
%define data dir
dirname=fullfile(data_dir,'gswarm',subdir,'orbit',num2str(year(start)));
end
%wrapper for the <format>_filename routines, transparent for different <format>s
function out=filename(format,satname,start,varargin)
p=machinery.inputParser;
p.addRequired( 'satname', @ischar);
p.addRequired( 'format', @ischar);
p.addRequired( 'start', @(i) isdatetime(i) && isscalar(i));
p.addParameter('data_dir',...
orbit.parameters('data_dir'),...
orbit.parameters('validation','data_dir'));
p.parse(format,satname,start,varargin{:});
%picking interface routine
interface=str2func(['orbit.',format,'_filename']);
%call interface routines
[filename,dirname]=interface(satname,start,p.Results.data_dir);
out=fullfile(dirname,filename);
end
%loads data from one single ASCII file, the format can be given or it
%is discovered from the header. Also handles compressed files.
%TODO: most of the zip-handling functionalityhas been implemented in simpletimeseries as is probably duplicate here.
function obj=load_ascii(filename,varargin)
p=machinery.inputParser;
p.addRequired( 'filename', @ischar);
p.addParameter('asciiformat','', @ischar);
% parse it
p.parse(filename,varargin{:});
%unwrap wildcards and place holders (output is always a cellstr)
filename=file.unwrap(filename,varargin{:});
assert(iscellstr(filename),['BUG TRAP: expecting file.unwrap to return a cellstr, not a ',class(filename),'.'])
%trivial call
if numel(filename)==0
obj=[];
disp('WARNING: input argument ''filename'' is empty, skipping.')
return
end
%if argument contains multiple filenames, then load all those files
if numel(filename)>1
for i=1:numel(filename)
disp(['reading data from file ',filename{i}])
%read the data from a single file
obj_now=orbit.load_ascii(filename{i},varargin{:});
%skip if empty
if isempty(obj_now)
continue
end
%append or initialize
if ~exist('obj','var')
obj=obj_now;
else
try
obj=obj.append(obj_now);
catch
obj=obj.augment(obj_now);
end
end
end
%in case there are no files, 'filename' will be empty and the loop will be skipped
if ~exist('obj','var')
obj=[];
end
return
end
%reduce cellstr
filename=filename{1};
%trivial call
if exist(filename,'file')==0
obj=[];
disp(['WARNING: cannot find file ''',filename,''', skipping it.'])
return
end
% retrieve the ascii format from the first line of the file, if not given in input arguments.
if isempty(p.Results.asciiformat)
%read first line of file
fid=file.open(filename);
hline = fgetl(fid);
fclose(fid);
%assign format ID
if contains(hline,'#c')
formatID='sp3c';
elseif contains(hline,'#k')
formatID='sp3k';
elseif contains(hline,'ITSG')
formatID='ifg';
elseif contains(hline,'LEOPOD') || contains(hline,'AIUB')
formatID='aiub';
else
formatID='numeric';
end
else
%propagate
formatID=p.Results.asciiformat;
end
%branch on format
switch lower(formatID)
case 'ifg'
[t,p,pc,header] = read_ifg(filename);
args={'pos',p,'pos_cor',pc};
case 'aiub'
[t,p,pc,m,header] = read_aiub(filename);
args={'pos',p,'pos_cor',pc,'mask',m};
case {'numeric','tudelft'}
[t,p,pc,c,cc,header] = read_numeric(filename);
args={'pos',p,'pos_cor',pc,'clk',c,'clk_cor',cc};
case {'sp3c','sp3xcom','sp3k'}
[t,p,v,pc,c,cc,header] = read_sp3(filename);
args={'pos',p,'pos_cor',pc,'vel',v,'clk',c,'clk_cor',cc};
otherwise
error(['unknown format ''',format,'''.'])
end
if ~isempty(t)
obj=orbit(t,...
args{:},...
'format', header.timeformat,...
'timesystem',header.timesystem,...
'satname', header.satname,...
'sp3id', header.sp3id,...
'frame', header.frame,...
'geodatum', header.geodatum,...
'orbit_type',header.type,...
'data_used', header.data_used,...
'agency', header.agency...
);
else
obj=[];
end
end
%reads data in any format, over any time period
function obj=import(format,satname,start,stop,varargin)
p=machinery.inputParser;
p.addRequired( 'format', @ischar);
p.addRequired( 'satname', @ischar);
p.addRequired( 'start', @(i) isdatetime(i) && isscalar(i));
p.addRequired( 'stop', @(i) isdatetime(i) && isscalar(i));
p.addParameter('cut24h', false, @islogical);
p.addParameter('resample',seconds(0),@isduration);
p.addParameter('only_convert_to_mat', false, @islogical);
% parse it
p.parse(format,satname,start,stop,varargin{:});
%clean
varargin=cells.vararginclean(varargin,{'cut24h','resample','only_convert_to_mat'});
% branch on format
switch lower(format)
case 'nrtdm'
% retrieve product name
product_name=orbit.nrtdm_product(satname);
% retrieve data
p=nrtdm(product_name,start,stop,varargin{:});
% initialize data according to its dimension
switch p.metadata.dimension
case 3
obj=orbit(p.ts.t,...
'pos',p.ts.y,'pos_units',p.metadata.units,...
'frame','ecf',...
varargin{:}...
);
case 6
obj=orbit(p.ts.t,...
'pos',p.ts.y(:,1:3),'pos_units',p.metadata.units(1:3),...
'vel',p.ts.y(:,4:6),'vel_units',p.metadata.units(4:6),...
'frame','ecf',...
varargin{:}...
);
case 9
obj=orbit(p.ts.t,...
'pos',p.ts.y(:,1:3),'pos_units',p.metadata.units(1:3),...
'vel',p.ts.y(:,4:6),'vel_units',p.metadata.units(4:6),...
'acc',p.ts.y(:,7:9),'acc_units',p.metadata.units(7:9),...
'frame','ecf',...
varargin{:}...
);
otherwise
error(['cannot handle data of size ',num2str(p.metadata.dimension).'.'])
end
otherwise
% build required file list (rounding start/stop to the start of the day
day_list=simpletimeseries.list(dateshift(start,'start','day'),dateshift(stop,'start','day'),days(1));
file_list=cell(size(day_list));
for i=1:numel(day_list)
file_list{i}=orbit.filename(format,satname,day_list(i),varargin{:});
end
% load data
first=true;
for i=1:numel(file_list)
%check if mat file is already available
[d,f]=fileparts(file_list{i});
mat_file=fullfile(d,[f,'.mat']);
if ~file.exist(mat_file)
% load ascii data
obj_now=orbit.load_ascii(file_list{i},'asciiformat',format);
if ~isempty(obj_now)
if p.Results.resample>seconds(0)
obj_now=obj_now.op('resample',p.Results.resample);
end
%save satname (those derived from the headers miss the '-kin' or '-l2' part)
obj_now.satname=satname;
%update sp3id if needed
if strcmp(obj_now.sp3id,'unknown') || strcmp(obj_now.sp3id,orbit.parameters('sp3id'))
obj_now.sp3id=orbit.translatesp3id(obj_now.satname);
end
% save it in mat format for next time
save(mat_file,'obj_now');
end
else
if ~p.Results.only_convert_to_mat
% load mat data
disp(['loading file ',mat_file])
S=load(mat_file);
obj_now=S.obj_now;
end
end
% if only converting orbits to mat, skip further operations
if p.Results.only_convert_to_mat
obj_now=[];
end
%skip if orbit file is missing
if isempty(obj_now)
continue
end
%cut to 24h if requested or if numerous files are being loaded
%(otherwise appending doesn't work)
if p.Results.cut24h || numel(file_list)>1
obj_now=obj_now.op('trim',day_list(i),day_list(i)+hours(24)-seconds(1));
end
%skip if trimming removes all data
if isempty(obj_now)
continue
end
%create/append
if first
obj=obj_now;
first=false;
else
% append remaining days
obj=obj.op('append',obj_now);
end
end
%nothing else to do if only converting orbits to mat
if p.Results.only_convert_to_mat
obj=[];
return
end
%fill gaps
obj=obj.op('trim',start,stop).op('resample');
end
end
%general test for the current object
function out=test_parameters(field,varargin)
%basic parameters
switch field
case 'l'; out=100; return
end
%optional parameters
switch numel(varargin)
case 0
l=orbit.test_parameters('l');
case 1
l=varargin{1};
end
%more parameters
switch lower(field)
case 'pos'
out=randn(l,3);
case 'vel'
out=randn(l,3)+1;
case 'acc'
out=randn(l,3)+2;
case 'step'
out=10;
case 'time'
k=orbit.test_parameters('step');
out=now+(1:k:k*l);
case 'start'
out=datetime(2015,2,1,23,0,0);
case 'stop'
if ~isduration(l)
error(['expecting input ''l'' to be of class ''duration'', not ''',class(l),'''.'])
end
out=orbit.test_parameters('start')+l;
case 'duration'
out=hours(3);
case 'satname'
out='swarma-kin';
case 'satname-l2'
out='swarma-l2';
case {'tudelft','aiub','ifg'}
out=orbit.import(...
field,...
orbit.test_parameters('satname'),...
orbit.test_parameters('start'),...
orbit.test_parameters('stop',l));
case 'sp3xcom'
out=orbit.import(...
'sp3xcom',...
orbit.test_parameters('satname-l2'),...
orbit.test_parameters('start'),...
orbit.test_parameters('stop',l));
otherwise
error(['unknown field ',field,'.'])
end
end
function out=test(l)
if ~exist('l','var') || isempty(l)
l=1e4;
end
switch class(l)
case 'cell'
figure
out=cell(size(l));
for i=1:numel(l)
out{i}=orbit.test(l{i}); hold on
end
plot_line_color
legend(l)
case 'char'
switch lower(l)
case 'formats'
out=orbit.test({'tudelft','aiub','ifg','sp3xcom'});
case 'rel'
a=orbit.test_parameters('tudelft',orbit.test_parameters('duration'));
b=orbit.test_parameters('ifg', orbit.test_parameters('duration'));
c=orbit.test_parameters('aiub', orbit.test_parameters('duration'));
d=orbit.test_parameters('sp3xcom',orbit.test_parameters('duration'));
out={d.relative(a),...
d.relative(b),...
d.relative(c)...
};
out{1}=out{1}.op('descriptor','tudelft');
out{2}=out{2}.op('descriptor','ifg');
out{3}=out{3}.op('descriptor','aiub');
if nargout==0
figure
for j=1:numel(out)
for i=1:3
subplot(3,numel(out),i+numel(out)*(j-1))
out{j}.pos.plot('column',i)
end
out{j}.print
end
end
case 'stats'
a=orbit.test('rel');
out=a.periodic_stats(orbit.test_parameters('duration')/10);
if nargout==0
figure
for i=1:3
subplot(3,1,i)
out.mean.pos.plot('column',i)
end
end
otherwise
out=orbit.test_parameters(l,hours(8));
if ~isa(out,'orbit')
error(['cannot handle test of type ''',l,'''.'])
end
out.pos.plot('columns',1,'line',{'o-'})
out.print
end
case 'double'
a=orbit(...
orbit.test_parameters('time',l),...
'pos',orbit.test_parameters('pos',l),...
'vel',orbit.test_parameters('vel',l),...
'acc',orbit.test_parameters('acc',l)...
);
figure
subplot(3,1,1)
a.pos.plot('columns',1)
subplot(3,1,2)
a.vel.plot('columns',1)
subplot(3,1,3)
a.acc.plot('columns',1)
otherwise
error(['cannot handle input ''l'' of class ''',class(l),'''.'])
end
end
end
methods
%% constructor
function obj=orbit(t,varargin)
p=machinery.inputParser;
p.addRequired('t',@(i) ~isscalar(i) || (isstruct(i) && isscalar(i))); %this can be vector char, double, datetime or scalar struct
%parse the arguments with the names defined in orbit.data_type_list
for j=1:numel(orbit.data_types)
%shorter names
dtn=orbit.data_types{j};
dts=orbit.data_type_list.(dtn).size;
%declare data types
p.addParameter( dtn, [], (@(i) isnumeric(i) && size(i,2)==dts && size(i,1)>0));
p.addParameter([dtn,'_units'], orbit.data_type_unit(dtn), @(i) iscellstr(i) && numel(i)==dts);
p.addParameter([dtn,'_names'], orbit.data_type_name(dtn), @(i) iscellstr(i) && numel(i)==dts);
end
%declare parameters p
[~,p,obj]=varargs.wrap('parser',p,'sinks',{obj},'sources',{orbit.parameters('obj')},'mandatory',{t},varargin{:});
%clean varargin
varargin=cells.vararginclean(varargin,p.Parameters);
% retrieve each data type
for j=1:numel(orbit.data_types)
%shorter names
data_type=orbit.data_types{j};
%skip if this data type is empty
if ~isempty(p.Results.(data_type))
%add new data type
obj=obj.add_data_type(...
t,...
data_type,p.Results.(data_type),...
'units', p.Results.([data_type,'_units']),...
'labels', p.Results.([data_type,'_names']),...
varargin{:}...
);
end
end
%initialize internal records
obj.localframei=[];
end
function obj=add_data_type(obj,t,data_type,data_value,varargin)
%simplify things
data_type=lower(data_type);
%parse input
p=machinery.inputParser;
p.addRequired('t' ,@(i) ~isscalar(i)); %this can be char, double, datetime
p.addRequired('data_type' ,@(i) ischar(i) && cells.isincluded(fieldnames(orbit.data_type_list),i));
p.addRequired('data_value',@(i) isnumeric(i) && all(size(data_value)==[numel(t),orbit.data_type_list.(data_type).size]));
p.addParameter('units',orbit.data_type_unit(data_type),@(i) iscellstr(i) && numel(i)==size(data_value,2))
p.addParameter('names',orbit.data_type_name(data_type),@(i) iscellstr(i) && numel(i)==size(data_value,2))
% parse it
p.parse(t,data_type,data_value,varargin{:});
varargin=cells.vararginclean(varargin,p.Parameters);
%sanity
assert(isempty(obj.(data_type)),['data of type ''',data_type,''' has already been created. Use another method to append data.'])
%call superclass for this data type
obj.(data_type)=simpletimeseries(...
p.Results.t,p.Results.data_value,...
'units', cells.patch_empty(p.Results.units,orbit.data_type_unit(data_type)),...
'labels',cells.patch_empty(p.Results.names,orbit.data_type_name(data_type)),...
varargin{:}...
);
end
function obj=copy_metadata(obj,obj_in,more_parameters,less_parameters)
if ~exist('less_parameters','var')
less_parameters={};
end
if ~exist('more_parameters','var')
more_parameters={};
end
pn=[orbit.parameters('list');more_parameters(:)];
for i=1:numel(pn)
%skip less parameters
if ismember(pn{i},less_parameters)
continue
end
%check if this is a relevant parameter to this object and obj_in
if isprop(obj,pn{i}) && isprop(obj_in,pn{i})
obj.(pn{i})=obj_in.(pn{i});
end
end
%propagate parameters of all non-empty data types
for j=1:numel(orbit.data_types)
%shorter names
data_type=orbit.data_types{j};
%sanity
if xor(isempty(obj.(data_type)),isempty(obj_in.(data_type)))
error(['error propagating metadata of type ',data_type,': it does not exist in both objects.'])
end
%skip if data type is empty
if ~isempty(obj.(data_type))
obj.(data_type)=obj.(data_type).copy_metadata(obj_in.(data_type),more_parameters,less_parameters);
end
end
end
function out=metadata(obj,more_parameters)
if ~exist('more_parameters','var')
more_parameters={};
end
warning off MATLAB:structOnObject
out=structs.filter(struct(obj),[orbit.parameters('list');more_parameters(:)]);
warning on MATLAB:structOnObject
end
function out=varargin(obj,more_parameters)
out=varargs(obj.metadata(more_parameters)).varargin;
end
%% info methods
function print(obj,tab)
if ~exist('tab','var') || isempty(tab)
tab=20;
end
disp(' --- Parameters --- ')
for i=1:numel(orbit.parameters('list'))
%shorter names
p=orbit.parameters('value',i);
disp([p,repmat(' ',1,tab-length(p)),' : ',str.show(obj.(p))])
end
% d_list=orbit.data_types;
d_list={'pos'};
for i=1:numel(d_list)
%shorter names
d=d_list{i};
if ~isempty(obj.(d))
disp([' --- ',d,' --- '])
obj.(d).print
end
end
end
%% time property
function t=get.time(obj)
t=[];
odt=orbit.data_types;
for i=1:numel(odt)
if ~isempty(obj.(odt{i}))
t=obj.(odt{i}).t;
break
end
end
assert(~isempty(t),'all data types are empty.')
end
function obj=set.time(obj,t)
odt=orbit.data_types;
for i=1:numel(odt)
if ~isempty(obj.(odt{i}))
obj.(odt{i})=obj.(odt{i}).interp(t,'interp1_args',{'spline'});
end
end
obj.check_time
end
function check_time(obj)
odt=orbit.data_types;
for i=2:numel(odt)
if ~obj.isempty(odt{i})
assert(obj.(odt{1}).istequal(obj.(odt{i})),...
['time domain discrepancy between data types ''',odt{1},''' and ''',odt{i},'''.']...
)
end
end
end
function out=get.tsys(obj)
out=obj.get('tsys');
end
function obj=set.tsys(obj,timesystem)
%get data types
odt=orbit.data_types;
%loop over all data types
for i=1:numel(odt)
%check if this data type is not empty and it responds to method
if ~isempty(obj.(odt{i}))
%check if this is a member
if isprop(obj.(odt{i}),'tsys')
%call this member (varargin is ignored)
obj.(odt{i}).tsys=timesystem;
end
end
end
%sanity
obj.check_time
end
%% satname property
function out=get.satname(obj)
out=obj.satname;
end
function obj=set.satname(obj,in)
obj.satname=orbit.translatesat(in);
end
%% general data_type scalar get method
function out=get(obj,method,varargin)
%get data types
odt=orbit.data_types;
%make room for outputs
out=cell(size(odt));
%loop over all data types
for i=1:numel(odt)
%check if this data type is not empty and it responds to method
if ~isempty(obj.(odt{i}))
%check if this is a member
if ismethod(obj.(odt{i}),method)
%use the method on it, pass additional arguments
out{i}=obj.(odt{i}).(method)(varargin{:});
elseif isprop(obj.(odt{i}),method)
%call this member (varargin is ignored)
out{i}=obj.(odt{i}).(method);
end
end
end
%remove dups and reduce to scalar if possible
out=cells.scalar(cells.rm_duplicates(out),'get');
%need to return something
assert(~isempty(out),'all data types are empty.')
end
%% management
function compatible(obj1,obj2,varargin)
%This method checks if the objectives are referring to the same
%type of data, i.e. the data length is not important.
parameters=orbit.compatible_parameter_list;
for i=1:numel(parameters)
if ~isequal(obj1.(parameters{i}),obj2.(parameters{i}))
error(['discrepancy in parameter ',parameters{i},': ''',...
obj1.(parameters{i}),''' ~= ''',obj2.(parameters{i}),'''.'])
end
end
%check that all data type as compatible as well
odt=orbit.data_types;
for i=1:numel(odt)
if ~isempty(obj1.(odt{i})) && ~isempty(obj2.(odt{i}))
obj1.(odt{i}).compatible(obj2.(odt{i}),varargin{:})
end
end
end
%object obj1 will have the time domain of obj2 (interpolated if needed)
function [obj1,obj2]=consolidate(obj1,obj2,varargin)
%compatibility check
obj1.compatible(obj2,varargin{:})
%consolidate all data types
counter=0;
odt=orbit.data_types;
for i=1:numel(odt)
if ~isempty(obj1.(odt{i})) && ~isempty(obj2.(odt{i}))
[obj1.(odt{i}),obj2.(odt{i})]=obj1.(odt{i}).interp2_lcm(obj2.(odt{i}));
counter=counter+1;
end
end
if counter==0
error('there were no common fields in the input objects.')
end
end
function out=isempty(obj,data_type)
if ~exist('data_type','var') || isempty(data_type)
odt=orbit.data_types;
for i=1:numel(odt)
if ~obj.isempty(odt{i});out=false;return;end
end
out=true;
else
out=isempty(obj.(data_type)) || all(obj.(data_type).y(:)==0);