<commit_before>#TODO: abstract grid file decorations to a dict

import os,sys,gc,time
import numpy as np
import geosoft.gxapi as gxapi
from .. import ipj as gxipj
from .. import vv as gxvv
from .. import utility as gxu


class GRDException(Exception):
    pass

#################
# constants

FILE_READ          = 0
FILE_READWRITE     = 1     #file exists, but can change properties
FILE_NEW           = 2

class GXgrd():
    """
    Grid and image class.
    
    Creation options:
    
        ====== =============================
        open() open an existing grid/image
        new()  create a new grid/image
        ====== =============================
        
    """

    gc = None
    _deleteFiles = False
    _filename = None

    def __repr__(self):
        return "{}({})".format(self.__class__,self.__dict__)

    def __str__(self):
        if self._filename == None:
            return '__memory__'
        else:
            return self._filename

    def __init__(self, fileName=None, dtype=None, mode=None, kx=1, dim=None):

        self._deleteFiles = False
        self._readonly = False
        self._np = None
        self._hpg = None

        # When working with very large grids (gigabyte+), the
        # file system cannot always keep up with closing/caching and re-opening the
        # grid. Though this is actually a system problem, we deal with this problem by attempting
        # to open a grid three times before raising an error.

        #rebuild a clean file name
        self._hgd = False
        if (fileName == None) or (len(fileName.strip()) == 0):
            self._filename = None
        else:
            self._np = GXgrd.nameParts(fileName)
            self._filename = GXgrd.decorateName(os.path.join(self._np[0],self._np[1]),self._np[4])

            if mode == FILE_NEW:
                #special case - HGD file, must work with a memory grid, save to HGD at end
                if self._np[4].lower() == 'hgd':
                    self._hgd = True

        # When working with very large grids (gigabyte+), the
        # file system cannot always keep up with closing/caching and re-opening the
        # grid. Though this is actually a system problem, we deal with this problem by attempting
        # to open a grid three times before raising an error.

        self._img = None
        attempt = 0
        while self._img == None:

            try:
                if (self._filename == None):
                    self._img = gxapi.GXIMG.create(gxu.gxType(dtype), kx, dim[0], dim[1])

                elif mode == FILE_NEW:
                    if self._hgd:
                        # for HGD grids, make a memory grid and save to an HGD on closing
                        self._img = gxapi.GXIMG.create(gxu.gxType(dtype), kx, dim[0], dim[1])
                    else:
                        self._img = gxapi.GXIMG.create_new_file(gxu.gxType(dtype), kx, dim[0], dim[1], self._filename )

                elif mode == FILE_READ:
                    self._img = gxapi.GXIMG.create_file(gxu.gxType(dtype), self._filename, gxapi.IMG_FILE_READONLY )
                    self._readonly = True

                else:
                    self._img = gxapi.GXIMG.create_file(gxu.gxType(dtype), self._filename, gxapi.IMG_FILE_READORWRITE )
            except Exception as e:
                time.sleep(0.1)
                gc.collect()
                attempt += 1
                if attempt > 10:

                    raise GRDException('Cannot open: {}\nBecause: {}'.format(self._filename,str(e)))

    def __del__(self):

        def df(fn):
            try:
                os.remove(fn)
            except OSError as e:
                pass

        if self._deleteFiles:
<commit_msg>Cleaned up gxapi exception handling on grd.py<commit_after>#TODO: abstract grid file decorations to a dict

import os,gc,time
import numpy as np
import geosoft.gxapi as gxapi
from .. import ipj as gxipj
from .. import vv as gxvv
from .. import utility as gxu


class GRDException(Exception):
    pass

#################
# constants

FILE_READ          = 0
FILE_READWRITE     = 1     #file exists, but can change properties
FILE_NEW           = 2

class GXgrd():
    """
    Grid and image class.
    
    Creation options:
    
        ====== =============================
        open() open an existing grid/image
        new()  create a new grid/image
        ====== =============================
        
    """

    gc = None
    _deleteFiles = False
    _filename = None

    def __repr__(self):
        return "{}({})".format(self.__class__,self.__dict__)

    def __str__(self):
        if self._filename == None:
            return '__memory__'
        else:
            return self._filename

    def __init__(self, fileName=None, dtype=None, mode=None, kx=1, dim=None):

        self._deleteFiles = False
        self._readonly = False
        self._np = None
        self._hpg = None

        # When working with very large grids (gigabyte+), the
        # file system cannot always keep up with closing/caching and re-opening the
        # grid. Though this is actually a system problem, we deal with this problem by attempting
        # to open a grid three times before raising an error.

        #rebuild a clean file name
        self._hgd = False
        if (fileName == None) or (len(fileName.strip()) == 0):
            self._filename = None
        else:
            self._np = GXgrd.nameParts(fileName)
            self._filename = GXgrd.decorateName(os.path.join(self._np[0],self._np[1]),self._np[4])

            if mode == FILE_NEW:
                #special case - HGD file, must work with a memory grid, save to HGD at end
                if self._np[4].lower() == 'hgd':
                    self._hgd = True

        # When working with very large grids (gigabyte+), the
        # file system cannot always keep up with closing/caching and re-opening the
        # grid. Though this is actually a system problem, we deal with this problem by attempting
        # to open a grid three times before raising an error.

        self._img = None
        attempt = 0
        while self._img == None:

            try:
                if (self._filename == None):
                    self._img = gxu.safeApiException(gxapi.GXIMG.create,
                                                     (gxu.gxType(dtype), kx, dim[0], dim[1]),
                                                     GRDException)

                elif mode == FILE_NEW:
                    if self._hgd:
                        # for HGD grids, make a memory grid and save to an HGD on closing
                        self._img = gxu.safeApiException(gxapi.GXIMG.create,
                                                         (gxu.gxType(dtype), kx, dim[0], dim[1]),
                                                         GRDException)
                    else:
                        self._img = gxu.safeApiException(gxapi.GXIMG.create_new_file,
                                                         (gxu.gxType(dtype), kx, dim[0], dim[1], self._filename),
                                                         GRDException)

                elif mode == FILE_READ:
                    self._img = gxu.safeApiException(gxapi.GXIMG.create_file,
                                                     (gxu.gxType(dtype), self._filename, gxapi.IMG_FILE_READONLY),
                                                     GRDException)
                    self._readonly = True

                else:
                    self._img = gxu.safeApiException(gxapi.GXIMG.create_file,
                                                     (gxu.gxType(dtype), self._filename, gxapi.IMG_FILE_READORWRITE),
                                                     GRDException)

            except GRDException as e:
                time.sleep(0.1)
                gc.collect()
                attempt += 1
                if attempt > 10:
                    raise GRDException('Cannot open: {}\nBecause: {}'.format(self._filename,str(e)))

    def __del__(self):

        def df(fn):
            try:
                os.remove(fn)
            except OSError as e:
                pass

        if self._deleteFiles:

{'fraction_non_alphanumeric': 0.10466812545587163, 'fraction_numerical': 0.005105762217359592, 'mean_word_length': 1.5205269607843137, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2063334', 'n_tokens_mistral': 2316, 'n_tokens_neox': 2184, 'n_words': 666}