/
netcdf_c.jl
2229 lines (1759 loc) · 87 KB
/
netcdf_c.jl
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
# This file is originally based netcdf_c.jl from NetCDF.jl
# Copyright (c) 2012-2013: Fabian Gans, Max-Planck-Institut fuer Biogeochemie, Jena, Germany
# MIT
# Several calls are commented out because they are not captured by tests
const NC_NAT = 0
const NC_BYTE = 1
const NC_CHAR = 2
const NC_SHORT = 3
const NC_INT = 4
const NC_LONG = NC_INT
const NC_FLOAT = 5
const NC_DOUBLE = 6
const NC_UBYTE = 7
const NC_USHORT = 8
const NC_UINT = 9
const NC_INT64 = 10
const NC_UINT64 = 11
const NC_STRING = 12
const NC_MAX_ATOMIC_TYPE = NC_STRING
const NC_VLEN = 13
const NC_OPAQUE = 14
const NC_ENUM = 15
const NC_COMPOUND = 16
const NC_FIRSTUSERTYPEID = 32
const NC_FILL = 0
const NC_NOFILL = 0x0100
const NC_NOWRITE = 0x0000
const NC_WRITE = 0x0001
const NC_CLOBBER = 0x0000
const NC_NOCLOBBER = 0x0004
const NC_DISKLESS = 0x0008
const NC_MMAP = 0x0010
const NC_64BIT_DATA = 0x0020
const NC_CDF5 = NC_64BIT_DATA
const NC_CLASSIC_MODEL = 0x0100
const NC_64BIT_OFFSET = 0x0200
const NC_LOCK = 0x0400
const NC_SHARE = 0x0800
const NC_NETCDF4 = 0x1000
const NC_MPIIO = 0x2000
const NC_MPIPOSIX = 0x4000
const NC_INMEMORY = 0x8000
const NC_PNETCDF = NC_MPIIO
const NC_FORMAT_CLASSIC = 1
const NC_FORMAT_64BIT_OFFSET = 2
const NC_FORMAT_64BIT = NC_FORMAT_64BIT_OFFSET
const NC_FORMAT_NETCDF4 = 3
const NC_FORMAT_NETCDF4_CLASSIC = 4
const NC_FORMAT_64BIT_DATA = 5
const NC_FORMAT_CDF5 = NC_FORMAT_64BIT_DATA
const NC_FORMATX_NC3 = 1
const NC_FORMATX_NC_HDF5 = 2
const NC_FORMATX_NC4 = NC_FORMATX_NC_HDF5
const NC_FORMATX_NC_HDF4 = 3
const NC_FORMATX_PNETCDF = 4
const NC_FORMATX_DAP2 = 5
const NC_FORMATX_DAP4 = 6
const NC_FORMATX_UNDEFINED = 0
const NC_FORMAT_NC3 = NC_FORMATX_NC3
const NC_FORMAT_NC_HDF5 = NC_FORMATX_NC_HDF5
const NC_FORMAT_NC4 = NC_FORMATX_NC4
const NC_FORMAT_NC_HDF4 = NC_FORMATX_NC_HDF4
const NC_FORMAT_PNETCDF = NC_FORMATX_PNETCDF
const NC_FORMAT_DAP2 = NC_FORMATX_DAP2
const NC_FORMAT_DAP4 = NC_FORMATX_DAP4
const NC_FORMAT_UNDEFINED = NC_FORMATX_UNDEFINED
const NC_SIZEHINT_DEFAULT = 0
const NC_UNLIMITED = Cint(0)
const NC_GLOBAL = Cint(-1)
const NC_MAX_DIMS = 1024
const NC_MAX_ATTRS = 8192
const NC_MAX_VARS = 8192
const NC_MAX_NAME = 256
const NC_MAX_VAR_DIMS = 1024
const NC_MAX_HDF4_NAME = 64
const NC_ENDIAN_NATIVE = 0
const NC_ENDIAN_LITTLE = 1
const NC_ENDIAN_BIG = 2
const NC_CHUNKED = 0
const NC_CONTIGUOUS = 1
const NC_NOCHECKSUM = 0
const NC_FLETCHER32 = 1
const NC_NOSHUFFLE = 0
const NC_SHUFFLE = 1
const NC_NOERR = 0
const NC2_ERR = -1
const NC_EBADID = -33
const NC_ENFILE = -34
const NC_EEXIST = -35
const NC_EINVAL = -36
const NC_EPERM = -37
const NC_ENOTINDEFINE = -38
const NC_EINDEFINE = -39
const NC_EINVALCOORDS = -40
const NC_EMAXDIMS = -41
const NC_ENAMEINUSE = -42
const NC_ENOTATT = -43
const NC_EMAXATTS = -44
const NC_EBADTYPE = -45
const NC_EBADDIM = -46
const NC_EUNLIMPOS = -47
const NC_EMAXVARS = -48
const NC_ENOTVAR = -49
const NC_EGLOBAL = -50
const NC_ENOTNC = -51
const NC_ESTS = -52
const NC_EMAXNAME = -53
const NC_EUNLIMIT = -54
const NC_ENORECVARS = -55
const NC_ECHAR = -56
const NC_EEDGE = -57
const NC_ESTRIDE = -58
const NC_EBADNAME = -59
const NC_ERANGE = -60
const NC_ENOMEM = -61
const NC_EVARSIZE = -62
const NC_EDIMSIZE = -63
const NC_ETRUNC = -64
const NC_EAXISTYPE = -65
const NC_EDAP = -66
const NC_ECURL = -67
const NC_EIO = -68
const NC_ENODATA = -69
const NC_EDAPSVC = -70
const NC_EDAS = -71
const NC_EDDS = -72
const NC_EDATADDS = -73
const NC_EDAPURL = -74
const NC_EDAPCONSTRAINT = -75
const NC_ETRANSLATION = -76
const NC_EACCESS = -77
const NC_EAUTH = -78
const NC_ENOTFOUND = -90
const NC_ECANTREMOVE = -91
const NC4_FIRST_ERROR = -100
const NC_EHDFERR = -101
const NC_ECANTREAD = -102
const NC_ECANTWRITE = -103
const NC_ECANTCREATE = -104
const NC_EFILEMETA = -105
const NC_EDIMMETA = -106
const NC_EATTMETA = -107
const NC_EVARMETA = -108
const NC_ENOCOMPOUND = -109
const NC_EATTEXISTS = -110
const NC_ENOTNC4 = -111
const NC_ESTRICTNC3 = -112
const NC_ENOTNC3 = -113
const NC_ENOPAR = -114
const NC_EPARINIT = -115
const NC_EBADGRPID = -116
const NC_EBADTYPID = -117
const NC_ETYPDEFINED = -118
const NC_EBADFIELD = -119
const NC_EBADCLASS = -120
const NC_EMAPTYPE = -121
const NC_ELATEFILL = -122
const NC_ELATEDEF = -123
const NC_EDIMSCALE = -124
const NC_ENOGRP = -125
const NC_ESTORAGE = -126
const NC_EBADCHUNK = -127
const NC_ENOTBUILT = -128
const NC_EDISKLESS = -129
const NC_ECANTEXTEND = -130
const NC_EMPI = -131
const NC4_LAST_ERROR = -131
const DIM_WITHOUT_VARIABLE = "This is a netCDF dimension but not a netCDF variable."
const NC_HAVE_NEW_CHUNKING_API = 1
const NC_EURL = NC_EDAPURL
const NC_ECONSTRAINT = NC_EDAPCONSTRAINT
const NC_INDEPENDENT = 0
const NC_COLLECTIVE = 1
# OpenMPI
const MPI_Comm = Ptr{Cvoid}
const MPI_Info = Ptr{Cvoid}
const NC_ENTOOL = NC_EMAXNAME
const NC_EXDR = -32
const NC_SYSERR = -31
const NC_FATAL = 1
# default fill values
const NC_FILL_BYTE = Int8(-127)
const NC_FILL_CHAR = '\0'
const NC_FILL_SHORT = Int16(-32767)
const NC_FILL_INT = Int32(-2147483647)
const NC_FILL_FLOAT = 9.9692099683868690f+36
const NC_FILL_DOUBLE = 9.9692099683868690e+36
const NC_FILL_UBYTE = UInt8(255)
const NC_FILL_USHORT = UInt16(65535)
const NC_FILL_UINT = UInt32(4294967295)
const NC_FILL_INT64 = Int64(-9223372036854775806)
const NC_FILL_UINT64 = UInt64(18446744073709551614)
const NC_FILL_STRING = ""
const nc_type = Cint
# type is immutable to ensure that it has the memory same layout
# as the C struct nc_vlen_t
struct nc_vlen_t{T}
len::Csize_t
p::Ptr{T}
end
const nclong = Cint
const NCSymbols = Dict{Cint,Symbol}(
NC_CONTIGUOUS => :contiguous,
NC_CHUNKED => :chunked
)
# Inverse mapping
const NCConstants = Dict(value => key for (key, value) in NCSymbols)
const NCChecksumSymbols = Dict{Cint,Symbol}(
NC_FLETCHER32 => :fletcher32,
NC_NOCHECKSUM => :nochecksum
)
# Inverse mapping
const NCChecksumConstants = Dict(value => key for (key, value) in NCChecksumSymbols)
function convert(::Type{Array{nc_vlen_t{T},N}},data::Array{Vector{T},N}) where {T,N}
tmp = Array{nc_vlen_t{T},N}(undef,size(data))
for (i,d) in enumerate(data)
tmp[i] = nc_vlen_t{T}(length(d), pointer(d))
end
return tmp
end
function nc_inq_libvers()
unsafe_string(ccall((:nc_inq_libvers,libnetcdf),Cstring,()))
end
function nc_strerror(ncerr::Integer)
unsafe_string(ccall((:nc_strerror,libnetcdf),Cstring,(Cint,),ncerr))
end
# function nc__create(path,cmode::Integer,initialsz::Integer,chunksizehintp,ncidp)
# check(ccall((:nc__create,libnetcdf),Cint,(Cstring,Cint,Cint,Ptr{Cint},Ptr{Cint}),path,cmode,initialsz,chunksizehintp,ncidp))
# end
function nc_create(path,cmode::Integer)
ncidp = Ref(Cint(0))
check(ccall((:nc_create,libnetcdf),Cint,(Cstring,Cint,Ptr{Cint}),path,cmode,ncidp))
return ncidp[]
end
# function nc__open(path,mode::Integer,chunksizehintp,ncidp)
# check(ccall((:nc__open,libnetcdf),Cint,(Cstring,Cint,Ptr{Cint},Ptr{Cint}),path,mode,chunksizehintp,ncidp))
# end
function nc_open(path,mode::Integer)
@debug "nc_open $path with mode $mode"
ncidp = Ref(Cint(0))
code = ccall((:nc_open,libnetcdf),Cint,(Cstring,Cint,Ptr{Cint}),path,mode,ncidp)
if code == NC_NOERR
return ncidp[]
else
# otherwise throw an error message
# with a more helpful error message (i.e. with the path)
throw(NetCDFError(code, "Opening path $(path): $(nc_strerror(code))"))
end
end
function nc_open_mem(path,mode::Integer,memory::Vector{UInt8})
@debug "nc_open $path with mode $mode"
ncidp = Ref(Cint(0))
code = ccall(
(:nc_open_mem,libnetcdf),Cint,
(Cstring,Cint,Csize_t,Ptr{UInt8},Ptr{Cint}),
path,mode,length(memory),memory,ncidp)
if code == NC_NOERR
return ncidp[]
else
# otherwise throw an error message
# with a more helpful error message (i.e. with the path)
throw(NetCDFError(code, "Opening path $(path): $(nc_strerror(code))"))
end
end
function nc_inq_path(ncid::Integer)
pathlenp = Ref(Csize_t(0))
check(ccall((:nc_inq_path,libnetcdf),Cint,(Cint,Ptr{Csize_t},Ptr{UInt8}),ncid,pathlenp,C_NULL))
path = zeros(UInt8,pathlenp[]+1)
check(ccall((:nc_inq_path,libnetcdf),Cint,(Cint,Ptr{Csize_t},Ptr{UInt8}),ncid,pathlenp,path))
return unsafe_string(pointer(path))
end
# function nc_inq_ncid(ncid::Integer,name,grp_ncid)
# check(ccall((:nc_inq_ncid,libnetcdf),Cint,(Cint,Cstring,Ptr{Cint}),ncid,name,grp_ncid))
# end
function nc_inq_grps(ncid::Integer)
numgrpsp = Ref(Cint(0))
check(ccall((:nc_inq_grps,libnetcdf),Cint,(Cint,Ptr{Cint},Ptr{Cint}),ncid,numgrpsp,C_NULL))
numgrps = numgrpsp[]
ncids = Vector{Cint}(undef,numgrps)
check(ccall((:nc_inq_grps,libnetcdf),Cint,(Cint,Ptr{Cint},Ptr{Cint}),ncid,numgrpsp,ncids))
return ncids
end
function nc_inq_grpname(ncid::Integer)
name = zeros(UInt8,NC_MAX_NAME+1)
check(ccall((:nc_inq_grpname,libnetcdf),Cint,(Cint,Ptr{UInt8}),ncid,name))
return unsafe_string(pointer(name))
end
# function nc_inq_grpname_full(ncid::Integer,lenp,full_name)
# check(ccall((:nc_inq_grpname_full,libnetcdf),Cint,(Cint,Ptr{Cint},Cstring),ncid,lenp,full_name))
# end
# function nc_inq_grpname_len(ncid::Integer,lenp)
# check(ccall((:nc_inq_grpname_len,libnetcdf),Cint,(Cint,Ptr{Cint}),ncid,lenp))
# end
# function nc_inq_grp_parent(ncid::Integer,parent_ncid)
# check(ccall((:nc_inq_grp_parent,libnetcdf),Cint,(Cint,Ptr{Cint}),ncid,parent_ncid))
# end
function nc_inq_grp_ncid(ncid::Integer,grp_name)
grp_ncid = Ref(Cint(0))
check(ccall((:nc_inq_grp_ncid,libnetcdf),Cint,(Cint,Cstring,Ptr{Cint}),ncid,grp_name,grp_ncid))
return grp_ncid[]
end
# function nc_inq_grp_full_ncid(ncid::Integer,full_name,grp_ncid)
# check(ccall((:nc_inq_grp_full_ncid,libnetcdf),Cint,(Cint,Cstring,Ptr{Cint}),ncid,full_name,grp_ncid))
# end
function nc_inq_varids(ncid::Integer)::Vector{Cint}
# first get number of variables
nvarsp = Ref(Cint(0))
check(ccall((:nc_inq_varids,libnetcdf),Cint,(Cint,Ptr{Cint},Ptr{Cint}),ncid,nvarsp,C_NULL))
nvars = nvarsp[]
varids = zeros(Cint,nvars)
check(ccall((:nc_inq_varids,libnetcdf),Cint,(Cint,Ptr{Cint},Ptr{Cint}),ncid,nvarsp,varids))
return varids
end
function nc_inq_dimids(ncid::Integer,include_parents::Bool)
ndimsp = Ref(Cint(0))
ndims = nc_inq_ndims(ncid)
dimids = Vector{Cint}(undef,ndims)
check(ccall((:nc_inq_dimids,libnetcdf),Cint,(Cint,Ptr{Cint},Ptr{Cint},Cint),ncid,ndimsp,dimids,include_parents))
return dimids
end
function nc_inq_typeids(ncid::Integer)
ntypesp = Ref(Cint(0))
check(ccall((:nc_inq_typeids,libnetcdf),Cint,(Cint,Ptr{Cint},Ptr{Cint}),ncid,ntypesp,C_NULL))
typeids = Vector{Cint}(undef,ntypesp[])
check(ccall((:nc_inq_typeids,libnetcdf),Cint,(Cint,Ptr{Cint},Ptr{Cint}),ncid,C_NULL,typeids))
return typeids
end
# function nc_inq_type_equal(ncid1::Integer,typeid1::Integer,ncid2::Integer,typeid2::Integer,equal)
# check(ccall((:nc_inq_type_equal,libnetcdf),Cint,(Cint,nc_type,Cint,nc_type,Ptr{Cint}),ncid1,typeid1,ncid2,typeid2,equal))
# end
"""
Create a group with the name `name` returnings its id.
"""
function nc_def_grp(parent_ncid::Integer,name)
new_ncid = Ref(Cint(0))
check(ccall((:nc_def_grp,libnetcdf),Cint,(Cint,Cstring,Ptr{Cint}),parent_ncid,name,new_ncid))
return new_ncid[]
end
# function nc_rename_grp(grpid::Integer,name)
# check(ccall((:nc_rename_grp,libnetcdf),Cint,(Cint,Cstring),grpid,name))
# end
function nc_def_compound(ncid::Integer,size::Integer,name)
typeidp = Ref{nc_type}()
check(ccall((:nc_def_compound,libnetcdf),Cint,(Cint,Cint,Cstring,Ptr{nc_type}),ncid,size,name,typeidp))
return typeidp[]
end
function nc_insert_compound(ncid::Integer,xtype::Integer,name,offset::Integer,field_typeid::Integer)
check(ccall((:nc_insert_compound,libnetcdf),Cint,(Cint,nc_type,Cstring,Csize_t,nc_type),ncid,xtype,name,offset,field_typeid))
end
function nc_insert_array_compound(ncid::Integer,xtype::Integer,name,offset::Integer,field_typeid::Integer,dim_sizes)
ndims = length(dim_sizes)
check(ccall((:nc_insert_array_compound,libnetcdf),Cint,(Cint,nc_type,Cstring,Cint,nc_type,Cint,Ptr{Cint}),ncid,xtype,name,offset,field_typeid,ndims,dim_sizes))
end
# function nc_inq_type(ncid::Integer,xtype::Integer,name,size)
# check(ccall((:nc_inq_type,libnetcdf),Cint,(Cint,nc_type,Cstring,Ptr{Cint}),ncid,xtype,name,size))
# end
# function nc_inq_typeid(ncid::Integer,name,typeidp)
# check(ccall((:nc_inq_typeid,libnetcdf),Cint,(Cint,Cstring,Ptr{nc_type}),ncid,name,typeidp))
# end
function nc_inq_compound(ncid::Integer,xtype::Integer)
name = zeros(UInt8,NC_MAX_NAME+1)
sizep = Ref{Csize_t}()
nfieldsp = Ref{Csize_t}()
check(ccall((:nc_inq_compound,libnetcdf),Cint,(Cint,nc_type,Ptr{UInt8},Ptr{Csize_t},Ptr{Csize_t}),ncid,xtype,name,sizep,nfieldsp))
return unsafe_string(pointer(name)), sizep[], nfieldsp[]
end
function nc_inq_compound_name(ncid::Integer,xtype::Integer)
name = zeros(UInt8,NC_MAX_NAME+1)
check(ccall((:nc_inq_compound_name,libnetcdf),Cint,(Cint,nc_type,Ptr{UInt8}),ncid,xtype,name))
return unsafe_string(pointer(name))
end
function nc_inq_compound_size(ncid::Integer,xtype::Integer)
sizep = Ref{Csize_t}()
check(ccall((:nc_inq_compound_size,libnetcdf),Cint,(Cint,nc_type,Ptr{Csize_t}),ncid,xtype,sizep))
return sizep[]
end
function nc_inq_compound_nfields(ncid::Integer,xtype::Integer)
nfieldsp = Ref{Csize_t}()
check(ccall((:nc_inq_compound_nfields,libnetcdf),Cint,(Cint,nc_type,Ptr{Csize_t}),ncid,xtype,nfieldsp))
return nfieldsp[]
end
# function nc_inq_compound_field(ncid::Integer,xtype::Integer,fieldid::Integer,name,offsetp,field_typeidp,ndimsp,dim_sizesp)
# check(ccall((:nc_inq_compound_field,libnetcdf),Cint,(Cint,nc_type,Cint,Cstring,Ptr{Cint},Ptr{nc_type},Ptr{Cint},Ptr{Cint}),ncid,xtype,fieldid,name,offsetp,field_typeidp,ndimsp,dim_sizesp))
# end
function nc_inq_compound_fieldname(ncid::Integer,xtype::Integer,fieldid::Integer)
name = zeros(UInt8,NC_MAX_NAME+1)
check(ccall((:nc_inq_compound_fieldname,libnetcdf),Cint,(Cint,nc_type,Cint,Ptr{UInt8}),ncid,xtype,fieldid,name))
return unsafe_string(pointer(name))
end
function nc_inq_compound_fieldindex(ncid::Integer,xtype::Integer,name)
fieldidp = Ref{Cint}()
check(ccall((:nc_inq_compound_fieldindex,libnetcdf),Cint,(Cint,nc_type,Cstring,Ptr{Cint}),ncid,xtype,name,fieldidp))
return fieldidp[]
end
function nc_inq_compound_fieldoffset(ncid::Integer,xtype::Integer,fieldid::Integer)
offsetp = Ref{Cint}()
check(ccall((:nc_inq_compound_fieldoffset,libnetcdf),Cint,(Cint,nc_type,Cint,Ptr{Cint}),ncid,xtype,fieldid,offsetp))
return offsetp[]
end
function nc_inq_compound_fieldtype(ncid::Integer,xtype::Integer,fieldid::Integer)
field_typeidp = Ref{nc_type}()
check(ccall((:nc_inq_compound_fieldtype,libnetcdf),Cint,(Cint,nc_type,Cint,Ptr{nc_type}),ncid,xtype,fieldid,field_typeidp))
return field_typeidp[]
end
function nc_inq_compound_fieldndims(ncid::Integer,xtype::Integer,fieldid::Integer)
ndimsp = Ref{Cint}()
check(ccall((:nc_inq_compound_fieldndims,libnetcdf),Cint,(Cint,nc_type,Cint,Ptr{Cint}),ncid,xtype,fieldid,ndimsp))
return ndimsp[]
end
function nc_inq_compound_fielddim_sizes(ncid::Integer,xtype::Integer,fieldid::Integer)
ndims = nc_inq_compound_fieldndims(ncid,xtype,fieldid)
dim_sizes = zeros(Cint,ndims)
check(ccall((:nc_inq_compound_fielddim_sizes,libnetcdf),Cint,(Cint,nc_type,Cint,Ptr{Cint}),ncid,xtype,fieldid,dim_sizes))
return dim_sizes
end
function nc_def_vlen(ncid::Integer,name,base_typeid::Integer)
xtypep = Ref(nc_type(0))
check(ccall((:nc_def_vlen,libnetcdf),Cint,(Cint,Cstring,nc_type,Ptr{nc_type}),ncid,name,base_typeid,xtypep))
return xtypep[]
end
function nc_inq_vlen(ncid::Integer,xtype::Integer)
# datum_size is sizeof(nc_vlen_t)
datum_sizep = Ref(Csize_t(0))
base_nc_typep = Ref(nc_type(0))
name = zeros(UInt8,NC_MAX_NAME+1)
check(ccall((:nc_inq_vlen,libnetcdf),Cint,(Cint,nc_type,Ptr{UInt8},Ptr{Csize_t},Ptr{nc_type}),ncid,xtype,name,datum_sizep,base_nc_typep))
return unsafe_string(pointer(name)),datum_sizep[],base_nc_typep[]
end
function nc_free_vlen(vl::nc_vlen_t{T}) where {T}
check(ccall((:nc_free_vlen,libnetcdf),Cint,(Ptr{nc_vlen_t{T}},),Ref(vl)))
end
# function nc_free_vlens(len::Integer,vlens)
# check(ccall((:nc_free_vlens,libnetcdf),Cint,(Cint,Ptr{nc_vlen_t}),len,vlens))
# end
# function nc_put_vlen_element(ncid::Integer,typeid1::Integer,vlen_element,len::Integer,data)
# check(ccall((:nc_put_vlen_element,libnetcdf),Cint,(Cint,Cint,Ptr{Nothing},Cint,Ptr{Nothing}),ncid,typeid1,vlen_element,len,data))
# end
# function nc_get_vlen_element(ncid::Integer,typeid1::Integer,vlen_element,len,data)
# check(ccall((:nc_get_vlen_element,libnetcdf),Cint,(Cint,Cint,Ptr{Nothing},Ptr{Cint},Ptr{Nothing}),ncid,typeid1,vlen_element,len,data))
# end
# function nc_free_string(len::Integer,data)
# check(ccall((:nc_free_string,libnetcdf),Cint,(Cint,Ptr{Ptr{UInt8}}),len,data))
# end
"""
name,size,base_nc_type,nfields,class = nc_inq_user_type(ncid::Integer,xtype::Integer)
"""
function nc_inq_user_type(ncid::Integer,xtype::Integer)
name = Vector{UInt8}(undef,NC_MAX_NAME+1)
sizep = Ref(Csize_t(0))
base_nc_typep = Ref(nc_type(0))
nfieldsp = Ref(Csize_t(0))
classp = Ref(Cint(0))
check(ccall((:nc_inq_user_type,libnetcdf),Cint,(Cint,nc_type,Ptr{UInt8},Ptr{Csize_t},Ptr{nc_type},Ptr{Csize_t},Ptr{Cint}),ncid,xtype,name,sizep,base_nc_typep,nfieldsp,classp))
return unsafe_string(pointer(name)),sizep[],base_nc_typep[],nfieldsp[],classp[]
end
function nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,typeid::Integer,data::Vector)
check(ccall((:nc_put_att,libnetcdf),Cint,(Cint,Cint,Cstring,nc_type,Csize_t,Ptr{Nothing}),
ncid,varid,name,typeid,length(data),data))
end
function nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data::AbstractString)
if Symbol(name) == :_FillValue
nc_put_att_string(ncid,varid,"_FillValue",[data])
else
check(ccall((:nc_put_att_text,libnetcdf),Cint,(Cint,Cint,Cstring,Csize_t,Cstring),
ncid,varid,name,sizeof(data),data))
end
end
function nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data::Vector{Char})
nc_put_att(ncid,varid,name,join(data))
end
# NetCDF does not necessarily support 64 bit integer attributes
function nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data::Int64)
if Symbol(name) == :_FillValue
nc_put_att(ncid,varid,name,ncType[Int64],[data])
else
nc_put_att(ncid,varid,name,Int32(data))
end
end
nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data::Vector{Int64}) =
nc_put_att(ncid,varid,name,Int32.(data))
function nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data::Number)
nc_put_att(ncid,varid,name,ncType[typeof(data)],[data])
end
function nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data::Char)
# UInt8('α')
# ERROR: InexactError: trunc(UInt8, 945)
nc_put_att(ncid,varid,name,ncType[typeof(data)],[UInt8(data)])
end
function nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data::Vector{T}) where T <: AbstractString
nc_put_att(ncid,varid,name,ncType[String],pointer.(data))
end
function nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data::Vector{T}) where {T}
nc_put_att(ncid,varid,name,ncType[T],data)
end
function nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data::Vector{Any})
T = promote_type(typeof.(data)...)
nc_put_att(ncid,varid,name,ncType[T],data)
end
# convert e.g. ranges to vectors
function nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data::AbstractVector)
nc_put_att(ncid,varid,name,Vector(data))
end
function nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data)
error("attributes can only be scalars or vectors")
end
function nc_get_att(ncid::Integer,varid::Integer,name)
xtype,len = nc_inq_att(ncid,varid,name)
if xtype == NC_CHAR
val = Vector{UInt8}(undef,len)
check(ccall((:nc_get_att,libnetcdf),Cint,(Cint,Cint,Cstring,Ptr{Nothing}),ncid,varid,name,val))
# Note
# fillvalues for character attributes must be returns as Char and not a strings
if name == "_FillValue"
return Char(val[1])
end
if any(val .== 0)
# consider the null terminating character if present
# see issue #12
return unsafe_string(pointer(val))
else
return unsafe_string(pointer(val),length(val))
end
elseif xtype == NC_STRING
val = Vector{Ptr{UInt8}}(undef,len)
check(ccall((:nc_get_att,libnetcdf),Cint,(Cint,Cint,Cstring,Ptr{Nothing}),ncid,varid,name,val))
str = unsafe_string.(val)
if len == 1
return str[1]
else
return str
end
else
val = Vector{jlType[xtype]}(undef,len)
check(ccall((:nc_get_att,libnetcdf),Cint,(Cint,Cint,Cstring,Ptr{Nothing}),ncid,varid,name,val))
if len == 1
return val[1]
else
return val
end
end
end
# Enum
function nc_def_enum(ncid::Integer,base_typeid::Integer,name)
typeidp = Ref(NCDatasets.nc_type(0))
check(ccall((:nc_def_enum,libnetcdf),Cint,(Cint,nc_type,Cstring,Ptr{nc_type}),ncid,base_typeid,name,typeidp))
return typeidp[]
end
function nc_inq_enum(ncid::Integer,xtype::Integer)
base_nc_typep = Ref(nc_type(0))
base_sizep = Ref(Csize_t(0))
num_membersp = Ref(Csize_t(0))
cname = zeros(UInt8,NC_MAX_NAME+1)
check(ccall((:nc_inq_enum,libnetcdf),Cint,(Cint,nc_type,Ptr{UInt8},Ptr{NCDatasets.nc_type},Ptr{Csize_t},Ptr{Csize_t}),ncid,xtype,cname,base_nc_typep,base_sizep,num_membersp))
type_name = unsafe_string(pointer(cname))
base_nc_type = base_nc_typep[]
num_members = num_membersp[]
base_size = base_sizep[]
return type_name,jlType[base_nc_type],base_size,num_members
end
function nc_insert_enum(ncid::Integer,xtype::Integer,name,value,
T = nc_inq_enum(ncid,typeid)[2])
valuep = Ref{T}(value)
check(ccall((:nc_insert_enum,libnetcdf),Cint,(Cint,nc_type,Cstring,Ptr{Nothing}),ncid,xtype,name,valuep))
end
function nc_inq_enum_member(ncid::Integer,xtype::Integer,idx::Integer,
T::Type = nc_inq_enum(ncid,typeid)[2])
valuep = Ref{T}()
cmember_name = zeros(UInt8,NCDatasets.NC_MAX_NAME+1)
check(ccall((:nc_inq_enum_member,libnetcdf),Cint,(Cint,nc_type,Cint,Ptr{UInt8},Ptr{Nothing}),ncid,xtype,idx,cmember_name,valuep))
member_name = unsafe_string(pointer(cmember_name))
return member_name,valuep[]
end
function nc_inq_enum_ident(ncid::Integer,xtype::Integer,value)
cidentifier = zeros(UInt8,NCDatasets.NC_MAX_NAME+1)
check(ccall((:nc_inq_enum_ident,libnetcdf),Cint,(Cint,nc_type,Clonglong,Ptr{UInt8}),ncid,xtype,Clonglong(value),cidentifier))
identifier = unsafe_string(pointer(cidentifier))
return identifier
end
# function nc_def_opaque(ncid::Integer,size::Integer,name,xtypep)
# check(ccall((:nc_def_opaque,libnetcdf),Cint,(Cint,Cint,Cstring,Ptr{nc_type}),ncid,size,name,xtypep))
# end
# function nc_inq_opaque(ncid::Integer,xtype::Integer,name,sizep)
# check(ccall((:nc_inq_opaque,libnetcdf),Cint,(Cint,nc_type,Cstring,Ptr{Cint}),ncid,xtype,name,sizep))
# end
# can the NetCDF variable varid receive the data?
function _nc_shape_check(ncid,varid,data,start,count,stride)
@debug ncid,varid,data,start,count,stride
end
function nc_put_var(ncid::Integer,varid::Integer,data::Array{Char,N}) where N
nc_put_var(ncid,varid,convert(Array{UInt8,N},data))
end
function nc_put_var(ncid::Integer,varid::Integer,data::Array{String,N}) where N
# pointer.(data) is surprisingly a scalar pointer Ptr{UInt8} if data is a
# Array{T,0}
tmp = map(pointer,data)
nc_put_var(ncid,varid,tmp)
end
function nc_put_var(ncid::Integer,varid::Integer,data::Array{Vector{T},N}) where {T,N}
nc_put_var(ncid,varid,convert(Array{nc_vlen_t{T},N},data))
end
function nc_unsafe_put_var(ncid::Integer,varid::Integer,data::Array)
check(ccall((:nc_put_var,libnetcdf),Cint,(Cint,Cint,Ptr{Nothing}),ncid,varid,data))
end
# data can be a range that must first be converted to an array
function nc_unsafe_put_var(ncid::Integer,varid::Integer,data)
check(ccall((:nc_put_var,libnetcdf),Cint,(Cint,Cint,Ptr{Nothing}),ncid,varid,Array(data)))
end
function nc_put_var(ncid::Integer,varid::Integer,data)
dimids = nc_inq_vardimid(ncid,varid)
ndims = length(dimids)
ncsize = ntuple(i -> nc_inq_dimlen(ncid,dimids[ndims-i+1]), ndims)
if isempty(nc_inq_unlimdims(ncid))
if ncsize != size(data)
path = nc_inq_path(ncid)
varname = nc_inq_varname(ncid,varid)
throw(NetCDFError(-1,"wrong size of variable '$varname' (size $ncsize) in file '$path' for an array of size $(size(data))"))
end
nc_unsafe_put_var(ncid,varid,data)
else
# honor this good advice:
# Take care when using this function with record variables (variables
# that use the ::NC_UNLIMITED dimension). If you try to write all the
# values of a record variable into a netCDF file that has no record data
# yet (hence has 0 records), nothing will be written. Similarly, if you
# try to write all the values of a record variable but there are more
# records in the file than you assume, more in-memory data will be
# accessed than you supply, which may result in a segmentation
# violation. To avoid such problems, it is better to use the nc_put_vara
# interfaces for variables that use the ::NC_UNLIMITED dimension.
# https://github.com/Unidata/netcdf-c/blob/48cc56ea3833df455337c37186fa6cd7fac9dc7e/libdispatch/dvarput.c#L895
startp = zeros(ndims)
countp = Int[reverse(size(data))...,]
nc_put_vara(ncid,varid,startp,countp,data)
end
end
function nc_get_var!(ncid::Integer,varid::Integer,ip::Array{Char,N}) where N
tmp = Array{UInt8,N}(undef,size(ip))
nc_get_var!(ncid,varid,tmp)
for i in eachindex(tmp)
ip[i] = Char(tmp[i])
end
end
function nc_get_var!(ncid::Integer,varid::Integer,ip::Array{String,N}) where N
tmp = Array{Ptr{UInt8},N}(undef,size(ip))
nc_get_var!(ncid,varid,tmp)
for i in eachindex(tmp)
#ip[:] = unsafe_string.(tmp)
ip[i] = unsafe_string(tmp[i])
end
end
function nc_get_var!(ncid::Integer,varid::Integer,ip::Array{Vector{T},N}) where {T,N}
tmp = Array{NCDatasets.nc_vlen_t{T},N}(undef,size(ip))
nc_get_var!(ncid,varid,tmp)
for i in eachindex(tmp)
ip[i] = unsafe_wrap(Vector{T},tmp[i].p,(tmp[i].len,))
end
end
function nc_get_var!(ncid::Integer,varid::Integer,ip)
check(ccall((:nc_get_var,libnetcdf),Cint,(Cint,Cint,Ptr{Nothing}),ncid,varid,ip))
end
function nc_put_var1(ncid::Integer,varid::Integer,indexp,op::Vector{T}) where T
tmp = nc_vlen_t{T}(length(op), pointer(op))
check(ccall((:nc_put_var1,libnetcdf),Cint,(Cint,Cint,Ptr{Csize_t},Ptr{Nothing}),ncid,varid,indexp,Ref(tmp)))
end
function nc_put_var1(ncid::Integer,varid::Integer,indexp,op::T) where T
@debug "nc_put_var1",indexp,op
check(ccall((:nc_put_var1,libnetcdf),Cint,(Cint,Cint,Ptr{Csize_t},Ptr{Nothing}),ncid,varid,indexp,T[op]))
end
function nc_put_var1(ncid::Integer,varid::Integer,indexp,op::Char)
@debug "nc_put_var1 char",indexp,op
check(ccall((:nc_put_var1,libnetcdf),Cint,(Cint,Cint,Ptr{Csize_t},Ptr{Nothing}),ncid,varid,indexp,[UInt8(op)]))
end
function nc_put_var1(ncid::Integer,varid::Integer,indexp,op::String)
@debug "nc_put_var1 String",indexp,op
check(ccall((:nc_put_var1_string,libnetcdf),Cint,(Cint,Cint,Ptr{Csize_t},Ptr{Cstring}),ncid,varid,indexp,[op]))
end
function nc_get_var1(::Type{Char},ncid::Integer,varid::Integer,indexp)
@debug "nc_get_var1",indexp
tmp = Ref(UInt8(0))
check(ccall((:nc_get_var1,libnetcdf),Cint,(Cint,Cint,Ptr{Csize_t},Ptr{Nothing}),ncid,varid,indexp,tmp))
return Char(tmp[])
end
function nc_get_var1(::Type{String},ncid::Integer,varid::Integer,indexp)
tmp = Ref(Ptr{UInt8}(0))
check(ccall((:nc_get_var1_string,libnetcdf),Cint,(Cint,Cint,Ptr{Csize_t},Ptr{Ptr{UInt8}}),ncid,varid,indexp,tmp))
return unsafe_string(tmp[])
end
function nc_get_var1(::Type{T},ncid::Integer,varid::Integer,indexp) where T
@debug "nc_get_var1" indexp
ip = Ref{T}()
check(ccall((:nc_get_var1,libnetcdf),Cint,(Cint,Cint,Ptr{Csize_t},Ptr{Nothing}),ncid,varid,indexp,ip))
return ip[]
end
function nc_get_var1(::Type{Vector{T}},ncid::Integer,varid::Integer,indexp) where T
ip = Ref(nc_vlen_t{T}(zero(T),Ptr{T}()))
check(ccall((:nc_get_var1,libnetcdf),Cint,(Cint,Cint,Ptr{Csize_t},Ptr{Nothing}),ncid,varid,indexp,ip))
#data = unsafe_wrap(Vector{T},ip[].p,(ip[].len,))
data = copy(unsafe_wrap(Vector{T},ip[].p,(ip[].len,)))
nc_free_vlen(ip[])
return data
end
function nc_put_vara(ncid::Integer,varid::Integer,startp,countp,op)
check(ccall((:nc_put_vara,libnetcdf),Cint,(Cint,Cint,Ptr{Csize_t},Ptr{Csize_t},Ptr{Nothing}),ncid,varid,startp,countp,op))
end
function nc_put_vara(ncid::Integer,varid::Integer,startp,countp,op::Array{Char,N}) where N
tmp = convert(Array{UInt8,N},op)
nc_put_vara(ncid,varid,startp,countp,tmp)
end
function nc_put_vara(ncid::Integer,varid::Integer,startp,countp,op::Array{String,N}) where N
nc_put_vara(ncid,varid,startp,countp,pointer.(op))
end
function nc_put_vara(ncid::Integer,varid::Integer,startp,countp,
op::Array{Vector{T},N}) where {T,N}
nc_put_vara(ncid,varid,startp,countp,
convert(Array{nc_vlen_t{T},N},op))
end
function nc_get_vara!(ncid::Integer,varid::Integer,startp,countp,ip)
@debug "nc_get_vara!",startp,indexp
check(ccall((:nc_get_vara,libnetcdf),Cint,(Cint,Cint,Ptr{Csize_t},Ptr{Csize_t},Ptr{Nothing}),ncid,varid,startp,countp,ip))
end
function nc_get_vara!(ncid::Integer,varid::Integer,startp,countp,ip::Array{Char,N}) where N
tmp = Array{UInt8,N}(undef,size(ip))
nc_get_vara!(ncid,varid,startp,countp,tmp)
for i in eachindex(tmp)
ip[i] = Char(tmp[i])
end
end
function nc_get_vara!(ncid::Integer,varid::Integer,startp,countp,ip::Array{String,N}) where N
tmp = Array{Ptr{UInt8},N}(undef,size(ip))
nc_get_vara!(ncid,varid,startp,countp,tmp)
for i in eachindex(tmp)
#ip[:] = unsafe_string.(tmp)
ip[i] = unsafe_string(tmp[i])
end
end
function nc_get_vara!(ncid::Integer,varid::Integer,startp,countp,ip::Array{Vector{T},N}) where {T,N}
tmp = Array{NCDatasets.nc_vlen_t{T},N}(undef,size(ip))
nc_get_vara!(ncid,varid,startp,countp,tmp)
for i in eachindex(tmp)
ip[i] = unsafe_wrap(Vector{T},tmp[i].p,(tmp[i].len,))
end
end
function nc_put_vars(ncid::Integer,varid::Integer,startp,countp,stridep,
op::Array{Char,N}) where N
tmp = Array{UInt8,N}(undef,size(op))
for i in eachindex(op)
tmp[i] = UInt8(op[i])
end
nc_put_vars(ncid,varid,startp,countp,stridep,tmp)
end
function nc_put_vars(ncid::Integer,varid::Integer,startp,countp,stridep,
op::Array{String,N}) where N
nc_put_vars(ncid,varid,startp,countp,stridep,pointer.(op))
end
function nc_put_vars(ncid::Integer,varid::Integer,startp,countp,stridep,
op::Array{Vector{T},N}) where {T,N}
nc_put_vars(ncid,varid,startp,countp,stridep,
convert(Array{nc_vlen_t{T},N},op))
end
function _nc_check_size_put_vars(ncid,varid,countp,op)
# dimension index for NetCDF variable
i1 = 1
# dimension index of data op
i2 = 1
# not_one(x) = x != 1
# if filter(not_one,reverse(countp)) != filter(not_one,[size(op)...])
# path = nc_inq_path(ncid)
# varname = nc_inq_varname(ncid,varid)
# throw(DimensionMismatch("size mismatch for variable '$(varname)' in file '$(path)'. Trying to write $(size(op)) elements while $(countp) are expected"))
# end
unlimdims = nc_inq_unlimdims(ncid)
dimids = nc_inq_vardimid(ncid,varid)
countp = reverse(countp)
dimids = reverse(dimids)
while true
if (i2 > ndims(op)) && (i1 > length(countp))
break
end
count_i1 =
if i1 <= length(countp)
countp[i1]
else
1
end
# ignore dimensions with only one element
if (count_i1 == 1) && (i1 <= length(countp))
i1 += 1
continue
end
if size(op,i2) == 1 && (i2 <= ndims(op))
i2 += 1
continue
end
# no test for unlimited dimensions
if (i1 <= length(dimids)) && (dimids[i1] in unlimdims)
# ok
elseif (size(op,i2) !== count_i1)
path = nc_inq_path(ncid)
varname = nc_inq_varname(ncid,varid)
throw(NetCDFError(NC_EEDGE,"size mismatch for variable '$(varname)' in file '$(path)'. Trying to write $(size(op)) elements while $(countp) are expected"))
end
i1 += 1
i2 += 1
end
end
function nc_put_vars(ncid::Integer,varid::Integer,startp,countp,stridep,op)
@debug "nc_put_vars: $startp,$countp,$stridep"
@debug "shape $(size(op))"
_nc_check_size_put_vars(ncid,varid,countp,op)
check(ccall((:nc_put_vars,libnetcdf),Cint,
(Cint,Cint,Ptr{Csize_t},Ptr{Csize_t},
Ptr{Cint},Ptr{Nothing}),ncid,varid,startp,countp,stridep,op))
end
function nc_get_vars!(ncid::Integer,varid::Integer,startp,countp,stridep,ip::Array{Char,N}) where N
@debug "nc_get_vars!: $startp,$countp,$stridep"
tmp = Array{UInt8,N}(undef,size(ip))
nc_get_vars!(ncid,varid,startp,countp,stridep,tmp)
for i in eachindex(tmp)