예제 #1
0
def prepare_netcdf():
    filename = sys.argv[1]
    magnitude,direction = sys.argv[2].split(',')

    if nc.__module__ == 'pupynere':
        raise Exception, "Pupynere cannot open netcdf files in append mode. Please install either PyNIO, netCDF4, Scientific.IO.NetCDF or pynetcdf."

    f = nc(filename, 'a')

    m = f.variables[magnitude]
    d = f.variables[direction]

    # Create vector components
    u = m[:]*np.ma.sin(d[:])
    v = m[:]*np.ma.cos(d[:])

    name = m.long_name.lower()
    f.renameVariable(direction, 'u')
    uvar = f.variables['u']
    del uvar.standard_name
    uvar.long_name = "zonal " + name
    uvar.units = m.units
    f.renameVariable(magnitude, 'v')
    vvar = f.variables['v']
    del vvar.standard_name
    vvar.long_name = "meridional " + name
    vvar.units = uvar.units
    f.close()
예제 #2
0
def prepare_netcdf():
    filename = sys.argv[1]
    uname,vname = sys.argv[2].split(',')

    if nc.__module__ == 'pupynere':
        raise Exception, "Pupynere cannot open netcdf files in append mode. Please install either PyNIO, netCDF4, Scientific.IO.NetCDF or pynetcdf."

    f = nc(filename, 'a')

    # create new magnitude variable
    whitelist = ['long_name', 'units', '_FillValue', 'missing_value']
    u = f.variables[uname]
    v = f.variables[vname]
    try:
        fill_value = u.missing_value
    except:
        fill_value = u._FillValue
    uv = np.ma.sqrt(u[:]**2 + v[:]**2)
    uvname = "%s_%s" % (uname, vname)
    uvf = f.createVariable(uvname, u.dtype, u.dimensions, fill_value=fill_value)
    uvf[:] = uv
    for attr in u.ncattrs():
        if attr in whitelist:
            attr_value = u.getncattr(attr)
            if attr == 'long_name':
                attr_value = attr_value.split(' ', 1)[1]
            uvf.setncattr(attr, attr_value)
    f.close()
예제 #3
0
def prepare_netcdf():
    filename = sys.argv[1]

    if nc.__module__ == 'pupynere':
        raise Exception, "Pupynere cannot open netcdf files in append mode. Please install either PyNIO, netCDF4, Scientific.IO.NetCDF or pynetcdf."

    f = nc(filename, 'a')

    # set actual range
    for name, var in f.variables.items():
        if name in f.dimensions or hasattr(var, 'actual_range'): continue
        data = fix_data(numpy.asarray(var[:]), var_attrs(var)) 
        var.actual_range = numpy.amin(data), numpy.amax(data)

    f.close()
예제 #4
0
def prepare_netcdf():
    filename = sys.argv[1]

    if nc.__module__ == 'pupynere':
        raise Exception, "Pupynere cannot open netcdf files in append mode. Please install either PyNIO, netCDF4, Scientific.IO.NetCDF or pynetcdf."

    f = nc(filename, 'a')

    # set actual range
    for name, var in f.variables.items():
        if name in f.dimensions or hasattr(var, 'actual_range'): continue
        data = fix_data(np.asarray(var[:]), var_attrs(var)) 
        var.actual_range = np.amin(data), np.amax(data)

    # create bounds for 1d dimension variables
    for name, dim in f.dimensions.items():
        if name in f.variables:
            var = f.variables[name]
            if hasattr(var, 'bounds'): continue
            ndims = len(var.shape)
            # We currently only do this for 1d variables
            if ndims > 1: continue
            vert_dim = u'nv'
            if vert_dim not in f.dimensions.keys():
                nv = f.createDimension(vert_dim, 2*ndims)
            bnds_name = name + '_bnds'
            var.bounds = bnds_name
            bdims = var.dimensions + (vert_dim,)
            bounds = f.createVariable(bnds_name, var.dtype, bdims)
            dv = 0.5*abs(var[-1]-var[0])/(len(var)-1)
            lbounds = var[:] - dv
            ubounds = var[:] + dv
            bounds[:,0] = lbounds
            bounds[:,1] = ubounds

    f.close()