def test_main_03_dims(self): import yaml psy.register_plotter('test_plotter', module='test_plotter', plotter_name='TestPlotter') fname2 = tempfile.NamedTemporaryFile( suffix='.pdf', prefix='test_psyplot_').name self._created_files.add(fname2) # create a formatoptions file fmt_file = tempfile.NamedTemporaryFile( suffix='.yml', prefix='test_psyplot_').name self._created_files.add(fmt_file) with open(fmt_file, 'w') as f: yaml.dump({'fmt1': 'fmt1', 'fmt2': 'fmt2'}, f) if not six.PY2: with self.assertRaisesRegex(ValueError, 'plotting method'): main.main([bt.get_file('test-t2m-u-v.nc'), '-o', fname2, '-d', 'time,1,2', 'y,3,4', '-n', 'u', 'v']) main.main([bt.get_file('test-t2m-u-v.nc'), '-o', fname2, '-d', 'time,1,2', 'y,3,4', '-n', 'u', 'v', '-pm', 'test_plotter', '-fmt', fmt_file]) mp = psy.gcp(True) self.assertEqual(len(mp), 2*2*2, msg=mp) all_dims = set(product((1, 2), (3, 4), ('u', 'v'))) for arr in mp: idims = arr.psy.idims all_dims -= {(idims['time'], idims['lat'], arr.name)} self.assertFalse(all_dims) for i, plotter in enumerate(mp.plotters): self.assertEqual(plotter['fmt1'], 'fmt1', msg='Wrong value for fmt1 of plotter %i!' % i) self.assertEqual(plotter['fmt2'], 'fmt2', msg='Wrong value for fmt2 of plotter %i!' % i)
def test_open_geotiff(self): """Test to open a GeoTiff file""" ds_ref = psyd.open_dataset(bt.get_file('test-t2m-u-v.nc')) ds_tiff = psyd.open_dataset( bt.get_file('test-t2m-1979-01-31T18-00-00.tif'), engine='gdal') self.assertListEqual(ds_tiff.Band1.values.tolist(), ds_ref.isel(time=0, lev=0).t2m.values.tolist())
def test_open_mf_geotiff(self): """Test to open multiple GeoTiff files and extract the time from the file name""" ds_ref = psyd.open_dataset(bt.get_file('test-t2m-u-v.nc')) ds_tiff = psyd.open_mfdataset(bt.get_file('test-t2m-*.tif'), engine='gdal', t_format='test-t2m-%Y-%m-%dT%H-%M-%S') self.assertListEqual( ds_ref.isel(time=[0, 1], lev=0).t2m.values.tolist(), ds_tiff.Band1.values.tolist()) self.assertListEqual( pd.to_datetime(ds_tiff.time.values).tolist(), pd.to_datetime(ds_ref.time[:2].values).tolist())
def test_main_02_alternative_ds(self): sp, fname1 = self._create_and_save_test_project() fname2 = tempfile.NamedTemporaryFile( suffix='.pdf', prefix='test_psyplot_').name self._created_files.add(fname2) sp.save_project(fname1, use_rel_paths=False) psy.close('all') main.main([bt.get_file('circumpolar_test.nc'), '-p', fname1, '-o', fname2]) self.assertTrue(osp.exists(fname2), msg='Missing ' + fname2) mp = psy.gcp(True) self.assertEqual(len(mp), 4) self.assertEqual( set(t[0] for t in mp._get_dsnames(mp.array_info( dump=False, use_rel_paths=False))), {bt.get_file('circumpolar_test.nc')})
def _create_and_save_test_project(self): psy.register_plotter('test_plotter', module='test_plotter', plotter_name='TestPlotter') sp = psy.plot.test_plotter(bt.get_file('test-t2m-u-v.nc'), name=['t2m', 'u'], time=[0, 1]) self.assertEqual(len(sp), 4, sp) fname = tempfile.NamedTemporaryFile( suffix='.pkl', prefix='test_psyplot_').name self._created_files.add(fname) sp.save_project(fname, use_rel_paths=False) return sp, fname
def test_get_enhanced_attrs_01_arr(self): """Test the :meth:`psyplot.plotter.Plotter.get_enhanced_attrs` method """ ds = psyd.open_dataset(bt.get_file('test-t2m-u-v.nc')) plotter = TestPlotter(ds.t2m) attrs = ds.t2m.attrs.copy() for key, val in ds.lon.attrs.items(): attrs['x' + key] = val for key, val in ds.lat.attrs.items(): attrs['y' + key] = val for key, val in ds.lev.attrs.items(): attrs['z' + key] = val for key, val in ds.time.attrs.items(): attrs['t' + key] = val attrs['xname'] = 'lon' attrs['yname'] = 'lat' attrs['zname'] = 'lev' attrs['tname'] = 'time' attrs['name'] = 't2m' self.assertEqual(dict(plotter.get_enhanced_attrs(plotter.plot_data)), dict(attrs))
def test_get_enhanced_attrs_02_list(self): """Test the :meth:`psyplot.plotter.Plotter.get_enhanced_attrs` method """ ds = psyd.open_dataset(bt.get_file('test-t2m-u-v.nc')) plotter = TestPlotter( psyd.InteractiveList( ds.psy.create_list(name=['t2m', 'u'], x=0, t=0))) attrs = {} for key, val in ds.t2m.attrs.items(): attrs['t2m' + key] = val for key, val in ds.u.attrs.items(): attrs['u' + key] = val for key, val in ds.lon.attrs.items(): attrs['x' + key] = val for key, val in ds.lat.attrs.items(): attrs['y' + key] = val attrs['x' + key] = val # overwrite the longitude information # the plot_data has priority over the base variable, therefore we # the plotter should replace the y information with the z information for key, val in ds.lev.attrs.items(): attrs['z' + key] = val attrs['y' + key] = val # overwrite the latitude information for key, val in ds.time.attrs.items(): attrs['t' + key] = val for key in set(ds.t2m.attrs) & set(ds.u.attrs): if ds.t2m.attrs[key] == ds.u.attrs[key]: attrs[key] = ds.t2m.attrs[key] attrs['zname'] = attrs['yname'] = 'lev' attrs['xname'] = 'lat' attrs['tname'] = 'time' attrs['lon'] = attrs['x'] = ds.lon.values[0] attrs['time'] = attrs['t'] = pd.to_datetime( ds.time.values[0]).isoformat() self.maxDiff = None self.assertEqual(dict(plotter.get_enhanced_attrs(plotter.plot_data)), dict(attrs))