@@ -15,8 +15,33 @@ as a data sink:
1515struct NetCDFDataset
1616 filename:: String
1717 mode:: UInt16
18+ handle:: Base.RefValue{Union{Nothing, NcFile}}
1819end
19- NetCDFDataset (filename;mode= " r" ) = mode == " r" ? NetCDFDataset (filename,NC_NOWRITE) : NetCDFDataset (filename,NC_WRITE)
20+ function NetCDFDataset (filename;mode= " r" )
21+ m = mode == " r" ? NC_NOWRITE : NC_WRITE
22+ NetCDFDataset (filename,m,Ref {Union{Nothing, NcFile}} (nothing ))
23+ end
24+ function dsopen (f,ds:: NetCDFDataset )
25+ if ds. handle[] === nothing
26+ NetCDF. open (f, ds. filename)
27+ else
28+ f (ds. handle[])
29+ end
30+ end
31+ function YAB. open_dataset_handle (f, ds:: NetCDFDataset )
32+ if ds. handle[] === nothing
33+ try
34+ ds. handle[] = NetCDF. open (ds. filename, mode= ds. mode)
35+ f (ds)
36+ finally
37+ ds. handle[]= nothing
38+ end
39+ else
40+ f (ds)
41+ end
42+ end
43+
44+
2045
2146import . NetCDF: AbstractDiskArray, readblock!, writeblock!, haschunks, eachchunk
2247
@@ -49,15 +74,19 @@ YAB.iscompressed(v::NetCDFVariable) = NetCDF.open(v->v.compress > 0, v.filename,
4974
5075Base. size (v:: NetCDFVariable ) = v. size
5176
52- YAB. get_var_dims (ds:: NetCDFDataset ,name) = NetCDF. open (v-> map (i-> i. name,v[name]. dim),ds. filename)
53- YAB. get_varnames (ds:: NetCDFDataset ) = NetCDF. open (v-> collect (keys (v. vars)),ds. filename)
54- YAB. get_var_attrs (ds:: NetCDFDataset , name) = NetCDF. open (v-> v[name]. atts,ds. filename)
55- YAB. get_global_attrs (ds:: NetCDFDataset ) = NetCDF. open (nc-> nc. gatts, ds. filename)
56- function Base. getindex (ds:: NetCDFDataset , i)
57- s,et = NetCDF. open (j-> (size (j),eltype (j)),ds. filename,i)
58- NetCDFVariable {et,length(s)} (ds. filename, i, s)
77+ YAB. get_var_dims (ds:: NetCDFDataset ,name) = dsopen (v-> map (i-> i. name,v[name]. dim),ds)
78+ YAB. get_varnames (ds:: NetCDFDataset ) = dsopen (v-> collect (keys (v. vars)),ds)
79+ YAB. get_var_attrs (ds:: NetCDFDataset , name) = dsopen (v-> v[name]. atts,ds)
80+ YAB. get_global_attrs (ds:: NetCDFDataset ) = dsopen (nc-> nc. gatts, ds)
81+ function YAB. get_var_handle (ds:: NetCDFDataset , i; persist = true )
82+ if persist || ds. handle[] === nothing
83+ s,et = NetCDF. open (j-> (size (j),eltype (j)),ds. filename,i)
84+ NetCDFVariable {et,length(s)} (ds. filename, i, s)
85+ else
86+ ds. handle[][i]
87+ end
5988end
60- Base. haskey (ds:: NetCDFDataset ,k) = NetCDF . open (nc-> haskey (nc. vars,k),ds. filename )
89+ Base. haskey (ds:: NetCDFDataset ,k) = dsopen (nc-> haskey (nc. vars,k),ds)
6190
6291function YAB. add_var (p:: NetCDFDataset , T:: Type , varname, s, dimnames, attr;
6392 chunksize= s, compress = - 1 )
0 commit comments