<commit_before>"""
*** OBJECTIVE

1. creating the TOPKAPI parameter file from GIS binary files (binary
grid format).  -main routine 'creat_param_file'

*** COMMENT

In the present program, to write the parameter file,
generally the grid are tranformed in an array by rearranging the cell
order from the West to East and North to South, as in the following
example:

The grid format is like:

GIS bingrid file
-9999  -9999  -9999  -9999  -9999  -9999  -9999
-9999      0      1  -9999  -9999  -9999  -9999
-9999      2      3     4   -9999  -9999  -9999
    5      6      7     8       9  -9999  -9999
-9999     10     11    12      13     14  -9999
-9999  -9999     15    16      17  -9999  -9999
-9999  -9999  -9999  -9999     18  -9999  -9999
-9999  -9999  -9999 -9999   -9999  -9999  -9999

The corresponding array extracted and ordered from West to East, North
to South is: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

"""
import sys
from warnings import warn
from ConfigParser import SafeConfigParser

import numpy as np
from numpy import ma
import networkx as nx
from osgeo import gdal

from pytopkapi import utils as ut

def compute_cell_coordinates(mask_fname):
    dset = gdal.Open(mask_fname)
    mask = dset.ReadAsArray()

    x0, dx, fy, y0, fx, dy = dset.GetGeoTransform()

    # Adjust x0 and y0 to give pixel centres. GDAL specifies (x0, y0)
    # as the top left corner of the top left pixel. PyTOPKAPI expects
    # pixel centres. At the centre of the first pixel the following
    # holds: (Xpixel, Yline) == (0.5, 0.5)
    x0 = x0 + dx/2.0 + fy/2.0
    y0 = y0 + fx/2.0 + dy/2.0

    Yline, Xpixel = np.nonzero(mask == 1)

    Xgeo = x0 + Xpixel*dx + Yline*fy
    Ygeo = y0 + Xpixel*fx + Yline*dy

    return Xgeo, Ygeo

def read_raster(rast_fname, file_format='GTiff'):
    """Read the data in a raster file

    Parameters
    ----------
    rast_fname : string
        The path to the raster file.
    file_format : string
        The file format of the raster file, currently this can only be
        GeoTIFF ('GTiff' - default).

    Returns
    -------
    data : Numpy ndarray
        The 2D raster data in a Numpy array. The dtype of the returned
        array is the same as the data type stored in the raster file.

    """
    if file_format != 'GTiff':
        err_str = 'Reading %s files not implemented.' % file_format
        raise NotImplementedError(err_str)
    else:
        dset = gdal.Open(rast_fname)
        data = dset.ReadAsArray()

    return data

def generate_param_file(ini_fname, isolated_cells=False):
    """Create a PyTOPKAPI parameter file

    Generates a parameter file from the catchment data provided in a
    set of georeferenced raster files. The input files may be GeoTIFF
    or 32-bit raster files with ArcGIS style headers.

    Parameters
    ----------
    ini_fname : string

        The path to an ini-style config file specifying the input data
        file locations and format. The ini file should contain the
        following sections and parameters:

        [raster_files]
        dem_fname = <path to DEM file>
        mask_fname = <path to catchment mask file>
        soil_depth_fname = <path to soil depth file><commit_msg>DOC: Update docstring to reflect current development status.<commit_after>"""
*** OBJECTIVE

1. creating the TOPKAPI parameter file from GIS binary files (binary
grid format).  -main routine 'creat_param_file'

*** COMMENT

In the present program, to write the parameter file,
generally the grid are tranformed in an array by rearranging the cell
order from the West to East and North to South, as in the following
example:

The grid format is like:

GIS bingrid file
-9999  -9999  -9999  -9999  -9999  -9999  -9999
-9999      0      1  -9999  -9999  -9999  -9999
-9999      2      3     4   -9999  -9999  -9999
    5      6      7     8       9  -9999  -9999
-9999     10     11    12      13     14  -9999
-9999  -9999     15    16      17  -9999  -9999
-9999  -9999  -9999  -9999     18  -9999  -9999
-9999  -9999  -9999 -9999   -9999  -9999  -9999

The corresponding array extracted and ordered from West to East, North
to South is: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

"""
import sys
from warnings import warn
from ConfigParser import SafeConfigParser

import numpy as np
from numpy import ma
import networkx as nx
from osgeo import gdal

from pytopkapi import utils as ut

def compute_cell_coordinates(mask_fname):
    dset = gdal.Open(mask_fname)
    mask = dset.ReadAsArray()

    x0, dx, fy, y0, fx, dy = dset.GetGeoTransform()

    # Adjust x0 and y0 to give pixel centres. GDAL specifies (x0, y0)
    # as the top left corner of the top left pixel. PyTOPKAPI expects
    # pixel centres. At the centre of the first pixel the following
    # holds: (Xpixel, Yline) == (0.5, 0.5)
    x0 = x0 + dx/2.0 + fy/2.0
    y0 = y0 + fx/2.0 + dy/2.0

    Yline, Xpixel = np.nonzero(mask == 1)

    Xgeo = x0 + Xpixel*dx + Yline*fy
    Ygeo = y0 + Xpixel*fx + Yline*dy

    return Xgeo, Ygeo

def read_raster(rast_fname, file_format='GTiff'):
    """Read the data in a raster file

    Parameters
    ----------
    rast_fname : string
        The path to the raster file.
    file_format : string
        The file format of the raster file, currently this can only be
        GeoTIFF ('GTiff' - default).

    Returns
    -------
    data : Numpy ndarray
        The 2D raster data in a Numpy array. The dtype of the returned
        array is the same as the data type stored in the raster file.

    """
    if file_format != 'GTiff':
        err_str = 'Reading %s files not implemented.' % file_format
        raise NotImplementedError(err_str)
    else:
        dset = gdal.Open(rast_fname)
        data = dset.ReadAsArray()

    return data

def generate_param_file(ini_fname, isolated_cells=False):
    """Create a PyTOPKAPI parameter file

    Generates a parameter file from the catchment data provided in a
    set of georeferenced raster files. The input files are currently
    limited to GeoTIFF. Support for 32-bit raster files with ArcGIS
    style headers is planned.

    Parameters
    ----------
    ini_fname : string

        The path to an ini-style config file specifying the input data
        file locations and format. The ini file should contain the
        following sections and parameters:

        [raster_files]
        dem_fname = <path to DEM file>
        mask_fname = <path to catchment mask file>
        soil_depth_fname = <path to soil depth file>
{'fraction_non_alphanumeric': 0.07433056325023084, 'fraction_numerical': 0.07171437365343182, 'mean_word_length': 2.337955829481253, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3617466', 'n_tokens_mistral': 2398, 'n_tokens_neox': 2026, 'n_words': 869}