def cut_positions(filename, blurred, *positions): blurred = int(blurred) pos = eval("".join(positions)) root, is_new = nc.open(filename) lat = nc.getvar(root, 'lat') lon = nc.getvar(root, 'lon') data = nc.getvar(root, 'data') time = nc.getvar(root, 'time') root_cut, is_new = nc.open('cut_positions.' + filename) timing_d = nc.getdim(root_cut, 'timing', data.shape[0]) northing_d = nc.getdim(root_cut, 'northing', len(pos)) easting_d = nc.getdim(root_cut, 'easting', 2) lat_cut = nc.getvar(root_cut, 'lat', 'f4', ('northing','easting',),4) lon_cut = nc.getvar(root_cut, 'lon', 'f4', ('northing','easting',),4) data_cut = nc.getvar(root_cut, 'data', 'f4', ('timing','northing','easting',),4) time_cut = nc.getvar(root_cut, 'time', 'f4', ('timing',),4) time_cut[:] = time[:] for i in range(len(pos)): show("\rCutting data: processing position %d / %d " % (i+1, len(pos))) x, y = search_position(pos[i], lat, lon) lat_cut[i,0] = lat[x,y] lon_cut[i,0] = lon[x,y] data_cut[:,i,0] = np.apply_over_axes(np.mean, data[:,x-blurred:x+blurred,y-blurred:y+blurred], axes=[1,2]) lat_cut[i,1], lon_cut[i,1], data_cut[:,i,1] = lat_cut[i,0], lon_cut[i,0], data_cut[:,i,0] nc.close(root) nc.close(root_cut)
def cut_positions(filename, blurred, *positions): blurred = int(blurred) pos = eval("".join(positions)) root = nc.open(filename)[0] lat = nc.getvar(root, 'lat') lon = nc.getvar(root, 'lon') data = nc.getvar(root, 'data') root_cut = nc.clonefile(root, 'cut_positions.' + filename, ['lat', 'lon', 'data'])[0] nc.getdim(root_cut, 'northing_cut', len(pos)) nc.getdim(root_cut, 'easting_cut', 2) lat_cut = nc.getvar(root_cut, 'lat', 'f4', ('northing_cut','easting_cut',),4) lon_cut = nc.getvar(root_cut, 'lon', 'f4', ('northing_cut','easting_cut',),4) data_cut = nc.getvar(root_cut, 'data', 'f4', ('timing','northing_cut','easting_cut',),4) ix = 0 for i in range(len(pos)): show("\rCutting data: processing position %d / %d " % (i+1, len(pos))) x, y = statistical_search_position(pos[i], lat, lon) if x and y: lat_cut[ix,0] = lat[x,y] lon_cut[ix,0] = lon[x,y] data_cut[:,ix,0] = np.apply_over_axes(np.mean, data[:,x-blurred:x+blurred,y-blurred:y+blurred], axes=[1,2]) if blurred > 0 else data[:,x,y] lat_cut[ix,1], lon_cut[ix,1], data_cut[:,ix,1] = lat_cut[ix,0], lon_cut[ix,0], data_cut[:,ix,0] ix += 1 nc.close(root) nc.close(root_cut)
def test_compact_multiple_files(self): t_root = nc.tailor("unittest0*.nc", dimensions=self.dimensions) t_data = nc.getvar(t_root, "data") data = nc.getvar(t_root.root, "data") self.assertEquals(data.shape, (5, 100, 200)) self.assertEquals(t_data.shape, (3, 40, 160)) self.assertEquals(nc.getvar(t_root, "time").shape, (3, 1)) self.assertTrue((t_data[:] == data[:3, 10:50, 20:-20]).all()) # The random values goes from 2.5 to 10 with 0.5 steps. t_data[:] = 1.5 self.assertTrue((t_data[:] == 1.5).all()) self.assertTrue((data[:3, 10:50, 20:-20] == 1.5).all()) self.assertTrue((data[:] != 1.5).any()) nc.close(t_root) with nc.loader("unittest0*.nc") as root: data = nc.getvar(root, "data") self.assertTrue((data[:3, 10:50, 20:-20] == 1.5).all()) self.assertTrue((data[:] != 1.5).any())
def test_compact_multiple_files(self): t_root = nc.tailor('unittest0*.nc', dimensions=self.dimensions) t_data = nc.getvar(t_root, 'data') data = nc.getvar(t_root.root, 'data') self.assertEquals(data.shape, (5, 100, 200)) self.assertEquals(t_data.shape, (3, 40, 160)) self.assertEquals(nc.getvar(t_root, 'time').shape, (3, 1)) self.assertTrue((t_data[:] == data[:3, 10:50, 20:-20]).all()) # The random values goes from 2.5 to 10 with 0.5 steps. t_data[:] = 1.5 self.assertTrue((t_data[:] == 1.5).all()) self.assertTrue((data[:3, 10:50, 20:-20] == 1.5).all()) self.assertTrue((data[:] != 1.5).any()) nc.close(t_root) with nc.loader('unittest0*.nc') as root: data = nc.getvar(root, 'data') self.assertTrue((data[:3, 10:50, 20:-20] == 1.5).all()) self.assertTrue((data[:] != 1.5).any())
def test_file_with_readonly_restriction(self): # check the file is NOT read only. filename = "unittest00.nc" can_write = os.access(filename, os.W_OK) self.assertTrue(can_write) # check if open an existent file. root = nc.tailor("unittest00.nc", dimensions=self.dimensions, read_only=True) self.assertEquals(root.files, ["unittest00.nc"]) self.assertEquals(root.pattern, "unittest00.nc") self.assertEquals(len(root.roots), 1) self.assertFalse(root.is_new) self.assertTrue(root.read_only) with self.assertRaisesRegexp(Exception, u"NetCDF: Write to read only"): var = nc.getvar(root, "data") var[:] = 0 # check if close an existent file. nc.close(root) with self.assertRaisesRegexp(RuntimeError, u"NetCDF: Not a valid ID"): nc.close(root)
def test_multiple_files_with_readonly_restriction(self): # check the files are NOT read only. filenames = map(lambda i: "unittest0%i.nc" % i, range(5)) can_write = map(lambda f: os.access(f, os.W_OK), filenames) self.assertTrue(all(can_write)) # check if open the pattern selection using using a package instance. root = nc.tailor("unittest0*.nc", dimensions=self.dimensions, read_only=True) self.assertEquals(root.files, ["unittest0%i.nc" % i for i in range(5)]) self.assertEquals(root.pattern, "unittest0*.nc") self.assertEquals(len(root.roots), 5) self.assertFalse(root.is_new) self.assertTrue(root.read_only) with self.assertRaisesRegexp(Exception, u"NetCDF: Write to read only"): var = nc.getvar(root, "data") var[:] = 0 # check if close the package with all the files. nc.close(root) with self.assertRaisesRegexp(RuntimeError, u"NetCDF: Not a valid ID"): nc.close(root)
def test_simple_file(self): root = nc.open("unittest00.nc")[0] t_root = nc.tailor(root, dimensions=self.dimensions) t_data = nc.getvar(t_root, "data") data = nc.getvar(root, "data") self.assertEquals(data.shape, (1, 100, 200)) self.assertEquals(t_data.shape, (1, 40, 160)) self.assertEquals(nc.getvar(t_root, "time").shape, (1,)) self.assertTrue((t_data[:] == data[:3, 10:50, 20:-20]).all()) # The random values goes from 2.5 to 10 with 0.5 steps. t_data[:] = 1.5 nc.sync(t_root) self.assertTrue((t_data[:] == 1.5).all()) self.assertTrue((data[:3, 10:50, 20:-20] == 1.5).all()) # self.assertTrue((data[:] != 1.5).any()) nc.close(t_root) with nc.loader("unittest00.nc") as root: data = nc.getvar(root, "data") self.assertTrue((data[:3, 10:50, 20:-20] == 1.5).all())
def test_simple_file(self): root = nc.open('unittest00.nc')[0] t_root = nc.tailor(root, dimensions=self.dimensions) t_data = nc.getvar(t_root, 'data') data = nc.getvar(root, 'data') self.assertEquals(data.shape, (1, 100, 200)) self.assertEquals(t_data.shape, (1, 40, 160)) self.assertEquals(nc.getvar(t_root, 'time').shape, (1, )) self.assertTrue((t_data[:] == data[:3, 10:50, 20:-20]).all()) # The random values goes from 2.5 to 10 with 0.5 steps. t_data[:] = 1.5 nc.sync(t_root) self.assertTrue((t_data[:] == 1.5).all()) self.assertTrue((data[:3, 10:50, 20:-20] == 1.5).all()) # self.assertTrue((data[:] != 1.5).any()) nc.close(t_root) with nc.loader('unittest00.nc') as root: data = nc.getvar(root, 'data') self.assertTrue((data[:3, 10:50, 20:-20] == 1.5).all())
def test_getvar_with_incomplete_limited_dimensions(self): self.dimensions.pop("time", None) root = nc.open("unittest0*.nc")[0] t_root = nc.tailor(root, dimensions=self.dimensions) t_data = nc.getvar(t_root, "data") data = nc.getvar(root, "data") nc.sync(root) self.assertEquals(data.shape, (5, 100, 200)) self.assertEquals(t_data.shape, (5, 40, 160)) self.assertEquals(nc.getvar(t_root, "time").shape, (5, 1)) self.assertTrue((t_data[:] == data[:, 10:50, 20:-20]).all()) # The random values goes from 2.5 to 10 with 0.5 steps. t_data[:] = 1.5 self.assertTrue((t_data[:] == 1.5).all()) self.assertTrue((data[:, 10:50, 20:-20] == 1.5).all()) self.assertTrue((data[:] != 1.5).any()) nc.close(t_root) with nc.loader("unittest0*.nc") as root: data = nc.getvar(root, "data") self.assertTrue((data[:, 10:50, 20:-20] == 1.5).all()) self.assertTrue((data[:] != 1.5).any())
def test_file_with_readonly_restriction(self): # check the file is NOT read only. filename = 'unittest00.nc' can_write = os.access(filename, os.W_OK) self.assertTrue(can_write) # check if open an existent file. root = nc.tailor('unittest00.nc', dimensions=self.dimensions, read_only=True) self.assertEquals(root.files, ['unittest00.nc']) self.assertEquals(root.pattern, 'unittest00.nc') self.assertEquals(len(root.roots), 1) self.assertFalse(root.is_new) self.assertTrue(root.read_only) with self.assertRaisesRegexp(Exception, u'NetCDF: Write to read only'): var = nc.getvar(root, 'data') var[:] = 0 # check if close an existent file. nc.close(root) with self.assertRaisesRegexp(RuntimeError, u'NetCDF: Not a valid ID'): nc.close(root)
def test_getvar_with_incomplete_limited_dimensions(self): self.dimensions.pop('time', None) root = nc.open('unittest0*.nc')[0] t_root = nc.tailor(root, dimensions=self.dimensions) t_data = nc.getvar(t_root, 'data') data = nc.getvar(root, 'data') nc.sync(root) self.assertEquals(data.shape, (5, 100, 200)) self.assertEquals(t_data.shape, (5, 40, 160)) self.assertEquals(nc.getvar(t_root, 'time').shape, (5, 1)) self.assertTrue((t_data[:] == data[:, 10:50, 20:-20]).all()) # The random values goes from 2.5 to 10 with 0.5 steps. t_data[:] = 1.5 self.assertTrue((t_data[:] == 1.5).all()) self.assertTrue((data[:, 10:50, 20:-20] == 1.5).all()) self.assertTrue((data[:] != 1.5).any()) nc.close(t_root) with nc.loader('unittest0*.nc') as root: data = nc.getvar(root, 'data') self.assertTrue((data[:, 10:50, 20:-20] == 1.5).all()) self.assertTrue((data[:] != 1.5).any())
def test_multiple_files_with_readonly_restriction(self): # check the files are NOT read only. filenames = map(lambda i: 'unittest0%i.nc' % i, range(5)) can_write = map(lambda f: os.access(f, os.W_OK), filenames) self.assertTrue(all(can_write)) # check if open the pattern selection using using a package instance. root = nc.tailor('unittest0*.nc', dimensions=self.dimensions, read_only=True) self.assertEquals(root.files, ['unittest0%i.nc' % i for i in range(5)]) self.assertEquals(root.pattern, 'unittest0*.nc') self.assertEquals(len(root.roots), 5) self.assertFalse(root.is_new) self.assertTrue(root.read_only) with self.assertRaisesRegexp(Exception, u'NetCDF: Write to read only'): var = nc.getvar(root, 'data') var[:] = 0 # check if close the package with all the files. nc.close(root) with self.assertRaisesRegexp(RuntimeError, u'NetCDF: Not a valid ID'): nc.close(root)
def cut_projected_linke(filename): root, is_new = nc.open(filename) lat = nc.getvar(root, 'lat') lon = nc.getvar(root, 'lon') data = nc.getvar(root, 'data') time = nc.getvar(root, 'time') root_cut, is_new = nc.open('wlinke.' + filename) timing_d = nc.getdim(root_cut, 'timing', data.shape[0]) northing_d = nc.getdim(root_cut, 'northing', data.shape[1]) easting_d = nc.getdim(root_cut, 'easting', data.shape[2]) lat_cut = nc.getvar(root_cut, 'lat', 'f4', ('northing','easting',),4) lon_cut = nc.getvar(root_cut, 'lon', 'f4', ('northing','easting',),4) data_cut = nc.getvar(root_cut, 'data', 'f4', ('timing','northing','easting',),4) time_cut = nc.getvar(root_cut, 'time', 'f4', ('timing',),4) lat_cut[:] = lat[:] lon_cut[:] = lon[:] data_cut[:] = data[:] time_cut[:] = time[:] linke.cut_projected(root_cut) nc.close(root) nc.close(root_cut)
def cut_projected_terrain(filename): from libs.dem import dem root = nc.open(filename)[0] lat = nc.getvar(root, 'lat') lon = nc.getvar(root, 'lon') data = nc.getvar(root, 'data') time = nc.getvar(root, 'data_time') root_cut = nc.open('wterrain.' + filename)[0] nc.getdim(root_cut, 'timing', data.shape[0]) nc.getdim(root_cut, 'northing', data.shape[1]) nc.getdim(root_cut, 'easting', data.shape[2]) lat_cut = nc.getvar(root_cut, 'lat', 'f4', ('northing','easting',),4) lon_cut = nc.getvar(root_cut, 'lon', 'f4', ('northing','easting',),4) data_cut = nc.getvar(root_cut, 'data', 'f4', ('timing','northing','easting',),4) time_cut = nc.getvar(root_cut, 'data_time', 'f4', ('timing',),4) lat_cut[:] = lat[:] lon_cut[:] = lon[:] data_cut[:] = data[:] time_cut[:] = time[:] dem.cut_projected(root_cut) nc.close(root) nc.close(root_cut)
def cut(filename, i_from, i_to): img_from = int(i_from) img_to = int(i_to) img_range = img_to - img_from root = nc.open(filename)[0] lat = nc.getvar(root, 'lat') lon = nc.getvar(root, 'lon') data = nc.getvar(root, 'data') time = nc.getvar(root, 'data_time') root_cut = nc.open('cut.' + filename)[0] nc.getdim(root_cut, 'timing', img_range) nc.getdim(root_cut, 'northing', data.shape[1]) nc.getdim(root_cut, 'easting', data.shape[2]) lat_cut = nc.getvar(root_cut, 'lat', 'f4', ('northing','easting',),4) lon_cut = nc.getvar(root_cut, 'lon', 'f4', ('northing','easting',),4) lat_cut[:] = lat[:] lon_cut[:] = lon[:] data_cut = nc.getvar(root_cut, 'data', 'f4', ('timing','northing','easting',),4) time_cut = nc.getvar(root_cut, 'data_time', 'f4', ('timing',),4) for i in range(img_range): data_cut[i] = data[img_from + i] time_cut[i] = time[img_from + i] nc.close(root) nc.close(root_cut)