-
Notifications
You must be signed in to change notification settings - Fork 5
/
nrtdm.m
353 lines (322 loc) · 10.4 KB
/
nrtdm.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
classdef nrtdm
%static
properties(Constant)
%default value of some internal parameters
default_list=struct(...
'time_format','yyyy-MM-dd hh:mm:ss.sss',...
'debug',true...
);
end
%read only
properties(SetAccess=private)
metadata
start
stop
ts
file_list
end
methods(Static)
function out=nrtdm_dir
out=getenv('NRTDM');
if ~exist(out,'dir')
out=fullfile(getenv('HOME'),'bin','nrtdm');
end
assert(exist(out,'dir')~=0,'Cannot find NRTDM dir')
end
function out=data_dir(check)
if ~exist('check','var') || isempty(check)
check=false;
end
out=file.resolve_home('~/data/swarm/nrtdmdata');
if check
assert(exist(out,'dir')~=0,'Cannot find NRTDM data dir')
end
end
function out=config_dir
out=fullfile(nrtdm.nrtdm_dir,'config');
end
function out=data_file_name(data_dir,product,t_now,extension)
if ~exist('data_dir','var') || isempty(data_dir)
data_dir=nrtdm.data_dir;
end
if ~exist(data_dir,'dir')
file.mkdir(data_dir);
end
out=fullfile(data_dir,product,...
[datestr(t_now,'yyyy'), '_',num2str(day(t_now,'dayofyear'),'%03i'),'.',extension]);
end
function test(product_name,time_start,time_stop)
if ~exist('product_name','var') || isempty(product_name)
product_name='SC_Panels/Accel_AeroRadiationPressure';
end
if ~exist('time_start','var') || isempty(time_start)
time_start=datetime(2015,6,1,12,0,0);
end
if ~exist('time_stop','var') || isempty(time_stop)
time_stop=datetime(2015,6,3,18,0,0);
end
nrtdm(product_name,time_start,time_stop,'data_dir',nrtdm.data_dir(true)).ts.plot
end
end
methods
function obj=nrtdm(product_name,time_start,time_stop,varargin)
%inits
obj.metadata=nrtdm_metadata(product_name);
obj.start=time.ToDateTime(time_start);
if ~exist('time_stop','var') || isempty(time_stop)
obj.stop=obj.start+days(1)-seconds(1);
else
obj.stop=time.ToDateTime(time_stop);
end
%load data
obj=obj.load(varargin{:});
end
function obj=load(obj,varargin)
%make room for daily data
day_list=time.day_list(obj.start,obj.stop);
daily=cell(size(day_list));
i=0;
for t=day_list
%convert data (faster loading afterwards, skips if already available)
file=nrtdm_convert(obj.metadata,t,varargin{:});
i=i+1;
%only save if file exists
if ~file.exist(file)
daily{i}='';
else
%save converted matlab data file
obj=obj.add_file(file);
%user feedback
disp([' reading data from file ',file])
%load data
daily{i}=load(file);
end
end
%concatenate the data
init_flag=true;
for i=1:numel(daily)
if ~isempty(daily{i})
if init_flag
obj.ts=daily{i}.ts;
init_flag=false;
else
obj.ts=obj.ts.append(daily{i}.ts);
end
end
end
%sanity
if init_flag
disp(['nrtdm: could not find any valid data for ',obj.metadata.product.str,...
' from ',datestr(obj.start),' to ',datestr(obj.stop),'.'])
obj.ts=simpletimeseries(...
[obj.start;obj.stop],...
nan(2,obj.metadata.dimension)...
);
else
%trim to selected start/stop times and fill in time domain
obj.ts=obj.ts.trim(obj.start,obj.stop).fill;
end
end
function obj=add_file(obj,file)
if isempty(obj.file_list)
obj.file_list={file};
else
obj.file_list{end+1}=file;
end
end
end
end
%this routine converts NRTDM binary files to Matlab binary files (done on daily terms)
function outfile=nrtdm_convert(metadata,t,varargin)
%convert input product to metadata
if isa(metadata,'nrtdm_metadata')
%do nothing
elseif isa(metadata,'nrtdm_product')
metadata=nrtdm_metadata(metadata.str);
elseif ischar(metadata)
metadata=nrtdm_metadata(metadata);
else
error(['can not understand class of input ''product'': ''',class(metadata),'''.'])
end
% Parse inputs
p=machinery.inputParser;
% optional arguments
p.addParameter('nrtdm_args','',@ischar);
p.addParameter('data_dir', nrtdm.data_dir, @ischar);
p.addParameter('config_dir',nrtdm.config_dir,@ischar);
p.addParameter('debug',false,@islogical);
% parse it
p.parse(varargin{:});
if isempty(p.Results.nrtdm_args)
%get NRTDM arguments
nrtdm_args=[...
'-noStopIfDataFileMissing ',...
'-noStopIfAllDataInvalid ',...
['-dir_nrtdmconfig="',p.Results.config_dir,'" '],...
['-dir_data="',p.Results.data_dir,'"']...
];
end
%easier names
t_start=t;
t_stop =t+days(1)-seconds(1);
%set time system as defined in the metadata
for tsys=simpletimeseries.valid_timesystems
if str.contains(lower(metadata.entries.epoch),lower(tsys{1}))
timesystem=lower(tsys{1});
break
end
end
%build one-day-long NRTDM time arguments
timearg=['t=',timesystem,':',...
datestr(t_start,'yyyymmddHHMMSS'),',',...
datestr(t_stop, 'yyyymmddHHMMSS')];
if (p.Results.debug); disp([' ~ Converting data for day ',datestr(t),' : ',metadata.product.str]); end
infile= metadata.data_filename(t,'orbit',fullfile(p.Results.data_dir,'orbit'));
outfile=metadata.data_filename(t,'mat', fullfile(p.Results.data_dir,'mat'));
%check if no file exists
if ~exist(infile,'file') && ~exist(outfile,'file')
disp(['!!! Cannot find source file : ',infile])
return
end
%check if only the converted file exists
if ~exist(infile,'file') && exist(outfile,'file')
disp([' z Only converted file exists : ',outfile])
return
end
%file_is_newer handles missing outfile
if file_is_newer(infile,outfile)
if (p.Results.debug); disp([' ~ Converting data from file: ',infile]); end
%load nrtdm data
[time,values]=nrtdm_read(metadata.full_entries,timearg,nrtdm_args);
%some data files only contain gaps
if numel(time)<2 || size(values,1)<2
disp(['!!! Source file filled with gaps, ignoring : ',infile])
return
end
%build data structure
ts=simpletimeseries(time,values,...
'units',metadata.units,...
'labels',metadata.fields_no_units,...
'timesystem',timesystem,...
'descriptor',metadata.product.str...
).fill;
%fill extremeties if they are not there (happens a lot in Swarm data)
if ts.t(1)>t_start
ts=ts.extend(t_start).epoch_update;
end
if ts.t(end)<t_stop
ts=ts.extend(t_stop);
end
%fill gaps
ts=ts.fill;
%save it
save(outfile,'ts');
else
if(p.Results.debug); disp([' z Data already converted : ',outfile]); end
end
end
% This is the low-level reading program
function [time,values]=nrtdm_read(product,timearg,nrtdm_args,exportdata)
debug_now=false;
if ~exist('exportdata','var') || isempty(exportdata)
%get NRTDM dir
nrtdm_dir=nrtdm.nrtdm_dir;
if isempty(nrtdm_dir); error('Could not get NRTDM dir, environmental variable $NRTDM is not defined.'); end
%build export data utility path and name
exportdata=fullfile(nrtdm_dir,'bin','exportdataproductscdf.exe');
end
if ~exist('nrtdm_args','var')
nrtdm_args='';
end
%setup env
if ismac
setenv('DYLD_LIBRARY_PATH',['/usr/local/bin:/opt/local/lib:',getenv('NRTDM'),'/ext/cdf/lib'])
end
%get data
com=[exportdata,' ',product,' ',timearg,' ',nrtdm_args,' -noheader -nofooter -out-screen < /dev/null'];
if debug_now
disp(['com = ',com]) %#ok<UNRCH>
end
[status,data_str]=system(com);
if status ~=0
error(...
['Failed to read data from NRTDM using the following command:',10,...
com,10,...
'command output was:',10,...
data_str])
end
if debug_now
disp(['status = ',num2str(status)]) %#ok<UNRCH>
disp('--- data_str start ---')
disp(data_str)
disp('--- data_str end ---')
end
%handle junk coming for the system command
if str.contains(data_str,27)
%create a random anchor
anchor=char(floor(25*rand(1,20)) + 65);
%use system to echo the random anchor (along with the junk we want to remove)
[~,echo_out]=system(['echo ',anchor]);
%remove the random anchor from output of system, isolating the junk
junk_str=strrep(echo_out,anchor,'');
%remove the junk from data_str
data_str=strrep(data_str,junk_str(1:end-1),'');
end
%number of data columns is the same as the number of products given in
%input (NOTICE: this does not contemplate special fields, such as (e.g.):
%SA_Basic/Accel_L1B/1-3
a=textscan(product,'%s');
if debug_now,disp('a='),disp(a{:}),end %#ok<UNRCH>
product_length=numel(a{1});
if debug_now,disp(['product_length=',num2str(product_length)]),end %#ok<UNRCH>
%build the format specifiers, taking into account the number of products
d='%f';
fmt=[d,'-',d,'-',d,' ',d,':',d,':%6.3f %s',repmat(' %f',1,product_length)];
if debug_now,disp(['fmt=',fmt]),end %#ok<UNRCH>
%parse data
%2014-01-01 00:00:07.000 UTC -0.314819944E-04
data_cell=textscan(data_str,fmt,...
'Delimiter',' ',...
'MultipleDelimsAsOne',true,...
'CommentStyle','#'...
);
if debug_now,disp(['size(data_cell)=',num2str(size(data_cell))]),end %#ok<UNRCH>
%converting to matlab representation of time
time=datetime(data_cell{1},data_cell{2},data_cell{3},data_cell{4},data_cell{5},data_cell{6});
if debug_now,disp(['size(time)=',num2str(size(time))]),end %#ok<UNRCH>
%output
values=cell2mat(data_cell(8:end));
if debug_now,disp(['size(values)=',num2str(size(values))]),end %#ok<UNRCH>
if numel(values)==0
keyboard
end
end
% returns true if the modification date of file 1 is later than that of file 2.
function newer_file_flag=file_is_newer(file1,file2,disp_flag)
if ~exist('disp_flag','var') || isempty(disp_flag)
disp_flag=true;
end
f1=dir(file1);
f2=dir(file2);
if isempty(f1)
if (disp_flag)
warning(['cannot find file ',file1])
end
newer_file_flag=false;
return
end
if isempty(f2)
if (disp_flag)
warning(['cannot find file ',file2])
end
newer_file_flag=true;
return
end
if numel(f1)>1
error([' this is not a single file: ',file1])
end
if numel(f2)>1
error([': this is not a single file: ',file2])
end
newer_file_flag=datenum(f1.datenum)>datenum(f2.datenum);
end