本文整理汇总了Python中scipy.io.netcdf_file函数的典型用法代码示例。如果您正苦于以下问题:Python netcdf_file函数的具体用法?Python netcdf_file怎么用?Python netcdf_file使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了netcdf_file函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load
def load(self,**kwargs):
""" Load Oscar fields for a given day"""
self._timeparams(**kwargs)
md = self.jd - pl.datestr2num('1992-10-05')
filename = os.path.join(self.datadir, "oscar_vel%i.nc" % self.yr)
if not os.path.exists(filename):
self.download(filename)
filenam2 = os.path.join(self.datadir, "oscar_vel%i.nc" % (self.yr+1))
if not os.path.exists(filenam2):
self.download(filenam2)
nc1 = netcdf_file(filename)
tvec = nc1.variables['time'][:]
t1 = int(np.nonzero((tvec<=md))[0].max())
print t1,max(tvec)
if t1<(len(tvec)-1):
nc2 = nc1
t2 = t1 +1
else:
nc2 = netcdf_file(filenam2)
t2 = 0
def readfld(ncvar):
return self.gmt.field(ncvar[t1, 0,:,:self.imt])[self.j1:self.j2,
self.i1:self.i2]
u1 = readfld(nc1.variables['u'])
v1 = readfld(nc1.variables['v'])
u2 = readfld(nc2.variables['u'])
v2 = readfld(nc2.variables['v'])
rat = float(md-tvec[t1])/float(tvec[t2]-tvec[t1])
self.u = u2*rat + u1*(1-rat)
self.v = v2*rat + v1*(1-rat)
print self.jd,md,t1,t2
示例2: load
def load(self, fld="nwnd", **kwargs):
"""Load field for a given julian date. Returns u,v, or nwnd(windnorm)"""
self._timeparams(**kwargs)
filename = os.path.join(self.datadir,
"uv%04i%02i%02i.nc" % (self.yr, self.mn, self.dy))
if not os.path.isfile(filename):
self.download(filename)
try:
nc = netcdf_file(filename)
except:
os.remove(filename)
self.download(filename)
try:
nc = netcdf_file(filename)
except:
filename = filename.rstrip(".nc") + "rt.nc"
if not os.path.isfile(filename):
self.download(filename)
try:
nc = netcdf_file(filename)
except TypeError:
os.remove(filename)
self.download(filename)
nc = netcdf_file(filename)
u = nc.variables['u'][:].copy()
v = nc.variables['v'][:].copy()
u[u<-999] = np.nan
v[v<-999] = np.nan
if (fld=="u") | (fld=="uvel"):
self.uvel = self.gmt.field(np.squeeze(u))
elif (fld=="v") | (fld=="vvel"):
self.vvel = self.gmt.field(np.squeeze(v))
else:
self.nwnd = self.gmt.field(np.squeeze(np.sqrt(u**2 + v**2)))
示例3: setup_grid
def setup_grid(self):
"""Setup necessary variables for grid """
g = netcdf_file(self.gridfile)
self.llat = g.variables['lat_rho'][:].copy()
self.llon = g.variables['lon_rho'][:].copy()
self.depth = g.variables['h'][:].copy()
self.Cs_r = np.array([-0.882485522505154, -0.778777844867132,
-0.687254423585503, -0.606483342183883,
-0.535200908367393, -0.472291883107274,
-0.416772032329648, -0.367772728223386,
-0.324527359249072, -0.286359336228826,
-0.252671506867986, -0.222936813095075,
-0.196690045050832, -0.173520562714503,
-0.153065871294677, -0.135005949869352,
-0.11905824454479, -0.104973247799366,
-0.0925305948496471, -0.0815356159649889,
-0.0718162907903607, -0.0632205570267136,
-0.0556139313622304, -0.0488774054330831,
-0.042905583895255, -0.0376050354769002,
-0.0328928312128658, -0.0286952469915389,
-0.0249466101148999, -0.021588271825806,
-0.0185676897273263, -0.0158376057382559,
-0.0133553067236276, -0.0110819562325242,
-0.00898198688799211, -0.00702254392277909,
-0.00517297115481568, -0.0034043313603439,
-0.00168895354075999, 0.])
示例4: load
def load(self,fldname, **kwargs):
""" Load velocity fields for a given day"""
if fldname == "uv":
self.load('u',jd=jd, yr=yr, mn=mn, dy=dy, hr=hr)
self.load('v',jd=jd, yr=yr, mn=mn, dy=dy, hr=hr)
self.uv = np.sqrt(self.u[:,1:,:]**2 + self.v[:,:,1:]**2)
return
self._timeparams(**kwargs)
tpos = np.nonzero(self.jdvec <= self.jd)[0].max()
vc = {'uvel': ['u', 'ecom.cdf', 9.155553e-05, 0],
'vvel': ['v', 'ecom.cdf', 9.155553e-05, 0],
'wvel': ['v', 'ecom.cdf', 6.103702e-08, 0],
'temp': ['temp', 'ecom.cdf', 0.0005340739, 12.5],
'salt': ['salt', 'ecom.cdf', 0.0006103702, 20],
'chlo': ['chl', 'bem_water.cdf', 0.001525925, 50],
'newp': ['np', 'bem_water.cdf', 0.001525925, 50],
'netp': ['pp', 'bem_water.cdf', 0.001525925, 50],
'tpoc': ['tpoc', 'bem_water.cdf', 0.01525925, 500],
}
try:
nc = netcdf_file(self.datadir + vc[fldname][1])
except KeyError:
raise KeyError, "%s is not included" % fldname
fld = nc.variables[vc[fldname][0]][tpos,:,
self.j1:self.j2,
self.i1:self.i2]
fld = (fld * vc[fldname][2] + vc[fldname][3]).astype(np.float32)
fld[:,self.landmask] = np.nan
self.__dict__[fldname] = fld
return tpos
示例5: get_data
def get_data(forecast_index, file_id):
datafile = DataFile.objects.get(pk=file_id)
file = netcdf_file(os.path.join(settings.MEDIA_ROOT, settings.WAVE_WATCH_DIR, datafile.file.name))
variable_names_in_file = file.variables.keys()
print variable_names_in_file
all_day_height = file.variables['HTSGW_surface'][:, :, :]
all_day_direction = file.variables['DIRPW_surface'][:,:,:]
all_day_lat = file.variables['latitude'][:, :]
all_day_long = file.variables['longitude'][:, :]
all_day_times = file.variables['time'][:]
#print "times: "
#for each in all_day_times:
#print each
basetime = datetime.datetime(1970,1,1,0,0,0)
# Check the first value of the forecast
forecast_zero = basetime + datetime.timedelta(all_day_times[0]/3600.0/24.0,0,0)
print(forecast_zero)
directions = all_day_direction[forecast_index, ::10, :]
directions_mod = 90.0 - directions + 180.0
index = directions_mod > 180
directions_mod[index] = directions_mod[index] - 360;
index = directions_mod < -180;
directions_mod[index] = directions_mod[index] + 360;
U = 10.*np.cos(np.deg2rad(directions_mod))
V = 10.*np.sin(np.deg2rad(directions_mod))
print "height:", all_day_height[:10, :10]
示例6: get_data
def get_data(file_id):
datafile = DataFile.objects.get(pk=file_id)
file = netcdf_file(os.path.join(settings.MEDIA_ROOT, settings.WAVE_WATCH_DIR, datafile.file.name))
variable_names_in_file = file.variables.keys()
print variable_names_in_file
# longs = [item for sublist in file.variables['longitude'][:1] for item in sublist]
# print "longs:"
# for each in longs:
# print each
# lats = file.variables['latitude'][:, 0]
# print "lats:"
# for each in lats:
# print each
all_day_height = file.variables['HTSGW_surface'][:, :, :]
all_day_lat = file.variables['latitude'][:, :]
all_day_long = file.variables['longitude'][:, :]
just_this_forecast_height = all_day_height[0][:1, :]
just_this_forecast_lat = all_day_lat[ :,0]
just_this_forecast_long= all_day_long[0][ :]
print "\n\n\nWAVE HEIGHTS "
for each in just_this_forecast_height:
print each
print "\n\n LATS:"
print just_this_forecast_lat
print "\n\n LONGS:"
print just_this_forecast_long
示例7: getVarData
def getVarData(fn,var):
def getMembers(tfh):
names = sorted(tfh.getnames())
names.remove('.')
names.remove('./input.cfg')
names.remove('./output.log')
return [tfh.getmember(n) for n in names]
def getPassCount(fl):
fh = netcdf_file(fl)
S = fh.variables['u'].shape
fh.close()
return S[0]
tfh = tarfile.open(fn)
members = getMembers(tfh)
Nf = len(members)
Np = getPassCount(tfh.extractfile(members[0]))
data = pl.empty( (Nf,Np) )
for k in range(len(members)):
fl = tfh.extractfile(members[k])
fh = netcdf_file(fl)
data[k,:] = fh.variables[var][:,0,0]
fh.close()
tfh.close()
return data
示例8: _default_cache_entry_factory
def _default_cache_entry_factory(self, key):
"""Called on a DataCache access __missing__() call.
Gets all (step, row, col) entries for the file indexed by key and reads all data
returning the entry for the requested key
Arguments:
key - (pixel_step, row, col) tuple
"""
# A cache miss will generate a file lookup, read and cache of the associated data.
path, _, _, _ = self._get_data_location(*key) # path of file containing our data
# Generate all the entries like the following for data in the file path
# {(step, row, col): [module_data object reference, 0-3], ... }
# First, enumerate data indices in current file.
indices = self._enumerate_all_data_indices_in_file(os.path.basename(path))
# OK, now read everything from the file
f = netcdf_file(path, 'r')
# buffer_ix, module_ix
for pixel_step, row, col, channel, buffer_ix, module_ix in indices:
self.module_data_cache[(pixel_step, row, col)] = \
[self._get_mode1_pixel_data(f, buffer_ix, module_ix), channel]
f.close()
return self.module_data_cache[key]
示例9: load
def load(self,fldname, **kwargs):
""" Load velocity fields for a given day"""
self._timeparams(**kwargs)
if fldname == "uv":
self.load('u',**kwargs)
self.load('v',**kwargs)
self.uv = np.sqrt(self.u[:,1:,:]**2 + self.v[:,:,1:]**2)
return
if self.opendap:
tpos = int(self.jd) - 714800
k1 = kwargs.get("k1", getattr(self, "k1", self.klev))
k2 = kwargs.get("k2", getattr(self, "k2", k1+1))
dapH = open_url(self.dapurl)
fld = dapH[fldname][tpos,k1:k2,self.j1:self.j2,self.i1:self.i2]
else:
filename = self.jd2filename(self.jd)
if not os.path.isfile(filename):
print "File missing"
url = urlparse.urljoin(self.dataurl, os.path.basename(filename))
self.retrive_file(url, filename)
with netcdf_file(filename) as nc:
fld = nc.variables[fldname][:].copy()
self.ssh = np.squeeze(nc.variables['zeta'][:])
self.zlev = ((self.depth + self.ssh)[np.newaxis,:,:] *
self.Cs_r[:,np.newaxis,np.newaxis])
fld[fld>9999] = np.nan
setattr(self, fldname, np.squeeze(fld))
示例10: read_woce_netcdf
def read_woce_netcdf(fnm):
""" Read a CTD cast from a WOCE NetCDF file. """
def getvariable(nc, key):
return nc.variables[key].data.copy()
nc = netcdf_file(fnm)
coords = (getvariable(nc, "longitude")[0], getvariable(nc, "latitude")[0])
pres = getvariable(nc, "pressure")
sal = getvariable(nc, "salinity")
salqc = getvariable(nc, "salinity_QC")
sal[salqc!=2] = np.nan
temp = getvariable(nc, "temperature")
# tempqc = getvariable(nc, "temperature_QC")
# temp[tempqc!=2] = np.nan
oxy = getvariable(nc, "oxygen")
oxyqc = getvariable(nc, "oxygen_QC")
oxy[oxyqc!=2] = np.nan
date = getvariable(nc, "woce_date")
time = getvariable(nc, "woce_time")
return narwhal.CTDCast(pres, sal, temp, oxygen=oxy,
coords=coords,
properties={"woce_time":time, "woce_date":date})
示例11: test_netcdf_wave_format
def test_netcdf_wave_format(self):
print("Running Wave NetCDF Format Test: ")
#directory of where files will be saved at
destination_directory = os.path.join(settings.MEDIA_ROOT, settings.WAVE_WATCH_DIR)
print "Downloading File"
#file names might need to be created dynamically in the future if ftp site changes
file_name = "outer.nc"
#Connect to FTP site to get the file modification data
ftp = FTP('cil-www.oce.orst.edu')
ftp.login()
#retrieve the ftp modified datetime format
ftp_dtm = ftp.sendcmd('MDTM' + " /pub/outgoing/ww3data/" + file_name)
#convert ftp datetime format to a string datetime
modified_datetime = datetime.strptime(ftp_dtm[4:], "%Y%m%d%H%M%S").strftime("%Y-%m-%d")
#Create File Name and Download actual File into media folder
url = urljoin(settings.WAVE_WATCH_URL, file_name)
filename = "{0}_{1}_{2}.nc".format("OuterGrid", modified_datetime, uuid4())
urllib.urlretrieve(url=url, filename=os.path.join(destination_directory, filename))
datafile_read_object = netcdf_file(os.path.join(settings.MEDIA_ROOT, settings.WAVE_WATCH_DIR, filename))
print "Checking Variables: latitude, longitude, HTSGW_surface"
surface = datafile_read_object.variables['HTSGW_surface'][:, :, :]
long = datafile_read_object.variables['longitude'][:]
lat = datafile_read_object.variables['latitude'][:]
ftp.quit()
self.assertIsNotNone(surface)
self.assertIsNotNone(long)
self.assertIsNotNone(lat)
示例12: __init__
def __init__(self, projname, casename="", **kwargs):
super(Partsat,self).__init__(projname, casename, *kwargs)
postgresql.DB.__init__(self, projname, casename, database='partsat')
self.flddict = {'par':('L3',),'chl':('box8',)}
if projname == 'oscar':
import pysea.MODIS
self.sat = pysea.NASA.nasa(res='4km',
ijarea=(700,1700,2000,4000))
def calc_jd(ints,intstart):
return self.base_iso + float(ints)/6-1
elif projname=="casco":
self.sat = casco.Sat(res='500m')
def calc_jd(ints,intstart):
return (self.base_iso +(ints-(intstart)*10800)/150 +
intstart/8)
elif projname=="gompom":
n = netcdf_file('/Users/bror/svn/modtraj/box8_gompom.cdf')
self.gomi = n.variables['igompom'][:]
self.gomj = n.variables['jgompom'][:]
self.sati = n.variables['ibox8'][:]
self.satj = n.variables['jbox8'][:]
elif projname=="jplSCB":
from njord import mati
self.sat = mati.Cal()
elif projname=="jplNow":
from njord import mati
self.sat = mati.Cal()
示例13: load
def load(self, mn=1):
""" Load mixed layer climatology for a given day"""
self.mn = mn
nc = netcdf_file(self.datadir + "/" + self.mldFile)
self.mld = gmtgrid.convert(nc.variables["mld"][self.mn - 1, self.j1 : self.j2, self.i1 : self.i2], self.gr)
self.mld[self.mld < 0] = np.nan
self.mld[self.mld > 1e4] = np.nan
示例14: setup_grid
def setup_grid(self):
"""Define lat and lon matrices for njord"""
gc = netcdf_file(self.gridfile)
self.lat = gc.variables['lat'][:].copy()
self.gmt = gmtgrid.Shift(gc.variables['lon'][:].copy())
self.lon = self.gmt.lonvec
self.llon,self.llat = np.meshgrid(self.lon,self.lat)
示例15: setup_grid
def setup_grid(self):
gc = netcdf_file(self.gridfile)
print self.gridfile
self.llat = gc.variables['y'][:]
self.llon = gc.variables['x'][:]
self.depth = gc.variables['depth'][:].copy()
self.depth[self.depth<0] = np.nan