class TestData(unittest.TestCase): def setUp(self): self.D = GeoData(None, None) self.D._init_sample_object(nt=None, ny=180, nx=360) self._tmpdir = tempfile.mkdtemp() def test_imshow(self): # test simple mapping using imshow only S = SingleMap(self.D) S.plot() def test_basemap(self): # test simple mapping using basemap S = SingleMap(self.D, backend='imshow') S.plot() def test_cartopy(self): # test simple mapping using cartopy S = SingleMap(self.D, backend='cartopy') S.plot(proj_prop={'projection' : 'robin'})
def map_trends(self, SIG_R, ME, PHI, SLOPE, var_t, force=False, time_unit=None): """ STD : GeoData ME : GeoData """ assert time_unit is not None, 'Time unit needs to be provided' if SIG_R.ndim == 3: if SIG_R.nt == 1: SIG_R.data = SIG_R.data[0, :, :] else: assert False if ME.ndim == 3: if ME.nt == 1: ME.data = ME.data[0, :, :] else: assert False if PHI.ndim == 3: if PHI.nt == 1: PHI.data = PHI.data[0, :, :] else: assert False if SLOPE.ndim == 3: if SLOPE.nt == 1: SLOPE.data = SLOPE.data[0, :, :] else: assert False assert SIG_R.data.ndim == 2 assert ME.data.ndim == 2 assert PHI.data.ndim == 2 assert SLOPE.data.ndim == 2 # coefficient of variation self._calc_cv(PHI, SLOPE, SIG_R, ME, var_t) print self.CV.min, self.CV.max # mask for valid pixels msk = ~self.CV.data.mask # vectors which correpond to data that should be interpolated to cvs = self.CV.data[msk].flatten() means = ME.data[msk].flatten() phis = PHI.data[msk].flatten() print 'NPixels: ', len(means) if hasattr(self, 'X'): if force: do_calc = True else: do_calc = False else: do_calc = True if do_calc: # interpolation z = self._interpolate_fast(cvs, means, phis) # map back to original geometry tmp = np.ones(ME.nx * ME.ny) * np.nan tmp[msk.flatten()] = z tmp = tmp.reshape((ME.ny, ME.nx)) X = GeoData(None, None) X._init_sample_object(nt=None, ny=ME.ny, nx=ME.nx, gaps=False) X.data = np.ma.array(tmp, mask=tmp != tmp) self.X = X self.X.lon = ME.lon * 1. self.X.lat = ME.lat * 1. self.X.unit = 'trend / ' + time_unit self.X._trend_unit = time_unit
def setUp(self): self.D = GeoData(None, None) self.D._init_sample_object(nt=1000, ny=1, nx=1) self._tmpdir = tempfile.mkdtemp()
class TestTrend(unittest.TestCase): def setUp(self): self.D = GeoData(None, None) self.D._init_sample_object(nt=1000, ny=1, nx=1) self._tmpdir = tempfile.mkdtemp() def _generate_sample_lut(self): lutname = tempfile.mktemp(suffix='.pkl') cvs = np.asarray([1.,2.,3.]) means = np.asarray([10.,20.]) phis = np.asarray([0.9]) lut = np.ones((len(phis),len(means), len(cvs)))*np.nan lut[0,0,0] = 0.1 lut[0,0,1] = 0.2 lut[0,0,2] = np.nan lut[0,1,0] = 0.4 lut[0,1,1] = 0.5 lut[0,1,2] = 0.6 #lut = np.asarray([[0.1,0.2,np.nan],[0.4,0.5,0.6]]) d = {'cvs' : cvs, 'means' : means, 'res' : lut, 'phis' : phis} cPickle.dump(d, open(lutname, 'w')) return lutname, d def test_mintrend_lut_interpolation(self): lutname, d = self._generate_sample_lut() P = MintrendPlot(lutname) self.assertEqual(P._lutname, lutname) def test_read_lut(self): # check that LUT is read properly lutname, d = self._generate_sample_lut() P = MintrendPlot(lutname) self.assertEqual(len(P.cvs),5) self.assertEqual(len(P.means),5) self.assertEqual(len(P.lut),5) self.assertEqual(list(P.lut), [0.1,0.2,0.4,0.5,0.6]) def test_interpolate(self): lutname, d = self._generate_sample_lut() P = MintrendPlot(lutname) #~ # first interpolate to same coordinates as given # should give same results # cvs, means, phis z = P._interpolate_fast([1.], [10.], [0.9]) self.assertEqual(z[0],0.1) z = P._interpolate_fast([2.], [10.], [0.9]) self.assertEqual(z[0],0.2) z = P._interpolate_fast([1.], [20.], [0.9]) self.assertEqual(z[0],0.4) z = P._interpolate_fast([2.], [20.], [0.9]) self.assertEqual(z[0],0.5) z = P._interpolate_fast([3.], [20.], [0.9]) self.assertEqual(z[0],0.6) # now check real interpolation z = P._interpolate_fast([2.], [15.], [0.9]) self.assertEqual(z[0],0.35) z = P._interpolate_fast([1.], [15.], [0.9]) self.assertEqual(z[0],0.25)
def map_trends(self, SIG_R, ME, PHI, SLOPE, var_t, force=False, time_unit=None): """ STD : GeoData ME : GeoData """ assert time_unit is not None, 'Time unit needs to be provided' if SIG_R.ndim == 3: if SIG_R.nt == 1: SIG_R.data = SIG_R.data[0,:,:] else: assert False if ME.ndim == 3: if ME.nt == 1: ME.data = ME.data[0,:,:] else: assert False if PHI.ndim == 3: if PHI.nt == 1: PHI.data = PHI.data[0,:,:] else: assert False if SLOPE.ndim == 3: if SLOPE.nt == 1: SLOPE.data = SLOPE.data[0,:,:] else: assert False assert SIG_R.data.ndim == 2 assert ME.data.ndim == 2 assert PHI.data.ndim == 2 assert SLOPE.data.ndim == 2 # coefficient of variation self._calc_cv(PHI, SLOPE, SIG_R, ME, var_t) print self.CV.min, self.CV.max # mask for valid pixels msk = ~self.CV.data.mask # vectors which correpond to data that should be interpolated to cvs = self.CV.data[msk].flatten() means = ME.data[msk].flatten() phis = PHI.data[msk].flatten() print 'NPixels: ', len(means) if hasattr(self, 'X'): if force: do_calc = True else: do_calc = False else: do_calc = True if do_calc: # interpolation z = self._interpolate_fast(cvs, means, phis) # map back to original geometry tmp = np.ones(ME.nx*ME.ny)*np.nan tmp[msk.flatten()] = z tmp = tmp.reshape((ME.ny,ME.nx)) X = GeoData(None, None) X._init_sample_object(nt=None, ny=ME.ny, nx=ME.nx, gaps=False) X.data = np.ma.array(tmp, mask=tmp != tmp) self.X = X self.X.lon = ME.lon*1. self.X.lat = ME.lat*1. self.X.unit = 'trend / ' + time_unit self.X._trend_unit = time_unit