# Path: pyteltools/geom/Shapefile.py
# def get_shape_type(input_filename):
# def get_lines(input_filename, shape_type):
# def get_open_polylines(input_filename):
# def get_polygons(input_filename):
# def get_all_fields(input_filename):
# def get_attribute_names(input_filename):
# def get_numeric_attribute_names(input_filename):
# def get_points(input_filename, indices=None, with_z=False):
# def write_shp_points_z(output_filename, z_name, points):
# def write_shp_lines(output_filename, shape_type, lines, attribute_name):
# 
# Path: pyteltools/slf/Serafin.py
# class Serafin:
#     """!
#     @brief A Serafin object corresponds to a single Serafin in file IO stream
# 
#     # Attributes:
#     - language <str>: language for variables ('fr' or 'en')
#     - mode <str>: file open mode
#     - file <_io.BufferedReader>: input Serafin stream
#     - filename <str>: path to file
#     - file_size <int>: size of file
#     """
#     def __init__(self, filename, mode, language):
#         self.language = language
#         self.mode = mode
# 
#         self.file = None  # will be opened when called using `with` statement
#         self.filename = filename
#         self.file_size = None
# 
#     def __enter__(self):
#         self.file = open(self.filename, self.mode)
#         return self
# 
#     def __exit__(self, exc_type, exc_val, exc_tb):
#         self.file.close()
#         return False
# 
# Path: pyteltools/utils/cli_base.py
# LINE_WIDTH = 80
# class CustomFormatter(argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter):
# class PyTelToolsArgParse(argparse.ArgumentParser):
#     def __init__(self, add_args=[], description=None, *args, **kwargs):
#     def add_known_argument(self, arg_id):
#     def _add_auto_arguments(self):
#     def _title_group(label):
#     def add_argument_group(self, name, *args, **kwargs):
#     def add_group_general(self, add_args=[]):
#     def parse_args(self, *args, **kwargs):


import numpy as np
import sys
from shapefile import ShapefileException
from shapely.geometry import Point
from pyteltools.conf import settings
from pyteltools.geom import Shapefile
from pyteltools.slf import Serafin
from pyteltools.utils.cli_base import logger, PyTelToolsArgParse

This tool has some limitations:
- vectors are not considered as such
- meshes have to be similar
"""



def slf_max_over_files(args):
    if args.vars is None:
        with Serafin.Read(args.in_slfs[0], args.lang) as resin:
            resin.read_header()
            var_IDs = resin.header.var_IDs if args.vars is None else args.vars
    else:
        var_IDs = args.vars

    if args.operation == 'max':
        fun = np.maximum
    elif args.operation == 'min':
        fun = np.minimum
    else:
        raise NotImplementedError

    # Read polygons
    if args.in_polygons is not None:
        if not args.in_polygons.endswith('.shp'):
            logger.critical('File "%s" is not a shp file.' % args.in_polygons)
            sys.exit(3)
        polygons = []
        try:

# Path: pyteltools/geom/Shapefile.py
# def get_shape_type(input_filename):
# def get_lines(input_filename, shape_type):
# def get_open_polylines(input_filename):
# def get_polygons(input_filename):
# def get_all_fields(input_filename):
# def get_attribute_names(input_filename):
# def get_numeric_attribute_names(input_filename):
# def get_points(input_filename, indices=None, with_z=False):
# def write_shp_points_z(output_filename, z_name, points):
# def write_shp_lines(output_filename, shape_type, lines, attribute_name):
# 
# Path: pyteltools/slf/Serafin.py
# class Serafin:
#     """!
#     @brief A Serafin object corresponds to a single Serafin in file IO stream
# 
#     # Attributes:
#     - language <str>: language for variables ('fr' or 'en')
#     - mode <str>: file open mode
#     - file <_io.BufferedReader>: input Serafin stream
#     - filename <str>: path to file
#     - file_size <int>: size of file
#     """
#     def __init__(self, filename, mode, language):
#         self.language = language
#         self.mode = mode
# 
#         self.file = None  # will be opened when called using `with` statement
#         self.filename = filename
#         self.file_size = None
# 
#     def __enter__(self):
#         self.file = open(self.filename, self.mode)
#         return self
# 
#     def __exit__(self, exc_type, exc_val, exc_tb):
#         self.file.close()
#         return False
# 
# Path: pyteltools/utils/cli_base.py
# LINE_WIDTH = 80
# class CustomFormatter(argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter):
# class PyTelToolsArgParse(argparse.ArgumentParser):
#     def __init__(self, add_args=[], description=None, *args, **kwargs):
#     def add_known_argument(self, arg_id):
#     def _add_auto_arguments(self):
#     def _title_group(label):
#     def add_argument_group(self, name, *args, **kwargs):
#     def add_group_general(self, add_args=[]):
#     def parse_args(self, *args, **kwargs):


# Path: cli/slf_max_over_files.py
import numpy as np
import sys
from shapefile import ShapefileException
from shapely.geometry import Point
from pyteltools.conf import settings
from pyteltools.geom import Shapefile
from pyteltools.slf import Serafin
from pyteltools.utils.cli_base import logger, PyTelToolsArgParse


This tool has some limitations:
- vectors are not considered as such
- meshes have to be similar
"""



def slf_max_over_files(args):
    if args.vars is None:
        with Serafin.Read(args.in_slfs[0], args.lang) as resin:
            resin.read_header()
            var_IDs = resin.header.var_IDs if args.vars is None else args.vars
    else:
        var_IDs = args.vars

    if args.operation == 'max':
        fun = np.maximum
    elif args.operation == 'min':
        fun = np.minimum
    else:
        raise NotImplementedError

    # Read polygons
    if args.in_polygons is not None:
        if not args.in_polygons.endswith('.shp'):
            logger.critical('File "%s" is not a shp file.' % args.in_polygons)
            sys.exit(3)
        polygons = []
        try:

            for polygon in Shapefile.get_polygons(args.in_polygons):