# Path: yatsm/utils.py
# def write_output(raster, output, image_ds, gdal_frmt, ndv, band_names=None):
#     """ Write raster to output file """
#     from osgeo import gdal, gdal_array
# 
#     logger.debug('Writing output to disk')
# 
#     driver = gdal.GetDriverByName(str(gdal_frmt))
# 
#     if len(raster.shape) > 2:
#         nband = raster.shape[2]
#     else:
#         nband = 1
# 
#     ds = driver.Create(
#         output,
#         image_ds.RasterXSize, image_ds.RasterYSize, nband,
#         gdal_array.NumericTypeCodeToGDALTypeCode(raster.dtype.type)
#     )
# 
#     if band_names is not None:
#         if len(band_names) != nband:
#             logger.error('Did not get enough names for all bands')
#             sys.exit(1)
# 
#     if raster.ndim > 2:
#         for b in range(nband):
#             logger.debug('    writing band {b}'.format(b=b + 1))
#             ds.GetRasterBand(b + 1).WriteArray(raster[:, :, b])
#             ds.GetRasterBand(b + 1).SetNoDataValue(ndv)
# 
#             if band_names is not None:
#                 ds.GetRasterBand(b + 1).SetDescription(band_names[b])
#                 ds.GetRasterBand(b + 1).SetMetadata({
#                     'band_{i}'.format(i=b + 1): band_names[b]
#                 })
#     else:
#         logger.debug('    writing band')
#         ds.GetRasterBand(1).WriteArray(raster)
#         ds.GetRasterBand(1).SetNoDataValue(ndv)
# 
#         if band_names is not None:
#             ds.GetRasterBand(1).SetDescription(band_names[0])
#             ds.GetRasterBand(1).SetMetadata({'band_1': band_names[0]})
# 
#     ds.SetProjection(image_ds.GetProjection())
#     ds.SetGeoTransform(image_ds.GetGeoTransform())
# 
#     ds = None


import logging
import os
import click
from osgeo import gdal
from . import options
from ..mapping import get_change_date, get_change_num
from ..utils import write_output
    """
    gdal_frmt = str(gdal_frmt)  # GDAL GetDriverByName doesn't work on Unicode

    frmt = '%Y%j'
    start_txt, end_txt = start_date.strftime(frmt), end_date.strftime(frmt)
    start_date, end_date = start_date.toordinal(), end_date.toordinal()

    try:
        image_ds = gdal.Open(image, gdal.GA_ReadOnly)
    except:
        logger.error('Could not open example image for reading')
        raise

    if map_type in ('first', 'last'):
        changemap, magnitudemap, magnitude_indices = get_change_date(
            start_date, end_date, result, image_ds,
            first=map_type == 'first', out_format=out_date_frmt,
            magnitude=magnitude,
            ndv=ndv, pattern='yatsm_r*', warn_on_empty=warn_on_empty
        )

        band_names = ['ChangeDate_s{s}-e{e}'.format(s=start_txt, e=end_txt)]
        write_output(changemap, output, image_ds, gdal_frmt, ndv,
                     band_names=band_names)

        if magnitudemap is not None:
            band_names = (['Magnitude Index {}'.format(i) for
                           i in magnitude_indices])
            name, ext = os.path.splitext(output)
            output = name + '_mag' + ext

# Path: yatsm/utils.py
# def write_output(raster, output, image_ds, gdal_frmt, ndv, band_names=None):
#     """ Write raster to output file """
#     from osgeo import gdal, gdal_array
# 
#     logger.debug('Writing output to disk')
# 
#     driver = gdal.GetDriverByName(str(gdal_frmt))
# 
#     if len(raster.shape) > 2:
#         nband = raster.shape[2]
#     else:
#         nband = 1
# 
#     ds = driver.Create(
#         output,
#         image_ds.RasterXSize, image_ds.RasterYSize, nband,
#         gdal_array.NumericTypeCodeToGDALTypeCode(raster.dtype.type)
#     )
# 
#     if band_names is not None:
#         if len(band_names) != nband:
#             logger.error('Did not get enough names for all bands')
#             sys.exit(1)
# 
#     if raster.ndim > 2:
#         for b in range(nband):
#             logger.debug('    writing band {b}'.format(b=b + 1))
#             ds.GetRasterBand(b + 1).WriteArray(raster[:, :, b])
#             ds.GetRasterBand(b + 1).SetNoDataValue(ndv)
# 
#             if band_names is not None:
#                 ds.GetRasterBand(b + 1).SetDescription(band_names[b])
#                 ds.GetRasterBand(b + 1).SetMetadata({
#                     'band_{i}'.format(i=b + 1): band_names[b]
#                 })
#     else:
#         logger.debug('    writing band')
#         ds.GetRasterBand(1).WriteArray(raster)
#         ds.GetRasterBand(1).SetNoDataValue(ndv)
# 
#         if band_names is not None:
#             ds.GetRasterBand(1).SetDescription(band_names[0])
#             ds.GetRasterBand(1).SetMetadata({'band_1': band_names[0]})
# 
#     ds.SetProjection(image_ds.GetProjection())
#     ds.SetGeoTransform(image_ds.GetGeoTransform())
# 
#     ds = None


# Path: yatsm/cli/changemap.py
import logging
import os
import click
from osgeo import gdal
from . import options
from ..mapping import get_change_date, get_change_num
from ..utils import write_output

    """
    gdal_frmt = str(gdal_frmt)  # GDAL GetDriverByName doesn't work on Unicode

    frmt = '%Y%j'
    start_txt, end_txt = start_date.strftime(frmt), end_date.strftime(frmt)
    start_date, end_date = start_date.toordinal(), end_date.toordinal()

    try:
        image_ds = gdal.Open(image, gdal.GA_ReadOnly)
    except:
        logger.error('Could not open example image for reading')
        raise

    if map_type in ('first', 'last'):
        changemap, magnitudemap, magnitude_indices = get_change_date(
            start_date, end_date, result, image_ds,
            first=map_type == 'first', out_format=out_date_frmt,
            magnitude=magnitude,
            ndv=ndv, pattern='yatsm_r*', warn_on_empty=warn_on_empty
        )

        band_names = ['ChangeDate_s{s}-e{e}'.format(s=start_txt, e=end_txt)]
        write_output(changemap, output, image_ds, gdal_frmt, ndv,
                     band_names=band_names)

        if magnitudemap is not None:
            band_names = (['Magnitude Index {}'.format(i) for
                           i in magnitude_indices])
            name, ext = os.path.splitext(output)
            output = name + '_mag' + ext

            write_output(magnitudemap, output, image_ds, gdal_frmt, ndv,