<commit_before>""" Classes for loading digital elevation models as numeric grids """

import os, sys
import numpy as np
import numexpr
import matplotlib
import matplotlib.pyplot as plt

import osr
from osgeo import gdal, gdalconst

sys.path.append('/usr/bin')
import gdal_merge

from copy import copy
from rasterio.fill import fillnodata
from utils import BoundingBox
sys.setrecursionlimit(10000)

FLOAT32_MIN = np.finfo(np.float32).min
GDAL_DRIVER_NAME = 'GTiff'


class CalculationMixin(object):

    def __init__(self):
        
        pass

    def _calculate_slope(self):
        """
        Calculate gradient of grid in x and y directions.

        Pads boundary so as to return slope grids of same size as object's 
        grid data
        
        Returns:
            slope_x: slope in x direction
            slope_y: slope in y direction
        """

        dx = self._georef_info.dx
        dy = self._georef_info.dy

        PAD_DX = 2
        PAD_DY = 2

        self._pad_boundary(PAD_DX, PAD_DY)
        z_pad = self._griddata
        
        slope_x = (z_pad[1:-1, 2:] - z_pad[1:-1, :-2])/(2*dx)
        slope_y = (z_pad[2, 1:-1] - z_pad[:-2, 1:-1])/(2*dx)
        
        return slope_x, slope_y
    
    def _calculate_laplacian(self):
        """
        Calculate curvature of grid in y direction.
        """
        
        return self._calculate_directional_laplacian(0)

    def _calculate_directional_laplacian(self, alpha):
        """
        Calculate curvature of grid in arbitrary direction.

        Args:
            alpha: direction angle (azimuth) in radians. 0 is north or y-axis.

        Returns:
            del2s: grid of curvature values
        """
 
        dx = self._georef_info.dx
        dy = self._georef_info.dy       
        z = self._griddata
        nan_idx = np.isnan(z)
        z[nan_idx] = 0
        
        dz_dx = np.diff(z, 1, 1)/dx
        d2z_dxdy = np.diff(dz_dx, 1, 0)/dx
        pad_x = np.zeros((d2z_dxdy.shape[0], 1))
        d2z_dxdy = np.hstack([pad_x, d2z_dxdy])<commit_msg>Fix old typo in finite differences calculation<commit_after>""" Classes for loading digital elevation models as numeric grids """

import os, sys
import numpy as np
import numexpr
import matplotlib
import matplotlib.pyplot as plt

import osr
from osgeo import gdal, gdalconst

sys.path.append('/usr/bin')
import gdal_merge

from copy import copy
from rasterio.fill import fillnodata
from utils import BoundingBox
sys.setrecursionlimit(10000)

FLOAT32_MIN = np.finfo(np.float32).min
GDAL_DRIVER_NAME = 'GTiff'


class CalculationMixin(object):

    def __init__(self):
        
        pass

    def _calculate_slope(self):
        """
        Calculate gradient of grid in x and y directions.

        Pads boundary so as to return slope grids of same size as object's 
        grid data
        
        Returns:
            slope_x: slope in x direction
            slope_y: slope in y direction
        """

        dx = self._georef_info.dx
        dy = self._georef_info.dy

        PAD_DX = 2
        PAD_DY = 2

        self._pad_boundary(PAD_DX, PAD_DY)
        z_pad = self._griddata
        
        slope_x = (z_pad[1:-1, 2:] - z_pad[1:-1, :-2])/(2*dx)
        slope_y = (z_pad[2:, 1:-1] - z_pad[:-2, 1:-1])/(2*dx)
        
        return slope_x, slope_y
    
    def _calculate_laplacian(self):
        """
        Calculate curvature of grid in y direction.
        """
        
        return self._calculate_directional_laplacian(0)

    def _calculate_directional_laplacian(self, alpha):
        """
        Calculate curvature of grid in arbitrary direction.

        Args:
            alpha: direction angle (azimuth) in radians. 0 is north or y-axis.

        Returns:
            del2s: grid of curvature values
        """
 
        dx = self._georef_info.dx
        dy = self._georef_info.dy       
        z = self._griddata
        nan_idx = np.isnan(z)
        z[nan_idx] = 0
        
        dz_dx = np.diff(z, 1, 1)/dx
        d2z_dxdy = np.diff(dz_dx, 1, 0)/dx
        pad_x = np.zeros((d2z_dxdy.shape[0], 1))
        d2z_dxdy = np.hstack([pad_x, d2z_dxdy])
{'fraction_non_alphanumeric': 0.09110681488641856, 'fraction_numerical': 0.018849685838569358, 'mean_word_length': 2.050110537951363, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3898103', 'n_tokens_mistral': 1467, 'n_tokens_neox': 1356, 'n_words': 369}