Skip to content

Commit

Permalink
changed the spatial aggregation for globvapour and snow area extent p…
Browse files Browse the repository at this point in the history
…rovider to use the cython aggregator.
  • Loading branch information
hans-permana committed Apr 14, 2016
1 parent 18e46b6 commit 39240e9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
18 changes: 10 additions & 8 deletions src/cablab/providers/globvapour.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import netCDF4
from cablab import BaseCubeSourceProvider
from cablab.util import NetCDFDatasetCache, aggregate_images
from skimage.transform import resize
import cablab.resize as resize

VAR_NAME = 'tcwv_res'

Expand Down Expand Up @@ -51,17 +51,19 @@ def compute_variable_images_from_sources(self, index_to_weight):
i = next(iter(new_indices))
file, time_index = self._get_file_and_time_index(i)
dataset = self.dataset_cache.get_dataset(file)
globvapour = resize(dataset.variables[VAR_NAME][time_index, :, :], (720, 1440), preserve_range=True,
order=3)
globvapour = dataset.variables[VAR_NAME]
_, lat_size, lon_size = globvapour.shape
globvapour = resize.resize(lon_size, lat_size, globvapour[time_index, :, :], 1440, 720, order=3)
else:
images = [None] * len(new_indices)
weights = [None] * len(new_indices)
j = 0
for i in new_indices:
file, time_index = self._get_file_and_time_index(i)
dataset = self.dataset_cache.get_dataset(file)
variable = resize(dataset.variables[VAR_NAME][time_index, :, :], (720, 1440), preserve_range=True,
order=3)
variable = dataset.variables[VAR_NAME]
_, lat_size, lon_size = variable.shape
variable = resize.resize(lon_size, lat_size, variable[time_index, :, :], 1440, 720, order=3)
images[j] = variable
weights[j] = index_to_weight[i]
j += 1
Expand All @@ -86,9 +88,9 @@ def _init_source_time_ranges(self):
dir_names = os.listdir(self.dir_path)

for dir_name in dir_names:
file_names = os.listdir(os.path.join(self.dir_path,dir_name))
file_names = os.listdir(os.path.join(self.dir_path, dir_name))
for file_name in file_names:
file = os.path.join(self.dir_path,dir_name,file_name)
file = os.path.join(self.dir_path, dir_name, file_name)
dataset = self.dataset_cache.get_dataset(file)
time = dataset.variables['time']
dates1 = netCDF4.num2date(time[:], 'days since 1970-01-01 00:00:00', calendar='gregorian')
Expand All @@ -103,4 +105,4 @@ def _init_source_time_ranges(self):
@staticmethod
def _last_day_of_month(any_day):
next_month = any_day.replace(day=28) + timedelta(days=4)
return next_month - timedelta(days=next_month.day)
return next_month - timedelta(days=next_month.day)
11 changes: 4 additions & 7 deletions src/cablab/providers/snow_area_extent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from cablab import BaseCubeSourceProvider
from cablab.util import NetCDFDatasetCache
from skimage.measure import block_reduce
import cablab.resize as resize

VAR_NAME = 'MFSC'

Expand Down Expand Up @@ -56,7 +57,7 @@ def compute_variable_images_from_sources(self, index_to_weight):
snow_area_extent.filled(numpy.nan)
else:
weight_sum = 0.0
snow_area_extent_sum = numpy.zeros((18000, 36000), dtype=numpy.float32)
snow_area_extent_sum = numpy.zeros((18000, 36000), dtype=numpy.float64)
for i in new_indices:
weight = index_to_weight[i]
file, time_index = self._get_file_and_time_index(i)
Expand All @@ -75,12 +76,8 @@ def compute_variable_images_from_sources(self, index_to_weight):
raise ValueError('illegal downscale factor, '
'the downscale factor has to be an integer value.')

print(snow_area_extent[snow_area_extent == numpy.nan])

snow_area_extent = block_reduce(snow_area_extent, (latitude_downscale_factor, longitude_downscale_factor),
func=numpy.ma.mean)

print(snow_area_extent[snow_area_extent == numpy.nan])
snow_area_extent = resize.resize(lon_size, lat_size, snow_area_extent, self.cube_config.grid_width,
self.cube_config.grid_height)

return {VAR_NAME: snow_area_extent}

Expand Down
4 changes: 2 additions & 2 deletions test/providers/test_snow_area_extent.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_temporal_coverage(self):
def test_get_images(self):
provider = SnowAreaExtentProvider(CubeConfig(), SOURCE_DIR)
provider.prepare()
images = provider.compute_variable_images(datetime(2005, 1, 1), datetime(2005, 1, 9))
images = provider.compute_variable_images(datetime(2003, 1, 1), datetime(2003, 2, 11))
self.assertIsNotNone(images)
self.assertTrue('MFSC' in images)
image = images['MFSC']
Expand All @@ -57,7 +57,7 @@ def test_get_images(self):
def test_get_images_from_single_time_period(self):
provider = SnowAreaExtentProvider(CubeConfig(), SOURCE_DIR)
provider.prepare()
images = provider.compute_variable_images(datetime(2003, 1, 1), datetime(2003, 1, 9))
images = provider.compute_variable_images(datetime(2003, 1, 1), datetime(2003, 1, 31))
self.assertIsNotNone(images)
self.assertTrue('MFSC' in images)
image = images['MFSC']
Expand Down

0 comments on commit 39240e9

Please sign in to comment.