• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python netCDF4.nc函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中netCDF4.nc函数的典型用法代码示例。如果您正苦于以下问题:Python nc函数的具体用法?Python nc怎么用?Python nc使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了nc函数的17个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: post

    def post(self, request, *args, **kwargs):
        dataset = self.form_class(request.POST, request.FILES)
        if dataset.is_valid():
            ds = dataset.save()

            # add attribute variables to dataset
            attr_data = nc(ds.data_file.path, 'r')
            variables = list(attr_data.variables.keys())
            for var in variables:
                shape = attr_data.variables[var].shape
                if len(shape) < 2:
                    shape = [shape[0], shape[0]]
                variable = Variable(name=var,
                                    long_name=attr_data.variables[var].long_name,
                                    dataset=ds,
                                    x_dimension=shape[0],
                                    y_dimension=shape[1]
                                    )
                variable.save()
            attr_data.close()

            # add elevation to dataset
            elev_data = nc(ds.elev_file.path, 'r')
            elev_shape = elev_data.variables['elev'].shape
            elev_variable = Variable(name='elev', dataset=ds, x_dimension=elev_shape[0], y_dimension=elev_shape[1])
            elev_variable.save()
            elev_data.close()
            return redirect('/explore/' + str(ds.id) + '/')
        else:
            return render(request, self.template_name, {'form': self.form_class()})
开发者ID:TaylorMutch,项目名称:EEMS3D,代码行数:30,代码来源:views.py


示例2: __init__

    def __init__(self, smapfile, areafile):
        with nc(smapfile) as f:
            self.smap = f.variables['state'][:]
            self.cmap = f.variables['county'][:]
        self.states = unique(self.smap)
        self.states = self.states[~self.states.mask]

        with nc(areafile) as f:
            self.acounties = f.variables['county'][:]
            self.area      = f.variables['sum_county'][:]
开发者ID:RDCEP,项目名称:1896,代码行数:10,代码来源:CropProgressCountyAdapter.py


示例3: get

    def get(self, request, *args, **kwargs):
        layer = str(kwargs['layer'])
        dataset = Dataset.objects.get(pk=kwargs['dataset'])
        response = dict()
        variables = Variable.objects.filter(dataset=dataset)
        found = False
        for variable in variables:
            if variable.name == layer:
                layer = variable.name
                found = True
                break

        if not found:
            return JsonResponse({layer: 'False'})

        if layer == 'elev' and dataset.has_elev_file:
            path = dataset.elev_file.path
        else:
            path = dataset.data_file.path

        with nc(path, 'r') as ds:

            var = ds.variables[layer][:]

            minimum = float(var.min())
            maximum = float(var.max())
            x = var.shape[1]
            y = var.shape[0]
            fill_value = str(ds.variables[layer]._FillValue)
            response[layer] = {'min': minimum, 'max': maximum, 'x':float(x), 'y':float(y), 'fill_value' : fill_value}

        return JsonResponse(response)
开发者ID:TaylorMutch,项目名称:EEMS3D,代码行数:32,代码来源:views.py


示例4: createAggFile

def createAggFile(filename, time, tunits, adata, anames, aunits, alongnames, scens, irr, leaddim, hasscen):
    if leaddim == 'scen':
        nscens = None
        ntime  = len(time)
    else:
        nscens = len(scens) if hasscen else 0
        ntime  = None

    with nc(filename, 'w', format = 'NETCDF4_CLASSIC') as f:
        f.createDimension('time', ntime)
        timevar = f.createVariable('time', 'i4', 'time')
        timevar[:] = time
        timevar.units = tunits
        timevar.long_name = 'time'

        if hasscen:
            f.createDimension('scen', nscens)
            scenvar = f.createVariable('scen', 'i4', 'scen')
            scenvar[:] = scens
            scenvar.units = 'no'
            scenvar.long_name = 'scenarios'

        f.createDimension('irr', len(irr) + 1)
        irrvar = f.createVariable('irr', 'i4', 'irr')
        irrvar[:] = range(1, len(irr) + 2)
        irrvar.units = 'mapping'
        irrvar.long_name = ', '.join(irr + ['sum'])

        for i in range(len(anames)):
            rname = str(anames[i])
            f.createDimension(rname, len(adata[i]))
            rvar = f.createVariable(rname, 'i4', rname)
            rvar[:] = adata[i]
            rvar.units = aunits[i]
            rvar.long_name = alongnames[i]
开发者ID:RDCEP,项目名称:ggcmi,代码行数:35,代码来源:agg.out.py


示例5: loadData

def loadData(file):
    with nc(file) as f:
        var = array(f.variables.keys())
        idx = array([v != 'lon' and v != 'lat' and v != 'time' for v in var])
        varname = var[idx][0]
        data = f.variables[varname][:]
    return data, varname
开发者ID:RDCEP,项目名称:ggcmi,代码行数:7,代码来源:ggcmi.corrector.py


示例6: get

    def get(self, request, *args, **kwargs):
        y_start = int(kwargs['y'])
        y_end = y_start + EEMS_TILE_SIZE[0]
        x_start = int(kwargs['x'])
        x_end = x_start + EEMS_TILE_SIZE[1]
        layer = 'elev'
        response = dict()

        with nc(elev_path, 'r') as ds:
            var = ds.variables[layer][:]

            if x_start > var.shape[1] or y_start > var.shape[0]:
                return JsonResponse({layer: 'False'})

            x_end = var.shape[1] if x_end > var.shape[1] else x_end
            y_end = var.shape[0] if y_end > var.shape[0] else y_end
            response['fill_value'] = str(ds.variables[layer]._FillValue)
            if isinstance(var, np.ma.core.MaskedArray):
                sub_matrix = var.data[y_start:y_end, x_start:x_end]
                response[layer] = sub_matrix.ravel().tolist()
                response['x'] = sub_matrix.shape[1]
                response['y'] = sub_matrix.shape[0]
            else:
                sub_matrix = var[y_start:y_end, x_start:x_end]
                response[layer] = sub_matrix.ravel().tolist()
                response['x'] = sub_matrix.shape[1]
                response['y'] = sub_matrix.shape[0]

        return JsonResponse(response)
开发者ID:TaylorMutch,项目名称:EEMS3D,代码行数:29,代码来源:focalviews.py


示例7: createAggFile

def createAggFile(filename, time, tunits, adata, anames, aunits, alongnames, scens, leaddim):
    if leaddim == 'scen':
        nscens = None
        ntime  = len(time)
    else:
        nscens = len(scens)
        ntime  = None

    with nc(filename, 'w', format = 'NETCDF4_CLASSIC') as f:
        f.createDimension('time', ntime)
        timevar = f.createVariable('time', 'i4', 'time')
        timevar[:] = time
        timevar.units = tunits
        timevar.long_name = 'time'
        f.createDimension('scen', nscens)
        scenvar = f.createVariable('scen', 'i4', 'scen')
        scenvar[:] = scens
        scenvar.units = 'no'
        scenvar.long_name = 'scenarios'
        for i in range(len(anames)):
            rname = anames[i] + '_index'
            f.createDimension(rname, len(adata[i]))
            rvar = f.createVariable(rname, 'i4', rname)
            rvar[:] = adata[i]
            rvar.units = aunits[i]
            rvar.long_name = alongnames[i]
开发者ID:AHCG,项目名称:psims,代码行数:26,代码来源:agg.out.noirr.py


示例8: __loadwts

    def __loadwts(self, var, vals):
        if self.wfile is None:
            return ones(len(vals))
        else:
            vars  = var.split('/')
            nvars = len(vars)

            v = [0] * nvars
            w = [0] * nvars
            with nc(self.wfile) as f:
                for i in range(nvars):
                    if f.variables[vars[i]].units == 'mapping':
                        v[i] = array(f.variables[vars[i]].long_name.split(', '))
                    else:
                        v[i] = f.variables[vars[i]][:]
                    w[i] = f.variables['weights_' + vars[i]][:]

            nvals = len(vals)
            wts = masked_array(zeros(nvals), mask = ones(nvals))
            for i in range(nvals):
                svals = vals[i].split('/')
                for j in range(nvars):
                    if svals[j].isdigit():
                        svals[j] = double(svals[j]) # convert to number
                    idx = where(v[j] == svals[j])[0]
                    if idx.size:
                        if isMaskedArray(wts[i]):
                            wts[i] = w[j][idx[0]]
                        else:
                            wts[i] *= w[j][idx[0]]

            return wts
开发者ID:RDCEP,项目名称:ggcmi,代码行数:32,代码来源:mmplotter.py


示例9: __init__

    def __init__(self, filename, time, tunits, scens, aggs, aggname, aggunits, agglongname):
        super(AggregationFile, self).__init__(filename)

        with nc(filename, 'w', format = 'NETCDF4_CLASSIC') as f:
            f.createDimension(aggname, len(aggs)) # create aggregation level
            aggsvar = f.createVariable(aggname, 'i4', aggname)
            aggsvar[:] = aggs
            aggsvar.units = aggunits
            aggsvar.long_name = agglongname

            f.createDimension('time', len(time)) # create time
            timevar = f.createVariable('time', 'i4', 'time')
            timevar[:] = time
            timevar.units = tunits
            timevar.long_name = 'time'

            f.createDimension('scen', len(scens)) # create scen
            scenvar = f.createVariable('scen', 'i4', 'scen')
            scenvar[:] = range(1, len(scens) + 1)
            scenvar.units = 'mapping'
            scenvar.long_name = ', '.join(scens)

            f.createDimension('irr', 3) # create irr
            irrvar = f.createVariable('irr', 'i4', 'irr')
            irrvar[:] = range(1, 4)
            irrvar.units = 'mapping'
            irrvar.long_name = 'ir, rf, sum'
开发者ID:RDCEP,项目名称:ggcmi,代码行数:27,代码来源:filespecs.py


示例10: createAggFile

def createAggFile(filename, time, tunits, adata, anames, aunits, alongnames, scens, irr, leaddim):
    if leaddim == "scen":
        nscens = None
        ntime = len(time)
    else:
        nscens = len(scens)
        ntime = None

    with nc(filename, "w", format="NETCDF4_CLASSIC") as f:
        f.createDimension("time", ntime)
        timevar = f.createVariable("time", "i4", "time")
        timevar[:] = time
        timevar.units = tunits
        timevar.long_name = "time"
        f.createDimension("scen", nscens)
        scenvar = f.createVariable("scen", "i4", "scen")
        scenvar[:] = scens
        scenvar.units = "no"
        scenvar.long_name = "scenarios"
        f.createDimension("irr", len(irr) + 1)
        irrvar = f.createVariable("irr", "i4", "irr")
        irrvar[:] = range(1, len(irr) + 2)
        irrvar.units = "mapping"
        irrvar.long_name = ", ".join(irr + ["sum"])
        for i in range(len(anames)):
            rname = anames[i] + "_index"
            f.createDimension(rname, len(adata[i]))
            rvar = f.createVariable(rname, "i4", rname)
            rvar[:] = adata[i]
            rvar.units = aunits[i]
            rvar.long_name = alongnames[i]
开发者ID:wande001,项目名称:psims,代码行数:31,代码来源:agg.out.py


示例11: __init__

 def __init__(self, filename, outdir, fmt):
     self.filename = filename
     self.outdir = outdir
     self.fmt = fmt
     with nc(filename) as f:
         self.varkeys = f.variables.keys()  # get available variables
         self.scennames = [s.strip(" ") for s in f.variables["scen"].long_name.split(",")]  # get scenario names
         self.irr = f.variables["irr"][:]  # get irrigation values and names
         self.irrnames = [i.strip(" ") for i in f.variables["irr"].long_name.split(",")]
         self.time = f.variables["time"][:]  # get time and units
         self.tunits = f.variables["time"].units
开发者ID:RDCEP,项目名称:ggcmi,代码行数:11,代码来源:aggplot.par.py


示例12: __init__

    def __init__(self, file, vars = None):
        with nc(file) as f:
            if vars is None:
                vars = setdiff1d(f.variables, ['latitude', 'longitude', 'time', 'ftime', 'scen'])
                newvars = list(vars.copy())
                for v in vars:
                    if compile('missing_*').match(v): # remove missing_* variables
                        newvars.remove(v)
                vars = array(newvars)

            self.lat, self.lon = f.variables['latitude'][0], f.variables['longitude'][0]

            self.scen  = f.variables['scen'][:]
            self.time  = f.variables['time'][:]
            self.ftime = f.variables['ftime'][:]            

            tunits = f.variables['time'].units
            ts = tunits.split('days since ')[1].split(' ')
            yr0, mth0, day0 = [int(t) for t in ts[0].split('-')[0 : 3]]
            if len(ts) > 1:
                hr0, min0, sec0 = [int(t) for t in ts[1].split(':')[0 : 3]]
            else:
                hr0 = min0 = sec0 = 0
            self.reftime = datetime(yr0, mth0, day0, hr0, min0, sec0)

            nv, ns, nt, nf = len(vars), len(self.scen), len(self.time), len(self.ftime)
            self.data      = masked_array(zeros((nv, ns, nt, nf)), mask = ones((nv, ns, nt, nf)))
            self.missing   = zeros((nv, ns, nt))
            self.units     = zeros(nv, dtype = '|S64')
            self.longnames = zeros(nv, dtype = '|S64')
            for i in range(nv):
                if vars[i] in f.variables:
                    var = f.variables[vars[i]]
                    msg = f.variables['missing_' + vars[i]]
                else:
                    vidx = foundVar(f.variables.keys(), vars[i])
                    var  = f.variables[f.variables.keys()[vidx]]
                    msg  = f.variables['missing_' + f.variables.keys()[vidx]]
                self.data[i] = var[:, :, :, 0, 0]
                self.missing[i] = msg[:]
                self.units[i] = var.units
                self.longnames[i] = var.long_name

            self.vars = vars # store variable names

            self.pridx = foundVar(vars, 'pr') # variable indices
            self.maidx = foundVar(vars, 'tmax')
            self.miidx = foundVar(vars, 'tmin')

        self.__fillMissing()
开发者ID:AHCG,项目名称:psims,代码行数:50,代码来源:pSIMSloader.py


示例13: createAggFile

def createAggFile(filename, time, tunits, adata, anames, aunits, alongnames):
    with nc(filename, 'w', format = 'NETCDF4_CLASSIC') as f:
        f.createDimension('time', len(time))
        timevar = f.createVariable('time', 'i4', ('time',))
        timevar[:] = time
        timevar.units = tunits
        timevar.long_name = 'time'
        for i in range(len(anames)):
            rname = anames[i] + '_index'
            f.createDimension(rname, len(adata[i]))
            rvar = f.createVariable(rname, 'i4', (rname,))
            rvar[:] = adata[i]
            rvar.units = aunits[i]
            rvar.long_name = alongnames[i]
开发者ID:RDCEP,项目名称:ggcmi,代码行数:14,代码来源:agg.single.py


示例14: __init__

    def __init__(self, path, layer_name):

        # parse .xtr file
        with nc(path, 'r') as ds:

            self.lats = ds.variables['lat'][:]
            self.lngs = ds.variables['lon'][:]
            self.numlngs = int(self.lngs.size)
            self.numlats = int(self.lats.size)
            self.minlng = float(self.lngs[0])
            self.maxlng = float(self.lngs[-1])
            self.minlat = float(self.lats[0])
            self.maxlat = float(self.lats[-1])
            self.data = ds.variables[layer_name][:]
            self.dtype = self.data.data.dtype
            self.ncattrs = {k: ds.variables[layer_name].getncattr(k) for k in ds.variables[layer_name].ncattrs()}
开发者ID:TaylorMutch,项目名称:EEMS3D,代码行数:16,代码来源:focalviews.py


示例15: __init__

    def __init__(self, filename, varnames = None, lats = None, lons = None, incl_global = False):
        f = nc(filename) # open file

        if varnames is None: # no variables specified
            varnames = f.variables.keys()
            varnames = [v for v in varnames if not v in ['lat', 'lon']] # remove lat, lon
            if incl_global: varnames += ['global']
        else:
            if not isinstance(varnames, list): # make list
                varnames = [varnames]

        self.lats, self.lons = f.variables['lat'][:], f.variables['lon'][:]

        self.dat = {'names': [], 'units': [], 'longnames': [], 'data': []}

        for v in varnames:
            if v != 'global' or (v == 'global' and 'global' in f.variables):
                var = f.variables[v]
                self.dat['names'].append(v)
                self.dat['units'].append(var.units if 'units' in var.ncattrs() else '')
                self.dat['longnames'].append(var.long_name if 'long_name' in var.ncattrs() else '')
                self.dat['data'].append(var[:])
            else:
                nlats = self.lats.size
                nlons = self.lons.size

                self.dat['names'].append('global') # global mask
                self.dat['units'].append('')
                self.dat['longnames'].append('')
                self.dat['data'].append(masked_array(ones((nlats, nlons)), mask = zeros((nlats, nlons))))

        f.close()

        tol = 1e-5
        if not lats is None: # restrict latitude range
            sellat = logical_and(self.lats >= lats.min() - tol, self.lats <= lats.max() + tol)
            self.lats = self.lats[sellat]
            for i in range(len(self.dat['names'])):
                self.dat['data'][i] = self.dat['data'][i][sellat]
        if not lons is None: # restrict longitude range
            sellon = logical_and(self.lons >= lons.min() - tol, self.lons <= lons.max() + tol)
            self.lons = self.lons[sellon]
            for i in range(len(self.dat['names'])):
                self.dat['data'][i] = self.dat['data'][i][:, sellon]
开发者ID:RDCEP,项目名称:ggcmi,代码行数:44,代码来源:aggmaskloader.py


示例16: __init__

 def __init__(self, filename, meta, mask, outdir, fmt):
     super(MaskPlotter, self).__init__(filename, outdir, fmt)
     with nc(self.filename) as f:
         self.area = f.variables['area_' + mask][:]
         self.indices = f.variables[mask + '_index'][:]
     metadata = [] # load meta data
     with open(meta, 'rU') as f:
         for row in csv.reader(f, delimiter = '\t'):
             metadata.append(row)
     self.metadic = {}
     for i in range(len(metadata)):
         md = metadata[i][0].split(',')
         self.metadic[md[0]] = md[1]
     filesplit = filename.split('_') # get model, climate, crop
     self.model = filesplit[0]
     self.climate = filesplit[1]
     self.crop = filesplit[3]
     totarea = self.area[:, 0, 0, :].sum(axis = 1)
     self.sidx = [a[0] for a in sorted(enumerate(totarea), key = lambda x: x[1], reverse = True)]
     self.mask = mask
开发者ID:RDCEP,项目名称:ggcmi,代码行数:20,代码来源:aggplot.py


示例17: createnc

def createnc(filename, lat, lon, time, time_units):
    # create file
    f = nc(filename, 'w', format = 'NETCDF4_CLASSIC')
    # create longitude
    f.createDimension('lon', len(lon))
    lon_var = f.createVariable('lon', 'f8', ('lon',), zlib = True, shuffle = False, complevel = 9, chunksizes = [len(lon)])
    lon_var[:] = lon
    lon_var.units = 'degrees_east'
    lon_var.long_name = 'longitude'  
    # create latitude
    f.createDimension('lat', len(lat))
    lat_var = f.createVariable('lat', 'f8', ('lat',), zlib = True, shuffle = False, complevel = 9, chunksizes = [len(lat)])
    lat_var[:] = lat
    lat_var.units = 'degrees_north'
    lat_var.long_name = 'latitude'
    # create time
    f.createDimension('time', None)
    time_var = f.createVariable('time', 'f8', ('time',), zlib = True, shuffle = False, complevel = 9, chunksizes = [1], endian = 'little')
    time_var[:] = time
    time_var.units = time_units
    time_var.long_name = 'time'
    # close file
    f.close()
开发者ID:glotter,项目名称:psims,代码行数:23,代码来源:out2GGCMI.py



注:本文中的netCDF4.nc函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python netCDF4.num2date函数代码示例发布时间:2022-05-27
下一篇:
Python netCDF4.date2num函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap