-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfetchYumaData.m
259 lines (238 loc) · 12.4 KB
/
fetchYumaData.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
% ----------------------------------------------------------------------- %
% fetchYmaData() - This function fetches the current almanac data for %
% the given day. It calculates the current day of the year and reads %
% date from the 2nd Spce Operation Squadron website: %
% https://gps.afspc.af.mil/gps/archive/ %
% %
% ----------------------------------------------------------------------- %
% Created by Kurt Pedrosa -- May 18th 2017 %
% ----------------------------------------------------------------------- %
function full_almanac_data = fetchYumaData()
% Get day of the year
% Note: calculation of day of the year sometime throws an error
% because it increases the current day faster than the website.
current_date = clock;
year = current_date(1);
day_of_year = floor( now - datenum( year, 1, 0, 0, 0, 0 ));
fprintf('Today is day number %d of the year %d.\n', day_of_year, year );
% print a empty line for spacing
fprintf('\n');
% Fetch the data from the website
base_url = 'https://gps.afspc.af.mil/gps/archive/2017/almanacs/yuma/';
file_type = '.alm';
full_url = strcat( base_url, num2str( day_of_year ), file_type );
file_name = strcat( 'almanac_for_day_', num2str( day_of_year ), file_type );
if exist( file_name, 'file') == 0
% Check Matlab version
if verLessThan('matlab', '8.0.1') % Function websave() introduced in R2014b
% Try
try
disp('Fetching Yuma Almanac Data...');
% print a empty line for spacing
fprintf('\n');
urlwrite( full_url, file_name );
catch ME
if strcmp( ME.identifier, 'MATLAB:urlwrite:ConnectionFailed')
disp('Matlab does not have the YUMA almanac data website certificate as a trusted keystore.');
error('Go to https://www.mathworks.com/matlabcentral/answers/92506-how-can-i-configure-matlab-to-allow-access-to-self-signed-https-servers for a solution');
else
fprintf('%s\n', ME.identifier );
end
end
else
% If newer than R2015 use websave
try
disp('Fetching Yuma Almanac data...');
% print a empty line for spacing
fprintf('\n');
websave( file_name, full_url );
catch ME
if strcmp( ME.identifier, 'MATLAB:webservices:HTTP404StatusCodeError' )
delete *.html *.alm;
error('Web service 404 Error. Check website and Day of Year calculation.');
else
fprintf('%s\n', ME.identifier);
end
end
end
disp('Checking YUMA data for presence of 32 satellite vehicles.');
% print a empty line for spacing
fprintf('\n');
data_not_formated = ExtractData( file_name );
% Ensure that all 32 SVs are accounted for.
% If 32 SVs not present, then create a dummy data for the missing SVs
% Dummy date includes decimal values of alternatind one's and zero's
dummy_data =...
[ 63 0.0208330154418945 696320 4.18879020381111 4.99335085024861e-07 ...
5461.3330078125 0 4.18878995511504 4.18878995511504 ...
0.00130176544189453 4.96584107168019e-09 ];
if size( data_not_formated, 1 ) ~= 32
numb_of_columns = size( data_not_formated, 2 );
data_column_temp = [];
for count_columns = 1:numb_of_columns
data_column_temp = [ data_column_temp 0 ];
end
data_temp = [ data_not_formated ; data_column_temp ];
end
for count_index = 1:32
find_result = find( data_temp( :, 1 ) == count_index );
if isempty( find_result )
data_temp =...
[ data_temp( 1:count_index-1, :);...
[ count_index dummy_data ];...
data_temp( count_index:end-1, :) ];
count_index = 1;
end
end
disp('Done. Almanac data is now available.');
% print a empty line for spacing
fprintf('\n');
full_almanac_data = data_temp;
elseif exist( file_name , 'file' ) == 2
disp('Checking YUMA data for presence of 32 satellite vehicles.');
% print a empty line for spacing
fprintf('\n');
data_not_formated = ExtractData( file_name );
% Ensure that all 32 SVs are accounted for.
% If 32 SVs not present, then create a dummy data for the missing SVs
% Dummy date includes decimal values of alternatind one's and zero's
dummy_data =...
[ 63 0.0208330154418945 696320 4.18879020381111 4.99335085024861e-07 ...
5461.3330078125 0 4.18878995511504 4.18878995511504 ...
0.00130176544189453 4.96584107168019e-09 ];
if size( data_not_formated, 1 ) ~= 32
numb_of_columns = size( data_not_formated, 2 );
data_column_temp = [];
for count_columns = 1:numb_of_columns
data_column_temp = [ data_column_temp 0 ];
end
data_temp = [ data_not_formated ; data_column_temp ];
end
for count_index = 1:32
find_result = find( data_temp( :, 1 ) == count_index );
if isempty( find_result )
data_temp =...
[ data_temp( 1:count_index-1, :);...
[ count_index dummy_data ];...
data_temp( count_index:end-1, :) ];
count_index = 1;
end
end
disp('Done. Almanac data is now available.');
% print a empty line for spacing
fprintf('\n');
full_almanac_data = data_temp;
else
error('Check fetchYumaData() for error. Almanac file existance is not defined.');
end
end
% ----------------------------------------------------------------------- %
% ExtractData() - This function takes in a file, reads through the file %
% extracting the data for each satellite and storying it into an array %
% %
% Important: A YUMA almanac file expected %
% ----------------------------------------------------------------------- %
% Created by Kurt Pedrosa -- May 18th 2017 %
% ----------------------------------------------------------------------- %
function all_sv_data = ExtractData( almanac_file )
all_sv_data = [];
% Open file
file_id = fopen( almanac_file );
% Read file
current_line = fgetl( file_id );
while ~feof( file_id )
% If current line is the SV ID start saving info
if strfind( current_line, 'ID')
id = ParseValueInCurrentLine( current_line );
sv_data = id ;
current_line = fgetl( file_id ); % Go to next line
% Get health
if strfind( current_line, 'Health')
health = ParseValueInCurrentLine( current_line );
sv_data = [ sv_data health ];
current_line = fgetl( file_id ); % Go to next line
% Get Eccentricity
if strfind( current_line, 'Eccentricity' )
eccentricity = ParseValueInCurrentLine( current_line );
sv_data = [ sv_data eccentricity ];
current_line = fgetl( file_id );
% Get Time of Applicability
if strfind( current_line, 'Time of Applicability')
time_of_app = ParseValueInCurrentLine( current_line );
sv_data = [ sv_data time_of_app ];
current_line = fgetl( file_id );
% Get Orbital Inclination
if strfind( current_line, 'Orbital Inclination' )
orbital_incl = ParseValueInCurrentLine( current_line );
sv_data = [ sv_data orbital_incl ];
current_line = fgetl( file_id );
% Get Rate of Right Ascen
if strfind( current_line, 'Rate of Right Ascen')
rate_of_right_ascen = ParseValueInCurrentLine( current_line );
sv_data = [ sv_data rate_of_right_ascen ];
current_line = fgetl( file_id );
% Get SQRT(A)
if strfind( current_line, 'SQRT(A)')
sqroot_a = ParseValueInCurrentLine( current_line );
sv_data = [ sv_data sqroot_a ];
current_line = fgetl( file_id );
% get Right Ascen at Week
if strfind( current_line, 'Right Ascen at Week')
ascen_at_week = ParseValueInCurrentLine( current_line );
sv_data = [ sv_data ascen_at_week ];
current_line = fgetl( file_id );
% Get Argument of Perigee
if strfind( current_line, 'Argument of Perigee')
arg_perigee = ParseValueInCurrentLine( current_line );
sv_data = [ sv_data arg_perigee ];
current_line = fgetl( file_id );
% Get Mean Anom
if strfind( current_line, 'Mean Anom')
mean_anom = ParseValueInCurrentLine( current_line );
sv_data = [ sv_data mean_anom ];
current_line = fgetl( file_id );
% Get Af0
if strfind( current_line, 'Af0' )
af0 = ParseValueInCurrentLine( current_line );
sv_data = [ sv_data af0 ];
current_line = fgetl( file_id );
if strfind( current_line, 'Af1')
af1 = ParseValueInCurrentLine( current_line );
sv_data = [ sv_data af1 ];
end
end
end
end
end
end
end
end
end
end
end
% Complie all the data into one array
% Referenced by SV ID as the first element on each row
all_sv_data = [ all_sv_data; sv_data ];
end
current_line = fgetl( file_id );
end
fclose( file_id );
end
% ----------------------------------------------------------------------- %
% ParseValueInCurrentLine() - This function takes in the current line %
% being read and finds the value at the end-of-the-line. For example %
% ID: 32 will return the number 32 %
% SQRT(A) (m 1/2) 5153.687012 will return 5153.687012 %
% %
% Important: A YUMA almanac file expected %
% ----------------------------------------------------------------------- %
% Created by Kurt Pedrosa -- May 18th 2017 %
% ----------------------------------------------------------------------- %
function parsed_value = ParseValueInCurrentLine( current_line )
for count_char = 0:length( current_line ) - 1
if strcmp( current_line( end-count_char ), ' ' )
parsed_value = str2double( current_line( end - ( count_char - 1 ):end )) ;
break;
end
end
end