def clone(src, dst_path, skip_globals, skip_dimensions, skip_variables):
if os.path.exists(dst_path):<EOL><INDENT>os.unlink(dst_path)<EOL><DEDENT>dst = netCDF4.Dataset(dst_path, '<STR_LIT:w>')<EOL>for attname in src.ncattrs():<EOL><INDENT>if attname not in skip_globals:<EOL><INDENT>setattr(dst, attname, getattr(src, attname))<EOL><DEDENT><DEDENT>unlimdim     = None<EOL>unlimdimname = False<EOL>for dimname, dim in src.dimensions.items():<EOL><INDENT>if dimname in skip_dimensions:<EOL><INDENT>continue<EOL><DEDENT>if dim.isunlimited():<EOL><INDENT>unlimdim     = dim<EOL>unlimdimname = dimname<EOL>dst.createDimension(dimname, None)<EOL><DEDENT>else:<EOL><INDENT>dst.createDimension(dimname, len(dim))<EOL><DEDENT><DEDENT>for varname, ncvar in src.variables.items():<EOL><INDENT>if varname in skip_variables:<EOL><INDENT>continue<EOL><DEDENT>hasunlimdim = False<EOL>if unlimdimname and unlimdimname in ncvar.dimensions:<EOL><INDENT>hasunlimdim = True<EOL><DEDENT>filler = None<EOL>if hasattr(ncvar, '<STR_LIT>'):<EOL><INDENT>filler = ncvar._FillValue<EOL><DEDENT>if ncvar.chunking == "<STR_LIT>":<EOL><INDENT>var = dst.createVariable(varname, ncvar.dtype, ncvar.dimensions, fill_value=filler)<EOL><DEDENT>else:<EOL><INDENT>var = dst.createVariable(varname, ncvar.dtype, ncvar.dimensions, fill_value=filler, chunksizes=ncvar.chunking())<EOL><DEDENT>for attname in ncvar.ncattrs():<EOL><INDENT>if attname == '<STR_LIT>':<EOL><INDENT>continue<EOL><DEDENT>else:<EOL><INDENT>setattr(var, attname, getattr(ncvar, attname))<EOL><DEDENT><DEDENT>nchunk = <NUM_LIT:1000><EOL>if hasunlimdim:<EOL><INDENT>if nchunk:<EOL><INDENT>start = <NUM_LIT:0><EOL>stop = len(unlimdim)<EOL>step = nchunk<EOL>if step < <NUM_LIT:1>:<EOL><INDENT>step = <NUM_LIT:1><EOL><DEDENT>for n in range(start, stop, step):<EOL><INDENT>nmax = n + nchunk<EOL>if nmax > len(unlimdim):<EOL><INDENT>nmax = len(unlimdim)<EOL><DEDENT>idata = ncvar[n:nmax]<EOL>var[n:nmax] = idata<EOL><DEDENT><DEDENT>else:<EOL><INDENT>idata = ncvar[:]<EOL>var[<NUM_LIT:0>:len(unlimdim)] = idata<EOL><DEDENT><DEDENT>else:<EOL><INDENT>idata = ncvar[:]<EOL>var[:] = idata<EOL><DEDENT>dst.sync()<EOL><DEDENT>src.close()<EOL>dst.close()<EOL>
Mostly ripped from nc3tonc4 in netCDF4-python.
Added ability to skip dimension and variables.
Removed all of the unpacking logic for shorts.