Skip to content

Commit

Permalink
Overview figure improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sambowers committed Apr 5, 2018
1 parent 8565d47 commit 6136107
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
31 changes: 21 additions & 10 deletions biota/IO.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,22 @@ def outputGeoTiff(data, filename, geo_t, proj, output_dir = os.getcwd(), dtype =



def _buildMap(fig, ax, data, lat, lon, title ='', cbartitle = '', vmin = 10., vmax = 60., cmap = 'YlGn'):
def _buildMap(fig, ax, data, lat, lon, title ='', cbartitle = '', vmin = 10., vmax = 40., cmap = 'YlGn'):
"""
Builds a standardised map for overviewFigure().
"""

import matplotlib.pyplot as plt

im = ax.imshow(data, vmin = vmin, vmax = vmax, cmap = cmap, interpolation = 'nearest')
im = ax.imshow(data, vmin = vmin, vmax = vmax, cmap = cmap, interpolation = 'nearest', aspect = 'auto')

ax.set_yticks(np.arange(0,data.shape[0],data.shape[0]/10))
ax.set_xticks(np.arange(0,data.shape[1],data.shape[1]/10))
ax.set_yticklabels(np.arange(lat, lat - 1.01, - 0.1))
ax.set_xticklabels(np.arange(lon, lon + 1.01, 0.1))
ax.tick_params(labelsize = 5)
ax.set_xlabel('Longitude', fontsize = 5)
ax.set_ylabel('Latitude', fontsize = 5)
ax.set_xlabel('Longitude', fontsize = 5)
ax.set_title(title, fontsize = 8)

cbar = fig.colorbar(im, ax = ax, fraction = 0.046, pad = 0.04)
Expand All @@ -120,6 +120,8 @@ def overviewFigure(tile_change, output = False, show = True):
Args:
tile_change: An ALOS change object from biota.LoadChange()
output: Set to True to output a summary png image
show: Set to True to display image on screen
"""

import matplotlib.pyplot as plt
Expand All @@ -133,8 +135,15 @@ def overviewFigure(tile_change, output = False, show = True):
AGB_t2 = np.ma.array(AGB_t2, mask = np.logical_or(AGB_t2.mask, AGB_t1 < 10.))

AGB_change = AGB_t2 - AGB_t1

AGB_pcChange = 100 * (AGB_change / AGB_t1) # %
#AGB_pcChange = 100 * (AGB_change / AGB_t1) # %

# Calculate change type
change_type = tile_change.getChangeType()
change_code = tile_change.ChangeCode

# Set minor loss and minor gain to nodata
change_code[np.logical_or(change_code == 3, change_code == 4)] = 0
change_code.mask[change_type.data == 0] = True

fig = plt.figure(figsize = (7, 6))

Expand All @@ -143,28 +152,30 @@ def overviewFigure(tile_change, output = False, show = True):
_buildMap(fig, ax1, AGB_t1, tile_change.lat, tile_change.lon, title = 'AGB %s'%str(tile_change.year_t1), cbartitle = 'tC/ha')

# Plot a map of AGB at t2
ax2 = fig.add_subplot(2, 2, 2)
ax2 = fig.add_subplot(2, 2, 2, sharex = ax1, sharey = ax1)
_buildMap(fig, ax2, AGB_t2, tile_change.lat, tile_change.lon, title = 'AGB %s'%str(tile_change.year_t2), cbartitle = 'tC/ha')

# Plot a map of absolute AGB change
ax3 = fig.add_subplot(2, 2, 3)
ax3 = fig.add_subplot(2, 2, 3, sharex = ax1, sharey = ax1)
_buildMap(fig, ax3, AGB_change, tile_change.lat, tile_change.lon, title = 'AGB change (%s-%s)'%(str(tile_change.data_t1.year),str(tile_change.year_t2)),
cbartitle = 'tC/ha', vmin = -10., vmax = 10., cmap = 'RdBu')

# Plot a map of % AGB change
ax4 = fig.add_subplot(2, 2, 4)
_buildMap(fig, ax4, AGB_pcChange, tile_change.lat, tile_change.lon, title = 'AGB change (%s-%s)'%(str(tile_change.data_t1.year),str(tile_change.year_t2)),
cbartitle = '%', vmin = -50., vmax = 50., cmap = 'RdBu')
ax4 = fig.add_subplot(2, 2, 4, sharex = ax1, sharey = ax1)
_buildMap(fig, ax4, change_code, tile_change.lat, tile_change.lon, title = 'Change type (%s-%s)'%(str(tile_change.data_t1.year),str(tile_change.year_t2)),
vmin = 1., vmax = 6., cmap = 'Spectral')

plt.tight_layout()

# Output image to png
if output:
output_pattern = tile_change.output_pattern.replace('.tif','.png')

output_path = os.path.abspath(os.path.expanduser('%s/%s'%(tile_change.output_dir, output_pattern%('OverviewFigure'))))

plt.savefig(output_path, dpi = 150)

# Display image on screen
if show:
plt.show()

Expand Down
34 changes: 18 additions & 16 deletions biota/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,31 +747,33 @@ def getChangeType(self, output = False):
change_type['degradation'] = F_F & CHANGE & DECREASE
change_type['minorloss'] = (F_F | F_NF) & NOCHANGE & DECREASE

change_type['aforestation'] = NF_F & CHANGE
change_type['afforestation'] = NF_F & CHANGE
change_type['growth'] = F_F & CHANGE & INCREASE
change_type['minorgain'] = (F_F | NF_F) & NOCHANGE & INCREASE

change_type['nonforest'] = NF_NF

self.ChangeType = change_type

if output:

# Image with coded change values
output_im = np.zeros((self.ySize, self.xSize), dtype = np.int8) + 99
# Also produce a c for output
change_code = np.zeros((self.ySize, self.xSize), dtype = np.int8) + self.nodata_byte

output_im[self.ChangeType['nonforest'].data] = 0
output_im[self.ChangeType['deforestation'].data] = 1
output_im[self.ChangeType['degradation'].data] = 2
output_im[self.ChangeType['minorloss'].data] = 3
output_im[self.ChangeType['minorgain'].data] = 4
output_im[self.ChangeType['growth'].data] = 5
output_im[self.ChangeType['aforestation'].data] = 6
change_code[change_type['nonforest'].data] = 0
change_code[change_type['deforestation'].data] = 1
change_code[change_type['degradation'].data] = 2
change_code[change_type['minorloss'].data] = 3
change_code[change_type['minorgain'].data] = 4
change_code[change_type['growth'].data] = 5
change_code[change_type['afforestation'].data] = 6

# Keep things tidy
output_im[self.mask] = self.nodata_byte
change_code[self.mask] = self.nodata_byte

self.__outputGeoTiff(output_im, 'ChangeType', dtype = gdal.GDT_Byte)
# Save to class
self.ChangeType = change_type
self.ChangeCode = change_code

if output:

self.__outputGeoTiff(self.changeCode, 'ChangeType', dtype = gdal.GDT_Byte)

return self.ChangeType

Expand Down

0 comments on commit 6136107

Please sign in to comment.