File: HDF5IO.c

package info (click to toggle)
csound-plugins 1.0.2~dfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,960 kB
  • sloc: cpp: 16,586; ansic: 7,522; python: 458; sh: 94; makefile: 25
file content (1404 lines) | stat: -rw-r--r-- 45,167 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
/*
 * Copyright (C) 2014 Edward Costello
 *
 * This software is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "HDF5IO.h"
#include <string.h>
#ifdef _MSC_VER
#include <io.h>
#else
#include <unistd.h>
#endif

#define HDF5ERROR(x) if (UNLIKELY((x) == -1)) \
    {csound->Die(csound, #x" error\nExiting\n");}

// Type strings to match the enum types
static const char typeStrings[8][12] = {
    "STRING_VAR",
    "ARATE_VAR",
    "KRATE_VAR",
    "IRATE_VAR",
    "ARATE_ARRAY",
    "KRATE_ARRAY",
    "IRATE_ARRAY",
    "UNKNOWN"
};

// Get the argument enum type from the opcode argument pointer
//
// Get the csound type from the argument
// Get the variable type name
// Do a string comparison and match the type to the enum type

ArgumentType HDF5IO_getArgumentTypeFromArgument(CSOUND *csound, MYFLT *argument)
{
    const CS_TYPE *csoundType = csound->GetTypeForArg((void *)argument);
    const char *type = csoundType->varTypeName;
    ArgumentType argumentType = UNKNOWN;

    if (strcmp("S", type) == 0) {

      argumentType = STRING_VAR;
    }
    else if (strcmp("a", type) == 0) {

      argumentType = ARATE_VAR;
    }
    else if (strcmp("k", type) == 0) {

      argumentType = KRATE_VAR;
    }
    else if (strcmp("i", type) == 0) {

      argumentType = IRATE_VAR;
    }
    else if (strcmp("[", type) == 0) {

      ARRAYDAT *array = (ARRAYDAT *)argument;

      if (strcmp("k", array->arrayType->varTypeName) == 0) {

        argumentType = KRATE_ARRAY;
      }
      else if (strcmp("a", array->arrayType->varTypeName) == 0) {

        argumentType = ARATE_ARRAY;
      }
      else if (strcmp("i", array->arrayType->varTypeName) == 0) {

        argumentType = IRATE_ARRAY;
      }
    }

    return argumentType;
}

// Get the matching argument enum type from a string
//
// Do a string comparison and assign the corresponding enum type to the string

ArgumentType HDF5IO_getArgumentTypeFromString(CSOUND *csound, const char *string)
{
     IGN(csound);
    ArgumentType type = UNKNOWN;

    if (strcmp("STRING_VAR", string) == 0) {

      type = STRING_VAR;
    }
    else if (strcmp("ARATE_VAR", string) == 0) {

      type = ARATE_VAR;
    }
    else if (strcmp("KRATE_VAR", string) == 0) {

      type = KRATE_VAR;
    }
    else if (strcmp("IRATE_VAR", string) == 0) {

      type = IRATE_VAR;
    }
    else if (strcmp("ARATE_ARRAY", string) == 0) {

      type = ARATE_ARRAY;
    }
    else if (strcmp("KRATE_ARRAY", string) == 0) {

      type = KRATE_ARRAY;
    }
    else if (strcmp("IRATE_ARRAY", string) == 0) {

      type = IRATE_ARRAY;
    }

    return type;
}

// Create or open a hdf5 file
//
// Allocate the memory for a hdf5 file struct
// Assign the path string to the file name in the struct
// Check if the file exists
// If it doesn't and this function is being called by the hdf5 write function,
// create the file
// If it exists, open the file for appending
// Find out what size the MYFLT floating point type is and assign the correct
// hdf5 type to it
// Return the pointer to the hdf5 file struct

HDF5File *HDF5IO_newHDF5File(CSOUND *csound, AUXCH *hdf5FileMemory,
                             STRINGDAT *path, bool openForWriting)
{
    csound->AuxAlloc(csound, sizeof(HDF5File), hdf5FileMemory);
    HDF5File *hdf5File = hdf5FileMemory->auxp;

    hdf5File->fileName = path->data;

    int32_t fileExists = access(hdf5File->fileName, 0);

    if (UNLIKELY(fileExists == -1)) {

      if (LIKELY(openForWriting == true)) {

        hdf5File->fileHandle = H5Fcreate(hdf5File->fileName, H5F_ACC_TRUNC,
                                         H5P_DEFAULT, H5P_DEFAULT);
        HDF5ERROR(hdf5File->fileHandle);
      }
      else {

        csound->Die(csound, "hdf5read: Error, file does not exist");
      }
    }
    else {

      hdf5File->fileHandle = H5Fopen(hdf5File->fileName, H5F_ACC_RDWR, H5P_DEFAULT);
      HDF5ERROR(hdf5File->fileHandle);
    }

#ifdef USE_DOUBLE
      hdf5File->floatSize = H5T_NATIVE_DOUBLE;
#else
      hdf5File->floatSize = H5T_NATIVE_FLOAT;
#endif
      //else {

      //csound->Die(csound,"HDF5IO: Illegal size for floating point type, exiting");
      //}

    return hdf5File;
}

// Write a string attribute to a hdf5 file dataset
//
// Create a new attribute of type C string
// Set the size of the string to 11 characters, this is the longest string
// length of the argument type strings
// Set the string to be null terminated
// Create the attribute in the hdf5 file
// Write the string to the attribute
// Close the open handles

void HDF5IO_writeStringAttribute(CSOUND *csound, HDF5File *self,
                                 HDF5Dataset *dataset,
                                 const char *attributeName,
                                 const char *attributeString)
{
     IGN(self);
    hid_t attributeID  = H5Screate(H5S_SCALAR);
    HDF5ERROR(attributeID);
    hid_t attributeType = H5Tcopy(H5T_C_S1);
    HDF5ERROR(attributeType);
    HDF5ERROR(H5Tset_size(attributeType, 11));
    HDF5ERROR(H5Tset_strpad(attributeType,H5T_STR_NULLTERM));
    hid_t attributeHandle = H5Acreate2(dataset->datasetID,
                                       attributeName, attributeType, attributeID,
                                       H5P_DEFAULT, H5P_DEFAULT);
    HDF5ERROR(attributeHandle);
    HDF5ERROR(H5Awrite(attributeHandle, attributeType, attributeString));
    HDF5ERROR(H5Sclose(attributeID));
    HDF5ERROR(H5Tclose(attributeType));
    HDF5ERROR(H5Aclose(attributeHandle));
}

// Read a string attribute from the hdf5 file
//
// Open the data set and get the info structure
// Get the attribute at index 0, this should probably be more general, needs
// more work
// Get the type of the attribute
// Read the attribute into memory and close all the handles

void HDF5IO_readStringAttribute(CSOUND *csound, HDF5File *self,
                                char *datasetName, char *attributeString)
{
    hid_t dataSetID = H5Dopen2(self->fileHandle, datasetName, H5P_DEFAULT);
    H5O_info_t oinfo;
#if HDF5_VERSION_MAJOR == 1 && HDF5_VERSION_MINOR >= 12
    HDF5ERROR(H5Oget_info1(dataSetID, &oinfo));
#else
    HDF5ERROR(H5Oget_info(dataSetID, &oinfo));
#endif
    hid_t attributeID = H5Aopen_by_idx(dataSetID, ".", H5_INDEX_CRT_ORDER,
                                       H5_ITER_INC, 0, H5P_DEFAULT, H5P_DEFAULT);
    HDF5ERROR(attributeID);
    hid_t attributeType = H5Aget_type(attributeID);
    HDF5ERROR(attributeType);
    hid_t attributeTypeMemory = H5Tget_native_type(attributeType, H5T_DIR_ASCEND);
    HDF5ERROR(attributeTypeMemory);
    HDF5ERROR(H5Aread(attributeID, attributeTypeMemory, attributeString));
    HDF5ERROR(H5Aclose(attributeID));
    HDF5ERROR(H5Tclose(attributeType));
    HDF5ERROR(H5Tclose(attributeTypeMemory));
    HDF5ERROR(H5Dclose(dataSetID));
}

// Find if csound is running in sample accurate mode
//
// Get the argument parameters passed to run csound
// Find the sample accurate parameter and return it

bool HDF5IO_getSampleAccurate(CSOUND *csound)
{
    OPARMS parameters = {0};
    csound->GetOParms(csound, &parameters);
    return parameters.sampleAccurate;
}


// Set everything up so datasets can be written from the opcodes input variables,
// i-rate datasets are written at initialisation all others are written on the
// performance pass.
//
// Get the amount of samples in a control pass
// Get the amount of arguments to the opcode, this doesn't include the first
// argument which is for the filename.
// Check that the first argument is a string for the filename, check others
// are not strings
// Register the callback to close the hdf5 file when performance finishes
// Get the path argument and open a hdf5 file, if it doesn't exist create it
// Create the datasets in the file so they can be written

int32_t HDF5Write_initialise(CSOUND *csound, HDF5Write *self)
{
    self->ksmps = csound->GetKsmps(csound);
    self->inputArgumentCount = self->INOCOUNT - 1;
    HDF5Write_checkArgumentSanity(csound, self);
    csound->RegisterDeinitCallback(csound, self, HDF5Write_finish);

    STRINGDAT *path = (STRINGDAT *)self->arguments[0];
    self->hdf5File = HDF5IO_newHDF5File(csound, &self->hdf5FileMemory, path, true);
    HDF5Write_createDatasets(csound, self);

    return OK;
}

// Write the input data to the associated dataset
//
// Enlarge the dataset first then get the file space
// Select a hyperslab with the necessary offset and size
// Create a memory space where the data is written to
// Write the data to the memory space
// Close the file space

void HDF5Write_writeData(CSOUND *csound, HDF5Write *self,
                         HDF5Dataset *dataset, MYFLT *data)
{
    HDF5ERROR(H5Dset_extent(dataset->datasetID, dataset->datasetSize));
    hid_t filespace = H5Dget_space(dataset->datasetID);
    HDF5ERROR(filespace);
    HDF5ERROR(H5Sselect_hyperslab(filespace, H5S_SELECT_SET, dataset->offset,
                                  NULL, dataset->chunkDimensions, NULL));
    hid_t memspace  = H5Screate_simple(dataset->rank, dataset->chunkDimensions,
                                       NULL);
    HDF5ERROR(memspace);
    HDF5ERROR(H5Dwrite(dataset->datasetID, self->hdf5File->floatSize, memspace,
                       filespace, H5P_DEFAULT, data));
    HDF5ERROR(H5Sclose(filespace));
}

// Write a-rate variables and arrays to the specified data set
//
// For sample accurate mode, get the offset and early variables
// Calculate the size of the incoming vector
// If the vector is 0 return, no more data to write
// Expand the dataset size by ksmps, because data is written in chunks
// For sample accurate mode the exact dataset size is set when writing is finished
// Write the data to the dataset
// Increment the offset by the size of the vector that was just written

void HDF5Write_writeAudioData(CSOUND *csound, HDF5Write *self,
                              HDF5Dataset *dataset, MYFLT *dataPointer)
{
    size_t offset = self->h.insdshead->ksmps_offset;
    size_t early  = self->h.insdshead->ksmps_no_end;

    int32_t vectorSize = (int32_t)(self->ksmps - offset - early);

    if (UNLIKELY(vectorSize == 0)) {
      return;
    }

    dataset->datasetSize[0] += self->ksmps;

    HDF5Write_writeData(csound, self, dataset, &dataPointer[offset]);

    dataset->offset[0] += vectorSize;
}

// Write k-rate variables and arrays to the specified data set
//
// Increment the data set size by 1
// Write the data to the dataset
// Increment the offset by 1

void HDF5Write_writeControlData(CSOUND *csound, HDF5Write *self,
                                HDF5Dataset *dataset, MYFLT *dataPointer)
{
    dataset->datasetSize[0]++;

    HDF5Write_writeData(csound, self, dataset, dataPointer);

    dataset->offset[0]++;
}

// Send each input argument to the necessary writing function
//
// Iterate through the dataset array, select the current
// Depending on the dataset type send to the necessary write function

int32_t HDF5Write_process(CSOUND *csound, HDF5Write *self)
{
    int32_t i;
    for (i = 0; i < self->inputArgumentCount; ++i) {

      HDF5Dataset *currentDataset = &self->datasets[i];

      switch (currentDataset->writeType) {

      case ARATE_ARRAY: {

        HDF5Write_writeAudioData(csound, self, currentDataset,
                             ((ARRAYDAT *)currentDataset->argumentPointer)->data);
        break;
      }
      case KRATE_ARRAY: {

        HDF5Write_writeControlData(csound, self, currentDataset,
                              ((ARRAYDAT *)currentDataset->argumentPointer)->data);
        break;
      }
      case ARATE_VAR: {

        HDF5Write_writeAudioData(csound, self, currentDataset,
                                 currentDataset->argumentPointer);
        break;
      }
      case KRATE_VAR: {

        HDF5Write_writeControlData(csound, self, currentDataset,
                                   currentDataset->argumentPointer);
        break;
      }
      default: {

        break;
      }
      }
    }
    return OK;
}

// Close the hdf5 file and set the a-rate dataset extents for sample accurate mode
//
// Check that the datasets exist
// Iterate through the datasets, if a-rate, set the size to be the same as
// current offset
// Set the set extent of the dataset to the size
// Close the datasets, then close the file

int32_t HDF5Write_finish(CSOUND *csound, void *inReference)
{
    HDF5Write *self = inReference;

    if (LIKELY(self->datasets != NULL)) {
      int32_t i;
      for (i = 0; i < self->inputArgumentCount; ++i) {

        HDF5Dataset *dataset = &self->datasets[i];

        switch (dataset->writeType) {

        case ARATE_ARRAY: {

          dataset->datasetSize[0] =
            dataset->offset[0];
          HDF5ERROR(H5Dset_extent(dataset->datasetID, dataset->datasetSize));
          break;
        }
        case ARATE_VAR: {

          dataset->datasetSize[0] =
            dataset->offset[0];
          HDF5ERROR(H5Dset_extent(dataset->datasetID, dataset->datasetSize));
          break;
        }
        default: {

          break;
        }
        }

        HDF5ERROR(H5Dclose(dataset->datasetID));
      }
    }

    HDF5ERROR(H5Fclose(self->hdf5File->fileHandle));

    return OK;
}

// Check that the correct types of inputs have been used in the opcode
//
// Get the argument type for the first input
// If it is not a string stop csound
// Iterate over the rest of the input arguments, if any are strings stop csound

void HDF5Write_checkArgumentSanity(CSOUND *csound, const HDF5Write *self)
{
    int32_t i;
    ArgumentType type = HDF5IO_getArgumentTypeFromArgument(csound,
                                                           self->arguments[0]);

    if (UNLIKELY(type != STRING_VAR)) {

      csound->Die(csound, "%s", Str("hdf5write: Error, first argument does not "
                              "appear to be a string, exiting"));
    }

    for (i = 0; i < self->inputArgumentCount; ++i) {

      type = HDF5IO_getArgumentTypeFromArgument(csound, self->arguments[i + 1]);

      if (UNLIKELY(type == STRING_VAR
                   ||
                   type == UNKNOWN)) {

        csound->Die(csound, Str("hdf5write: Error, unable to identify type "
                                "of argument %d"), i);
      }
    }
}

// Create, or delete and create again a dataset in an hdf5 file
//
// Check to see if the dataset exists
// If it exists delete it
// Create the data space, set the writing chunk size and the empty space fill value
// Create the data set in the data space and write the argument type as a string
// attribute

void HDF5Write_initialiseHDF5Dataset(CSOUND *csound, HDF5Write *self,
                                     HDF5Dataset *dataset)
{
    htri_t result = H5Lexists(self->hdf5File->fileHandle,
                              dataset->datasetName, H5P_DEFAULT);

    if (result == 1) {

      HDF5ERROR(H5Ldelete(self->hdf5File->fileHandle,
                          dataset->datasetName, H5P_DEFAULT));
    }

    hid_t dataspaceID = H5Screate_simple(dataset->rank, dataset->chunkDimensions,
                                         dataset->maxDimensions);
    HDF5ERROR(dataspaceID);
    hid_t cparams = H5Pcreate(H5P_DATASET_CREATE);
    HDF5ERROR(cparams);

    HDF5ERROR(H5Pset_chunk(cparams, dataset->rank, dataset->chunkDimensions));

    MYFLT zero = 0;

    HDF5ERROR(H5Pset_fill_value(cparams, self->hdf5File->floatSize, &zero));

    dataset->datasetID = H5Dcreate2(self->hdf5File->fileHandle,
                                    dataset->datasetName,
                                    self->hdf5File->floatSize,
                                    dataspaceID, H5P_DEFAULT, cparams, H5P_DEFAULT);
    HDF5ERROR(dataset->datasetID);
    HDF5IO_writeStringAttribute(csound, self->hdf5File, dataset,
                                "Variable Type", typeStrings[dataset->writeType]);

}

// Set up the variables for writing an array dataset
//
// Get the array from the argument pointer
// If its an i-rate array the rank is copied, if not we add a dimension
// Allocate arrays for the chunk sizes, maximum sizes, data set sizes and
// offset sizes
// Copy the sizes from the input array variables to the allocated size arrays
// If it's an a-rate array set the last chunk dimension to ksmps, last max
// dimension to unlimited and dataset size to 0
// If it's a k-rate array set the last chunk dimension to 1 and last max
// dimension to unlimited
// If it's an i-rate array just return

void HDF5Write_newArrayDataset(CSOUND *csound, HDF5Write *self,
                               HDF5Dataset *dataset)
{
    ARRAYDAT *array = (ARRAYDAT *)dataset->argumentPointer;
    int32_t i;
    if (dataset->writeType == IRATE_ARRAY) {

      dataset->rank = array->dimensions;
    }
    else {

      dataset->rank = array->dimensions + 1;
    }

    csound->AuxAlloc(csound, dataset->rank * sizeof(hsize_t),
                     &dataset->chunkDimensionsMemory);
    dataset->chunkDimensions = dataset->chunkDimensionsMemory.auxp;

    csound->AuxAlloc(csound, dataset->rank * sizeof(hsize_t),
                     &dataset->maxDimensionsMemory);
    dataset->maxDimensions = dataset->maxDimensionsMemory.auxp;

    csound->AuxAlloc(csound, dataset->rank * sizeof(hsize_t),
                     &dataset->datasetSizeMemory);
    dataset->datasetSize = dataset->datasetSizeMemory.auxp;

    csound->AuxAlloc(csound, dataset->rank * sizeof(hsize_t),
                     &dataset->offsetMemory);
    dataset->offset = dataset->offsetMemory.auxp;

    for (i = 0; i < array->dimensions; ++i) {

      dataset->chunkDimensions[i + 1] = array->sizes[i];
      dataset->maxDimensions[i + 1] = array->sizes[i];
      dataset->datasetSize[i + 1] = array->sizes[i];
    }

    switch (dataset->writeType) {

    case ARATE_ARRAY: {

      dataset->chunkDimensions[0] = self->ksmps;
      dataset->maxDimensions[0] = H5S_UNLIMITED;
      dataset->datasetSize[0] = 0;

      break;
    }
    case KRATE_ARRAY: {

      dataset->chunkDimensions[0] = 1;
      dataset->maxDimensions[0] = H5S_UNLIMITED;
      break;
    }
    case IRATE_ARRAY: {

      return;
    }
    default: {

      csound->Die(csound, "%s", Str("This should not happen, exiting"));
      break;
    }
    }
}

// Set up variables for writing a scalar dataset
//
// Set the rank to 1
// Allocate memory for chunk sizes, maximum sizes, dataset sizes and offsets
// If the argument is not an i-rate variable:
//  Set the chunk dimensions to ksmps if a-rate, 1 if k-rate
//  Set maximum dimensions to unlimited
//  Set the data size to 0
// If it is an i-rate variable:
//  Set the data set size to 1
//  Set the chunk dimensions to 1
//  Set the maximum dimensions to 1
// Set the offset to 0

void HDF5Write_newScalarDataset(CSOUND *csound, HDF5Write *self,
                                HDF5Dataset *dataset)
{
    dataset->rank = 1;
    csound->AuxAlloc(csound, sizeof(hsize_t), &dataset->chunkDimensionsMemory);
    dataset->chunkDimensions = dataset->chunkDimensionsMemory.auxp;

    csound->AuxAlloc(csound, sizeof(hsize_t), &dataset->maxDimensionsMemory);
    dataset->maxDimensions = dataset->maxDimensionsMemory.auxp;

    csound->AuxAlloc(csound, sizeof(hsize_t), &dataset->datasetSizeMemory);
    dataset->datasetSize = dataset->datasetSizeMemory.auxp;

    csound->AuxAlloc(csound, sizeof(hsize_t), &dataset->offsetMemory);
    dataset->offset = dataset->offsetMemory.auxp;

    if (dataset->writeType != IRATE_VAR) {

      dataset->chunkDimensions[0] =
        dataset->writeType == ARATE_VAR ? self->ksmps : 1;
      dataset->maxDimensions[0] = H5S_UNLIMITED;
      dataset->datasetSize[0] = 0;
    }
    else {
      dataset->datasetSize[0] = 1;
      dataset->chunkDimensions[0] = 1;
      dataset->maxDimensions[0] = 1;
    }

    dataset->offset[0] = 0;
}

// Create the datasets for each argument to be written
//
// Allocate the memory for the datasets array
// Get the current empty dataset from the array
// Set it's dataset name as the input arguments name + 1 which is after the
// file path string
// Get the argument pointer from the arguments + 1 after the file path string
// Get the enum write type from the argument pointer
// Depending on the write type set up the variables in the correct way for
// writing during performance
// If the variables are i-rate set up the variables and write them

void HDF5Write_createDatasets(CSOUND *csound, HDF5Write *self)
{
    int32_t i;
    csound->AuxAlloc(csound, sizeof(HDF5Dataset) * self->inputArgumentCount,
                     &self->datasetsMemory);
    self->datasets = self->datasetsMemory.auxp;

    for (i = 0; i < self->inputArgumentCount; ++i) {

      HDF5Dataset *currentDataset = &self->datasets[i];
      currentDataset->datasetName = csound->GetInputArgName(self, (int32_t)i + 1);
      currentDataset->argumentPointer = self->arguments[i + 1];
      currentDataset->writeType =
        HDF5IO_getArgumentTypeFromArgument(csound, currentDataset->argumentPointer);

      switch (currentDataset->writeType) {

      case ARATE_ARRAY: {

        HDF5Write_newArrayDataset(csound, self, currentDataset);
        HDF5Write_initialiseHDF5Dataset(csound, self, currentDataset);
        break;
      }
      case KRATE_ARRAY: {

        HDF5Write_newArrayDataset(csound, self, currentDataset);
        HDF5Write_initialiseHDF5Dataset(csound, self, currentDataset);
        break;
      }
      case IRATE_ARRAY: {

        HDF5Write_newArrayDataset(csound, self, currentDataset);
        HDF5Write_initialiseHDF5Dataset(csound, self, currentDataset);
        HDF5Write_writeData(csound, self, currentDataset,
                            ((ARRAYDAT *)currentDataset->argumentPointer)->data);
        break;
      }
      case ARATE_VAR: {

        HDF5Write_newScalarDataset(csound, self, currentDataset);
        HDF5Write_initialiseHDF5Dataset(csound, self, currentDataset);
        break;
      }
      case KRATE_VAR: {

        HDF5Write_newScalarDataset(csound, self, currentDataset);
        HDF5Write_initialiseHDF5Dataset(csound, self, currentDataset);
        break;
      }
      case IRATE_VAR: {

        HDF5Write_newScalarDataset(csound, self, currentDataset);
        HDF5Write_initialiseHDF5Dataset(csound, self, currentDataset);
        HDF5Write_writeData(csound, self, currentDataset,
                            currentDataset->argumentPointer);
        break;
      }
      default: {

        break;
      }
      }
    }
}

// Open datasets in a hdf5 file so they can be read by the opcode
//
// Get the amount of samples in a control pass
// Get the amount of input arguments minus the file path argument
// Get the amount of output arguments
// Check that input == output arguments and input arguments are strings,
// output not strings
// Register the finish callback to close the hdf5 file when performance
// is finished
// Check csound is running in sample accurate mode
// Get the path string from the first argument
// Open the hdf5 file then open the hdf5 datasets

int32_t HDF5Read_initialise(CSOUND *csound, HDF5Read *self)
{
    self->ksmps = csound->GetKsmps(csound);
    self->inputArgumentCount = self->INOCOUNT - 1;
    self->outputArgumentCount = self->OUTOCOUNT;
    HDF5Read_checkArgumentSanity(csound, self);
    csound->RegisterDeinitCallback(csound, self, HDF5Read_finish);
    self->isSampleAccurate = HDF5IO_getSampleAccurate(csound);
    //STRINGDAT *path = (STRINGDAT *)self->arguments[self->outputArgumentCount];
    self->hdf5File = HDF5IO_newHDF5File(csound, &self->hdf5FileMemory, self->path, false);
    HDF5Read_openDatasets(csound, self);

    return OK;
}

// Copy data from the a-rate variable read sample buffer to the output array data
//
// This function is used to copy data from a sample buffer each hdf5
// struct to an array data member, this is because in sample accurate
// mode the stride of the data read from the hdf5 file isn't correct
// and must be offset properly before it is written to the array data

void HDF5Read_copySampleBufferToArray(size_t channelCount, MYFLT *sampleBuffer,
                                      MYFLT *arrayData, size_t vectorSize,
                                      size_t offset, size_t ksmps)
{
    size_t channel;
    for (channel = 0; channel < channelCount; ++channel) {

      memcpy(&arrayData[ksmps * channel + offset],
             &sampleBuffer[vectorSize * channel],
             sizeof(MYFLT) * vectorSize);
    }
}

// Read data from an hdf5 file dataset
//
// Get the specified file space
// Select the hyperslab at the specified offset and chunk dimension
// Create a memory space from where the data is read
// Read the data from the memory space
// Close the open file and memory spaces

void HDF5Read_readData(CSOUND *csound, HDF5Read *self, HDF5Dataset *dataset,
                       hsize_t *offset, hsize_t *chunkDimensions,
                       MYFLT *dataPointer)
{
    uint64_t kCount = csound->GetKcounter(csound);

    if (kCount > dataset->datasetSize[0]) {

      return;
    }

    hid_t filespace = H5Dget_space(dataset->datasetID);
    HDF5ERROR(filespace);
    HDF5ERROR(H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset,
                                  NULL, chunkDimensions, NULL));
    hid_t memspace  = H5Screate_simple(dataset->rank, chunkDimensions, NULL);
    HDF5ERROR(memspace);
    HDF5ERROR(H5Dread(dataset->datasetID, self->hdf5File->floatSize,
                      memspace, filespace, H5P_DEFAULT, dataPointer));
    HDF5ERROR(H5Sclose(filespace));
    HDF5ERROR(H5Sclose(memspace));


}

// Read data at audio rate from a hdf5 file dataset
//
// If the offset is larger than the size of the dataset there is no more
// data to read so return
// Get the offset and early variables and work out the size of data to read
// If the read vector size plus the offset is larger than the dataset size
// reduce the vector size accordingly
// If the vector size is less than the ksmps value, use the sample
// buffer to store read data so the stride can be corrected before
// writing it to the array data, if not just point directly to array
// data
// Create the chunk dimensions variable, set the required size
// Read data from the hdf5 file
// If the vector size is not equal to ksmps correct the stride of data
// Increment the offset by the vector size

void HDF5Read_readAudioData(CSOUND *csound, HDF5Read *self,
                            HDF5Dataset *dataset, MYFLT *inputDataPointer)
{
    if (dataset->offset[0] >=
        dataset->datasetSize[0]) {
      return;
    }

    size_t offset = self->h.insdshead->ksmps_offset;
    size_t early  = self->h.insdshead->ksmps_no_end;

    size_t vectorSize = (int32_t)(self->ksmps - offset - early);

    if (vectorSize + dataset->offset[0] >
        dataset->datasetSize[0]) {

      vectorSize = (int32_t)(dataset->datasetSize[0] -
                         dataset->offset[0]);
    }
    MYFLT *dataPointer =
      vectorSize != self->ksmps ? dataset->sampleBuffer : inputDataPointer;
        // FIXME if this is called frequently or on the audio thread then this won't
        // work and will need a different solution
#ifdef _MSC_VER
    //hsize_t* chunkDimensions = malloc (dataset->rank * sizeof (hsize_t));
    hsize_t chunkDimensions[16];
#else
    hsize_t chunkDimensions[dataset->rank];
#endif
    memcpy(&chunkDimensions[1], &dataset->datasetSize[1],
           sizeof(hsize_t) * (dataset->rank-1)); // Reads too much
    chunkDimensions[0] = vectorSize;

    //    memcpy (chunkDimensions, dataset->datasetSize,
    //            sizeof (hsize_t)/* * dataset->rank */
    //            );
    //chunkDimensions[dataset->rank - 1] = vectorSize;

    HDF5Read_readData (csound, self, dataset, dataset->offset,
                       chunkDimensions, dataPointer);

    if (vectorSize != self->ksmps) {

      HDF5Read_copySampleBufferToArray(dataset->elementCount,
                                       dataset->sampleBuffer, inputDataPointer,
                                       vectorSize, offset, self->ksmps);
    }

    dataset->offset[0] += vectorSize;

#ifdef _MSC_VER
    //free (chunkDimensions);
#endif

}

// Read data at control rate from a hdf5 dataset
//
// If the offset of the dataset is larger than the data set size, no
// more data to read to return
// Create chunk dimension variable and set the appropriate size
// Read the data from the dataset
// Increment the offset variable

void HDF5Read_readControlData(CSOUND *csound, HDF5Read *self,
                              HDF5Dataset *dataset, MYFLT *dataPointer)
{
    if (dataset->offset[0] >=
        dataset->datasetSize[0]) {

      return;
    }

        // FIXME if this is called frequently or on the audio thread then this won't
        // work and will need a different solution
#ifndef _MSC_VER
    hsize_t chunkDimensions[dataset->rank];
#else
    hsize_t* chunkDimensions = malloc (dataset->rank * sizeof (hsize_t));
#endif
    memcpy(&chunkDimensions[1], &dataset->datasetSize[1],
           sizeof(hsize_t) * (dataset->rank - 1));
    chunkDimensions[0] = 1;

    HDF5Read_readData(csound, self, dataset, dataset->offset,
                      chunkDimensions, dataPointer);
    dataset->offset[0]++;
#ifdef _MSC_VER
    free (chunkDimensions);
#endif

}

// Read dataset variables during performance time
//
// Iterate through each of the opened datasets,
// Depending on the dataset read type use the appropriate read function
// to read the data

int32_t HDF5Read_process(CSOUND *csound, HDF5Read *self)
{
    int32_t i;
    for (i = 0; i < self->inputArgumentCount; ++i) {

      HDF5Dataset *dataset = &self->datasets[i];

      if (dataset->readAll == true) {

        continue;
      }

      switch (dataset->readType) {

      case ARATE_ARRAY: {

        HDF5Read_readAudioData(csound, self, dataset,
                               ((ARRAYDAT *)dataset->argumentPointer)->data);
        break;
      }
      case KRATE_ARRAY: {

        HDF5Read_readControlData(csound, self, dataset,
                                 ((ARRAYDAT *)dataset->argumentPointer)->data);
        break;
      }
      case ARATE_VAR: {

        HDF5Read_readAudioData(csound, self, dataset, dataset->argumentPointer);
        break;
      }
      case KRATE_VAR: {

        HDF5Read_readControlData(csound, self, dataset, dataset->argumentPointer);
        break;
      }

      default: {

        break;
      }
      }
    }
    return OK;
}

// Close the necessary variables when reading has finished
//
// Iterate through open datasets closing them in the hdf5 file
// Close the hdf5 file

int32_t HDF5Read_finish(CSOUND *csound, void *inReference)
{
    HDF5Read *self = inReference;
    int32_t i;
    for (i = 0; i < self->inputArgumentCount; ++i) {

      HDF5Dataset *dataset = &self->datasets[i];

      HDF5ERROR(H5Dclose(dataset->datasetID));
    }

    HDF5ERROR(H5Fclose(self->hdf5File->fileHandle));

    return OK;
}

// Check the input and output arguments for validity
//
// Check to see that if the amount of input arguments matches output arguments
// Check that the input arguments are strings, and the output arguments are
// not strings

void HDF5Read_checkArgumentSanity(CSOUND *csound, const HDF5Read *self)
{
    int32_t i;
    if (UNLIKELY(self->inputArgumentCount != self->outputArgumentCount)) {

      if (self->inputArgumentCount > self->outputArgumentCount) {

        csound->Die(csound, "%s", Str("hdf5read: Error, more input arguments than "
                                "output arguments, exiting"));
      }
      else {

        csound->Die(csound, "%s", Str("hdf5read: Error, more output arguments than "
                                "input arguments, exiting"));
      }
    }

    for (i = 0; i < self->inputArgumentCount; ++i) {

      ArgumentType outputType =
        HDF5IO_getArgumentTypeFromArgument(csound, self->arguments[i]);

      if (UNLIKELY(outputType == STRING_VAR)) {

        csound->Die(csound, Str("hdf5read: Error, output argument %d appears "
                                "to be a string, exiting"), i + 1);
      }
      else if (UNLIKELY(outputType == UNKNOWN)) {

        csound->Die(csound, Str("hdf5read: Error, output argument %d type "
                                "is unknown, exiting"), i + 1);
      }
    }
}

// Open the specified dataset in the hdf5 file
//
// Check that the hdf5 dataset exists in the file, if it doesn't stop csound
// If it does then open it

void HDF5Read_initialiseHDF5Dataset(CSOUND *csound, HDF5Read *self,
                                    HDF5Dataset *dataset)
{
    htri_t result = H5Lexists(self->hdf5File->fileHandle,
                              dataset->datasetName, H5P_DEFAULT);

    if (UNLIKELY(result <= 0)) {

      csound->Die(csound, "%s", Str("hdf5read: Error, dataset does not exist or "
                              "cannot be found in file"));
    }

    dataset->datasetID = H5Dopen2(self->hdf5File->fileHandle,
                                  dataset->datasetName, H5P_DEFAULT);
    HDF5ERROR(dataset->datasetID);
}

// Check opcode read types are compatible with the types written to the hdf5 dataset
//
// Get the written type from the hdf5 file
// If the opcode read type for the dataset is an array and the write type is
// an array or a-rate or k-rate variable return
// If the opcode read type is an a-rate or k-rate variable and the write type
// is an a-rate or k-rate vairable return
// If the opcode read type is an i-reate variable and the write type is an
// i-rate variable return
// Otherwise stop csound

void HDF5Read_checkReadTypeSanity(CSOUND *csound, HDF5Read *self,
                                  HDF5Dataset *dataset)
{
    char attributeString[12] = {"UNKNOWN\0"};
    HDF5IO_readStringAttribute(csound, self->hdf5File,
                               dataset->datasetName, attributeString);
    dataset->writeType = HDF5IO_getArgumentTypeFromString(csound, attributeString);

    if (dataset->readType == ARATE_ARRAY
        ||
        dataset->readType == KRATE_ARRAY
        ||
        dataset->readType == IRATE_ARRAY) {

      if (dataset->writeType == ARATE_ARRAY
          ||
          dataset->writeType == KRATE_ARRAY
          ||
          dataset->writeType == IRATE_ARRAY
          ||
          dataset->writeType == ARATE_VAR
          ||
          dataset->writeType == KRATE_VAR) {

        return;
      }
    }
    else if (dataset->readType == ARATE_VAR
             ||
             dataset->readType == KRATE_VAR) {

      if (dataset->writeType == ARATE_VAR
          ||
          dataset->writeType == KRATE_VAR) {

        return;
      }
    }
    else if (dataset->readType == IRATE_VAR) {

      if (dataset->writeType == IRATE_VAR) {

        return;
      }
    }
    else {

      csound->Die(csound, "%s", Str("hdf5read: Unable to read saved type of "
                              "dataset, exiting"));
    }
}

// Allocate the memory for an output array from the opcode
//
// Get the pointer to the output argument and cast to array
// Get the rank and allocate the dimension sizes memory
// Assign first dimension size to array sizes and dataset element count
// If the rank is greater than 1, multiply dimensions to get element count
// Allocate data space for the array using the element count

void HDF5Read_allocateArray(CSOUND *csound, HDF5Dataset *dataset,
                            hsize_t rank, hsize_t *dimensions)
{
    ARRAYDAT *array = dataset->argumentPointer;
    array->dimensions = (int32_t)rank;
    array->sizes = csound->Calloc(csound, sizeof(int32_t) * rank);

    array->sizes[0] = (int32_t)dimensions[0];
    dataset->elementCount = dimensions[0];

    if (rank > 1) {
      size_t i;
      for (i = 1; i < rank; ++i) {

        array->sizes[i] = (int32_t)dimensions[i];
        dataset->elementCount *= array->sizes[i];

      }
    }

    CS_VARIABLE *arrayVariable = array->arrayType->createVariable(csound, NULL);
    array->arrayMemberSize = arrayVariable->memBlockSize;
    array->data =
      csound->Calloc(csound,
                     arrayVariable->memBlockSize * dataset->elementCount);
}


// Initialise the dataset and prepare for reading an a-rate, k-rate or i-rate
// array during performance time
//
// Get the data space from the dataset in the hdf5 file
// Get the rank from the data space
// Allocate the size array for specified rank
// Get the dimensions of the dataset and copy to dataset size array
// If requested output type for dataset is not an i-rate array:
// Create the dimensions variable and copy the dataset size minus the last
// dimension to the array
// Then allocate the array data for the output argument
// Allocate the memory for the offset variable
// If it's an a-rate array and sample accurate allocate data for the sample buffer
// Else if it's an i-rate array copy the array dimensions including the last one
// Then allocate the array data for the output argument
// Cast the argument pointer to an array, then read the data into the array from
// the hdf5 file

void HDF5Read_initialiseArrayOutput(CSOUND *csound, HDF5Read *self,
                                    HDF5Dataset *dataset)
{

    hid_t dataspaceID = H5Dget_space(dataset->datasetID);
    HDF5ERROR(dataspaceID);
    dataset->rank = H5Sget_simple_extent_ndims(dataspaceID);

    csound->AuxAlloc(csound, sizeof(hsize_t) * dataset->rank,
                     &dataset->datasetSizeMemory);
    dataset->datasetSize = dataset->datasetSizeMemory.auxp;

    H5Sget_simple_extent_dims(dataspaceID, dataset->datasetSize, NULL);
    HDF5ERROR(H5Sclose(dataspaceID));

    if (dataset->readType != IRATE_ARRAY && dataset->readAll == false) {
        // FIXME if this is called frequently or on the audio thread then this won't
        // work and will need a different solution
#ifdef _MSC_VER
      hsize_t* arrayDimensions = malloc ((dataset->rank - 1) * sizeof (hsize_t));
#else
      hsize_t arrayDimensions[dataset->rank - 1];
#endif
      memcpy(arrayDimensions, &dataset->datasetSize[1],
             (dataset->rank - 1) * sizeof(hsize_t));

      HDF5Read_allocateArray(csound, dataset, (dataset->rank - 1), arrayDimensions);

      csound->AuxAlloc(csound, sizeof(hsize_t) * dataset->rank,
                       &dataset->offsetMemory);
      dataset->offset = dataset->offsetMemory.auxp;

      if (dataset->readType == ARATE_ARRAY
          ||
          self->isSampleAccurate == true) {

          csound->AuxAlloc(csound,
                           dataset->elementCount * self->ksmps * sizeof(MYFLT),
                           &dataset->sampleBufferMemory);
          dataset->sampleBuffer = dataset->sampleBufferMemory.auxp;
      }
#ifdef _MSC_VER
      free (arrayDimensions);
#endif
    }
    else {
#ifdef _MSC_VER
      hsize_t* arrayDimensions = malloc ((dataset->rank) * sizeof (hsize_t));
#else
      hsize_t arrayDimensions[dataset->rank];
#endif
      memcpy(arrayDimensions, dataset->datasetSize,
             dataset->rank * sizeof(hsize_t));
      HDF5Read_allocateArray(csound, dataset, dataset->rank, arrayDimensions);
      ARRAYDAT *array = dataset->argumentPointer;
#ifdef _MSC_VER
      hsize_t* offset = malloc ((dataset->rank) * sizeof (hsize_t));
#else
      hsize_t offset[dataset->rank];
#endif
      memset(offset, 0, sizeof(hsize_t) * dataset->rank);
      HDF5Read_readData(csound, self, dataset, offset,
                        arrayDimensions, array->data);
#ifdef _MSC_VER
      free (offset);
#endif
    }
}

// Initialise the dataset and prepare for reading an a-rate, k-rate or i-rate
// variable during performance time
//
// Get the data space from the dataset in the hdf5 file
// Set the rank as 1, the variable is a scalar
// If the dataset read type is a-rate or k-rate:
// Allocate the dataset size memory, get the size of the dataset and copy to
// the size memory
// Allocate offset memory and set to 0
// If the dataset is to be read at a-rate and we are running sample accurate
// allocate the sample buffer
// Otherwise create array dimesions variable, set to 1, create offset variable,
// set to 0 and read the i-rate variable

void HDF5Read_initialiseScalarOutput(CSOUND *csound, HDF5Read *self,
                                     HDF5Dataset *dataset)
{
    hid_t dataspaceID = H5Dget_space(dataset->datasetID);
    HDF5ERROR(dataspaceID);
    dataset->rank = 1;

    if (dataset->readType == ARATE_VAR
        ||
        dataset->readType == KRATE_VAR
        ||
        dataset->readType == IRATE_VAR) {

      csound->AuxAlloc(csound, sizeof(hsize_t) * dataset->rank,
                       &dataset->datasetSizeMemory);
      dataset->datasetSize = dataset->datasetSizeMemory.auxp;

      H5Sget_simple_extent_dims(dataspaceID, dataset->datasetSize, NULL);
      HDF5ERROR(H5Sclose(dataspaceID));

      csound->AuxAlloc(csound, sizeof(hsize_t), &dataset->offsetMemory);
      dataset->offset = dataset->offsetMemory.auxp;
      memset(dataset->offset, 0, sizeof(hsize_t));

      if (dataset->readType == ARATE_VAR
          &&
          self->isSampleAccurate == true) {

        csound->AuxAlloc(csound, self->ksmps * sizeof(MYFLT),
                         &dataset->sampleBufferMemory);
        dataset->sampleBuffer = dataset->sampleBufferMemory.auxp;
        dataset->elementCount = 1;
      }

      if (dataset->readType == IRATE_VAR) {

        hsize_t arrayDimensions[1] = {1};
        hsize_t offset[1] = {0};
        HDF5Read_readData(csound, self, dataset, offset, arrayDimensions,
                          dataset->argumentPointer);
      }
    }

}

// Open the datasets in an hdf5 file for reading
//
// Allocate memory for the datasets array
// Iterate through the datasets in the array
// Assign the input argument name to the dataset name
// Get the read type from the arguments
// Assign the argument data pointer
// Check that the read type and write type are compatiblea
// Initialise the data set using the corresponding function for read type specified

void HDF5Read_openDatasets(CSOUND *csound, HDF5Read *self)
{
    int32_t i;
    csound->AuxAlloc(csound, sizeof(HDF5Dataset) * self->inputArgumentCount,
                     &self->datasetsMemory);
    self->datasets = self->datasetsMemory.auxp;

    for (i = 0; i < self->inputArgumentCount; ++i) {

      HDF5Dataset *currentDataset = &self->datasets[i];
      STRINGDAT *inputArgument = (STRINGDAT *)self->names[i];

      csound->AuxAlloc(csound, sizeof(char) * strlen(inputArgument->data)+1,
                       &currentDataset->datasetNameMemory);
      currentDataset->datasetName = currentDataset->datasetNameMemory.auxp;
      strcpy(currentDataset->datasetName, inputArgument->data);

      if (currentDataset->datasetName[strlen(inputArgument->data) - 1] == '*') {

        currentDataset->readAll = true;
        currentDataset->datasetName[strlen(inputArgument->data) - 1] = '\0';
      }
      currentDataset->readType =
        HDF5IO_getArgumentTypeFromArgument(csound, self->arguments[i]);
      currentDataset->argumentPointer = self->arguments[i];
      HDF5Read_checkReadTypeSanity(csound, self, currentDataset);
      HDF5Read_initialiseHDF5Dataset(csound, self, currentDataset);

      switch (currentDataset->readType) {
      case ARATE_ARRAY: {

        HDF5Read_initialiseArrayOutput(csound, self, currentDataset);
        break;
      }
      case KRATE_ARRAY: {

        HDF5Read_initialiseArrayOutput(csound, self, currentDataset);
        break;
      }
      case IRATE_ARRAY: {

        HDF5Read_initialiseArrayOutput(csound, self, currentDataset);
        break;
      }
      case ARATE_VAR: {

        HDF5Read_initialiseScalarOutput(csound, self, currentDataset);
        break;
      }
      case KRATE_VAR: {

        HDF5Read_initialiseScalarOutput(csound, self, currentDataset);
        break;
      }
      case IRATE_VAR: {

        HDF5Read_initialiseScalarOutput(csound, self, currentDataset);
        break;
      }
      default:
        break;

      }
    }

}


static OENTRY localops[] = {

  {
    .opname = "hdf5write",
    .dsblksiz = sizeof(HDF5Write),
    .thread = 3,
    .outypes = "",
    .intypes = "*",
    .iopadr = (SUBR)HDF5Write_initialise,
    .kopadr = (SUBR)HDF5Write_process,
    .aopadr = NULL
  },
  {
    .opname = "hdf5read",
    .dsblksiz = sizeof(HDF5Read),
    .thread = 3,
    .outypes = "********************",
    .intypes = "SW",
    .iopadr = (SUBR)HDF5Read_initialise,
    .kopadr = (SUBR)HDF5Read_process,
    .aopadr = NULL
  }
};

LINKAGE