<commit_before>"""
This file is part of pyS5p

https://github.com/rmvanhees/pys5p.git

The class LV2io provides read access to S5p Tropomi S5P_OFFL_L2 products

Note
----
The Python h5py module can read the operational netCDF4 products without any
problems, however, the SRON science products contain incompatible attributes.
Thus should be fixed when more up-to-date netCDF software is used to generate
the products. Currently, the Python netCDF4 module is used to read the science
products.

Copyright (c) 2018 SRON - Netherlands Institute for Space Research
   All Rights Reserved

License:  BSD-3-Clause
"""
from pathlib import Path

import numpy as np

# - global parameters ------------------------------

# - local functions --------------------------------


# - class definition -------------------------------
class LV2io():
    """
    This class should offer all the necessary functionality to read Tropomi
    S5P_OFFL_L2 products
    """
    def __init__(self, lv2_product):
        """
        Initialize access to an S5P_L2 product

        Parameters
        ----------
        lv2_product :  string
           full path to S5P Tropomi level 2 product
        """
        import h5py
        science_inst = ['SRON Netherlands Institute for Space Research']

        # initialize class-attributes
        self.filename = lv2_product
        self.science_product = False
        self.fid = None

        if not Path(lv2_product).is_file():
            raise FileNotFoundError('{} does not exist'.format(lv2_product))

        # open LV2 product as HDF5 file
        self.fid = h5py.File(lv2_product, "r")
        try:
            if self.fid.attrs['institution'] in science_inst:
                self.science_product = True
        except OSError:
            self.science_product = True

        if self.science_product:
            from netCDF4 import Dataset

            self.fid.close()
            self.fid = Dataset(lv2_product, "r", format="NETCDF4")

            self.ground_pixel = self.fid['/instrument/ground_pixel'][:].max()
            self.ground_pixel += 1
            self.scanline = self.fid['/instrument/scanline'][:].max()
            self.scanline += 1
            if self.fid['/instrument/scanline'].size % self.ground_pixel != 0:
                raise ValueError('not all scanlines are complete')
        else:
            self.ground_pixel = self.fid['/PRODUCT/ground_pixel'].size
            self.scanline = self.fid['/PRODUCT/scanline'].size<commit_msg>lv2_io.py: fixed typo in attribute call<commit_after>"""
This file is part of pyS5p

https://github.com/rmvanhees/pys5p.git

The class LV2io provides read access to S5p Tropomi S5P_OFFL_L2 products

Note
----
The Python h5py module can read the operational netCDF4 products without any
problems, however, the SRON science products contain incompatible attributes.
Thus should be fixed when more up-to-date netCDF software is used to generate
the products. Currently, the Python netCDF4 module is used to read the science
products.

Copyright (c) 2018 SRON - Netherlands Institute for Space Research
   All Rights Reserved

License:  BSD-3-Clause
"""
from pathlib import Path

import numpy as np

# - global parameters ------------------------------

# - local functions --------------------------------


# - class definition -------------------------------
class LV2io():
    """
    This class should offer all the necessary functionality to read Tropomi
    S5P_OFFL_L2 products
    """
    def __init__(self, lv2_product):
        """
        Initialize access to an S5P_L2 product

        Parameters
        ----------
        lv2_product :  string
           full path to S5P Tropomi level 2 product
        """
        import h5py
        science_inst = ['SRON Netherlands Institute for Space Research']

        # initialize class-attributes
        self.filename = lv2_product
        self.science_product = False
        self.fid = None

        if not Path(lv2_product).is_file():
            raise FileNotFoundError('{} does not exist'.format(lv2_product))

        # open LV2 product as HDF5 file
        self.fid = h5py.File(lv2_product, "r")
        try:
            if self.get_attr('institution') in science_inst:
                self.science_product = True
        except OSError:
            self.science_product = True

        if self.science_product:
            from netCDF4 import Dataset

            self.fid.close()
            self.fid = Dataset(lv2_product, "r", format="NETCDF4")

            self.ground_pixel = self.fid['/instrument/ground_pixel'][:].max()
            self.ground_pixel += 1
            self.scanline = self.fid['/instrument/scanline'][:].max()
            self.scanline += 1
            # alternative set flag sparse
            if self.fid['/instrument/scanline'].size % self.ground_pixel != 0:
                raise ValueError('not all scanlines are complete')
        else:
            self.ground_pixel = self.fid['/PRODUCT/ground_pixel'].size
            self.scanline = self.fid['/PRODUCT/scanline'].size
{'fraction_non_alphanumeric': 0.12067260138476756, 'fraction_numerical': 0.01483679525222552, 'mean_word_length': 2.096142069810165, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '1626062', 'n_tokens_mistral': 1436, 'n_tokens_neox': 1388, 'n_words': 428}