@@ -318,12 +318,8 @@ def maybe_decode_store(store, lock=False):
318318 return maybe_decode_store (store )
319319
320320
321- def open_dataarray (filename_or_obj , group = None , decode_cf = True ,
322- mask_and_scale = True , decode_times = True ,
323- concat_characters = True , decode_coords = True , engine = None ,
324- chunks = None , lock = None , cache = None , drop_variables = None ):
325- """
326- Opens an DataArray from a netCDF file containing a single data variable.
321+ def open_dataarray (* args , ** kwargs ):
322+ """Open an DataArray from a netCDF file containing a single data variable.
327323
328324 This is designed to read netCDF files with only one data variable. If
329325 multiple variables are present then a ValueError is raised.
@@ -353,6 +349,10 @@ def open_dataarray(filename_or_obj, group=None, decode_cf=True,
353349 decode_times : bool, optional
354350 If True, decode times encoded in the standard NetCDF datetime format
355351 into datetime objects. Otherwise, leave them encoded as numbers.
352+ autoclose : bool, optional
353+ If True, automatically close files to avoid OS Error of too many files
354+ being open. However, this option doesn't work with streams, e.g.,
355+ BytesIO.
356356 concat_characters : bool, optional
357357 If True, concatenate along the last dimension of character arrays to
358358 form string arrays. Dimensions will only be concatenated over (and
@@ -400,10 +400,7 @@ def open_dataarray(filename_or_obj, group=None, decode_cf=True,
400400 --------
401401 open_dataset
402402 """
403- dataset = open_dataset (filename_or_obj , group , decode_cf ,
404- mask_and_scale , decode_times ,
405- concat_characters , decode_coords , engine ,
406- chunks , lock , cache , drop_variables )
403+ dataset = open_dataset (* args , ** kwargs )
407404
408405 if len (dataset .data_vars ) != 1 :
409406 raise ValueError ('Given file dataset contains more than one data '
@@ -536,7 +533,7 @@ def open_mfdataset(paths, chunks=None, concat_dim=_CONCAT_DIM_DEFAULT,
536533 'h5netcdf' : backends .H5NetCDFStore }
537534
538535
539- def to_netcdf (dataset , path = None , mode = 'w' , format = None , group = None ,
536+ def to_netcdf (dataset , path_or_file = None , mode = 'w' , format = None , group = None ,
540537 engine = None , writer = None , encoding = None , unlimited_dims = None ):
541538 """This function creates an appropriate datastore for writing a dataset to
542539 disk as a netCDF file
@@ -547,18 +544,19 @@ def to_netcdf(dataset, path=None, mode='w', format=None, group=None,
547544 """
548545 if encoding is None :
549546 encoding = {}
550- if path is None :
551- path = BytesIO ()
547+ if path_or_file is None :
552548 if engine is None :
553549 engine = 'scipy'
554- elif engine is not None :
550+ elif engine != 'scipy' :
555551 raise ValueError ('invalid engine for creating bytes with '
556552 'to_netcdf: %r. Only the default engine '
557553 "or engine='scipy' is supported" % engine )
558- else :
554+ elif isinstance ( path_or_file , basestring ) :
559555 if engine is None :
560- engine = _get_default_engine (path )
561- path = _normalize_path (path )
556+ engine = _get_default_engine (path_or_file )
557+ path_or_file = _normalize_path (path_or_file )
558+ else : # file-like object
559+ engine = 'scipy'
562560
563561 # validate Dataset keys, DataArray names, and attr keys/values
564562 _validate_dataset_names (dataset )
@@ -575,17 +573,18 @@ def to_netcdf(dataset, path=None, mode='w', format=None, group=None,
575573 # if a writer is provided, store asynchronously
576574 sync = writer is None
577575
578- store = store_cls (path , mode , format , group , writer )
576+ target = path_or_file if path_or_file is not None else BytesIO ()
577+ store = store_cls (target , mode , format , group , writer )
579578
580579 if unlimited_dims is None :
581580 unlimited_dims = dataset .encoding .get ('unlimited_dims' , None )
582581 try :
583582 dataset .dump_to_store (store , sync = sync , encoding = encoding ,
584583 unlimited_dims = unlimited_dims )
585- if isinstance ( path , BytesIO ) :
586- return path .getvalue ()
584+ if path_or_file is None :
585+ return target .getvalue ()
587586 finally :
588- if sync :
587+ if sync and isinstance ( path_or_file , basestring ) :
589588 store .close ()
590589
591590 if not sync :
0 commit comments