def _init_orchidee_geo(self, dataset): """initializes the geospatial indexer from nav_lat and nav_lon in the dataset""" lats = dataset.variables['nav_lat'][:, 0] lons = dataset.variables['nav_lon'][0, :] delta_lat = lats[1] - lats[0] delta_lon = lons[1] - lons[0] lat_samp = qi.LinearSamplingFunction(1. / delta_lat, x_zero=lats[0]) lon_samp = qi.LinearSamplingFunction(1. / delta_lon, x_zero=lons[0]) self._samp = qi.OrthoIndexer([lat_samp, lon_samp])
def test_get_unit_val(self): """given index, compute the unit""" samp_fn = q.LinearSamplingFunction(1 / (0.25 * u.day)) hourly = np.arange(0, 1, 1. / 4) * u.day test_ind = samp_fn.get_index(hourly) test_hourly = samp_fn.get_unit_val(test_ind) self.assertTrue(np.all(test_hourly == hourly))
def setUp(self): # test scenario is "(latitude, longitude, time)" self.data = np.arange(27).reshape(3, 3, 3) # axis 0: one sample per 30 deg latitude lats = q.LinearSamplingFunction(1. / (30 * u.deg)) # axis 1: one sample per 60 deg longitude lons = q.LinearSamplingFunction(1. / (60 * u.deg)) #axis 2: one sample per week times = q.TimeSinceEpochFunction(7 * u.day, time.Time('2002:001', format='yday')) self.indexer = q.OrthoIndexer([lats, lons, times]) self.ui_array = q.UnitIndexNdArray(self.data, self.indexer)
def test_minmaxbin(self): """tests initialization from minmaxbin flag""" samp_fn = q.LinearSamplingFunction(minmaxbin=(-5, 5, 10), includemax=True) self.assertTrue(samp_fn.includemax) self.assertEqual(samp_fn.maxbin, 9) self.assertEqual(samp_fn.maxval, 5) self.assertEqual(0, samp_fn.get_index(-5)) self.assertEqual(9, samp_fn.get_index(5.)) self.assertEqual(5, samp_fn.get_index(0)) samp_fn = q.LinearSamplingFunction(minmaxbin=(-5, 5, 10), includemax=False) self.assertFalse(samp_fn.includemax) self.assertEqual(samp_fn.maxbin, 9) self.assertEqual(samp_fn.maxval, 5) self.assertEqual(10, samp_fn.get_index(5))
def test_xzero_create(self): """tests for correct offset when x-intercept is provided to constructor""" samp_fn = q.LinearSamplingFunction(1. / (3 * u.day), offset=4, x_zero=24 * u.day) self.assertEqual(-8, samp_fn.offset) self.assertEqual(0, samp_fn.get_index(24 * u.day)) self.assertEqual(1, samp_fn.get_index(27 * u.day))
def test_unit_mismatch(self): """scale factor not same units as unitted quantity""" samp_fn = q.LinearSamplingFunction(1 / (0.25 * u.day)) hourly = np.arange(0, 24) * u.hour hourly_i = np.trunc((hourly / (0.25 * u.day)).to( u.dimensionless_unscaled)).astype(np.int) test_ind = samp_fn.get_index(hourly) self.assertTrue(np.all(test_ind == hourly_i))
def test_transitive(self): """ensure that underlying operation is applied""" ops = [ q.LinearSamplingFunction(1 / u.day, -5), q.IdentitySamplingFunction() ] oi = q.OrthoIndexer(ops) self.assertTrue(np.all((0, 3) == oi.get_index((5 * u.day, 3))))
def __init__(self, template, ncfile, landcover_codes, year): # open file / copy info from template super(BurnedAreaCounts, self).__init__(template, ncfile) self.copyVariable('nav_lat') self.copyVariable('nav_lon') self.copyDimension('days') self.createDimension('landcover', len(landcover_codes)) srcvar = self._ncfile.variables dims = self._ncfile.dimensions self.x = len(dims['x']) self.y = len(dims['y']) self.days = len(dims['days']) self.landcover = len(landcover_codes) # make coordinate variable for landcover self.add_variable(np.array(landcover_codes, dtype=np.int8), 'landcover', ('landcover', )) # make indexers for various axes delta_lat = (srcvar['nav_lat'][1, 0] - srcvar['nav_lat'][0, 0]) * u.deg delta_lon = (srcvar['nav_lon'][0, 1] - srcvar['nav_lon'][0, 0]) * u.deg self.lat_idx = q.LinearSamplingFunction( 1. / delta_lat, x_zero=srcvar['nav_lat'][0, 0] * u.deg) self.lon_idx = q.LinearSamplingFunction( 1. / delta_lon, x_zero=srcvar['nav_lon'][0, 0] * u.deg) self.time_idx = q.TimeSinceEpochFunction( 1 * u.day, time.Time('%d:001' % year, format='yday')) self.lc_idx = q.CoordinateVariableSamplingFunction(landcover_codes) #create the netcdf variable self.count = self.create_variable('count', ('y', 'x', 'landcover', 'days'), np.int16, chunk=(self.y, self.x, self.landcover, 1)) self.last_day = -1
def init_indexers(minmax) : indexers = [] for cur_minmax in minmax : indexers.append(q.LinearSamplingFunction(minmaxbin=cur_minmax,includemax=True)) return q.OrthoIndexer(indexers)