<commit_before>import numpy
from osgeo import gdal
from osgeo.gdalconst import GA_ReadOnly
from core import gdalObject, gdalProperties
from utils import change_geotransform


def createNewImage(outfilepath, cols, rows, bands, datatype,
                       drivername=None, geotransform=None, projection=None):
        """
        Creates a new image file using specifed image properties.

        Required Argument(s):
            - outfilepath: The output path for the new file.
            - cols: Number of columns in the output image.
            - rows: Number of rows in the output image.
            - bands: Number of bands in the output image.
            - datatype: The datatype for the bands in the output image.

        Optional Argument(s):
            - drivername: This is the file type for the output specified by GDAL driver name. The default is GeoTIFF.
            - geotransform: This is the geotransform for the output image.
            - projection: This is the projection for the output image.

        Returns:
            - None
        """

        if drivername is None:
            drivername = "GTiff"

        driver = gdal.GetDriverByName(drivername)
        driver.Register()

        gdalImage = driver.Create(outfilepath, cols, rows, bands, datatype)

        if gdalImage is None:
            raise Exception("Error encountered creating output file.") #TODO convert to classed exception
        else:

            if geotransform:
                gdalImage.SetGeoTransform(geotransform)

            if projection:
                gdalImage.SetProjection(projection)

        return gdalImage


def copySchemaToNewImage(gdalPropertiesObject, outfilepath, numberofbands=None, drivername=None, datatype=None,
                         cols=None, rows=None, geotransform=None, projection=None):
        """
        Creates a new image using the properties of an existing image which has been loaded into a gdalPropertiesObject.

        Required Argument(s):
            - gdalPropertiesObject: A properties object containing all the original image's attributes.
            - outfilepath: The output path for the new file.

        Optional Argument(s):
            - numberofbands: If this is not specified, it will use the number of bands in the input image.
            - drivername: This is the file type for the output specified by GDAL driver name. The default is GeoTIFF.
            - datatype: The datatype of the pixels in the new image.
            - cols: The number of columns to use in the new image.
            - rows: The number of rows to use in the new image.
            - geotransform: The geotransform to set for the new image.
            - projection: The projection to set for the new image.

            Any omitted optional arguments, unless otherwise specified, will have a default value set by that property
            of the copied image.

        Returns:
            - newimage: gdal image object of the new image.
        """

        if drivername is None:
            drivername = "GTiff"

        if datatype is None:
            datatype = gdalPropertiesObject.datatype

        if cols is None:
            cols = gdalPropertiesObject.cols

        if rows is None:
            rows = gdalPropertiesObject.rows

        if geotransform is None:
            geotransform = gdalPropertiesObject.geotransform

        if projection is None:
            projection = gdalPropertiesObject.projection

        if not numberofbands:
            numberofbands = gdalPropertiesObject.bands

        newimage = createNewImage(outfilepath, cols, rows, numberofbands, datatype, drivername=drivername,
                                  geotransform=geotransform, projection=projection)

        return newimage


def openImage(infilepath):
    """
    Opens a raster file and updates the instance attributes.

    Required Argument(s):
        - infilepath: The path to the raster to be opened.

    Optional Argument(s):
        - None

    Returns:
        - None
    """

    gdal.AllRegister()
    image = gdal.Open(infilepath, GA_ReadOnly)

    if image is None:
        raise Exception("Error encountered opening file.") #TODO convert to classed exception

    return image


def clip_raster_to_extent(inraster, outraster, xmin, ymin, xextent, yextent):
    """

    """
    #TODO Docstring

    original = openImage(inraster)
    originalproperties = gdalProperties(original)

    newgeotransform = change_geotransform(originalproperties.geotransform, xmin, ymin)

    outds = copySchemaToNewImage(originalproperties, outraster, cols=xextent, rows=yextent, geotransform=newgeotransform)

    for i in range(1, originalproperties.bands + 1):
        band = original.GetRasterBand(i)
        outband = outds.GetRasterBand(i)

        nodatavalue = band.GetNoDataValue()
        data = band.ReadAsArray(xmin, ymin, xextent, yextent)

        outband.WriteArray(data, 0, 0)
        outband.SetNoDataValue(nodatavalue)
        outband.FlushCache()
<commit_msg>Add read option to open image method<commit_after>import numpy
from osgeo import gdal
from osgeo.gdalconst import GA_ReadOnly
from core import gdalObject, gdalProperties
from utils import change_geotransform


def createNewImage(outfilepath, cols, rows, bands, datatype,
                       drivername=None, geotransform=None, projection=None):
        """
        Creates a new image file using specifed image properties.

        Required Argument(s):
            - outfilepath: The output path for the new file.
            - cols: Number of columns in the output image.
            - rows: Number of rows in the output image.
            - bands: Number of bands in the output image.
            - datatype: The datatype for the bands in the output image.

        Optional Argument(s):
            - drivername: This is the file type for the output specified by GDAL driver name. The default is GeoTIFF.
            - geotransform: This is the geotransform for the output image.
            - projection: This is the projection for the output image.

        Returns:
            - None
        """

        if drivername is None:
            drivername = "GTiff"

        driver = gdal.GetDriverByName(drivername)
        driver.Register()

        gdalImage = driver.Create(outfilepath, cols, rows, bands, datatype)

        if gdalImage is None:
            raise Exception("Error encountered creating output file.") #TODO convert to classed exception
        else:

            if geotransform:
                gdalImage.SetGeoTransform(geotransform)

            if projection:
                gdalImage.SetProjection(projection)

        return gdalImage


def copySchemaToNewImage(gdalPropertiesObject, outfilepath, numberofbands=None, drivername=None, datatype=None,
                         cols=None, rows=None, geotransform=None, projection=None):
        """
        Creates a new image using the properties of an existing image which has been loaded into a gdalPropertiesObject.

        Required Argument(s):
            - gdalPropertiesObject: A properties object containing all the original image's attributes.
            - outfilepath: The output path for the new file.

        Optional Argument(s):
            - numberofbands: If this is not specified, it will use the number of bands in the input image.
            - drivername: This is the file type for the output specified by GDAL driver name. The default is GeoTIFF.
            - datatype: The datatype of the pixels in the new image.
            - cols: The number of columns to use in the new image.
            - rows: The number of rows to use in the new image.
            - geotransform: The geotransform to set for the new image.
            - projection: The projection to set for the new image.

            Any omitted optional arguments, unless otherwise specified, will have a default value set by that property
            of the copied image.

        Returns:
            - newimage: gdal image object of the new image.
        """

        if drivername is None:
            drivername = "GTiff"

        if datatype is None:
            datatype = gdalPropertiesObject.datatype

        if cols is None:
            cols = gdalPropertiesObject.cols

        if rows is None:
            rows = gdalPropertiesObject.rows

        if geotransform is None:
            geotransform = gdalPropertiesObject.geotransform

        if projection is None:
            projection = gdalPropertiesObject.projection

        if not numberofbands:
            numberofbands = gdalPropertiesObject.bands

        newimage = createNewImage(outfilepath, cols, rows, numberofbands, datatype, drivername=drivername,
                                  geotransform=geotransform, projection=projection)

        return newimage


def openImage(infilepath, readonly=True):
    """
    Opens a raster file and updates the instance attributes.

    Required Argument(s):
        - infilepath: The path to the raster to be opened.

    Optional Argument(s):
        - None

    Returns:
        - None
    """

    gdal.AllRegister()
    if readonly is True:
        image = gdal.Open(infilepath, GA_ReadOnly)
    elif readonly is False:
        image = gdal.Open(infilepath)
    else:
        raise Exception("Error: the read status could not be be determined.")

    if image is None:
        raise Exception("Error encountered opening file.") #TODO convert to classed exception

    return image


def clip_raster_to_extent(inraster, outraster, xmin, ymin, xextent, yextent):
    """

    """
    #TODO Docstring

    original = openImage(inraster)
    originalproperties = gdalProperties(original)

    newgeotransform = change_geotransform(originalproperties.geotransform, xmin, ymin)

    outds = copySchemaToNewImage(originalproperties, outraster, cols=xextent, rows=yextent, geotransform=newgeotransform)

    for i in range(1, originalproperties.bands + 1):
        band = original.GetRasterBand(i)
        outband = outds.GetRasterBand(i)

        nodatavalue = band.GetNoDataValue()
        data = band.ReadAsArray(xmin, ymin, xextent, yextent)

        outband.WriteArray(data, 0, 0)
        outband.SetNoDataValue(nodatavalue)
        outband.FlushCache()

{'fraction_non_alphanumeric': 0.061033777864304486, 'fraction_numerical': 0.0007787403874233427, 'mean_word_length': 2.316333118140736, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5754039', 'n_tokens_mistral': 2806, 'n_tokens_neox': 2637, 'n_words': 964}