@earthyscience/netcdf4-wasm 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  // WASM module loading and wrapping functionality
2
- import { NC_CONSTANTS } from './constants.js';
2
+ import { NC_CONSTANTS, DATA_TYPE_SIZE } from './constants.js';
3
3
  const NC_MAX_NAME = 256;
4
4
  const NC_MAX_DIMS = 1024;
5
5
  const NC_MAX_VARS = 8192;
@@ -79,6 +79,7 @@ export class WasmModuleLoader {
79
79
  const nc_get_att_longlong_wrapper = module.cwrap('nc_get_att_longlong_wrapper', 'number', ['number', 'number', 'string', 'number']);
80
80
  const nc_get_att_string_wrapper = module.cwrap('nc_get_att_string_wrapper', 'number', ['number', 'number', 'string', 'number']);
81
81
  const nc_free_string_wrapper = module.cwrap('nc_free_string_wrapper', 'number', ['number', 'number']);
82
+ const nc_get_att_schar_wrapper = module.cwrap('nc_get_att_schar_wrapper', 'number', ['number', 'number', 'string', 'number']);
82
83
  // 8-bit unsigned
83
84
  const nc_get_att_uchar_wrapper = module.cwrap('nc_get_att_uchar_wrapper', 'number', ['number', 'number', 'string', 'number']);
84
85
  // 16-bit unsigned
@@ -119,6 +120,18 @@ export class WasmModuleLoader {
119
120
  const nc_inq_grp_full_ncid_wrapper = module.cwrap('nc_inq_grp_full_ncid_wrapper', 'number', ['number', 'string', 'number']);
120
121
  const nc_inq_grpname_full_wrapper = module.cwrap('nc_inq_grpname_full_wrapper', 'number', ['number', 'number', 'number']);
121
122
  const nc_inq_grpname_len_wrapper = module.cwrap('nc_inq_grpname_len_wrapper', 'number', ['number', 'number']);
123
+ // enum wrappers
124
+ const nc_inq_typeids_wrapper = module.cwrap('nc_inq_typeids_wrapper', 'number', ['number', 'number', 'number']);
125
+ const nc_inq_type_wrapper = module.cwrap('nc_inq_type_wrapper', 'number', ['number', 'number', 'number', 'number']);
126
+ const nc_inq_user_type_wrapper = module.cwrap('nc_inq_user_type_wrapper', 'number', ['number', 'number', 'number', 'number', 'number', 'number', 'number']);
127
+ const nc_def_enum_wrapper = module.cwrap('nc_def_enum_wrapper', 'number', ['number', 'number', 'string', 'number']);
128
+ const nc_insert_enum_wrapper = module.cwrap('nc_insert_enum_wrapper', 'number', ['number', 'number', 'string', 'number']);
129
+ const nc_inq_enum_wrapper = module.cwrap('nc_inq_enum_wrapper', 'number', ['number', 'number', 'number', 'number', 'number', 'number']);
130
+ const nc_inq_enum_member_wrapper = module.cwrap('nc_inq_enum_member_wrapper', 'number', ['number', 'number', 'number', 'number', 'number']);
131
+ const nc_inq_enum_ident_wrapper = module.cwrap('nc_inq_enum_ident_wrapper', 'number', ['number', 'number', 'number', 'number']);
132
+ // generic type inquiry wrapper
133
+ const nc_get_var_wrapper = module.cwrap('nc_get_var_wrapper', 'number', ['number', 'number', 'number']);
134
+ const nc_get_vara_wrapper = module.cwrap('nc_get_vara_wrapper', 'number', ['number', 'number', 'number', 'number', 'number']);
122
135
  return {
123
136
  ...module,
124
137
  nc_open: (path, mode) => {
@@ -429,11 +442,11 @@ export class WasmModuleLoader {
429
442
  //---- Variable Getters ----//
430
443
  nc_get_vara_short: (ncid, varid, start, count) => {
431
444
  const totalLength = count.reduce((a, b) => a * b, 1);
432
- const dataPtr = module._malloc(totalLength * 4);
433
- const startPtr = module._malloc(start.length * 8);
434
- const countPtr = module._malloc(count.length * 8);
435
- start.forEach((val, i) => module.setValue(startPtr + i * 8, val, 'i64'));
436
- count.forEach((val, i) => module.setValue(countPtr + i * 8, val, 'i64'));
445
+ const dataPtr = module._malloc(totalLength * 2);
446
+ const startPtr = module._malloc(start.length * 4);
447
+ const countPtr = module._malloc(count.length * 4);
448
+ start.forEach((val, i) => module.setValue(startPtr + i * 4, val, 'i32'));
449
+ count.forEach((val, i) => module.setValue(countPtr + i * 4, val, 'i32'));
437
450
  const result = nc_get_vara_short_wrapper(ncid, varid, startPtr, countPtr, dataPtr);
438
451
  const data = result === NC_CONSTANTS.NC_NOERR
439
452
  ? new Int16Array(module.HEAP16.buffer, dataPtr, totalLength).slice()
@@ -445,11 +458,11 @@ export class WasmModuleLoader {
445
458
  },
446
459
  nc_get_vara_int: (ncid, varid, start, count) => {
447
460
  const totalLength = count.reduce((a, b) => a * b, 1);
448
- const dataPtr = module._malloc(totalLength * 8);
449
- const startPtr = module._malloc(start.length * 8);
450
- const countPtr = module._malloc(count.length * 8);
451
- start.forEach((val, i) => module.setValue(startPtr + i * 8, val, 'i64'));
452
- count.forEach((val, i) => module.setValue(countPtr + i * 8, val, 'i64'));
461
+ const dataPtr = module._malloc(totalLength * 4);
462
+ const startPtr = module._malloc(start.length * 4);
463
+ const countPtr = module._malloc(count.length * 4);
464
+ start.forEach((val, i) => module.setValue(startPtr + i * 4, val, 'i32'));
465
+ count.forEach((val, i) => module.setValue(countPtr + i * 4, val, 'i32'));
453
466
  const result = nc_get_vara_int_wrapper(ncid, varid, startPtr, countPtr, dataPtr);
454
467
  const data = result === NC_CONSTANTS.NC_NOERR
455
468
  ? new Int32Array(module.HEAP32.buffer, dataPtr, totalLength).slice()
@@ -478,10 +491,10 @@ export class WasmModuleLoader {
478
491
  nc_get_vara_double: (ncid, varid, start, count) => {
479
492
  const totalLength = count.reduce((a, b) => a * b, 1);
480
493
  const dataPtr = module._malloc(totalLength * 8);
481
- const startPtr = module._malloc(start.length * 8);
482
- const countPtr = module._malloc(count.length * 8);
483
- start.forEach((val, i) => module.setValue(startPtr + i * 8, val, 'i64'));
484
- count.forEach((val, i) => module.setValue(countPtr + i * 8, val, 'i64'));
494
+ const startPtr = module._malloc(start.length * 4);
495
+ const countPtr = module._malloc(count.length * 4);
496
+ start.forEach((val, i) => module.setValue(startPtr + i * 4, val, 'i32'));
497
+ count.forEach((val, i) => module.setValue(countPtr + i * 4, val, 'i32'));
485
498
  const result = nc_get_vara_double_wrapper(ncid, varid, startPtr, countPtr, dataPtr);
486
499
  const data = result === NC_CONSTANTS.NC_NOERR
487
500
  ? new Float64Array(module.HEAPF64.buffer, dataPtr, totalLength).slice()
@@ -494,10 +507,10 @@ export class WasmModuleLoader {
494
507
  nc_get_vara_longlong: (ncid, varid, start, count) => {
495
508
  const totalLength = count.reduce((a, b) => a * b, 1);
496
509
  const dataPtr = module._malloc(totalLength * 8);
497
- const startPtr = module._malloc(start.length * 8);
498
- const countPtr = module._malloc(count.length * 8);
499
- start.forEach((val, i) => module.setValue(startPtr + i * 8, val, 'i64'));
500
- count.forEach((val, i) => module.setValue(countPtr + i * 8, val, 'i64'));
510
+ const startPtr = module._malloc(start.length * 4);
511
+ const countPtr = module._malloc(count.length * 4);
512
+ start.forEach((val, i) => module.setValue(startPtr + i * 4, val, 'i32'));
513
+ count.forEach((val, i) => module.setValue(countPtr + i * 4, val, 'i32'));
501
514
  const result = nc_get_vara_longlong_wrapper(ncid, varid, startPtr, countPtr, dataPtr);
502
515
  const data = result === NC_CONSTANTS.NC_NOERR
503
516
  ? new BigInt64Array(module.HEAP64.buffer, dataPtr, totalLength).slice()
@@ -510,10 +523,10 @@ export class WasmModuleLoader {
510
523
  nc_get_vara_schar: (ncid, varid, start, count) => {
511
524
  const totalLength = count.reduce((a, b) => a * b, 1);
512
525
  const dataPtr = module._malloc(totalLength * 1);
513
- const startPtr = module._malloc(start.length * 8);
514
- const countPtr = module._malloc(count.length * 8);
515
- start.forEach((val, i) => module.setValue(startPtr + i * 8, val, 'i64'));
516
- count.forEach((val, i) => module.setValue(countPtr + i * 8, val, 'i64'));
526
+ const startPtr = module._malloc(start.length * 4);
527
+ const countPtr = module._malloc(count.length * 4);
528
+ start.forEach((val, i) => module.setValue(startPtr + i * 4, val, 'i32'));
529
+ count.forEach((val, i) => module.setValue(countPtr + i * 4, val, 'i32'));
517
530
  const result = nc_get_vara_schar_wrapper(ncid, varid, startPtr, countPtr, dataPtr);
518
531
  const data = result === NC_CONSTANTS.NC_NOERR
519
532
  ? new Int8Array(module.HEAP8.buffer, dataPtr, totalLength).slice()
@@ -526,10 +539,10 @@ export class WasmModuleLoader {
526
539
  nc_get_vara_uchar: (ncid, varid, start, count) => {
527
540
  const totalLength = count.reduce((a, b) => a * b, 1);
528
541
  const dataPtr = module._malloc(totalLength * 1);
529
- const startPtr = module._malloc(start.length * 8);
530
- const countPtr = module._malloc(count.length * 8);
531
- start.forEach((val, i) => module.setValue(startPtr + i * 8, val, 'i64'));
532
- count.forEach((val, i) => module.setValue(countPtr + i * 8, val, 'i64'));
542
+ const startPtr = module._malloc(start.length * 4);
543
+ const countPtr = module._malloc(count.length * 4);
544
+ start.forEach((val, i) => module.setValue(startPtr + i * 4, val, 'i32'));
545
+ count.forEach((val, i) => module.setValue(countPtr + i * 4, val, 'i32'));
533
546
  const result = nc_get_vara_uchar_wrapper(ncid, varid, startPtr, countPtr, dataPtr);
534
547
  const data = result === NC_CONSTANTS.NC_NOERR
535
548
  ? new Uint8Array(module.HEAPU8.buffer, dataPtr, totalLength).slice()
@@ -542,10 +555,10 @@ export class WasmModuleLoader {
542
555
  nc_get_vara_ushort: (ncid, varid, start, count) => {
543
556
  const totalLength = count.reduce((a, b) => a * b, 1);
544
557
  const dataPtr = module._malloc(totalLength * 2);
545
- const startPtr = module._malloc(start.length * 8);
546
- const countPtr = module._malloc(count.length * 8);
547
- start.forEach((val, i) => module.setValue(startPtr + i * 8, val, 'i64'));
548
- count.forEach((val, i) => module.setValue(countPtr + i * 8, val, 'i64'));
558
+ const startPtr = module._malloc(start.length * 4);
559
+ const countPtr = module._malloc(count.length * 4);
560
+ start.forEach((val, i) => module.setValue(startPtr + i * 4, val, 'i32'));
561
+ count.forEach((val, i) => module.setValue(countPtr + i * 4, val, 'i32'));
549
562
  const result = nc_get_vara_ushort_wrapper(ncid, varid, startPtr, countPtr, dataPtr);
550
563
  const data = result === NC_CONSTANTS.NC_NOERR
551
564
  ? new Uint16Array(module.HEAPU16.buffer, dataPtr, totalLength).slice()
@@ -558,10 +571,10 @@ export class WasmModuleLoader {
558
571
  nc_get_vara_uint: (ncid, varid, start, count) => {
559
572
  const totalLength = count.reduce((a, b) => a * b, 1);
560
573
  const dataPtr = module._malloc(totalLength * 4);
561
- const startPtr = module._malloc(start.length * 8);
562
- const countPtr = module._malloc(count.length * 8);
563
- start.forEach((val, i) => module.setValue(startPtr + i * 8, val, 'i64'));
564
- count.forEach((val, i) => module.setValue(countPtr + i * 8, val, 'i64'));
574
+ const startPtr = module._malloc(start.length * 4);
575
+ const countPtr = module._malloc(count.length * 4);
576
+ start.forEach((val, i) => module.setValue(startPtr + i * 4, val, 'i32'));
577
+ count.forEach((val, i) => module.setValue(countPtr + i * 4, val, 'i32'));
565
578
  const result = nc_get_vara_uint_wrapper(ncid, varid, startPtr, countPtr, dataPtr);
566
579
  const data = result === NC_CONSTANTS.NC_NOERR
567
580
  ? new Uint32Array(module.HEAPU32.buffer, dataPtr, totalLength).slice()
@@ -574,10 +587,10 @@ export class WasmModuleLoader {
574
587
  nc_get_vara_ulonglong: (ncid, varid, start, count) => {
575
588
  const totalLength = count.reduce((a, b) => a * b, 1);
576
589
  const dataPtr = module._malloc(totalLength * 8);
577
- const startPtr = module._malloc(start.length * 8);
578
- const countPtr = module._malloc(count.length * 8);
579
- start.forEach((val, i) => module.setValue(startPtr + i * 8, val, 'i64'));
580
- count.forEach((val, i) => module.setValue(countPtr + i * 8, val, 'i64'));
590
+ const startPtr = module._malloc(start.length * 4);
591
+ const countPtr = module._malloc(count.length * 4);
592
+ start.forEach((val, i) => module.setValue(startPtr + i * 4, val, 'i32'));
593
+ count.forEach((val, i) => module.setValue(countPtr + i * 4, val, 'i32'));
581
594
  const result = nc_get_vara_ulonglong_wrapper(ncid, varid, startPtr, countPtr, dataPtr);
582
595
  const data = result === NC_CONSTANTS.NC_NOERR
583
596
  ? new BigUint64Array(module.HEAP64.buffer, dataPtr, totalLength).slice()
@@ -590,10 +603,10 @@ export class WasmModuleLoader {
590
603
  nc_get_vara_string: (ncid, varid, start, count) => {
591
604
  const totalLength = count.reduce((a, b) => a * b, 1);
592
605
  const dataPtr = module._malloc(totalLength * 4);
593
- const startPtr = module._malloc(start.length * 8);
594
- const countPtr = module._malloc(count.length * 8);
595
- start.forEach((val, i) => module.setValue(startPtr + i * 8, val, 'i64'));
596
- count.forEach((val, i) => module.setValue(countPtr + i * 8, val, 'i64'));
606
+ const startPtr = module._malloc(start.length * 4);
607
+ const countPtr = module._malloc(count.length * 4);
608
+ start.forEach((val, i) => module.setValue(startPtr + i * 4, val, 'i32'));
609
+ count.forEach((val, i) => module.setValue(countPtr + i * 4, val, 'i32'));
597
610
  const result = nc_get_vara_string_wrapper(ncid, varid, startPtr, countPtr, dataPtr);
598
611
  let data;
599
612
  if (result === NC_CONSTANTS.NC_NOERR) {
@@ -849,6 +862,16 @@ export class WasmModuleLoader {
849
862
  module._free(dataPtr);
850
863
  return { result, data };
851
864
  },
865
+ nc_get_att_schar: (ncid, varid, name, length) => {
866
+ const dataPtr = module._malloc(length);
867
+ const result = nc_get_att_schar_wrapper(ncid, varid, name, dataPtr);
868
+ let data;
869
+ if (result === NC_CONSTANTS.NC_NOERR) {
870
+ data = new Int8Array(module.HEAP8.buffer, dataPtr, length).slice();
871
+ }
872
+ module._free(dataPtr);
873
+ return { result, data };
874
+ },
852
875
  // 16-bit unsigned (NC_USHORT)
853
876
  nc_get_att_ushort: (ncid, varid, name, length) => {
854
877
  const dataPtr = module._malloc(length * 2);
@@ -882,6 +905,245 @@ export class WasmModuleLoader {
882
905
  module._free(dataPtr);
883
906
  return { result, data };
884
907
  },
908
+ // Enum Type Functions
909
+ nc_inq_typeids: (ncid, maxTypeIds) => {
910
+ const ntypesPtr = module._malloc(4);
911
+ const typeidsPtr = module._malloc(maxTypeIds * 4);
912
+ const result = nc_inq_typeids_wrapper(ncid, ntypesPtr, typeidsPtr);
913
+ let ntypes;
914
+ let typeids;
915
+ if (result === NC_CONSTANTS.NC_NOERR) {
916
+ ntypes = module.getValue(ntypesPtr, 'i32');
917
+ typeids = [];
918
+ for (let i = 0; i < ntypes; i++) {
919
+ typeids.push(module.getValue(typeidsPtr + i * 4, 'i32'));
920
+ }
921
+ }
922
+ module._free(ntypesPtr);
923
+ module._free(typeidsPtr);
924
+ return { result, ntypes, typeids };
925
+ },
926
+ nc_inq_type: (ncid, xtype) => {
927
+ const namePtr = module._malloc(NC_CONSTANTS.NC_MAX_NAME + 1);
928
+ const sizePtr = module._malloc(8);
929
+ const result = nc_inq_type_wrapper(ncid, xtype, namePtr, sizePtr);
930
+ let name;
931
+ let size;
932
+ if (result === NC_CONSTANTS.NC_NOERR) {
933
+ name = module.UTF8ToString(namePtr);
934
+ size = Number(module.getValue(sizePtr, 'i64'));
935
+ }
936
+ module._free(namePtr);
937
+ module._free(sizePtr);
938
+ return { result, name, size };
939
+ },
940
+ nc_inq_user_type: (ncid, xtype) => {
941
+ const namePtr = module._malloc(NC_CONSTANTS.NC_MAX_NAME + 1);
942
+ const sizePtr = module._malloc(8);
943
+ const baseTypePtr = module._malloc(4);
944
+ const nfieldsPtr = module._malloc(8);
945
+ const classPtr = module._malloc(4);
946
+ const result = nc_inq_user_type_wrapper(ncid, xtype, namePtr, sizePtr, baseTypePtr, nfieldsPtr, classPtr);
947
+ let name;
948
+ let size;
949
+ let baseType;
950
+ let nfields;
951
+ let typeClass;
952
+ if (result === NC_CONSTANTS.NC_NOERR) {
953
+ name = module.UTF8ToString(namePtr);
954
+ size = Number(module.getValue(sizePtr, 'i64'));
955
+ baseType = module.getValue(baseTypePtr, 'i32');
956
+ nfields = Number(module.getValue(nfieldsPtr, 'i64'));
957
+ typeClass = module.getValue(classPtr, 'i32');
958
+ }
959
+ module._free(namePtr);
960
+ module._free(sizePtr);
961
+ module._free(baseTypePtr);
962
+ module._free(nfieldsPtr);
963
+ module._free(classPtr);
964
+ return { result, name, size, baseType, nfields, typeClass };
965
+ },
966
+ nc_def_enum: (ncid, baseTypeId, name) => {
967
+ const typeidPtr = module._malloc(4);
968
+ const result = nc_def_enum_wrapper(ncid, baseTypeId, name, typeidPtr);
969
+ let typeid;
970
+ if (result === NC_CONSTANTS.NC_NOERR) {
971
+ typeid = module.getValue(typeidPtr, 'i32');
972
+ }
973
+ module._free(typeidPtr);
974
+ return { result, typeid };
975
+ },
976
+ nc_insert_enum: (ncid, xtype, name, value) => {
977
+ const valuePtr = module._malloc(8); // Always allocate 8 bytes (max needed)
978
+ if (typeof value === 'bigint') {
979
+ // Write bigint as 64-bit integer
980
+ const i64Array = new BigInt64Array(module.HEAP8.buffer, valuePtr, 1);
981
+ i64Array[0] = value;
982
+ }
983
+ else {
984
+ // Write number as 32-bit integer (works for byte, short, int, uint)
985
+ module.setValue(valuePtr, value, 'i32');
986
+ }
987
+ const result = nc_insert_enum_wrapper(ncid, xtype, name, valuePtr);
988
+ module._free(valuePtr);
989
+ return { result };
990
+ },
991
+ nc_inq_enum: (ncid, xtype) => {
992
+ const namePtr = module._malloc(NC_CONSTANTS.NC_MAX_NAME + 1);
993
+ const baseTypePtr = module._malloc(4);
994
+ const baseSizePtr = module._malloc(8); // size_t can be 8 bytes
995
+ const numMembersPtr = module._malloc(8); // size_t can be 8 bytes
996
+ const result = nc_inq_enum_wrapper(ncid, xtype, namePtr, baseTypePtr, baseSizePtr, numMembersPtr);
997
+ let name;
998
+ let baseType;
999
+ let baseSize;
1000
+ let numMembers;
1001
+ if (result === NC_CONSTANTS.NC_NOERR) {
1002
+ name = module.UTF8ToString(namePtr);
1003
+ baseType = module.getValue(baseTypePtr, 'i32');
1004
+ // Try reading as 32-bit size_t first
1005
+ baseSize = Number(module.getValue(baseSizePtr, 'i32'));
1006
+ numMembers = Number(module.getValue(numMembersPtr, 'i32'));
1007
+ }
1008
+ module._free(namePtr);
1009
+ module._free(baseTypePtr);
1010
+ module._free(baseSizePtr);
1011
+ module._free(numMembersPtr);
1012
+ return { result, name, baseType, baseSize, numMembers };
1013
+ },
1014
+ nc_inq_enum_member: (ncid, xtype, idx, baseType) => {
1015
+ const namePtr = module._malloc(NC_CONSTANTS.NC_MAX_NAME + 1);
1016
+ const valuePtr = module._malloc(8);
1017
+ const result = nc_inq_enum_member_wrapper(ncid, xtype, idx, namePtr, valuePtr);
1018
+ let name;
1019
+ let value;
1020
+ if (result === NC_CONSTANTS.NC_NOERR) {
1021
+ name = module.UTF8ToString(namePtr);
1022
+ // Read value based on base type
1023
+ switch (baseType) {
1024
+ case NC_CONSTANTS.NC_BYTE:
1025
+ value = module.getValue(valuePtr, 'i8');
1026
+ break;
1027
+ case NC_CONSTANTS.NC_UBYTE:
1028
+ value = module.getValue(valuePtr, 'i8') & 0xFF;
1029
+ break;
1030
+ case NC_CONSTANTS.NC_SHORT:
1031
+ value = module.getValue(valuePtr, 'i16');
1032
+ break;
1033
+ case NC_CONSTANTS.NC_USHORT:
1034
+ value = module.getValue(valuePtr, 'i16') & 0xFFFF;
1035
+ break;
1036
+ case NC_CONSTANTS.NC_INT:
1037
+ value = module.getValue(valuePtr, 'i32');
1038
+ break;
1039
+ case NC_CONSTANTS.NC_UINT:
1040
+ value = module.getValue(valuePtr, 'i32') >>> 0;
1041
+ break;
1042
+ case NC_CONSTANTS.NC_INT64:
1043
+ value = module.getValue(valuePtr, 'i64');
1044
+ break;
1045
+ case NC_CONSTANTS.NC_UINT64:
1046
+ value = module.getValue(valuePtr, 'i64');
1047
+ break;
1048
+ default:
1049
+ value = module.getValue(valuePtr, 'i32');
1050
+ }
1051
+ }
1052
+ module._free(namePtr);
1053
+ module._free(valuePtr);
1054
+ return { result, name, value };
1055
+ },
1056
+ nc_inq_enum_ident: (ncid, xtype, value) => {
1057
+ const identPtr = module._malloc(NC_CONSTANTS.NC_MAX_NAME + 1);
1058
+ const numValue = typeof value === 'bigint' ? Number(value) : value;
1059
+ const result = nc_inq_enum_ident_wrapper(ncid, xtype, numValue, identPtr);
1060
+ let identifier;
1061
+ if (result === NC_CONSTANTS.NC_NOERR) {
1062
+ identifier = module.UTF8ToString(identPtr);
1063
+ }
1064
+ module._free(identPtr);
1065
+ return { result, identifier };
1066
+ },
1067
+ // generic nc_get_var function to module
1068
+ nc_get_var_generic: (ncid, varid, length, nctype) => {
1069
+ const elementSize = DATA_TYPE_SIZE[nctype];
1070
+ const dataPtr = module._malloc(length * elementSize);
1071
+ const result = nc_get_var_wrapper(ncid, varid, dataPtr);
1072
+ let data;
1073
+ if (result === NC_CONSTANTS.NC_NOERR) {
1074
+ switch (nctype) {
1075
+ case NC_CONSTANTS.NC_BYTE:
1076
+ data = new Int8Array(module.HEAP8.buffer, dataPtr, length).slice();
1077
+ break;
1078
+ case NC_CONSTANTS.NC_UBYTE:
1079
+ data = new Uint8Array(module.HEAPU8.buffer, dataPtr, length).slice();
1080
+ break;
1081
+ case NC_CONSTANTS.NC_SHORT:
1082
+ data = new Int16Array(module.HEAP16.buffer, dataPtr, length).slice();
1083
+ break;
1084
+ case NC_CONSTANTS.NC_USHORT:
1085
+ data = new Uint16Array(module.HEAPU16.buffer, dataPtr, length).slice();
1086
+ break;
1087
+ case NC_CONSTANTS.NC_INT:
1088
+ data = new Int32Array(module.HEAP32.buffer, dataPtr, length).slice();
1089
+ break;
1090
+ case NC_CONSTANTS.NC_UINT:
1091
+ data = new Uint32Array(module.HEAPU32.buffer, dataPtr, length).slice();
1092
+ break;
1093
+ case NC_CONSTANTS.NC_INT64:
1094
+ data = new BigInt64Array(module.HEAP64.buffer, dataPtr, length).slice();
1095
+ break;
1096
+ case NC_CONSTANTS.NC_UINT64:
1097
+ data = new BigUint64Array(module.HEAPU64.buffer, dataPtr, length).slice();
1098
+ break;
1099
+ }
1100
+ }
1101
+ module._free(dataPtr);
1102
+ return { result, data };
1103
+ },
1104
+ nc_get_vara_generic: (ncid, varid, start, count, nctype) => {
1105
+ const elementSize = DATA_TYPE_SIZE[nctype];
1106
+ const totalLength = count.reduce((a, b) => a * b, 1);
1107
+ const dataPtr = module._malloc(totalLength * elementSize);
1108
+ const startPtr = module._malloc(start.length * 4);
1109
+ const countPtr = module._malloc(count.length * 4);
1110
+ start.forEach((val, i) => module.setValue(startPtr + i * 4, val, 'i32'));
1111
+ count.forEach((val, i) => module.setValue(countPtr + i * 4, val, 'i32'));
1112
+ const result = nc_get_vara_wrapper(ncid, varid, startPtr, countPtr, dataPtr);
1113
+ let data;
1114
+ if (result === NC_CONSTANTS.NC_NOERR) {
1115
+ switch (nctype) {
1116
+ case NC_CONSTANTS.NC_BYTE:
1117
+ data = new Int8Array(module.HEAP8.buffer, dataPtr, totalLength).slice();
1118
+ break;
1119
+ case NC_CONSTANTS.NC_UBYTE:
1120
+ data = new Uint8Array(module.HEAPU8.buffer, dataPtr, totalLength).slice();
1121
+ break;
1122
+ case NC_CONSTANTS.NC_SHORT:
1123
+ data = new Int16Array(module.HEAP16.buffer, dataPtr, totalLength).slice();
1124
+ break;
1125
+ case NC_CONSTANTS.NC_USHORT:
1126
+ data = new Uint16Array(module.HEAPU16.buffer, dataPtr, totalLength).slice();
1127
+ break;
1128
+ case NC_CONSTANTS.NC_INT:
1129
+ data = new Int32Array(module.HEAP32.buffer, dataPtr, totalLength).slice();
1130
+ break;
1131
+ case NC_CONSTANTS.NC_UINT:
1132
+ data = new Uint32Array(module.HEAPU32.buffer, dataPtr, totalLength).slice();
1133
+ break;
1134
+ case NC_CONSTANTS.NC_INT64:
1135
+ data = new BigInt64Array(module.HEAP64.buffer, dataPtr, totalLength).slice();
1136
+ break;
1137
+ case NC_CONSTANTS.NC_UINT64:
1138
+ data = new BigUint64Array(module.HEAPU64.buffer, dataPtr, totalLength).slice();
1139
+ break;
1140
+ }
1141
+ }
1142
+ module._free(dataPtr);
1143
+ module._free(startPtr);
1144
+ module._free(countPtr);
1145
+ return { result, data };
1146
+ },
885
1147
  };
886
1148
  }
887
1149
  }