-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_mef_channel.c
1992 lines (1712 loc) · 99.8 KB
/
write_mef_channel.c
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
/**********************************************************************************************************************
Copyright 2022, Mayo Foundation, Rochester MN. All rights reserved.
This library contains functions to convert data samples to MEF version 3.0
initialize_mef_channel_data() should be called first for each channel, which initializes the data in the channel
structure. Then write_mef_channel_data() is called with the actual sample data to be written to the mef. Finally,
close_mef_channel_file() will close out the channel mef file, and free allocated memory.
To compile for a 64-bit intel system, linking with the following files is necessary:
meflib.c, mefrec.c
Usage and modification of this source code is governed by the Apache 2.0 license.
You may not use this file except in compliance with this License.
A copy of the Apache 2.0 License may be obtained at http://www.apache.org/licenses/LICENSE-2.0
Thanks to all who acknowledge the Mayo Systems Electrophysiology Laboratory, Rochester, MN
in academic publications of their work facilitated by this software.
Written by Dan Crepeau 9/2011, originally to write MEF 2.1 files.
Minor changes added 2/2012 to allow temporary index files to be written.
Updated 10/2014 to write MEF 3.0.
Updated 5/2015 for MEF 3.0 format changes.
Updated 5/2016 for MEF 3.0 library updates and bug fixes.
Updated 2/2017 to add support for exporting functions to .dll
Updated 10/2017 initial support for MEF 3.0 annotations.
Updated 10/2020 initial support for MEF 3.0 video channels.
When compiling for Windows .dll, define _EXPORT_FOR_DLL
When using this code, the time-series data must be pre-sorted in increasing time order. Unordered
packets will cause data discontinuities.
*************************************************************************************************************************/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
//#include <limits.h>
#include "write_mef_channel.h"
#include "mefrec.h"
// TBD Won't be thread-safe without pthread, but for now ignore it
//pthread_mutex_t lock1 = PTHREAD_MUTEX_INITIALIZER;
// this creates a single static data block for use by all channels
// this assumes all channels will need the same size data block
ui1* GetDataBlockBuffer(ui8 block_len)
{
static ui1 *buff = 0;
if (buff == 0)
buff = (ui1 *) malloc(block_len * 8);
return buff;
}
#ifdef _EXPORT_FOR_DLL
__declspec(dllexport)
#endif
si4 append_mef_channel_data(CHANNEL_STATE *channel_state,
si1 *chan_map_name,
si4 new_segment_number,
si1 *mef_3_level_1_password,
si1 *mef_3_level_2_password,
si1 *mef3_session_directory,
ui8 num_secs_per_segment,
si4 bit_shift_flag
)
{
extern MEF_GLOBALS *MEF_globals;
ui4 max_samps;
SEGMENT prev_segment;
si1 prev_segment_name[MEF_SEGMENT_BASE_FILE_NAME_BYTES];
si1 command[512];
si1 extension[TYPE_BYTES];
si1 mef3_session_path_extracted[MEF_FULL_FILE_NAME_BYTES];
si1 mef3_session_path[MEF_FULL_FILE_NAME_BYTES], mef3_session_name[MEF_BASE_FILE_NAME_BYTES];
si1 channel_path[MEF_FULL_FILE_NAME_BYTES], segment_path[MEF_FULL_FILE_NAME_BYTES], segment_name[MEF_SEGMENT_BASE_FILE_NAME_BYTES];
UNIVERSAL_HEADER *uh;
TIME_SERIES_METADATA_SECTION_2 *md2;
METADATA_SECTION_3 *md3;
char word[256];
// must be a new segment greater than zero
if (!(new_segment_number > 0))
return 0;
channel_state->if_appending = 1;
sprintf(prev_segment_name, "%s/%s.timd/%s-%06d.segd", mef3_session_directory, chan_map_name, chan_map_name, new_segment_number - 1);
(void) read_MEF_segment(&prev_segment, prev_segment_name, TIME_SERIES_CHANNEL_TYPE, mef_3_level_2_password, NULL, MEF_FALSE, MEF_FALSE);
channel_state->raw_data_ptr_start = (si4 *) calloc((size_t) ((prev_segment.metadata_fps->metadata.time_series_section_2->block_interval / 1e6) *
prev_segment.metadata_fps->metadata.time_series_section_2->sampling_frequency
* 2), sizeof(si4));
if (channel_state->raw_data_ptr_start == NULL)
{
fprintf(stderr, "Insufficient memory to allocate temporary channel buffer\n");
exit(1);
}
channel_state->raw_data_ptr_current = channel_state->raw_data_ptr_start;
channel_state->block_hdr_time = 0;
channel_state->block_boundary = 0;
channel_state->last_chan_timestamp = 0;
channel_state->max_block_size = 0;
channel_state->max_block_len = 0;
channel_state->number_of_index_entries = 0;
channel_state->number_of_discontinuity_entries = 0;
channel_state->block_sample_index = 0;
channel_state->number_of_samples = 0;
channel_state->discontinuity_flag = 1; // first block is by definition discontinuous
channel_state->bit_shift_flag = bit_shift_flag;
channel_state->block_len = 0; // this will be overwritten when write_mef_channel_data() is called // TBD this variable isn't used
channel_state->chan_num = prev_segment.metadata_fps->metadata.time_series_section_2->acquisition_channel_number;
// get mef3 session name and path from passed directory
extract_path_parts(mef3_session_directory, mef3_session_path_extracted, mef3_session_name, extension);
MEF_snprintf(mef3_session_path, MEF_FULL_FILE_NAME_BYTES, "%s/%s.%s", mef3_session_path_extracted, mef3_session_name, SESSION_DIRECTORY_TYPE_STRING);
// make mef3 session directory
// already exists
//sprintf(command, "mkdir -p \"%s\" 2> /dev/null", mef3_session_path);
//system(command);
// set up a generic fps for universal header and password data
channel_state->gen_fps = allocate_file_processing_struct(UNIVERSAL_HEADER_BYTES, NO_FILE_TYPE_CODE, NULL, NULL, 0);
initialize_universal_header(channel_state->gen_fps, MEF_FALSE, MEF_FALSE, MEF_FALSE);
uh = channel_state->gen_fps->universal_header;
uh->segment_number = new_segment_number;
MEF_strncpy(uh->session_name, prev_segment.metadata_fps->universal_header->session_name, MEF_BASE_FILE_NAME_BYTES);
MEF_strncpy(uh->anonymized_name, prev_segment.metadata_fps->universal_header->anonymized_name, UNIVERSAL_HEADER_ANONYMIZED_NAME_BYTES);
uh->start_time = UNIVERSAL_HEADER_START_TIME_NO_ENTRY;
uh->end_time = UNIVERSAL_HEADER_END_TIME_NO_ENTRY;
if (mef_3_level_2_password != NULL)
{
if (mef_3_level_1_password == NULL)
{
fprintf(stderr, "If a level 2 password is specified, then a level 1 password must be specified also. Exiting...\n");
exit(0);
}
channel_state->pwd = channel_state->gen_fps->password_data = process_password_data(NULL, mef_3_level_1_password, mef_3_level_2_password, uh);
}
else
channel_state->pwd = channel_state->gen_fps->password_data = NULL;
// make mef3 channel directory
sprintf(channel_path, "%s/%s.%s", mef3_session_path, chan_map_name, TIME_SERIES_CHANNEL_DIRECTORY_TYPE_STRING);
// already exists
//sprintf(command, "mkdir %s 2> /dev/null", channel_path);
//system(command);
// copy channel name into generic universal header
MEF_strncpy(channel_state->gen_fps->universal_header->channel_name, prev_segment.metadata_fps->universal_header->channel_name, MEF_BASE_FILE_NAME_BYTES);
// make mef3 segment name
generate_segment_name(channel_state->gen_fps, segment_name);
// save this for later for creating new segments
strcpy(channel_state->channel_path, channel_path);
// make mef3 segment directory
sprintf(segment_path, "%s/%s.%s", channel_path, segment_name, SEGMENT_DIRECTORY_TYPE_STRING);
sprintf(command, "mkdir %s", segment_path);
system(command);
// generate level UUID into generic universal_header
generate_UUID(channel_state->gen_fps->universal_header->level_UUID);
// set up mef3 time series metadata file
channel_state->metadata_fps = allocate_file_processing_struct(METADATA_FILE_BYTES, TIME_SERIES_METADATA_FILE_TYPE_CODE, NULL, channel_state->gen_fps, UNIVERSAL_HEADER_BYTES);
MEF_snprintf(channel_state->metadata_fps->full_file_name, MEF_FULL_FILE_NAME_BYTES, "%s/%s.%s", segment_path, segment_name, TIME_SERIES_METADATA_FILE_TYPE_STRING);
uh = channel_state->metadata_fps->universal_header;
generate_UUID(uh->file_UUID);
uh->number_of_entries = 1;
uh->maximum_entry_size = METADATA_FILE_BYTES;
initialize_metadata(channel_state->metadata_fps);
if (mef_3_level_2_password != NULL)
{
channel_state->metadata_fps->metadata.section_1->section_2_encryption = LEVEL_1_ENCRYPTION_DECRYPTED;
channel_state->metadata_fps->metadata.section_1->section_3_encryption = LEVEL_2_ENCRYPTION_DECRYPTED;
}
else
{
channel_state->metadata_fps->metadata.section_1->section_2_encryption = NO_ENCRYPTION;
channel_state->metadata_fps->metadata.section_1->section_3_encryption = NO_ENCRYPTION;
}
md2 = channel_state->metadata_fps->metadata.time_series_section_2;
MEF_strncpy(md2->channel_description, prev_segment.metadata_fps->metadata.time_series_section_2->channel_description, METADATA_CHANNEL_DESCRIPTION_BYTES);
MEF_strncpy(md2->session_description, prev_segment.metadata_fps->metadata.time_series_section_2->session_description, METADATA_SESSION_DESCRIPTION_BYTES);
md2->recording_duration = METADATA_RECORDING_DURATION_NO_ENTRY;
md2->sampling_frequency = prev_segment.metadata_fps->metadata.time_series_section_2->sampling_frequency;
md2->low_frequency_filter_setting = prev_segment.metadata_fps->metadata.time_series_section_2->low_frequency_filter_setting;
md2->high_frequency_filter_setting = prev_segment.metadata_fps->metadata.time_series_section_2->high_frequency_filter_setting;
md2->notch_filter_frequency_setting = prev_segment.metadata_fps->metadata.time_series_section_2->notch_filter_frequency_setting;
md2->AC_line_frequency = prev_segment.metadata_fps->metadata.time_series_section_2->AC_line_frequency;
md2->units_conversion_factor = prev_segment.metadata_fps->metadata.time_series_section_2->units_conversion_factor;
MEF_strncpy(md2->units_description, "microvolts", TIME_SERIES_METADATA_UNITS_DESCRIPTION_BYTES);
md2->maximum_native_sample_value = TIME_SERIES_METADATA_MAXIMUM_NATIVE_SAMPLE_VALUE_NO_ENTRY; // must test against NaN later on
md2->minimum_native_sample_value = TIME_SERIES_METADATA_MINIMUM_NATIVE_SAMPLE_VALUE_NO_ENTRY;
md2->start_sample = prev_segment.metadata_fps->metadata.time_series_section_2->start_sample + prev_segment.metadata_fps->metadata.time_series_section_2->number_of_samples;
md2->number_of_samples = 0; // fill in when convert RED blocks
md2->number_of_blocks = 0; // fill in when convert RED blocks
md2->maximum_block_bytes = 0; // fill in when convert RED blocks
md2->maximum_block_samples = 0; // fill in when convert RED blocks
md2->maximum_difference_bytes = 0; // fill in when convert RED blocks
md2->block_interval = prev_segment.metadata_fps->metadata.time_series_section_2->block_interval;
md2->number_of_discontinuities = 0; // fill in when convert RED blocks
md2->maximum_contiguous_blocks = 0; // fill in when convert RED blocks
md2->maximum_contiguous_block_bytes = 0; // fill in when convert RED blocks;
md2->maximum_contiguous_samples = 0; // fill in when convert RED blocks;
md2->acquisition_channel_number = prev_segment.metadata_fps->metadata.time_series_section_2->acquisition_channel_number; // for purposes of this program, these two will always be the same
md3 = channel_state->metadata_fps->metadata.section_3;
md3->recording_time_offset = prev_segment.metadata_fps->metadata.section_3->recording_time_offset;
MEF_globals->recording_time_offset = md3->recording_time_offset; // TBD make this thread_safe?
md3->GMT_offset = prev_segment.metadata_fps->metadata.section_3->GMT_offset;
MEF_globals->GMT_offset = md3->GMT_offset; // TBD make this thread safe?
//channel_state->gmt_offset_in_hours = gmt_offset; // not used, since we already know offsets
MEF_strncpy(md3->subject_name_1, prev_segment.metadata_fps->metadata.section_3->subject_name_1, METADATA_SUBJECT_NAME_BYTES);
MEF_strncpy(md3->subject_name_2, prev_segment.metadata_fps->metadata.section_3->subject_name_2, METADATA_SUBJECT_NAME_BYTES);
MEF_strncpy(md3->subject_ID, prev_segment.metadata_fps->metadata.section_3->subject_ID, METADATA_SUBJECT_ID_BYTES);
MEF_strncpy(md3->recording_location, prev_segment.metadata_fps->metadata.section_3->recording_location, METADATA_RECORDING_LOCATION_BYTES);
// set up mef3 time series indices file
channel_state->ts_inds_fps = allocate_file_processing_struct(UNIVERSAL_HEADER_BYTES, TIME_SERIES_INDICES_FILE_TYPE_CODE, NULL, channel_state->metadata_fps, UNIVERSAL_HEADER_BYTES);
MEF_snprintf(channel_state->ts_inds_fps->full_file_name, MEF_FULL_FILE_NAME_BYTES, "%s/%s.%s", segment_path, segment_name, TIME_SERIES_INDICES_FILE_TYPE_STRING);
uh = channel_state->ts_inds_fps->universal_header;
generate_UUID(uh->file_UUID);
uh->number_of_entries = 0; // fill in when convert RED blocks
uh->maximum_entry_size = TIME_SERIES_INDEX_BYTES;
channel_state->ts_inds_fps->directives.io_bytes = UNIVERSAL_HEADER_BYTES; // write out the universal header, then the RED blocks piecemeal
channel_state->ts_inds_fps->directives.close_file = MEF_FALSE;
write_MEF_file(channel_state->ts_inds_fps);
channel_state->inds_file_offset = UNIVERSAL_HEADER_BYTES;
channel_state->ts_inds_fps->universal_header->body_CRC = CRC_START_VALUE;
// set up mef3 time series data file
channel_state->ts_data_fps = allocate_file_processing_struct(UNIVERSAL_HEADER_BYTES, TIME_SERIES_DATA_FILE_TYPE_CODE, NULL, channel_state->metadata_fps, UNIVERSAL_HEADER_BYTES);
MEF_snprintf(channel_state->ts_data_fps->full_file_name, MEF_FULL_FILE_NAME_BYTES, "%s/%s.%s", segment_path, segment_name, TIME_SERIES_DATA_FILE_TYPE_STRING);
uh = channel_state->ts_data_fps->universal_header;
generate_UUID(uh->file_UUID);
uh->number_of_entries = 0; // fill in when convert RED blocks
uh->maximum_entry_size = 0; // fill in when converet RED blocks
channel_state->ts_data_fps->directives.io_bytes = UNIVERSAL_HEADER_BYTES; // write out the universal header, then the RED blocks piecemeal
channel_state->ts_data_fps->directives.close_file = MEF_FALSE;
write_MEF_file(channel_state->ts_data_fps);
channel_state->data_file_offset = UNIVERSAL_HEADER_BYTES;
channel_state->ts_data_fps->universal_header->body_CRC = CRC_START_VALUE;
// allocate new memory for new RED blocks
max_samps = (prev_segment.metadata_fps->metadata.time_series_section_2->block_interval / 1e6) *
prev_segment.metadata_fps->metadata.time_series_section_2->sampling_frequency * 2;
channel_state->rps = RED_allocate_processing_struct(max_samps, RED_MAX_COMPRESSED_BYTES(max_samps, 1), 0, RED_MAX_DIFFERENCE_BYTES(max_samps), 0, 0, channel_state->pwd);
//channel_state->rps->directives.return_block_extrema = MEF_TRUE;
// free original_data of rps, since we'll never use it anyway, and that pointer will be re-assigned before each RED compression
// TBD modify it so original_data is not allocated in the first place?
free(channel_state->rps->original_data);
// set up discontinuity state information
channel_state->discont_contiguous_blocks = 0;
channel_state->discont_contiguous_samples = 0;
channel_state->discont_contiguous_bytes = 0;
// make these part of the channel state to keep everything thread-safe
channel_state->out_data = (ui1 *) malloc(32000 * 8); // This assumes 1 second blocks, sampled at 32000 Hz
channel_state->temp_time_series_index = (ui1*) calloc(sizeof(ui1), 8+8+8+4+4+4+4+4+1+RED_BLOCK_PROTECTED_REGION_BYTES+RED_BLOCK_DISCRETIONARY_REGION_BYTES);
channel_state->num_secs_per_segment = num_secs_per_segment;
channel_state->next_segment_start_time = 0;
channel_state->start_sample = 0;
// TBD fix this
//free_segment(&prev_segment, MEF_FALSE);
return(1);
}
#ifdef _EXPORT_FOR_DLL
__declspec(dllexport)
#endif
si4 initialize_mef_channel_data ( CHANNEL_STATE *channel_state,
sf8 secs_per_block,
si1 *chan_map_name,
si4 bit_shift_flag,
sf8 low_frequency_filter_setting,
sf8 high_frequency_filter_setting,
sf8 notch_filter_frequency,
sf8 AC_line_frequency,
sf8 units_conversion_factor,
si1 *channel_description,
sf8 sampling_frequency,
si8 block_interval,
si4 chan_num,
si1 *mef3_session_directory,
sf4 gmt_offset,
si1 *session_description,
si1 *anonymized_subject_name,
si1 *subject_first_name,
si1 *subject_second_name,
si1 *subject_id,
si1 *institution,
si1 *mef_3_level_1_password,
si1 *mef_3_level_2_password,
si1 *study_comments,
si1 *channel_comments,
ui8 num_secs_per_segment
)
{
extern int errno;
extern MEF_GLOBALS *MEF_globals;
ui4 max_samps;
si1 command[512];
si1 extension[TYPE_BYTES];
si1 mef3_session_path_extracted[MEF_FULL_FILE_NAME_BYTES];
si1 mef3_session_path[MEF_FULL_FILE_NAME_BYTES], mef3_session_name[MEF_BASE_FILE_NAME_BYTES];
si1 channel_path[MEF_FULL_FILE_NAME_BYTES], segment_path[MEF_FULL_FILE_NAME_BYTES], segment_name[MEF_SEGMENT_BASE_FILE_NAME_BYTES];
UNIVERSAL_HEADER *uh;
TIME_SERIES_METADATA_SECTION_2 *md2;
METADATA_SECTION_3 *md3;
channel_state->if_appending = 0;
//fprintf(stderr, "sizeof struct: %d\n", sizeof(CHANNEL_STATE));
//fprintf(stderr, "in initialize_mef_channel_data()\n");
// add 10% to buffer size to account for possible sample frequency drift
//fprintf(stderr,"%f, %f\n", secs_per_block, sampling_frequency);
channel_state->raw_data_ptr_start = (si4 *) calloc((size_t) (secs_per_block * sampling_frequency * 2), sizeof(si4));
if (channel_state->raw_data_ptr_start == NULL)
{
fprintf(stderr, "Insufficient memory to allocate temporary channel buffer\n");
exit(1);
}
channel_state->raw_data_ptr_current = channel_state->raw_data_ptr_start;
channel_state->block_hdr_time = 0;
channel_state->block_boundary = 0;
channel_state->last_chan_timestamp = 0;
channel_state->max_block_size = 0;
channel_state->max_block_len = 0;
channel_state->number_of_index_entries = 0;
channel_state->number_of_discontinuity_entries = 0;
channel_state->block_sample_index = 0;
channel_state->number_of_samples = 0;
channel_state->discontinuity_flag = 1; // first block is by definition discontinuous
channel_state->bit_shift_flag = bit_shift_flag;
channel_state->block_len = 0; // this will be overwritten when write_mef_channel_data() is called
channel_state->chan_num = chan_num;
// get mef3 session name and path from passed directory
extract_path_parts(mef3_session_directory, mef3_session_path_extracted, mef3_session_name, extension);
MEF_snprintf(mef3_session_path, MEF_FULL_FILE_NAME_BYTES, "%s/%s.%s", mef3_session_path_extracted, mef3_session_name, SESSION_DIRECTORY_TYPE_STRING);
// check for password differences
if (mef_3_level_1_password != NULL && mef_3_level_2_password != NULL)
{
if (!strcmp(mef_3_level_1_password, mef_3_level_2_password))
{
// While it is not technically a violation of the MEF 3.0 spec to have the level 1 and level 2
// passwords be the same, the intention is that having different passwords provide different
// levels of access to different users.
//
// (Note: the validation of the level 2 password is dependent upon the plain-text of the level
// 1 password, and thus if level 2 passwords are in use, then the user decoding the file needs
// to only type in the level 2 password, to provide access to both level 1 and level 2.)
//
// If this check (that the two passwords should be different) is removed, then it should be
// verified that that viewers and decoding tools can correctly give level 2 access, and not
// just level 1 access, when level 2 access is desired. This may not be implemented correctly
// in all decoding tools.
fprintf(stderr, "Level 1 and level 2 password should be different. Exiting...\n");
exit(0);
}
}
//fprintf(stdout, "path: %s\n", mef3_session_path);
// make mef3 session directory
#ifndef _WIN32
sprintf(command, "mkdir \"%s\" 2> /dev/null", mef3_session_path);
#else
sprintf(command, "mkdir \"%s\" > nul 2> nul", mef3_session_path);
#endif
system(command);
// set up a generic fps for universal header and password data
channel_state->gen_fps = allocate_file_processing_struct(UNIVERSAL_HEADER_BYTES, NO_FILE_TYPE_CODE, NULL, NULL, 0);
initialize_universal_header(channel_state->gen_fps, MEF_FALSE, MEF_FALSE, MEF_FALSE);
uh = channel_state->gen_fps->universal_header;
uh->segment_number = 0;
MEF_strncpy(uh->session_name, mef3_session_name, MEF_BASE_FILE_NAME_BYTES);
MEF_strncpy(uh->anonymized_name, anonymized_subject_name, UNIVERSAL_HEADER_ANONYMIZED_NAME_BYTES);
uh->start_time = UNIVERSAL_HEADER_START_TIME_NO_ENTRY;
uh->end_time = UNIVERSAL_HEADER_END_TIME_NO_ENTRY;
if (mef_3_level_2_password != NULL)
{
if (mef_3_level_1_password == NULL)
{
fprintf(stderr, "If a level 2 password is specified, then a level 1 password must be specified also. Exiting...\n");
exit(0);
}
channel_state->pwd = channel_state->gen_fps->password_data = process_password_data(NULL, mef_3_level_1_password, mef_3_level_2_password, uh);
}
else
channel_state->pwd = channel_state->gen_fps->password_data = NULL;
// make mef3 channel directory
sprintf(channel_path, "%s/%s.%s", mef3_session_path, chan_map_name, TIME_SERIES_CHANNEL_DIRECTORY_TYPE_STRING);
#ifndef _WIN32
sprintf(command, "mkdir %s", channel_path);
#else
sprintf(command, "mkdir \"%s\" > nul 2> nul", channel_path);
#endif
system(command);
// copy channel name into generic universal header
MEF_strncpy(channel_state->gen_fps->universal_header->channel_name, chan_map_name, MEF_BASE_FILE_NAME_BYTES);
// make mef3 segment name
generate_segment_name(channel_state->gen_fps, segment_name);
// save this for later for creating new segments
strcpy(channel_state->channel_path, channel_path);
//fprintf(stdout, "segment path: %s\n", segment_path);
// make mef3 segment directory
sprintf(segment_path, "%s/%s.%s", channel_path, segment_name, SEGMENT_DIRECTORY_TYPE_STRING);
#ifndef _WIN32
sprintf(command, "mkdir %s", segment_path);
#else
sprintf(command, "mkdir \"%s\" > nul 2> nul", segment_path);
#endif
system(command);
// generate level UUID into generic universal_header
generate_UUID(channel_state->gen_fps->universal_header->level_UUID);
// set up mef3 time series metadata file
channel_state->metadata_fps = allocate_file_processing_struct(METADATA_FILE_BYTES, TIME_SERIES_METADATA_FILE_TYPE_CODE, NULL, channel_state->gen_fps, UNIVERSAL_HEADER_BYTES);
MEF_snprintf(channel_state->metadata_fps->full_file_name, MEF_FULL_FILE_NAME_BYTES, "%s/%s.%s", segment_path, segment_name, TIME_SERIES_METADATA_FILE_TYPE_STRING);
uh = channel_state->metadata_fps->universal_header;
generate_UUID(uh->file_UUID);
uh->number_of_entries = 1;
uh->maximum_entry_size = METADATA_FILE_BYTES;
initialize_metadata(channel_state->metadata_fps);
if (mef_3_level_2_password != NULL)
{
channel_state->metadata_fps->metadata.section_1->section_2_encryption = LEVEL_1_ENCRYPTION_DECRYPTED;
channel_state->metadata_fps->metadata.section_1->section_3_encryption = LEVEL_2_ENCRYPTION_DECRYPTED;
}
else
{
channel_state->metadata_fps->metadata.section_1->section_2_encryption = NO_ENCRYPTION;
channel_state->metadata_fps->metadata.section_1->section_3_encryption = NO_ENCRYPTION;
}
md2 = channel_state->metadata_fps->metadata.time_series_section_2;
if (channel_comments)
MEF_strncpy(md2->channel_description, channel_comments, METADATA_CHANNEL_DESCRIPTION_BYTES);
else
md2->channel_description[0] = '\0';
if (session_description)
MEF_strncpy(md2->session_description, session_description, METADATA_SESSION_DESCRIPTION_BYTES);
else
md2->session_description[0] = '\0';
md2->recording_duration = METADATA_RECORDING_DURATION_NO_ENTRY;
md2->sampling_frequency = sampling_frequency;
md2->low_frequency_filter_setting = low_frequency_filter_setting;
md2->high_frequency_filter_setting = high_frequency_filter_setting;
md2->notch_filter_frequency_setting = notch_filter_frequency;
md2->AC_line_frequency = AC_line_frequency;
md2->units_conversion_factor = units_conversion_factor;
MEF_strncpy(md2->units_description, "microvolts", TIME_SERIES_METADATA_UNITS_DESCRIPTION_BYTES);
md2->maximum_native_sample_value = TIME_SERIES_METADATA_MAXIMUM_NATIVE_SAMPLE_VALUE_NO_ENTRY; // must test against NaN later on
md2->minimum_native_sample_value = TIME_SERIES_METADATA_MINIMUM_NATIVE_SAMPLE_VALUE_NO_ENTRY;
md2->start_sample = 0;
md2->number_of_samples = 0; // fill in when convert RED blocks
md2->number_of_blocks = 0; // fill in when convert RED blocks
md2->maximum_block_bytes = 0; // fill in when convert RED blocks
md2->maximum_block_samples = 0; // fill in when convert RED blocks
md2->maximum_difference_bytes = 0; // fill in when convert RED blocks
md2->block_interval = block_interval;
md2->number_of_discontinuities = 0; // fill in when convert RED blocks
md2->maximum_contiguous_blocks = 0; // fill in when convert RED blocks
md2->maximum_contiguous_block_bytes = 0; // fill in when convert RED blocks;
md2->maximum_contiguous_samples = 0; // fill in when convert RED blocks;
md2->acquisition_channel_number = chan_num; // for purposes of this program, these two will always be the same
md3 = channel_state->metadata_fps->metadata.section_3;
md3->recording_time_offset = MEF_globals->recording_time_offset;
md3->GMT_offset = MEF_globals->GMT_offset; // TBD does this do anything?
channel_state->gmt_offset_in_hours = gmt_offset;
if (subject_first_name)
MEF_strncpy(md3->subject_name_1, subject_first_name, METADATA_SUBJECT_NAME_BYTES);
else
md3->subject_name_1[0] = '\0';
if (subject_second_name)
MEF_strncpy(md3->subject_name_2, subject_second_name, METADATA_SUBJECT_NAME_BYTES);
else
md3->subject_name_2[0] = '\0';
if (subject_id)
MEF_strncpy(md3->subject_ID, subject_id, METADATA_SUBJECT_ID_BYTES);
else
md3->subject_ID[0] = '\0';
if (institution)
MEF_strncpy(md3->recording_location, institution, METADATA_RECORDING_LOCATION_BYTES);
else
md3->recording_location[0] = '\0';
// set up mef3 time series indices file
channel_state->ts_inds_fps = allocate_file_processing_struct(UNIVERSAL_HEADER_BYTES, TIME_SERIES_INDICES_FILE_TYPE_CODE, NULL, channel_state->metadata_fps, UNIVERSAL_HEADER_BYTES);
MEF_snprintf(channel_state->ts_inds_fps->full_file_name, MEF_FULL_FILE_NAME_BYTES, "%s/%s.%s", segment_path, segment_name, TIME_SERIES_INDICES_FILE_TYPE_STRING);
uh = channel_state->ts_inds_fps->universal_header;
generate_UUID(uh->file_UUID);
uh->number_of_entries = 0; // fill in when convert RED blocks
uh->maximum_entry_size = TIME_SERIES_INDEX_BYTES;
channel_state->ts_inds_fps->directives.io_bytes = UNIVERSAL_HEADER_BYTES; // write out the universal header, then the RED blocks piecemeal
channel_state->ts_inds_fps->directives.close_file = MEF_FALSE;
write_MEF_file(channel_state->ts_inds_fps);
channel_state->inds_file_offset = UNIVERSAL_HEADER_BYTES;
channel_state->ts_inds_fps->universal_header->body_CRC = CRC_START_VALUE;
// set up mef3 time series data file
channel_state->ts_data_fps = allocate_file_processing_struct(UNIVERSAL_HEADER_BYTES, TIME_SERIES_DATA_FILE_TYPE_CODE, NULL, channel_state->metadata_fps, UNIVERSAL_HEADER_BYTES);
MEF_snprintf(channel_state->ts_data_fps->full_file_name, MEF_FULL_FILE_NAME_BYTES, "%s/%s.%s", segment_path, segment_name, TIME_SERIES_DATA_FILE_TYPE_STRING);
uh = channel_state->ts_data_fps->universal_header;
generate_UUID(uh->file_UUID);
uh->number_of_entries = 0; // fill in when convert RED blocks
uh->maximum_entry_size = 0; // fill in when converet RED blocks
channel_state->ts_data_fps->directives.io_bytes = UNIVERSAL_HEADER_BYTES; // write out the universal header, then the RED blocks piecemeal
channel_state->ts_data_fps->directives.close_file = MEF_FALSE;
write_MEF_file(channel_state->ts_data_fps);
channel_state->data_file_offset = UNIVERSAL_HEADER_BYTES;
channel_state->ts_data_fps->universal_header->body_CRC = CRC_START_VALUE;
// allocate new memory for new RED blocks
max_samps = secs_per_block * sampling_frequency * 2;
channel_state->rps = RED_allocate_processing_struct(max_samps, RED_MAX_COMPRESSED_BYTES(max_samps, 1), 0, RED_MAX_DIFFERENCE_BYTES(max_samps), 0, 0, channel_state->pwd);
//channel_state->rps->directives.return_block_extrema = MEF_TRUE;
// free original_data of rps, since we'll never use it anyway, and that pointer will be re-assigned before each RED compression
// TBD modify it so original_data is not allocated in the first place?
free(channel_state->rps->original_data);
// set up discontinuity state information
channel_state->discont_contiguous_blocks = 0;
channel_state->discont_contiguous_samples = 0;
channel_state->discont_contiguous_bytes = 0;
// make these part of the channel state to keep everything thread-safe
channel_state->out_data = (ui1 *) malloc(32000 * 8); // This assumes 1 second blocks, sampled at 32000 Hz
channel_state->temp_time_series_index = (ui1*) calloc(sizeof(ui1), 8+8+8+4+4+4+4+4+1+RED_BLOCK_PROTECTED_REGION_BYTES+RED_BLOCK_DISCRETIONARY_REGION_BYTES);
channel_state->num_secs_per_segment = num_secs_per_segment;
channel_state->next_segment_start_time = 0;
channel_state->start_sample = 0;
// creating .mefd file is not supported in case of encrypted files, since Persyst won't read encrypted files anyway
if (mef_3_level_1_password != NULL || mef_3_level_2_password != NULL)
return (0);
// TBD: a mutex should be used around this function call (or within it) if channel creation is done in a threaded way.
update_mefd_file(mef3_session_path, mef3_session_name, chan_map_name, anonymized_subject_name);
return(0);
}
void update_mefd_file(si1 *mef3_session_path, si1 *mef3_session_name, si1* chan_name, si1* anonymized_subject_name)
{
FILE* mefd_fp;
si1 mefd_file_name[MEF_FULL_FILE_NAME_BYTES];
UNIVERSAL_HEADER mefd_uh;
si1 file_name_output[1024]; // TODO: Make this "1024" a constant in the .h file
si1 file_name_input[1024];
si4 i;
// TBD: this function might need to be protected by a mutex if channel creation is done in a threaded way.
// update MEFD file (used by Persyst)
sprintf(mefd_file_name, "%s/%s.mefd", mef3_session_path, mef3_session_name);
memset(file_name_output, 0, 1024);
// check for case where .mefd file doesn't exist yet, in which case we create
// it and add the currect channel as the first entry.
if (access(mefd_file_name, 0) == -1)
{
// write default universal header
memset(&mefd_uh, 0, UNIVERSAL_HEADER_BYTES);
mefd_fp = fopen(mefd_file_name, "wb");
fwrite(&mefd_uh, sizeof(UNIVERSAL_HEADER), (size_t)1, mefd_fp);
mefd_uh.body_CRC = CRC_START_VALUE;
// write channel name
memset(file_name_output, 0, 1024);
sprintf(file_name_output, "%s.%s", chan_name, TIME_SERIES_CHANNEL_DIRECTORY_TYPE_STRING);
fwrite(file_name_output, 1024, 1, mefd_fp);
mefd_uh.body_CRC = CRC_update((ui1*)file_name_output, 1024, mefd_uh.body_CRC);
// rewrite universal header of mefd file, this time with correct info
fseek(mefd_fp, 0, SEEK_SET);
memset(mefd_uh.channel_name, 0, MEF_BASE_FILE_NAME_BYTES);
sprintf(mefd_uh.file_type_string, "%s", "mefd");
mefd_uh.mef_version_major = MEF_VERSION_MAJOR;
mefd_uh.mef_version_minor = MEF_VERSION_MINOR;
mefd_uh.byte_order_code = MEF_LITTLE_ENDIAN;
mefd_uh.start_time = UUTC_NO_ENTRY;
mefd_uh.end_time = UUTC_NO_ENTRY;
if (anonymized_subject_name != NULL)
MEF_strncpy(mefd_uh.anonymized_name, anonymized_subject_name, UNIVERSAL_HEADER_ANONYMIZED_NAME_BYTES);
else
MEF_strncpy(mefd_uh.anonymized_name, "not_entered", UNIVERSAL_HEADER_ANONYMIZED_NAME_BYTES);
if (mef3_session_name != NULL)
MEF_strncpy(mefd_uh.session_name, mef3_session_name, MEF_BASE_FILE_NAME_BYTES);
else
MEF_strncpy(mefd_uh.session_name, "not_entered", MEF_BASE_FILE_NAME_BYTES);
// Set level UUIDs to zero - this should be coordinated with records files at this level, in order for a UUID here to really be valid
//generate_UUID(mefd_uh.level_UUID); // already zero'd out
generate_UUID(mefd_uh.file_UUID);
mefd_uh.maximum_entry_size = 1024;
mefd_uh.number_of_entries = 1;
mefd_uh.segment_number = -3; // session level
memset(mefd_uh.level_1_password_validation_field, 0, PASSWORD_VALIDATION_FIELD_BYTES);
memset(mefd_uh.level_2_password_validation_field, 0, PASSWORD_VALIDATION_FIELD_BYTES);
mefd_uh.header_CRC = CRC_calculate(&mefd_uh + CRC_BYTES, UNIVERSAL_HEADER_BYTES - CRC_BYTES);
fwrite(&mefd_uh, sizeof(UNIVERSAL_HEADER), (size_t)1, mefd_fp);
// close .mefd file
fclose(mefd_fp);
return;
}
// file exists, so read it and see what's in it
mefd_fp = fopen(mefd_file_name, "rb+");
fread(&mefd_uh, UNIVERSAL_HEADER_BYTES, 1, mefd_fp);
sprintf(file_name_output, "%s.%s", chan_name, TIME_SERIES_CHANNEL_DIRECTORY_TYPE_STRING);
for (i = 0; i < mefd_uh.number_of_entries; i++)
{
fread(&file_name_input, 1024, 1, mefd_fp);
if (!strncmp(file_name_input, file_name_output, 1024))
{
// this channel is already listed in the .mefd file, so no need to continue
fclose(mefd_fp);
return;
}
}
// at this point, we know we need to add the current channel to the .mefd file
// rewrite universal header
mefd_uh.number_of_entries++;
mefd_uh.body_CRC = CRC_update((ui1*)file_name_output, 1024, mefd_uh.body_CRC);
mefd_uh.header_CRC = CRC_calculate(&mefd_uh + CRC_BYTES, UNIVERSAL_HEADER_BYTES - CRC_BYTES);
e_fseek(mefd_fp, 0, SEEK_SET, mefd_file_name, __FUNCTION__, __LINE__, USE_GLOBAL_BEHAVIOR);
(void)e_fwrite(&mefd_uh, sizeof(UNIVERSAL_HEADER), (size_t)1, mefd_fp, mefd_file_name, __FUNCTION__, __LINE__, USE_GLOBAL_BEHAVIOR);
fclose(mefd_fp);
// reopen in append mode so we can append new file name
mefd_fp = fopen(mefd_file_name, "ab+");
(void)e_fwrite(&file_name_output, 1024, (size_t)1, mefd_fp, mefd_file_name, __FUNCTION__, __LINE__, USE_GLOBAL_BEHAVIOR);
fclose(mefd_fp);
}
si4 process_filled_block( CHANNEL_STATE *channel_state, si4* raw_data_ptr_start, ui4 num_entries,
ui8 block_len, si4 discontinuity_flag, ui8 block_hdr_time)
{
ui1 *out_data;
si4 *ddp, bit_shift_flag;
ui8 i;
extern MEF_GLOBALS *MEF_globals;
ui1 data_key[240];
FILE *ofp;
sf8 sampling_frequency;
int chan_num;
//static ui1 generated_offset = 0;
//TIME_SERIES_INDEX time_series_index;
ui1 *temp_time_series_index;
sf8 temp_sf8;
RED_PROCESSING_STRUCT *rps;
FILE_PROCESSING_STRUCT *ts_inds_fps;
FILE_PROCESSING_STRUCT *ts_data_fps;
FILE_PROCESSING_STRUCT *metadata_fps;
TIME_SERIES_METADATA_SECTION_2 *md2;
UNIVERSAL_HEADER *uh_meta;
UNIVERSAL_HEADER *uh_data;
UNIVERSAL_HEADER *uh_inds;
TIME_SERIES_INDEX temp_struct;
// fprintf(stderr, "in process_filled_block\n");
chan_num = channel_state->chan_num;
// bring in data from channel_state struct
ofp = channel_state->out_file;
bit_shift_flag = channel_state->bit_shift_flag;
ts_data_fps = channel_state->ts_data_fps;
ts_inds_fps = channel_state->ts_inds_fps;
metadata_fps = channel_state->metadata_fps;
temp_time_series_index = channel_state->temp_time_series_index;
rps = channel_state->rps;
sampling_frequency = channel_state->metadata_fps->metadata.time_series_section_2->sampling_frequency;
// do nothing if there is nothing to be done
if (num_entries == 0)
return (0);
if (block_len == 0)
return (0); // this should never happen, but check for it anyway
//pthread_mutex_lock(&lock1);
// only care about generating offset times if this is a brand-new session.
// if we are appending to existing session, we alrady have offset times
if ((channel_state->if_appending == 0) && (MEF_globals->recording_time_offset_mode & (RTO_APPLY | RTO_APPLY_ON_OUTPUT)))
{
// this only be done for one channel, assumes all channels have same offset
if (MEF_globals->recording_time_offset == MEF_GLOBALS_RECORDING_TIME_OFFSET_DEFAULT) {
// generate recording time offset & GMT offset
//TBD bring in GMT offset
generate_recording_time_offset(block_hdr_time, (si4) (channel_state->gmt_offset_in_hours * 3600.0));
//generated_offset = 1;
}
}
//pthread_mutex_unlock(&lock1);
memset(data_key, 0, 240); // for now, assume no data encryption
// use a static out_data buffer, this buffer is shared across channels, so this will only
// work if the block_len of all channels is the same. If different channels need different
// block_lens (ie, different sampling rates), then this block buffer will need to be part
// of the channel struct.
//out_data = GetDataBlockBuffer(block_len);
// previous method (GetDataBlockBuffer) is not thread-safe, so use this method.
// TBD clean this up, so it will works with any block_len, rather than hard-coding
// 1 second blocks.
out_data = channel_state->out_data;
if (bit_shift_flag)
{
//shift 2 bits to 18 bit resolution
ddp = raw_data_ptr_start;
for(i = num_entries; i--;)
{
if (*ddp >= 0)
*ddp++ = (si4) (((sf8) *ddp / (sf8) 4.0) + 0.5);
else
*ddp++ = (si4) (((sf8) *ddp / (sf8) 4.0) - 0.5);
}
}
// set up RED compression
rps->original_data = rps->original_ptr = raw_data_ptr_start;
rps->directives.discontinuity = (discontinuity_flag== 1) ? MEF_TRUE : MEF_FALSE;
rps->block_header->number_of_samples = num_entries;
rps->block_header->start_time = block_hdr_time;
// RED compress data block
(void) RED_encode(rps);
if (channel_state->num_secs_per_segment > 0 )
check_for_new_segment(channel_state, rps->block_header->start_time);
// write block to output file
//fwrite(out_data, sizeof(si1), RED_block_size, ofp);
(void) e_fwrite(rps->compressed_data, sizeof(ui1), (size_t) channel_state->rps->block_header->block_bytes, ts_data_fps->fp, ts_data_fps->full_file_name, __FUNCTION__, __LINE__, USE_GLOBAL_BEHAVIOR);
// update data body CRC
ts_data_fps->universal_header->body_CRC = CRC_update(rps->compressed_data, rps->block_header->block_bytes, ts_data_fps->universal_header->body_CRC);
// set recording_start_time on first pass
uh_meta = channel_state->metadata_fps->universal_header;
uh_data = channel_state->ts_data_fps->universal_header;
uh_inds = channel_state->ts_inds_fps->universal_header;
if (uh_meta->start_time == UNIVERSAL_HEADER_START_TIME_NO_ENTRY)
{
// needs to be offset, since universal header will always be written unencrypted
// these timestamps are already offset, because the offsetting occurs in the RED compression routine
uh_meta->start_time = rps->block_header->start_time;
uh_data->start_time = rps->block_header->start_time;
uh_inds->start_time = rps->block_header->start_time;
// set start time for next segment
if (MEF_globals->recording_time_offset_mode & (RTO_APPLY | RTO_APPLY_ON_OUTPUT))
channel_state->next_segment_start_time = rps->block_header->start_time - (channel_state->num_secs_per_segment * 1e6); // subtract, since times are offset
else
channel_state->next_segment_start_time = rps->block_header->start_time + (channel_state->num_secs_per_segment * 1e6);
}
md2 = metadata_fps->metadata.time_series_section_2;
RED_find_extrema(rps->original_data, rps->block_header->number_of_samples, &temp_struct);
// update segment metadata files
// maximum_native_sample_value
if (md2->units_conversion_factor >= 0)
temp_sf8 = (sf8) temp_struct.maximum_sample_value * md2->units_conversion_factor;
else
temp_sf8 = (sf8) temp_struct.minimum_sample_value * md2->units_conversion_factor; // units_conversion_factor is negative, so use min value instead
if (isnan(md2->maximum_native_sample_value))
md2->maximum_native_sample_value = temp_sf8;
if (temp_sf8 > md2->maximum_native_sample_value)
md2->maximum_native_sample_value = temp_sf8;
// minimum_native_sample_value
if (md2->units_conversion_factor >= 0)
temp_sf8 = (sf8) temp_struct.minimum_sample_value * md2->units_conversion_factor;
else
temp_sf8 = (sf8) temp_struct.maximum_sample_value * md2->units_conversion_factor; // units_conversion_factor is negative, so use max value instead
if (isnan(md2->minimum_native_sample_value))
md2->minimum_native_sample_value = temp_sf8;
if (temp_sf8 < md2->minimum_native_sample_value)
md2->minimum_native_sample_value = temp_sf8;
// maximum_block_bytes
if (rps->block_header->block_bytes > md2->maximum_block_bytes)
md2->maximum_block_bytes = rps->block_header->block_bytes;
// maximum_difference_bytes
if (rps->block_header->difference_bytes > md2->maximum_difference_bytes)
md2->maximum_difference_bytes = rps->block_header->difference_bytes;
// maximum_block_samples
if (rps->block_header->number_of_samples > md2->maximum_block_samples)
md2->maximum_block_samples = rps->block_header->number_of_samples;
// number_of_samples
md2->number_of_samples = md2->number_of_samples + num_entries;
// number_of_blocks
md2->number_of_blocks = md2->number_of_blocks + 1;
// number_of_discontinuities
if (discontinuity_flag == 1)
md2->number_of_discontinuities = md2->number_of_discontinuities + 1;
// in theory the next two only need to be set once, but we need to wait until this function, when we have real data,
// in order to know what the offset and GMT times are.
// recording_time_offset
metadata_fps->metadata.section_3->recording_time_offset = MEF_globals->recording_time_offset;
// GMT offset
metadata_fps->metadata.section_3->GMT_offset = MEF_globals->GMT_offset;
// update metadata recording_duration and end_time for all files
uh_meta->end_time = block_hdr_time + (si8) (((((sf8) channel_state->rps->block_header->number_of_samples) / md2->sampling_frequency) * (sf8) 1e6) + (sf8) 0.5);
// needs to be offset, since universal header will always be written unencrypted
if (MEF_globals->recording_time_offset_mode & (RTO_APPLY | RTO_APPLY_ON_OUTPUT))
apply_recording_time_offset(&uh_meta->end_time);
uh_data->end_time = uh_meta->end_time;
uh_inds->end_time = uh_meta->end_time;
md2->recording_duration = uh_meta->end_time - uh_meta->start_time;
// offset time values could be negative, so reverse sign if negative
if (md2->recording_duration < 0)
md2->recording_duration = 0 - md2->recording_duration;
// update number_of_entries
uh_data->number_of_entries = uh_data->number_of_entries + 1;
uh_inds->number_of_entries = uh_inds->number_of_entries + 1;
// update maximum_entry_size, this only applies to data, which is the largest number of samples in a RED block
if (num_entries > uh_data->maximum_entry_size)
uh_data->maximum_entry_size = num_entries;
// set up block entry, TBD test to see if this works
//time_series_index.file_offset = channel_state->data_file_offset;
//time_series_index.start_time = channel_state->rps->block_header->start_time;
//time_series_index.start_sample = start_sample;
//time_series_index.number_of_samples = channel_state->rps->block_header->number_of_samples;
//time_series_index.block_bytes = channel_state->rps->block_header->block_bytes;
//time_series_index.maximum_sample_value = channel_state->rps->compression.maximum_sample_value;
//time_series_index.minimum_sample_value = channel_state->rps->compression.minimum_sample_value;
//time_series_index.flags =channel_state->rps->block_header->flags
// write index entry to output mtf file, TBD, this might not be necessary
memcpy(temp_time_series_index, &(channel_state->data_file_offset), sizeof(ui8));
memcpy(temp_time_series_index+8, &(channel_state->rps->block_header->start_time), sizeof(ui8));
memcpy(temp_time_series_index+16, &(channel_state->start_sample), sizeof(ui8));
memcpy(temp_time_series_index+24, &(channel_state->rps->block_header->number_of_samples), sizeof(ui4));
memcpy(temp_time_series_index+28, &(channel_state->rps->block_header->block_bytes), sizeof(ui4));
memcpy(temp_time_series_index+32, &(temp_struct.maximum_sample_value), sizeof(si4));
memcpy(temp_time_series_index+36, &(temp_struct.minimum_sample_value), sizeof(si4));
memset(temp_time_series_index+40, 0, 4);
memcpy(temp_time_series_index+44, &(channel_state->rps->block_header->flags), sizeof(ui1));
// write block index entry
//(void) e_fwrite(&block_index, sizeof(BLOCK_INDEX), (size_t) 1, block_inds_fps->fp, block_inds_fps->full_file_name, __FUNCTION__, __LINE__, USE_GLOBAL_BEHAVIOR);
(void) e_fwrite(temp_time_series_index, sizeof(ui1), (size_t) (45+RED_BLOCK_PROTECTED_REGION_BYTES+RED_BLOCK_DISCRETIONARY_REGION_BYTES), ts_inds_fps->fp, ts_inds_fps->full_file_name, __FUNCTION__, __LINE__, USE_GLOBAL_BEHAVIOR);
// update CRC
ts_inds_fps->universal_header->body_CRC = CRC_update(temp_time_series_index, 45+RED_BLOCK_PROTECTED_REGION_BYTES+RED_BLOCK_DISCRETIONARY_REGION_BYTES, ts_inds_fps->universal_header->body_CRC);
// update index file offset
channel_state->inds_file_offset += (45+RED_BLOCK_PROTECTED_REGION_BYTES+RED_BLOCK_DISCRETIONARY_REGION_BYTES);
// update discontinuity index
if (discontinuity_flag == 1)
{
//channel_state->discont_block_number = number_of_index_entries;
channel_state->discont_contiguous_blocks = 1;
channel_state->discont_contiguous_samples = channel_state->rps->block_header->number_of_samples;
channel_state->discont_contiguous_bytes = channel_state->rps->block_header->block_bytes;
//channel_state->discont_contiguous_duration_start = block_hdr_time;
}
else
{
//channel_state->discont_block_number = doesn't change
channel_state->discont_contiguous_blocks++;
channel_state->discont_contiguous_samples += channel_state->rps->block_header->number_of_samples;
channel_state->discont_contiguous_bytes += channel_state->rps->block_header->block_bytes;
}
// update metadata file
// maximum_contiguous_blocks
if (channel_state->discont_contiguous_blocks > md2->maximum_contiguous_blocks)
md2->maximum_contiguous_blocks = channel_state->discont_contiguous_blocks;
// maximum_contiguous_samples
if (channel_state->discont_contiguous_samples > md2->maximum_contiguous_samples)
md2->maximum_contiguous_samples = channel_state->discont_contiguous_samples;
// maximum_contiguous_block_bytes
if (channel_state->discont_contiguous_bytes > md2->maximum_contiguous_block_bytes)
md2->maximum_contiguous_block_bytes = channel_state->discont_contiguous_bytes;
// update fields for next time
channel_state->data_file_offset += channel_state->rps->block_header->block_bytes;
channel_state->start_sample += channel_state->rps->block_header->number_of_samples;
// update mef header fields relating to block index
channel_state->number_of_index_entries++;
channel_state->number_of_samples += num_entries;
// fprintf(stderr, "done with process_filled_block()");
update_metadata(channel_state); // necessary for real-time applications, otherwise, comment out this line.
return(0);
}
#ifdef _EXPORT_FOR_DLL
__declspec (dllexport)
#endif
si4 initialize_meflib_dll()
{
extern MEF_GLOBALS *MEF_globals;
(void)initialize_meflib();
MEF_globals->recording_time_offset_mode = RTO_IGNORE; // turn off timestamp offsetting by default
return 0;
}
#ifdef _EXPORT_FOR_DLL
__declspec (dllexport)
#endif
si4 write_mef_channel_data( CHANNEL_STATE *channel_state,
ui8 *packet_times,
si4 *samps,
ui8 n_packets_to_process,
sf8 secs_per_block,
sf8 sampling_frequency)
{
si4 *raw_data_ptr_start, *raw_data_ptr_current;
ui8 block_len, block_hdr_time, block_boundary;
ui8 last_chan_timestamp;
si4 discontinuity_flag;
si8 j;
si8 block_interval;
int chan_num;
// fprintf(stderr, "in write_mef_channel_data()\n");
chan_num = channel_state->chan_num;
// bring in data from channel_state struct
raw_data_ptr_start = channel_state->raw_data_ptr_start;
raw_data_ptr_current = channel_state->raw_data_ptr_current;
block_hdr_time = channel_state->block_hdr_time;
block_boundary = channel_state->block_boundary;
last_chan_timestamp = channel_state->last_chan_timestamp;
discontinuity_flag = channel_state->discontinuity_flag;
block_interval = channel_state->metadata_fps->metadata.time_series_section_2->block_interval;
// this is updated everytime, although it should never change between calls.
// TBD add test to make sure it doesn't change? This needs to be a parameter call because sometimes you don't
// know the correct sampling frequency until data acutally arrives
channel_state->metadata_fps->metadata.time_series_section_2->sampling_frequency = sampling_frequency;
// set local constants
block_len = (ui8) ceil(secs_per_block * sampling_frequency); //user-defined block size (s), convert to # of samples
channel_state->block_len = block_len;
for (j = 0; j < n_packets_to_process; ++j)
{
// set timestamp for the first block processed
if (block_hdr_time == 0)