Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed MCD and QA excl #14

Merged
merged 1 commit into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions model/BurnScarMap.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class BurnScarMap(object):
DTYPE = np.uint8
COLS = 2400
ROWS = 2400
EXCLUSION_TILES = ['v00', 'v01', 'v14', 'v15', 'v16', 'v17']

# -------------------------------------------------------------------------
# generateAnnualBurnScarMap
Expand All @@ -30,8 +31,7 @@ def generateAnnualBurnScarMap(

# Test to see if tile in list of tiles which do not
# need a burn scar product.
inclusionDays = Utils.INCLUSIONS.get(tile[3:])
exclusionDays = Utils.EXCLUSIONS.get(tile[3:])
exclusionTile = tile[3:] in BurnScarMap.EXCLUSION_TILES

try:
subdirhdfs = BurnScarMap._getAllFiles(
Expand All @@ -40,7 +40,7 @@ def generateAnnualBurnScarMap(
subdir, 'Burn Date', 'Uncertainty') for subdir in subdirhdfs]
except FileNotFoundError:
msg = 'MCD64A1 not found for {}.'.format(tile)
if exclusionDays or inclusionDays:
if exclusionTile:
if logger:
logger.info(msg + ' Using empty burn scar product.')
burnScarMapList = []
Expand Down
4 changes: 2 additions & 2 deletions model/QAMap.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def generateQA(
# -------------------------------------------------------------------------
@staticmethod
def _getGMTEDArray(tile, demDir):
exclusionDays = Utils.EXCLUSIONS.get(tile[3:])
exclusionTile = tile[3:] in Utils.QA_ANTARCTIC_EXCLUSION
try:
demSearchTerm = 'GMTED.{}.slope.tif'.format(tile)
demSlopeDatasetPath = QAMap._getStaticDatasetPath(
Expand All @@ -101,7 +101,7 @@ def _getGMTEDArray(tile, demDir):
demSlopeDataArray = demSlopeDataset.GetRasterBand(
1).ReadAsArray()
except FileNotFoundError as initialException:
if exclusionDays:
if exclusionTile:
demSlopeDataArray = np.zeros(
(BandReader.COLS, BandReader.ROWS),
dtype=QAMap.DTYPE)
Expand Down
9 changes: 5 additions & 4 deletions model/SevenClass.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SevenClassMap(object):

TIF_BASE_NAME = 'Master_7class_maxextent_'
DTYPE = np.uint8
NODATA = 253
NODATA = 250

# -------------------------------------------------------------------------
# generateSevenClass
Expand Down Expand Up @@ -43,9 +43,9 @@ def generateSevenClass(

outputSevenClassArray = np.zeros((BandReader.COLS, BandReader.ROWS))

exclusionDays = Utils.EXCLUSIONS.get(tile[3:])
exclusionTile = tile[3:] in Utils.QA_ANTARCTIC_EXCLUSION

if exclusionDays:
if exclusionTile:

if logger:
msg = 'Antarctic tiles have no seven class. Filling nodata.'
Expand All @@ -57,7 +57,8 @@ def generateSevenClass(
staticSevenPath = SevenClassMap._getStaticSevenClassPath(
staticSevenClassDir, tile)
staticSevenDataset = gdal.Open(staticSevenPath)
staticSevenArray = staticSevenDataset.GetRasterBand(1).ReadAsArray()
staticSevenArray = \
staticSevenDataset.GetRasterBand(1).ReadAsArray()

restArray = annualProductArray.copy()

Expand Down
4 changes: 3 additions & 1 deletion model/Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ class Utils(object):
'v01': DayRange(161, 256),
'v02': DayRange(145, 288),
'v03': DayRange(129, 304)}

EXCLUSIONS = {'v17': DayRange(177, 256),
'v16': DayRange(161, 256),
'v15': DayRange(145, 288),
'v14': DayRange(129, 304)}

QA_ANTARCTIC_EXCLUSION = ['v17', 'v16', 'v15']

# -------------------------------------------------------------------------
# getImageName
# -------------------------------------------------------------------------
Expand Down
46 changes: 40 additions & 6 deletions model/tests/test_BurnScarMap.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ class BurnScarTestCase(unittest.TestCase):
# testHandleMcdError
# -------------------------------------------------------------------------
def testHandleMcdError(self):
tile0 = 'h16v01'
tile1 = 'h13v14'
tile0 = 'h20v00'
tile1 = 'h16v01'
tile2 = 'h15v14'
tile3 = 'h13v15'
tile4 = 'h13v16'
tile5 = 'h13v17'
dummyDir = tempfile.gettempdir()

streamHandler = logging.StreamHandler(sys.stdout)
Expand All @@ -35,23 +39,53 @@ def testHandleMcdError(self):

self.assertTrue(os.path.exists(burnScarOutputPathTile0))

burnScarOutputPathTile0 = BurnScarMap.generateAnnualBurnScarMap(
burnScarOutputPathTile1 = BurnScarMap.generateAnnualBurnScarMap(
'MOD', 2020, tile=tile1, mcdDir='Test',
classifierName='rf', outDir=dummyDir, logger=logger)

self.assertTrue(os.path.exists(burnScarOutputPathTile0))
self.assertTrue(os.path.exists(burnScarOutputPathTile1))

burnScarOutputPathTile2 = BurnScarMap.generateAnnualBurnScarMap(
'MOD', 2020, tile=tile2, mcdDir='Test',
classifierName='rf', outDir=dummyDir, logger=logger)

self.assertTrue(os.path.exists(burnScarOutputPathTile2))

burnScarOutputPathTile3 = BurnScarMap.generateAnnualBurnScarMap(
'MOD', 2020, tile=tile3, mcdDir='Test',
classifierName='rf', outDir=dummyDir, logger=logger)

self.assertTrue(os.path.exists(burnScarOutputPathTile3))

burnScarOutputPathTile4 = BurnScarMap.generateAnnualBurnScarMap(
'MOD', 2020, tile=tile4, mcdDir='Test',
classifierName='rf', outDir=dummyDir, logger=logger)

self.assertTrue(os.path.exists(burnScarOutputPathTile4))

burnScarOutputPathTile5 = BurnScarMap.generateAnnualBurnScarMap(
'MOD', 2020, tile=tile5, mcdDir='Test',
classifierName='rf', outDir=dummyDir, logger=logger)

self.assertTrue(os.path.exists(burnScarOutputPathTile5))

# -------------------------------------------------------------------------
# testHandleMcdError
# -------------------------------------------------------------------------
def testThrowMcdError(self):
tile = 'h09v05'
tile0 = 'h16v02'
tile1 = 'h17v13'
dummyDir = tempfile.gettempdir()

streamHandler = logging.StreamHandler(sys.stdout)
logger.addHandler(streamHandler)

with self.assertRaises(FileNotFoundError):
BurnScarMap.generateAnnualBurnScarMap(
'MOD', 2020, tile=tile, mcdDir='Test',
'MOD', 2020, tile=tile0, mcdDir='Test',
classifierName='rf', outDir=dummyDir, logger=logger)

with self.assertRaises(FileNotFoundError):
BurnScarMap.generateAnnualBurnScarMap(
'MOD', 2020, tile=tile1, mcdDir='Test',
classifierName='rf', outDir=dummyDir, logger=logger)