本文整理汇总了Python中pycmbs.data.Data.save方法的典型用法代码示例。如果您正苦于以下问题:Python Data.save方法的具体用法?Python Data.save怎么用?Python Data.save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pycmbs.data.Data
的用法示例。
在下文中一共展示了Data.save方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _import_regional_file
# 需要导入模块: from pycmbs.data import Data [as 别名]
# 或者: from pycmbs.data.Data import save [as 别名]
def _import_regional_file(self, region_file, varname, targetgrid=None, logfile=None):
"""
check if the regional file can be either imported or if
regions are provided as vector data. In the latter case
the regions are rasterized and results are stored in a netCDF
file
Parameters
----------
region_file : str
name of file defining the region. This is either a netCDF
file which contains the mask as different integer values
or it is a *.reg file which contains the regions as
vector data.
varname : str
name of variable in netCDF file
targetgrid : str
name of targetgrid; either 't63grid' or the name of a file
with a valid geometry
Returns
-------
region_filename, region_file_varname
"""
if not os.path.exists(region_file):
raise ValueError('ERROR: region file is not existing: ' + region_file)
ext = os.path.splitext(region_file)[1]
if ext == '.nc':
# netCDF file was given. Try to read variable
if varname is None:
raise ValueError('ERROR: no variable name given!')
try:
tmp = Data(region_file, varname, read=True)
except:
raise ValueError('ERROR: the regional masking file can not be read!')
del tmp
# everything is fine
return region_file, varname
elif ext == '.reg':
# regions were given as vector files. Read it and
# rasterize the data and store results in a temporary
# file
import tempfile
if targetgrid is None:
raise ValueError('ERROR: targetgrid needs to be specified for vectorization of regions!')
if targetgrid == 't63grid':
ls_mask = get_T63_landseamask(True, area='global', mask_antarctica=False)
else:
ls_mask = get_generic_landseamask(True, area='global', target_grid=targetgrid,
mask_antarctica=False)
# temporary netCDF filename
region_file1 = tempfile.mktemp(prefix='region_mask_', suffix='.nc')
R = RegionParser(region_file) # read region vector data
M = Raster(ls_mask.lon, ls_mask.lat)
polylist = []
if logfile is not None:
logf = open(logfile, 'w')
else:
logf = None
id = 1
for k in R.regions.keys():
reg = R.regions[k]
polylist.append(pycmbsPolygon(id, zip(reg.lon, reg.lat)))
if logf is not None: # store mapping table
logf.write(k + '\t' + str(id) + '\n')
id += 1
M.rasterize_polygons(polylist)
if logf is not None:
logf.close()
# generate dummy output file
O = Data(None, None)
O.data = M.mask
O.lat = ls_mask.lat
O.lon = ls_mask.lon
varname = 'regions'
O.save(region_file1, varname=varname, format='nc', delete=True)
print('Regionfile was store in file: %s' % region_file1)
# check again that file is readable
try:
tmp = Data(region_file1, varname, read=True)
except:
print region_file1, varname
raise ValueError('ERROR: the generated region file is not readable!')
del tmp
return region_file1, varname
else:
raise ValueError('ERROR: unsupported file type')
示例2: TestData
# 需要导入模块: from pycmbs.data import Data [as 别名]
# 或者: from pycmbs.data.Data import save [as 别名]
class TestData(unittest.TestCase):
def setUp(self):
self.nx = 20
self.ny = 10
self.tempfile = tempfile.mktemp(suffix='.nc')
self.gfile1 = tempfile.mktemp(suffix='.nc')
self.gfile2 = tempfile.mktemp(suffix='.nc')
self.gfile3 = tempfile.mktemp(suffix='.nc')
self.x = Data(None, None)
self.x._init_sample_object(nt=10, ny=self.ny, nx=self.nx)
self.x.save(self.tempfile, varname='myvar')
# generate some arbitrary geometry file
F = NetCDFHandler()
F.open_file(self.gfile1, 'w')
F.create_dimension('ny', size=self.ny)
F.create_dimension('nx', size=self.nx)
F.create_variable('lat', 'd', ('ny', 'nx'))
F.create_variable('lon', 'd', ('ny', 'nx'))
F.assign_value('lat', np.ones((self.ny,self.nx)) * 5.)
F.assign_value('lon', np.ones((self.ny,self.nx)) * 3.)
F.close()
F = NetCDFHandler()
F.open_file(self.gfile2, 'w')
F.create_dimension('ny', size=self.ny)
F.create_dimension('nx', size=self.nx)
F.create_variable('latitude', 'd', ('ny', 'nx'))
F.create_variable('longitude', 'd', ('ny', 'nx'))
F.assign_value('latitude', np.ones((self.ny,self.nx)) * 7.)
F.assign_value('longitude', np.ones((self.ny,self.nx)) * 8.)
F.close()
F = NetCDFHandler()
F.open_file(self.gfile3, 'w')
F.create_dimension('ny', size=self.ny*2)
F.create_dimension('nx', size=self.nx*3)
F.create_variable('latitude', 'd', ('ny', 'nx'))
F.create_variable('longitude', 'd', ('ny', 'nx'))
F.assign_value('latitude', np.ones((self.ny*2,self.nx*3)) * 7.)
F.assign_value('longitude', np.ones((self.ny*2,self.nx*3)) * 8.)
F.close()
def test_read_coordinates(self):
# read data normal
x1 = Data(self.tempfile, 'myvar', read=True)
self.assertEqual(x1.nx,self.nx)
self.assertEqual(x1.ny,self.ny)
# read data with separate geometry file 'lat', 'lon' names
x2 = Data(self.tempfile, 'myvar', read=True, geometry_file=self.gfile1)
self.assertTrue(np.all(x2.lat == 5.))
self.assertTrue(np.all(x2.lon == 3.))
# read data with separate geometry file 'latitude', 'longitude' names
x3 = Data(self.tempfile, 'myvar', read=True, geometry_file=self.gfile2)
self.assertTrue(np.all(x3.lat == 7.))
self.assertTrue(np.all(x3.lon == 8.))
# read data with separate geometry file 'lat', 'lon' names, invalid geometry
with self.assertRaises(ValueError):
x4 = Data(self.tempfile, 'myvar', read=True, geometry_file=self.gfile3)