Skip to content

Commit

Permalink
Remove last Basemap dependencies in mapping routines
Browse files Browse the repository at this point in the history
  • Loading branch information
wcarthur committed Oct 9, 2020
1 parent d8e635e commit 0f7a1bb
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 30 deletions.
18 changes: 10 additions & 8 deletions DataProcess/DataProcess.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,21 @@ def processData(self, restrictToWindfieldDomain=False):

if config.has_option('DataProcess', 'InputFile'):
inputFile = config.get('DataProcess', 'InputFile')
self.logger.info(f"Input file from DataProcess: {inputFile}")

if config.has_option('DataProcess', 'Source'):
source = config.get('DataProcess', 'Source')
self.logger.info('Loading %s dataset', source)
fn = config.get(source, 'filename')
path = config.get(source, 'path')
self.logger.info(f"Loading {source} dataset")
fn = config.get(source, 'Filename')
path = config.get(source, 'Path')
inputFile = pjoin(path, fn)
self.logger.info(f"Input file set to {inputFile}")

# If input file has no path information, default to tcrm input folder
if len(os.path.dirname(inputFile)) == 0:
inputFile = pjoin(self.tcrm_input_dir, inputFile)

self.logger.info("Processing {0}".format(inputFile))
self.logger.info(f"Processing {inputFile}")

self.source = config.get('DataProcess', 'Source')

Expand Down Expand Up @@ -218,8 +220,8 @@ def processData(self, restrictToWindfieldDomain=False):
except (ValueError, KeyError):

try:
self.logger.info(("Filtering input data by season:"
"season > {0}". format(startSeason)))
self.logger.info(("Filtering input data by season: "
f"season >= {startSeason}"))
# Find indicies that satisfy minimum season filter
idx = np.where(inputData['season'] >= startSeason)[0]
# Filter records:
Expand Down Expand Up @@ -263,7 +265,7 @@ def processData(self, restrictToWindfieldDomain=False):
flSaveFile(self.origin_year, np.transpose(origin_seasonOrYear),
header, ',', fmt='%d')

pressure = np.array(inputData['pressure'], 'd')
pressure = np.array(inputData['pressure'], 'float32')
novalue_index = np.where(pressure == sys.maxsize)
pressure = metutils.convert(pressure, inputPressureUnits, "hPa")
pressure[novalue_index] = sys.maxsize
Expand All @@ -278,7 +280,7 @@ def processData(self, restrictToWindfieldDomain=False):
self.progressbar.update(0.25)

try:
vmax = np.array(inputData['vmax'], 'd')
vmax = np.array(inputData['vmax'], 'float32')
except (ValueError, KeyError):
self.logger.warning("No max wind speed data")
vmax = np.empty(indicator.size, 'f')
Expand Down
2 changes: 1 addition & 1 deletion PlotInterface/AutoPlotHazard.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def plotMap(self):

for i, year in enumerate(years):
log.debug("Plotting %d-year return period hazard map", year)
title = '%d-Year Return Period Cyclonic Wind Hazard' % (year)
title = '%d-Year ARI Cyclonic Wind Hazard' % (year)
imageFilename = '%d_yrRP_hazard_map.png' % (year)
filename = pjoin(self.plotPath, imageFilename)
cbarlab = "Wind speed (%s)"%self.plotUnits.units
Expand Down
40 changes: 23 additions & 17 deletions PlotInterface/maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
=============================================
.. module:: maps
:synopsis: Generate map images using `mpl_toolkits.basemap`
:synopsis: Generate map images using `cartopy`
.. moduleauthor: Craig Arthur <craig.arthur@ga.gov.au>
Expand Down Expand Up @@ -177,7 +177,7 @@ def addGraticule(self, axes, mapobj):
:param mapobj: Current `GeoAxes` instance to annotate.
"""

from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
xmin, xmax, ymin, ymax = mapobj.get_extent()

dx = abs(xmin - xmax)
Expand All @@ -195,7 +195,10 @@ def addGraticule(self, axes, mapobj):

gl = mapobj.gridlines(xlocs=meridians, ylocs=parallels,
draw_labels=True)
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
gl.xlabels_top = False
gl.ylabels_right = False

def addCoastline(self, mapobj):
"""
Expand All @@ -205,7 +208,7 @@ def addCoastline(self, mapobj):
:param mapobj: Current `GeoAxes` instance to add coastlines to.
"""
mapobj.coastlines(resolution='50m')
mapobj.coastlines(resolution='10m')

def fillContinents(self, mapobj, fillcolor="#FFDAB5"):
"""
Expand All @@ -218,6 +221,16 @@ def fillContinents(self, mapobj, fillcolor="#FFDAB5"):
"""
mapobj.add_feature(cartopy.feature.LAND, color=fillcolor)

def maskOceans(self, mapobj, fillcolor="#66ccff"):
"""
Mask oceans with a blue background in the current `GeoAxes` instance.
:param mapobj: Current `GeoAxes` instance to colour fill the oceans
:param fillcolor: Optional colour to use (default #66CCFF)
"""

mapobj.add_feature(cartopy.feature.OCEAN, color=fillcolor)

def addMapScale(self, mapobj):
"""
Add a map scale to the curent `Basemap` instance. This
Expand All @@ -228,7 +241,7 @@ def addMapScale(self, mapobj):
:param mapobj: Current `Basemap` instance to add the scale bar to.
"""
return # TODO: migrate to cartopy
return # TODO: migrate to cartopy - see https://stackoverflow.com/questions/32333870/

midlon = (mapobj.lonmax - mapobj.lonmin) / 2.
midlat = (mapobj.latmax - mapobj.latmin) / 2.
Expand Down Expand Up @@ -344,7 +357,7 @@ def add(self, data, xgrid, ygrid, title, lvls, cbarlab, map_kwargs):
define the contour levels to use.
:param str cbarlab: Label for the color bar.
:param dict map_kwargs: A dict containing keyword arguments for
setting up the :class:`basemap` instance.
setting up the :class:`GeoAxes` instance.
"""
super(FilledContourMapFigure, self).add(data, xgrid, ygrid,
Expand Down Expand Up @@ -392,24 +405,19 @@ def subplot(self, axes, subfigure):
map keyword arguments.
"""
from mpl_toolkits.basemap import maskoceans

data, xgrid, ygrid, title, lvls, cbarlab, map_kwargs = subfigure
mapobj, mx, my = self.createMap(axes, xgrid, ygrid, map_kwargs)
dmask = data.mask
masked_data = maskoceans(xgrid, ygrid, data, inlands=False)
omask = ma.getmask(masked_data)
nmask = ma.mask_or(dmask, omask)
masked_data.mask = nmask
cmap = selectColormap(lvls)
CS = mapobj.contourf(mx, my, masked_data, levels=lvls,
CS = mapobj.contourf(mx, my, data, levels=lvls,
extend='both', cmap=cmap)
CB = self.colorbar(CS, ticks=lvls[::2], ax=axes, extend='both')
CB.set_label(cbarlab)
axes.set_title(title)
self.labelAxes(axes)
self.addGraticule(axes, mapobj)
self.addCoastline(mapobj)
self.fillContinents(mapobj, fillcolor="#EEEEEE")
self.maskOceans(mapobj)
self.addMapScale(mapobj)

class ArrayMapFigure(MapFigure):
Expand Down Expand Up @@ -461,12 +469,10 @@ def subplot(self, axes, subfigure):
map keyword arguments.
"""
from mpl_toolkits.basemap import maskoceans
data, xgrid, ygrid, title, \
datarange, cbarlab, map_kwargs = subfigure

masked_data = maskoceans(xgrid, ygrid, data, inlands=False)
subfigure = (masked_data, xgrid, ygrid, title,
subfigure = (data, xgrid, ygrid, title,
datarange, cbarlab, map_kwargs)
super(MaskedArrayMapFigure, self).subplot(axes, subfigure)

Expand Down Expand Up @@ -553,7 +559,7 @@ def plot(self, data, xgrid, ygrid, title, lvls, cbarlab, map_kwargs):
# Smooth the data to reduce 'lines-on-a-map' inferences:
dx = np.mean(np.diff(xgrid))
dmask = data.mask
data = smooth(data, int(1/dx))
#data = smooth(data, int(1/dx))
data = ma.array(data, mask=dmask)
self.add(data, xgrid, ygrid, title, lvls, cbarlab, map_kwargs)
self.cmap = sns.light_palette("orange", as_cmap=True)
Expand Down
2 changes: 1 addition & 1 deletion PlotInterface/tracks.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def saveTrackMap(tracks, xgrid, ygrid, title, map_kwargs, filename):
domain.
:param str title: Title string for the plot.
:param dict map_kwargs: Keyword args that will define the
:class:`Basemap` instance.
:class:`GeoAxes` instance.
:param str filename: Path to the file to save the image.
"""
Expand Down
10 changes: 8 additions & 2 deletions hazard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,14 @@ def calculateHazard(self, tilelimits):

log.info("Using {0} distribution for the hazard curves".format(self.evd))
#Vr = loadFilesFromPath(self.inputPath, tilelimits)
#Rp, loc, scale, shp = evfunc(Vr, self.years, self.numSim, self.nodata, self.minRecords)

#Rp, loc, scale, shp = evfunc(Vr, self.years, self.numSim, self.nodata,
#self.minRecords)
if self.evd not in ["GPD", "GEV", "power", "emp"]:
msg = (f"Invalid extreme value distribution function: {self.evd} \n"
"Set 'Hazard--ExtremeValueDistribution' to one of the following: \n"
"GPD, GEV, power or emp")
raise ValueError (msg)

if self.evd == 'GPD':
log.info("Using the GPD distribution for the hazard curves")
Vr = loadFilesFromPath(self.inputPath, tilelimits)
Expand Down
1 change: 0 additions & 1 deletion tcrm.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import argparse
import time
import sys
import imp
import os

from os.path import join as pjoin, realpath, isdir, dirname
Expand Down

0 comments on commit 0f7a1bb

Please sign in to comment.