@@ -345,85 +345,68 @@ end
345
345
nomissing (a:: AbstractArray ,value) = a
346
346
export nomissing
347
347
348
-
349
- function Base. getindex (v:: Variable ,indexes:: Int... )
348
+ # This method needs to be duplicated instead of using an Union. Otherwise a DiskArrays fallback is called instead which impacts performances
349
+ # (see https://github.com/Alexander-Barth/NCDatasets.jl/pull/205#issuecomment-1589575041)
350
+ function readblock! (v:: Variable , aout, indexes:: TI... ) where TI <: Union{AbstractUnitRange,StepRange}
350
351
datamode (v. ds)
351
- return nc_get_var1 (eltype (v),v. ds. ncid,v. varid,[i- 1 for i in indexes[ndims (v): - 1 : 1 ]])
352
+ _readblock! (v, aout, indexes... )
353
+ return aout
352
354
end
353
355
354
- function Base. setindex! (v:: Variable{T,N} ,data,indexes:: Int... ) where N where T
355
- @debug " $(@__LINE__ ) "
356
- datamode (v. ds)
357
- # use zero-based indexes and reversed order
358
- nc_put_var1 (v. ds. ncid,v. varid,[i- 1 for i in indexes[ndims (v): - 1 : 1 ]],T (data))
359
- return data
360
- end
356
+ _readblock! (v:: Variable , aout, indexes:: AbstractUnitRange... ) = _read_data_from_nc! (v, aout, indexes... )
357
+ _readblock! (v:: Variable , aout, indexes:: StepRange... ) = _read_data_from_nc! (v, aout, indexes... )
361
358
362
- function Base. getindex (v:: Variable{T,N} ,indexes:: Colon... ) where {T,N}
363
- datamode (v. ds)
364
- data = Array {T,N} (undef,size (v))
365
- nc_get_var! (v. ds. ncid,v. varid,data)
359
+ readblock! (v:: Variable , aout) = _read_data_from_nc! (v:: Variable , aout)
366
360
367
- # special case for scalar NetCDF variable
368
- if N == 0
369
- return data[]
370
- else
371
- return data
372
- end
361
+ function _read_data_from_nc! (v:: Variable , aout, indexes:: Int... )
362
+ aout .= nc_get_var1 (eltype (v),v. ds. ncid,v. varid,[i- 1 for i in reverse (indexes)])
373
363
end
374
364
375
- function Base. setindex! (v:: Variable{T,N} ,data:: T ,indexes:: Colon... ) where {T,N}
376
- @debug " setindex! colon $data "
377
- datamode (v. ds) # make sure that the file is in data mode
378
- tmp = fill (data,size (v))
379
- nc_put_var (v. ds. ncid,v. varid,tmp)
380
- return data
365
+ function _read_data_from_nc! (v:: Variable{T,N} , aout, indexes:: TR... ) where {T,N} where TR <: Union{StepRange{Int,Int},UnitRange{Int}}
366
+ start,count,stride,jlshape = ncsub (indexes)
367
+ nc_get_vars! (v. ds. ncid,v. varid,start,count,stride,aout)
381
368
end
382
369
383
- # union types cannot be used to avoid ambiguity
384
- for data_type = [Number, String, Char]
385
- @eval begin
386
- # call to v .= 123
387
- function Base. setindex! (v:: Variable{T,N} ,data:: $data_type ) where {T,N}
388
- @debug " setindex! $data "
389
- datamode (v. ds) # make sure that the file is in data mode
390
- tmp = fill (convert (T,data),size (v))
391
- nc_put_var (v. ds. ncid,v. varid,tmp)
392
- return data
393
- end
394
-
395
- Base. setindex! (v:: Variable ,data:: $data_type ,indexes:: Colon... ) = setindex! (v:: Variable ,data)
396
-
397
- function Base. setindex! (v:: Variable{T,N} ,data:: $data_type ,indexes:: StepRange{Int,Int} ...) where {T,N}
398
- datamode (v. ds) # make sure that the file is in data mode
399
- start,count,stride,jlshape = ncsub (indexes[1 : ndims (v)])
400
- tmp = fill (convert (T,data),jlshape)
401
- nc_put_vars (v. ds. ncid,v. varid,start,count,stride,tmp)
402
- return data
403
- end
404
- end
370
+ function _read_data_from_nc! (v:: Variable{T,N} , aout, indexes:: Union{Int,Colon,AbstractRange{<:Integer}} ...) where {T,N}
371
+ sz = size (v)
372
+ start,count,stride = ncsub2 (sz,indexes... )
373
+ jlshape = _shape_after_slice (sz,indexes... )
374
+ nc_get_vars! (v. ds. ncid,v. varid,start,count,stride,aout)
405
375
end
406
376
407
- function Base. setindex! (v:: Variable{T,N} ,data:: AbstractArray{T,N} ,indexes:: Colon... ) where {T,N}
408
- datamode (v. ds) # make sure that the file is in data mode
377
+ _read_data_from_nc! (v:: Variable , aout) = _read_data_from_nc! (v, aout, 1 )
409
378
410
- nc_put_var (v. ds. ncid,v. varid,data)
379
+ function writeblock! (v:: Variable , data, indexes:: TI... ) where TI <: Union{AbstractUnitRange,StepRange}
380
+ datamode (v. ds)
381
+ _write_data_to_nc (v, data, indexes... )
411
382
return data
412
383
end
413
384
414
- function Base. setindex! (v:: Variable{T,N} ,data:: AbstractArray{T2,N} ,indexes:: Colon... ) where {T,T2,N}
415
- datamode (v. ds) # make sure that the file is in data mode
416
- tmp =
417
- if T <: Integer
418
- round .(T,data)
419
- else
420
- convert (Array{T,N},data)
421
- end
385
+ function _write_data_to_nc (v:: Variable{T,N} ,data,indexes:: Int... ) where {T,N}
386
+ nc_put_var1 (v. ds. ncid,v. varid,[i- 1 for i in reverse (indexes)],T (data[1 ]))
387
+ end
422
388
423
- nc_put_var (v. ds. ncid,v. varid,tmp)
424
- return data
389
+ _write_data_to_nc (v:: Variable , data) = _write_data_to_nc (v, data, 1 )
390
+
391
+ function _write_data_to_nc (v:: Variable{T, N} , data, indexes:: StepRange{Int,Int} ...) where {T, N}
392
+ start,count,stride,jlshape = ncsub (indexes)
393
+ nc_put_vars (v. ds. ncid,v. varid,start,count,stride,T .(data))
394
+ end
395
+
396
+ function _write_data_to_nc (v:: Variable , data, indexes:: Union{AbstractRange{<:Integer}} ...)
397
+ ind = prod (length .(indexes)) == 1 ? first .(indexes) : normalizeindexes (size (v),indexes)
398
+ return _write_data_to_nc (v, data, ind... )
425
399
end
426
400
401
+ getchunksize (v:: Variable ) = getchunksize (haschunks (v),v)
402
+ getchunksize (:: DiskArrays.Chunked , v:: Variable ) = chunking (v)[2 ]
403
+ # getchunksize(::DiskArrays.Unchunked, v::Variable) = DiskArrays.estimate_chunksize(v)
404
+ getchunksize (:: DiskArrays.Unchunked , v:: Variable ) = size (v)
405
+ eachchunk (v:: CFVariable ) = eachchunk (v. var)
406
+ haschunks (v:: CFVariable ) = haschunks (v. var)
407
+ eachchunk (v:: Variable ) = DiskArrays. GridChunks (v, Tuple (getchunksize (v)))
408
+ haschunks (v:: Variable ) = (chunking (v)[1 ] == :contiguous ? DiskArrays. Unchunked () : DiskArrays. Chunked ())
409
+
427
410
_normalizeindex (n,ind:: Base.OneTo ) = 1 : 1 : ind. stop
428
411
_normalizeindex (n,ind:: Colon ) = 1 : 1 : n
429
412
_normalizeindex (n,ind:: Int ) = ind: 1 : ind
477
460
return start,count,stride
478
461
end
479
462
480
- function Base. getindex (v:: Variable{T,N} ,indexes:: TR... ) where {T,N} where TR <: Union{StepRange{Int,Int},UnitRange{Int}}
481
- start,count,stride,jlshape = ncsub (indexes[1 : N])
482
- data = Array {T,N} (undef,jlshape)
483
-
484
- datamode (v. ds)
485
- nc_get_vars! (v. ds. ncid,v. varid,start,count,stride,data)
486
- return data
487
- end
488
-
489
- function Base. setindex! (v:: Variable{T,N} ,data:: T ,indexes:: StepRange{Int,Int} ...) where {T,N}
490
- datamode (v. ds) # make sure that the file is in data mode
491
- start,count,stride,jlshape = ncsub (indexes[1 : ndims (v)])
492
- tmp = fill (data,jlshape)
493
- nc_put_vars (v. ds. ncid,v. varid,start,count,stride,tmp)
494
- return data
495
- end
496
-
497
- function Base. setindex! (v:: Variable{T,N} ,data:: Array{T,N} ,indexes:: StepRange{Int,Int} ...) where {T,N}
498
- datamode (v. ds) # make sure that the file is in data mode
499
- start,count,stride,jlshape = ncsub (indexes[1 : ndims (v)])
500
- nc_put_vars (v. ds. ncid,v. varid,start,count,stride,data)
501
- return data
502
- end
503
-
504
- # data can be Array{T2,N} or BitArray{N}
505
- function Base. setindex! (v:: Variable{T,N} ,data:: AbstractArray ,indexes:: StepRange{Int,Int} ...) where {T,N}
506
- datamode (v. ds) # make sure that the file is in data mode
507
- start,count,stride,jlshape = ncsub (indexes[1 : ndims (v)])
508
-
509
- tmp = convert (Array{T,ndims (data)},data)
510
- nc_put_vars (v. ds. ncid,v. varid,start,count,stride,tmp)
511
-
512
- return data
513
- end
514
-
515
-
516
-
517
-
518
- function Base. getindex (v:: Variable{T,N} ,indexes:: Union{Int,Colon,AbstractRange{<:Integer}} ...) where {T,N}
519
- sz = size (v)
520
- start,count,stride = ncsub2 (sz,indexes... )
521
- jlshape = _shape_after_slice (sz,indexes... )
522
- data = Array {T} (undef,jlshape)
523
-
524
- datamode (v. ds)
525
- nc_get_vars! (v. ds. ncid,v. varid,start,count,stride,data)
526
-
527
- return data
528
- end
529
-
530
- # NetCDF scalars indexed as []
531
- Base. getindex (v:: Variable{T, 0} ) where T = v[1 ]
532
-
533
-
534
-
535
- function Base. setindex! (v:: Variable ,data,indexes:: Union{Int,Colon,AbstractRange{<:Integer}} ...)
536
- ind = normalizeindexes (size (v),indexes)
537
-
538
- # make arrays out of scalars (arrays can have zero dimensions)
539
- if (ndims (data) == 0 ) && ! (data isa AbstractArray)
540
- data = fill (data,length .(ind))
541
- end
542
-
543
- return v[ind... ] = data
544
- end
545
-
546
-
547
- Base. getindex (v:: Union{MFVariable,DeferVariable,Variable} ,ci:: CartesianIndices ) = v[ci. indices... ]
548
- Base. setindex! (v:: Union{MFVariable,DeferVariable,Variable} ,data,ci:: CartesianIndices ) = setindex! (v,data,ci. indices... )
463
+ Base. getindex (v:: Union{MFVariable,DeferVariable} ,ci:: CartesianIndices ) = v[ci. indices... ]
464
+ Base. setindex! (v:: Union{MFVariable,DeferVariable} ,data,ci:: CartesianIndices ) = setindex! (v,data,ci. indices... )
0 commit comments