(filename4,filename3,clobber=False,nchunk=10,quiet=False,format='NETCDF3_64BIT')
convert a netcdf 4 file (filename4) in NETCDF4_CLASSIC format
    to a netcdf 3 file (filename3) in NETCDF3_64BIT format.
convert a netcdf 4 file (filename4) in NETCDF4_CLASSIC format
    to a netcdf 3 file (filename3) in NETCDF3_64BIT format.
convert a netcdf 4 file ( filename4 ) in NETCDF4_CLASSIC format to a netcdf 3 file ( filename3 ) in NETCDF3_64BIT format .
def _nc4tonc3(filename4,filename3,clobber=False,nchunk=10,quiet=False,format='NETCDF3_64BIT'):
    """convert a netcdf 4 file (filename4) in NETCDF4_CLASSIC format
    to a netcdf 3 file (filename3) in NETCDF3_64BIT format."""

    from netCDF4 import Dataset

    ncfile4 = Dataset(filename4,'r')
    if ncfile4.file_format != 'NETCDF4_CLASSIC':
        raise IOError('input file must be in NETCDF4_CLASSIC format')
    ncfile3 = Dataset(filename3,'w',clobber=clobber,format=format)
    # create dimensions. Check for unlimited dim.
    unlimdimname = False
    unlimdim = None
    # create global attributes.
    if not quiet: sys.stdout.write('copying global attributes ..\n')
    #for attname in ncfile4.ncattrs():
    #    setattr(ncfile3,attname,getattr(ncfile4,attname))
    ncfile3.setncatts(ncfile4.__dict__)
    if not quiet: sys.stdout.write('copying dimensions ..\n')
    for dimname,dim in ncfile4.dimensions.items():
        if dim.isunlimited():
            unlimdimname = dimname
            unlimdim = dim
            ncfile3.createDimension(dimname,None)
        else:
            ncfile3.createDimension(dimname,len(dim))
    # create variables.
    for varname,ncvar in ncfile4.variables.items():
        if not quiet:
            sys.stdout.write('copying variable %s\n' % varname)
        # is there an unlimited dimension?
        if unlimdimname and unlimdimname in ncvar.dimensions:
            hasunlimdim = True
        else:
            hasunlimdim = False
        if hasattr(ncvar, '_FillValue'):
            FillValue = ncvar._FillValue
        else:
            FillValue = None
        var = ncfile3.createVariable(varname,ncvar.dtype,ncvar.dimensions,fill_value=FillValue)
        # fill variable attributes.
        attdict = ncvar.__dict__
        if '_FillValue' in attdict:
            del attdict['_FillValue']
        var.setncatts(attdict)
        #for attname in ncvar.ncattrs():
        #    if attname == '_FillValue': continue
        #    setattr(var,attname,getattr(ncvar,attname))
        # fill variables with data.
        if hasunlimdim: # has an unlim dim, loop over unlim dim index.
            # range to copy
            if nchunk:
                start = 0; stop = len(unlimdim); step = nchunk
                if step < 1:
                    step = 1
                for n in range(start, stop, step):
                    nmax = n+nchunk
                    if nmax > len(unlimdim):
                        nmax=len(unlimdim)
                    var[n:nmax] = ncvar[n:nmax]
            else:
                var[0:len(unlimdim)] = ncvar[:]
        else: # no unlim dim or 1-d variable, just copy all data at once.
            var[:] = ncvar[:]
        ncfile3.sync() # flush data to disk
    # close files.
    ncfile3.close()
    ncfile4.close()
def _nc4tonc3 ( filename4 , filename3 , clobber = False , nchunk = 10 , quiet = False , format = 'NETCDF3_64BIT' ) : from netCDF4 import Dataset ncfile4 = Dataset ( filename4 , 'r' ) if ncfile4 . file_format != 'NETCDF4_CLASSIC' : raise IOError ( 'input file must be in NETCDF4_CLASSIC format' ) ncfile3 = Dataset ( filename3 , 'w' , clobber = clobber , format = format ) # create dimensions. Check for unlimited dim. unlimdimname = False unlimdim = None # create global attributes. if not quiet : sys . stdout . write ( 'copying global attributes ..\n' ) #for attname in ncfile4.ncattrs(): #    setattr(ncfile3,attname,getattr(ncfile4,attname)) ncfile3 . setncatts ( ncfile4 . __dict__ ) if not quiet : sys . stdout . write ( 'copying dimensions ..\n' ) for dimname , dim in ncfile4 . dimensions . items ( ) : if dim . isunlimited ( ) : unlimdimname = dimname unlimdim = dim ncfile3 . createDimension ( dimname , None ) else : ncfile3 . createDimension ( dimname , len ( dim ) ) # create variables. for varname , ncvar in ncfile4 . variables . items ( ) : if not quiet : sys . stdout . write ( 'copying variable %s\n' % varname ) # is there an unlimited dimension? if unlimdimname and unlimdimname in ncvar . dimensions : hasunlimdim = True else : hasunlimdim = False if hasattr ( ncvar , '_FillValue' ) : FillValue = ncvar . _FillValue else : FillValue = None var = ncfile3 . createVariable ( varname , ncvar . dtype , ncvar . dimensions , fill_value = FillValue ) # fill variable attributes. attdict = ncvar . __dict__ if '_FillValue' in attdict : del attdict [ '_FillValue' ] var . setncatts ( attdict ) #for attname in ncvar.ncattrs(): #    if attname == '_FillValue': continue #    setattr(var,attname,getattr(ncvar,attname)) # fill variables with data. if hasunlimdim : # has an unlim dim, loop over unlim dim index. # range to copy if nchunk : start = 0 stop = len ( unlimdim ) step = nchunk if step < 1 : step = 1 for n in range ( start , stop , step ) : nmax = n + nchunk if nmax > len ( unlimdim ) : nmax = len ( unlimdim ) var [ n : nmax ] = ncvar [ n : nmax ] else : var [ 0 : len ( unlimdim ) ] = ncvar [ : ] else : # no unlim dim or 1-d variable, just copy all data at once. var [ : ] = ncvar [ : ] ncfile3 . sync ( ) # flush data to disk # close files. ncfile3 . close ( ) ncfile4 . close ( )