-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_InterCDF.m
More file actions
805 lines (689 loc) · 24.4 KB
/
write_InterCDF.m
File metadata and controls
805 lines (689 loc) · 24.4 KB
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% WRITTEN BY ACHIM MORSCHHAUSER, GFZ POTSDAM, 2016
% mailto: mors/gfz-potsdam.de
%
% Write INTERMAGNET CDF file. The format of the file is based on the
% INTERMAGNET Discussion Document DD22, version 2.3, 23/01/2013.
%
% This function is based on the NASA cdf library for MATLAB which has to
% be added to the search path. This can be done by:
% addpath('Path to NASA CDF library')
% The internal MATLAB cdf library is not able to read
% InterCDF files as the TT2000 date format is not supported with version
% R2015b or earlier.
%
% TODO: Additional user-specified data
%
% Arguments:
% ==========
%
% The arguments are given in their order.
%
% IN:
% ===
%
% A.) Mandatory fields
% --------------------
%
% NONE
%
% B.) Mandatory argument-value pairs:
% -----------------------------------
%
% The argument name and argument type are given, e.g. GeomagV, STRING, is
% used as read_InterCDF('GeoMagV','XYZ'). Argument-value pairs can have
% arbitrary order and are not case-sensitive.
%
% GeoMag ARRAY Geomagnetic data for writing to CDF.
% OF DOUBLES Different rows correspond to diff.
% data types. Data types are specified in
% the correct order in the field
% 'ElementsRecorded'.
%
% !!!! WARNING: UNITS ARE AUTOMATICALLY SET TO 'NT' OR
% ------------- 'DEGREES OF ARC'.
%
% ElementsRecorded STRING Describes the recorded components, e.g.
% 'XYZ'. Valid components are any
% combination of 'XYZHDEVIFSG'.
%
% TimeV ARRAY Timestamps for geomagnetic vector data.
% OF TIME_FORMAT Mandatory only if scalar data are
% (see below) specified in ElementsRecorded.
%
% TimeS ARRAY Timestamps for geomagnetic scalar data.
% OF TIME_FORMAT Mandatory only if vector data are
% (see below) specified in ELementsRecorded.
%
% TimeFormat STRING Format of time. Admissible values are:
% + TT2000 DEFAULT: Nanoseconds since
% midday of 1st January 2000 AD
% + Datenum Fractional days since
% December 31,1 BC
% + Datetime MATLAB datetime object
% + JD2000 Fractional days since
% January 1st, 2000 AD
% + POSIX Number of seconds since
% 1st January, 1970
%
% G_ATTR STRUCT The required Metadata as struct with
% the following fields (case-sensitive!):
% - IagaCode STRING
% - PublicationLevel STRING
% - PublicationDate TIME_FORMAT
% - ObservatoryName STRING
% - Latitude DOUBLE
% - Longitude DOUBLE
% - Elevation DOUBLE
% - Institution STRING
% - Source STRING
% - SandardLevel STRING
% Must contain, if StandardLevel~='NONE':
% - StandardName STRING
% Must contain, if StandardLevel=='PARTIAL'
% - PartialStandDesc ARRAY OF DOUBLE
% OR SINGLE STRING
%
%
% C.) Single optional fields
% --------------------------
%
% If an argument is given which is not listed here, it will be
% interpreted as an global attribute which is requested. The arguments
% should be passed as strings, e.g. read_InterCDF('GeoMagS').
% Single optional fields can have an arbitrary order and are not
% case-sensitive.
%
% NoCheck If specified, cdf file will not be checked for
% correctness. Errors might result if cdf is not
% properly according to INTERMAGNET standard.
%
% D.) Optional argument-value pairs:
% ----------------------------------
%
% Temp ARRAY Temperature(s). Each temperature time
% OF DOUBLES series corresponds to a row in Temp.
% If temperature is specified, then
% Temp_Loc and Temp_Time need to be
% specified as well.
%
% Temp_Loc CELL ARRAY Location where the respective temperature
% OF STRINGS has been recorded.
%
% Temp_Time ARRAY OF Time for each temperature.
% TIME_FORMAT
%
% FILLVAL ARRAY OF The value used to show data is
% DOUBLES missing. Default is 99999. Must be
% specified for all data in the
% order according to elementsrecorded,
% followed by the values for the
% temperatures.
%
% VALIDMIN ARRAY OF As above, but for the smallest valid
% DOUBLES value. Default is -79999, but -360 for
% 'DI'.
%
%
% VALIDMAX ARRAY OF As above, but for the largest valid
% DOUBLES value. Default is 79999, but 360 for
% 'DI'.
%
% FILENAME STRING Filename with *.cdf extension.
% If not specified, an INTERMAGNET
% standard filename will be used.
%
% The following attributes can either be given as specified below, or
% may equally be added to G_Attr. If both are specified, the values as
% specified here will be used and warning will be displayed.
%
% VectorSensOrient STRING
%
% StandardVersion STRING
%
% TermsofUse STRING
%
% UniqueIdentifier STRING
%
% ParentIdentifiers STRING
%
% ReferenceLinks STRING
%
%
%
%
% EXAMPLE USAGE:
% ==============
%
% Write simple file without temperature and 1s sampling:
%
% cdf_file='test.cdf';
% M=spdfcdfinfo(cdf_file);
% [T D]=write_InterCDF('elementsrecorded','HDZ','geomag',20.*rand(3,10),...
% 'timev',(1:10).*1e9,...
% 'g_attr',M.GlobalAttributes,'timeformat','tt2000');
%
% Read CDF, change ElementsRecorded to XYZ, and save:
%
% cdf_file='test.cdf';
% R=read_InterCDF(cdf_file,'geomagv','XYZ','g_attr');
% D=[R.GeoMagV.X'; R.GeoMagV.Y'; R.GeoMagV.Z'];
% [T D]=write_InterCDF('filename','XYZ.cdf','elementsrecorded','XYZ',...
% 'geomag',D,'timev',R.GeoMagV.Time,...
% 'g_attr',R.g_attr,'timeformat','tt2000');
%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [M, D, VD]=write_InterCDF(varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Preamble
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Some valid values definitions
%
valid_TimeFormats={'tt2000' 'datenum' 'datetime' 'jd2000' 'posix'};
elements.valid='DEFGHISVXYZ';
elements.units={'Degrees of arc' 'nT' 'nT' 'nT' 'nT' 'Degrees of arc' ...
'nT' 'nT' 'nT' 'nT' 'nT'};
%
% Optional global attributes
%
IN.g_attr.opt={'VectorSensOrient' 'StandardVersion' 'TermsofUse' ...
'UniqueIdentifier' 'ParentIdentifiers' 'ReferenceLinks'};
%
% Initialize flags for optional arguments
%
% Data
IN.geomag.set=0;
IN.elementsrecorded.set=0;
IN.timev.set=0;
IN.times.set=0;
IN.timeformat.set=0;
IN.g_attr.set=0;
IN.nocheck.set=0;
IN.temp.set=0;
IN.temp_loc.set=0;
IN.temp_time.set=0;
IN.fillval.set=0;
IN.validmin.set=0;
IN.validmax.set=0;
IN.filename.set=0;
IN.vectorsensorient.set=0;
IN.standardversion.set=0;
IN.termsofuse.set=0;
IN.uniqueidentifier.set=0;
IN.parentidentifiers.set=0;
IN.referencelinks.set=0;
% Sampling rate // used for filename generation
sampling.v=[];
sampling.s=[];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Process input arguments
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Parse optional input arguments. Checking for correct format and
% consistency is done later.
%
arg=1;
while arg<nargin
% Check if arguments are all strings
if ~ischar(varargin{arg})
error(['Argument ' num2str(arg+1) ' must be a string, but is '...
class(varargin{arg}) '.' ])
else
switch lower(varargin{arg})
case 'geomag'
get_input('geomag','double');
case 'elementsrecorded'
get_input('elementsrecorded','char');
case 'timev'
get_input('timev','numeric')
case 'times'
get_input('times','numeric')
case 'timeformat'
get_input('timeformat','char')
case 'g_attr'
get_input('g_attr','struct');
case 'nocheck'
if (IN.(item).set)
warning(['Warning: ' item ' multiply specified. ' ...
'Using first occurence.']);
end
IN.nocheck.set=1;
IN.nocheck.data=varargin{arg};
case 'temp'
get_input('temp','double');
case 'temp_loc'
get_input('temp_loc','cell');
case 'temp_time'
get_input('temp_time','numeric')
case 'fillval'
get_input('fillval','double');
case 'validmin'
get_input('validmin','double');
case 'validmax'
get_input('validmax','double');
case 'filename'
get_input('filename','char');
otherwise
% Check for optional global attributes
if (any(strcmpi(IN.g_attr.opt,varargin{arg})))
get_input(lower(varargin{arg}),'char');
else
error(['Argument ' num2str(arg+1) ' not understood: '...
varargin{arg}])
end
end
end
arg=arg+1;
end
%
% Check if input was already specified. If not, set IN parameters
% accordingly.
%
function get_input(item,type)
if (IN.(item).set)
warning(['Warning: ' item ' multiply specified. Using' ...
' first occurence.']);
elseif isa(varargin{arg+1},type)
IN.(item).data=varargin{arg+1};
IN.(item).set=1;
else
error(['Warning: ' item ' is of wrong type. Given:' ...
class(varargin{arg+1}) ', but must be: ' type '.']);
end
arg=arg+1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Perform some basic consistency checks. Thorough testing is done later.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Number of elements recorded
if (IN.elementsrecorded.set)
nor=length(IN.elementsrecorded.data);
else
error('ElementsRecorded was not specified.');
end
% Number of temperatures recorded
if IN.temp.set
not=size(IN.temp.data,2);
else
not=0;
end
% Number of overall variables
nov=nor+not;
%
% 1) Check if geomagnetic data is provided.
% 2) Check if number of elements recorded is consistent with number of data
% provided.
%
if ~(IN.geomag.set)
error('No geomagnetic field data is provided. Nothing to write.');
elseif size(IN.geomag.data,1)~=nor
error('Number of ElementsRecorded and rows in GeoMag must match.');
end
%
% Check if recorded elements are valid elements
%
for i=1:nor
if isempty(strfind(elements.valid,IN.elementsrecorded.data(i)))
error(['Element ' IN.elementsrecorded.data(i) ' is not a '...
'valid element.']);
end
end
%
% Check if optional METADATA are uniquely specified
%
if IN.g_attr.set
for i=1:length(IN.g_attr.opt)
element=lower(IN.g_attr.opt{i});
if IN.(element).set && ...
any(strcmp(fields(IN.g_attr.data),IN.g_attr.opt{i}))
disp(['Warning: ' element ' explicitly specified and '...
'part of G_Attr! Using ' IN.(element).data ...
' as value for ' element]);
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Set some default variable attributes:
% - FILLVAL 99999
% - VALIDMIN DI: -360, T: -273.15, ELSE: -79999
% - VALIDMAX DI: 360, ELSE: 79999
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Check if number of entries is correctly set.
%
function var_attr(item,default,nov)
if (IN.(item).set)
if (length(IN.(item).data) ~= nov)
error(['Number of ' item ' entries provided does not ' ...
'match the number of elements in ElementsRecorded']);
end
else
IN.(item).data=default*ones(1,nov);
end
end
%
% FILLVAL: Entry used for missing value
%
var_attr('fillval',99999,nov);
%
% VALDIMIN: Lowest valid number
%
var_attr('validmin',-79999,nov);
if ~(IN.validmin.set)
% Different default for DI
pos=regexp(IN.elementsrecorded.data,'[DI]');
if ~isempty(pos)
IN.validmin.data(pos)=-360*ones(1,length(pos));
end
% Different default for temperatures
if (not > 0)
IN.validmin.data(nor+1:end)=-273.15*ones(1,not);
end
end
%
% VALDIMAX: Maximum valid number
%
var_attr('validmax',79999,nov);
if ~(IN.validmax.set)
% Different default for DI
pos=regexp(IN.elementsrecorded.data,'[DI]');
if ~isempty(pos)
IN.validmax.data(pos)=360*ones(1,length(pos));
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Build data and variable attributes
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Fill what can be filled
%
V.FILLVAL=cell(nov,2);
V.FILLVAL(:,2) = num2cell(IN.fillval.data);
V.VALIDMIN=cell(nov,2);
V.VALIDMIN(:,2) = num2cell(IN.validmin.data);
V.VALIDMAX=cell(nov,2);
V.VALIDMAX(:,2) = num2cell(IN.validmax.data);
V.DISPLAY_TYPE = cell(nov,2);
V.DISPLAY_TYPE(:,2) = repmat({'time_series'},nov,1);
%
% Variable Number Counter
%
varNUM=1;
%
% GeomagneticField data
%
% Iterate over geomagnetic field elements
for i=1:nor
% Current element (e.g. X)
element=IN.elementsrecorded.data(i);
% Variable Name
vname=['GeomagneticField' element];
% Write data
D{1,varNUM}=vname;
D{2,varNUM}=IN.geomag.data(i,:);
% Write variable attributes
V.FIELDNAM{varNUM,1}=vname;
V.FIELDNAM{varNUM,2}=['Geomagnetic Field Element ' element];
V.UNITS{varNUM,1}=vname;
V.UNITS{varNUM,2}=...
elements.units{strfind(elements.valid,element)};
V.FILLVAL{varNUM,1} = vname;
V.VALIDMIN{varNUM,1} = vname;
V.VALIDMAX{varNUM,1} = vname;
V.DEPEND_0{varNUM,1} = vname;
if (regexp(element,'[SG]')>0)
V.DEPEND_0{varNUM,2} = 'GeomagneticScalarTimes';
else
V.DEPEND_0{varNUM,2} = 'GeomagneticVectorTimes';
end
V.DISPLAY_TYPE{varNUM,1} = vname;
V.LABLAXIS{varNUM,1} = vname;
V.LABLAXIS{varNUM,2} = element;
VD{1,varNUM} = vname;
VD{2,varNUM} = 'double';
M.Variables{varNUM,1} = vname;
varNUM=varNUM+1;
end
%
% Temperatures
%
% Iterate over given temperature fields
for i=1:not
% Variable name
vname=['Temperature' num2str(i)];
% Write data
D{1,varNUM}=vname;
D{2,varNUM}=IN.temp.data(i,:);
% Write variable attributes
V.FIELDNAM{varNUM,1}=vname;
V.FIELDNAM{varNUM,2}=['Temperature ' IN.temp_loc.data{i}];
V.UNITS{varNUM,1}=vname;
V.UNITS{varNUM,2}='Celsius';
V.FILLVAL{varNUM,1} = vname;
V.VALIDMIN{varNUM,1} = vname;
V.VALIDMAX{varNUM,1} = vname;
V.DEPEND_0{varNUM,1} = vname;
V.DEPEND_0{varNUM,2} = ['Temperature' num2str(i) 'Times'];
V.DISPLAY_TYPE{varNUM,1} = vname;
V.LABLAXIS{varNUM,1} = vname;
V.LABLAXIS{varNUM,2} = '?';
VD{1,varNUM} = vname;
VD{2,varNUM} = 'double';
M.Variables{varNUM,1} = vname;
varNUM=varNUM+1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Write timestamps.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Set time format
%
if ~IN.timeformat.set
error('Time format must be set!');
elseif ~any(strcmp(IN.timeformat.data,valid_TimeFormats))
error('Time Format is not a valid format!');
end
% Convert to TT2000
function Tc=convert_to_tt2000(T,TimeFormat)
switch TimeFormat
case 'datenum'
Tc=spdfdatenumtott2000(T);
case 'datetime'
if (~isa(T,'datetime'))
error('Input data was specified as datetime, but is not');
else
tmp1=(T.Second-floor(T.Second))*1000;
tmp2=(tmp1-floor(tmp1))*1000;
tmp3=(tmp2-floor(tmp2))*1000;
milli=floor(tmp1);
mikro=floor(tmp2);
nano=floor(tmp3);
Tc=spdfcomputett2000([T.Year T.Month T.Day T.Hour ...
T.Minute floor(T.Second) milli mikro nano]);
end
case 'jd2000'
Tc=spdfdatenumtott2000(T+730486);
case 'posix'
Tc=spdfdatenumtott2000((T+719529)/68400);
case 'tt2000'
Tc=int64(T);
otherwise
Tc=T;
end
end
%
% Geomagnetic vector data
%
if ~IN.timev.set && ...
~(isempty(regexp(IN.elementsrecorded.data,'[DEFHIVXYZ]','once')))
error(['Vector time series TimeV mus be set when vector elements '...
'are specified.']);
elseif (IN.timev.set)
vname='GeomagneticVectorTimes';
D{1,varNUM}=vname;
D{2,varNUM}=convert_to_tt2000(IN.timev.data,IN.timeformat.data);
VD{1,varNUM} = vname;
VD{2,varNUM} = 'tt2000';
M.Variables{varNUM,1} = vname;
sampling.v=varNUM;
varNUM=varNUM+1;
end
%
% Geomagnetic scalar data
%
if ~IN.times.set && ...
~(isempty(regexp(IN.elementsrecorded.data,'[GS]','once')))
error(['Scalar time series TimeS mus be set when scalar elements '...
'are specified.']);
elseif (IN.times.set)
vname='GeomagneticScalarTimes';
D{1,varNUM}=vname;
D{2,varNUM}=convert_to_tt2000(IN.times.data,IN.timeformat.data);
VD{1,varNUM} = vname;
VD{2,varNUM} = 'tt2000';
M.Variables{varNUM,1} = vname;
sampling.s=varNUM;
varNUM=varNUM+1;
end
%
% Temperature data
%
if (IN.temp.set)
if ~IN.temp_loc.set
error('Temp_Loc is missing.');
elseif (length(IN.temp_loc.data)~=size(IN.temp.data,1))
error('Temperature location for some temperatures missing.');
end
if ~IN.temp_time.set
error('Temp_Time is missing.');
elseif (size(IN.temp.data,1)~=size(IN.temp_time.data,1))
error('Time series for some temperatures missing.');
else
for i=1:not
vname=['Temperature' num2str(i) 'Times'];
D{1,varNUM}=vname;
D{2,varNUM}=convert_to_tt2000(IN.temp.data(i,:),...
IN.timeformat.data);
VD{1,varNUM} = vname;
VD{2,varNUM} = 'tt2000';
M.Variables{varNUM,1} = vname;
varNUM=varNUM+1;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Build GlobalAttributes
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (IN.g_attr.set && ~isempty(IN.g_attr.data))
M.GlobalAttributes=IN.g_attr.data;
elseif isempty(IN.g_attr.data)
error('Global Attributes (G_ATTR) is empty!');
else
error('Global Attributes (G_ATTR) must be specified!');
end
% Write elementsrecorded
M.GlobalAttributes.ElementsRecorded{1}=IN.elementsrecorded.data;
% Write optional elements
for i=IN.g_attr.opt
if (IN.(lower(i{1})).set)
tmp{1}=IN.(lower(i{1})).data;
M.GlobalAttributes.(i{1})=tmp;
end
end
% VariableAttributes
M.VariableAttributes=V;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Check consistency and validity of data provided.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (~IN.nocheck.set || IN.nocheck.data)
M.Variables(:,4)=VD(2,:);
[valid, desc]=check_InterCDF(M,D(2,:));
% Display any errors / warnings
fprintf(desc);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Write data.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if valid>0
%
% AUTOMATICALLY GENERATE FILENAME
%
if ~(IN.filename.set)
% Determine sampling period
if ~isempty(sampling.v) && length(D{2,sampling.v})>1
starttime=spdfbreakdowntt2000(D{2,sampling.v}(1));
% 1s sampling
if diff(D{2,sampling.v}(1:2))==1e9
datetime=num2str(starttime(1:6),...
'%04u%02u%02u_%02u%02u%02u');
cad='PT1S';
% 1m sampling
elseif diff(D{2,sampling.v}(1:2))==1e9*60
datetime=num2str(starttime(1:5),...
'%04u%02u%02u_%02u%02u');
cad='PT1M';
% 1h sampling
elseif diff(D{2,sampling.v}(1:2))==1e9*60*60
datetime=num2str(starttime(1:4),'%04u%02u%02u_%02u');
cad='PT1H';
% 1d sampling
elseif diff(D{2,sampling.v}(1:2))==1e9*60*60*24
datetime=num2str(starttime(1:3),'%04u%02u%02u');
cad='P1D';
% 1 month sampling
elseif diff(D{2,sampling.v}(1:2))<=1e9*60*60*32 && ...
diff(D{2,sampling.v}(1:2))>=1e9*60*60*27
datetime=num2str(starttime(1:2),'%04u%02u');
cad='P1M';
elseif diff(D{2,sampling.v}(1:2))<=1e9*60*60*368 && ...
diff(D{2,sampling.v}(1:2))>=1e9*60*60*365
datetime=num2str(starttime(1),'%04u');
cad='P1Y';
else
datetime=num2str(starttime(1:6),...
'%04u%02u%02u_%02u%02u%02u');
cad='UNKNOWN';
end
elseif isempty(sampling.v)
error(['Cannot generate filename as no geomagnetic vector'...
' data was specified!']);
else
error(['Cannot generate filename as sampling period'...
' cannot be determined from single datum.']);
end
IN.filename.data=[M.GlobalAttributes.IagaCode{1} '_' ...
datetime '_' cad '_' ...
M.GlobalAttributes.PublicationLevel{1} '.cdf'];
end
%
% WRITE DATA
%
spdfcdfwrite(IN.filename.data,D,...
'VariableAttributes',M.VariableAttributes,'Vardatatypes',VD,...
'GlobalAttributes',M.GlobalAttributes);
disp(['CDF successfully written to ' IN.filename.data]);
else
error('Cannot write file. Incomplete data!\n')
end
end