Пример #1
0
def sha(ens):
    """ returns a dictionary linking the name of all
        the file in the ensemble and the SHA1# of
        those files
    Parameters
    ----------
    ens : Datanode
    
    Return
    ------
    dictionary of strings
    """        
    def hashfile(array):
        sha1 = hashlib.sha1(array)
        return sha1.hexdigest()
    data = {}
    for f in ens.objects('ncfile'):
         data[f.name] = hashfile(cdo.readMaArray(f.name, varname=f.parent.name))
    return data  
Пример #2
0
def sha(ens):
    """ returns a dictionary linking the name of all
        the file in the ensemble and the SHA1# of
        those files
    Parameters
    ----------
    ens : Datanode
    
    Return
    ------
    dictionary of strings
    """
    def hashfile(array):
        sha1 = hashlib.sha1(array)
        return sha1.hexdigest()

    data = {}
    for f in ens.objects('ncfile'):
        data[f.name] = hashfile(cdo.readMaArray(f.name, varname=f.parent.name))
    return data
Пример #3
0
def loadvar(ifile, varname, cdostr=None, **kwargs):
    """
        Load variables from a NetCDF file with optional pre-processing.

        Load a CMIP5 netcdf variable "varname" from "ifile" and an optional
        cdo string for preprocessing the data from the netCDF files.
        Requires netCDF4, CDO and CDO python bindings.
        Returns a masked array, var.
      """
    # Open the variable using NetCDF4 to get scale and offset attributes.
    nc = Dataset(ifile, 'r')
    ncvar = nc.variables[varname]

    # apply cdo string if it exists
    if (cdostr):
        opslist = cdostr.split()
        base_op = opslist[0].replace('-', '')
        if len(opslist) > 1:
            ops_str = ' '.join(opslist[1::]) + ' ' + ifile
            var = getattr(cdo, base_op)(input=ops_str, returnMaArray=varname)
        else:
            var = getattr(cdo, base_op)(input=ifile, returnMaArray=varname)

    else:
        var = cdo.readMaArray(ifile, varname=varname)

    # Apply any scaling and offsetting needed:
    try:
        var_offset = ncvar.add_offset
    except:
        var_offset = 0
    try:
        var_scale = ncvar.scale_factor
    except:
        var_scale = 1

    # var = var*var_scale + var_offset
    # return var
    return np.squeeze(var)
Пример #4
0
def loadvar(ifile, varname, cdostr=None, **kwargs):
    """
        Load variables from a NetCDF file with optional pre-processing.

        Load a CMIP5 netcdf variable "varname" from "ifile" and an optional
        cdo string for preprocessing the data from the netCDF files.
        Requires netCDF4, CDO and CDO python bindings.
        Returns a masked array, var.
      """
    # Open the variable using NetCDF4 to get scale and offset attributes.
    nc = Dataset(ifile, 'r')
    ncvar = nc.variables[varname]
    
    # apply cdo string if it exists
    if(cdostr):
        opslist = cdostr.split()
        base_op = opslist[0].replace('-', '')
        if len(opslist) > 1:
            ops_str = ' '.join(opslist[1::]) + ' ' + ifile
            var = getattr(cdo, base_op)(input=ops_str, returnMaArray=varname)
        else:
            var = getattr(cdo, base_op)(input=ifile, returnMaArray=varname)

    else:
        var = cdo.readMaArray(ifile, varname=varname)

    # Apply any scaling and offsetting needed:
    try:
        var_offset = ncvar.add_offset
    except:
        var_offset = 0
    try:
        var_scale = ncvar.scale_factor
    except:
        var_scale = 1

    # var = var*var_scale + var_offset
    # return var
    return np.squeeze(var)