<commit_before>##############################################################
# general utilities for Sentinel-1
# <NAME> 2016-2017
##############################################################
import sys

if sys.version_info >= (3, 0):
    from urllib.parse import urlparse, urlunparse, urlencode
    from urllib.request import urlopen
else:
    from urllib import urlopen, urlencode
    from urlparse import urlparse, urlunparse

import os
import re
import ssl
import time
from datetime import datetime

from ..ancillary import finder

try:
    import argparse
except ImportError:
    try:
        os.remove(os.path.join(os.path.dirname(sys.argv[0]), 'locale.pyc'))
    finally:
        import argparse


def init_parser():
    """
    initialize argument parser for S1 processing utilities
    """
    parser = argparse.ArgumentParser()
    parser.add_argument('-t', '--transform', action='store_true', help='transform the final DEM to UTM coordinates')
    parser.add_argument('-l', '--logfiles', action='store_true', help='create logfiles of the executed GAMMA commands')
    parser.add_argument('-i', '--intermediates', action='store_true', help='keep intermediate files')
    parser.add_argument('-q', '--quiet', action='store_true', help='suppress standard console prints')
    parser.add_argument('-tr', '--targetresolution', default=20, help='the target resolution in meters for x and y',
                        type=int)
    parser.add_argument('-fg', '--func_geoback', default=2, help='backward geocoding interpolation function; '
                                                                 '0 - Nearest Neighbor, 1 - Bicubic Spline, 2 - Bicubic Spline-Log; '
                                                                 'method 1: negative values possible (e.g. in urban areas) - use method 2 to avoid this',
                        type=int)
    parser.add_argument('-fi', '--func_interp', default=0,
                        help='function for interpolation of layover/shadow/foreshortening/DEM gaps; '
                             '0 - set to 0, 1 - linear interpolation, 2 - actual value, 3 - nn-thinned', type=int)
    parser.add_argument('-poe', '--poedir', default=None,
                        help='directory containing aux_poeorb (precise orbit ephemerides) orbit state vector files')
    parser.add_argument('-res', '--resdir', default=None,
                        help='directory containing aux_resorb (restituted orbit) orbit state vector files')
    parser.add_argument('zipfile', help='S1 zipped scene archive to be used')
    parser.add_argument('tempdir', help='temporary directory for intermediate files')
    parser.add_argument('outdir', help='output directory')
    parser.add_argument('srtmdir', help='directory containing SRTM hgt tiles (subdirectories possible)')
    return parser


# todo check existence not by file name but by start and stop time; files are sometimes re-published
class OSV(object):
    """
    interface for management of S1 Orbit State Vector (OSV) files

    input is a directory which is to contain, or already contains, OSV files. Two subdirectories are expected and created otherwise:
    one for Precise Orbit Ephemerides (POE) named POEORB and one for Restituted Orbit (RES) files named RESORB

    using method 'match' the corresponding POE (priority) or RES file is returned for a timestamp
    timestamps are always handled in the format YYYYMMDDThhmmss
    """
    def __init__(self, osvdir):
        self.remote_poe = 'https://qc.sentinel1.eo.esa.int/aux_poeorb/'
        self.remote_res = 'https://qc.sentinel1.eo.esa.int/aux_resorb/'
        self.outdir_poe = os.path.join(osvdir, 'POEORB')
        self.outdir_res = os.path.join(osvdir, 'RESORB')
        self.pattern = 'S1[AB]_OPER_AUX_(?:POE|RES)ORB_OPOD_[0-9TV_]{48}\.EOF'
        self.pattern_fine = 'S1[AB]_OPER_AUX_' \
                            '(?P<type>(?:POE|RES)ORB)_OPOD_' \
                            '(?P<publish>[0-9]{8}T[0-9]{6})_V' \
                            '(?P<start>[0-9]{8}T[0-9]{6})_' \
                            '(?P<stop>[0-9]{8}T[0-9]{6})\.EOF'
        if sys.version_info >= (2, 7, 9):
            self.sslcontext = ssl._create_unverified_context()
        else:
            #https://github.com/airnotifier/airnotifier/issues/136
            self.sslcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
            self.sslcontext.check_hostname = False
            self.sslcontext.verify_mode = ssl.CERT_NONE

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        return

    def _init_dir(self):
        """
        create directories if they don't exist yet
        """
        for dir in [self.outdir_poe, self.outdir_res]:
            if not os.path.isdir(dir):
                os.makedirs(dir)

    def _typeEvaluate(self, osvtype):
        """
        evaluate the 'osvtype' method argument and return the corresponding remote repository and local directory
        :param osvtype: the type of orbit files required; either 'POE' or 'RES'
        :return: the remote repository and local directory of the osv type
        """
        if osvtype not in ['POE', 'RES']:
            raise IOError('type must be either "POE" or "RES"')
        if osvtype == 'POE':
            return self.remote_poe, self.outdir_poe
        else:
            return self.remote_res, self.outdir_res

    def catch(self, osvtype='POE', start=None, stop=None):
        """
        check a server for files<commit_msg>OSV.__init__: explicit error for Python version < 2.7.9<commit_after>##############################################################
# general utilities for Sentinel-1
# <NAME> 2016-2017
##############################################################
import sys

if sys.version_info >= (3, 0):
    from urllib.parse import urlparse, urlunparse, urlencode
    from urllib.request import urlopen
else:
    from urllib import urlopen, urlencode
    from urlparse import urlparse, urlunparse

import os
import re
import ssl
import time
from datetime import datetime

from ..ancillary import finder

try:
    import argparse
except ImportError:
    try:
        os.remove(os.path.join(os.path.dirname(sys.argv[0]), 'locale.pyc'))
    finally:
        import argparse


def init_parser():
    """
    initialize argument parser for S1 processing utilities
    """
    parser = argparse.ArgumentParser()
    parser.add_argument('-t', '--transform', action='store_true', help='transform the final DEM to UTM coordinates')
    parser.add_argument('-l', '--logfiles', action='store_true', help='create logfiles of the executed GAMMA commands')
    parser.add_argument('-i', '--intermediates', action='store_true', help='keep intermediate files')
    parser.add_argument('-q', '--quiet', action='store_true', help='suppress standard console prints')
    parser.add_argument('-tr', '--targetresolution', default=20, help='the target resolution in meters for x and y',
                        type=int)
    parser.add_argument('-fg', '--func_geoback', default=2, help='backward geocoding interpolation function; '
                                                                 '0 - Nearest Neighbor, 1 - Bicubic Spline, 2 - Bicubic Spline-Log; '
                                                                 'method 1: negative values possible (e.g. in urban areas) - use method 2 to avoid this',
                        type=int)
    parser.add_argument('-fi', '--func_interp', default=0,
                        help='function for interpolation of layover/shadow/foreshortening/DEM gaps; '
                             '0 - set to 0, 1 - linear interpolation, 2 - actual value, 3 - nn-thinned', type=int)
    parser.add_argument('-poe', '--poedir', default=None,
                        help='directory containing aux_poeorb (precise orbit ephemerides) orbit state vector files')
    parser.add_argument('-res', '--resdir', default=None,
                        help='directory containing aux_resorb (restituted orbit) orbit state vector files')
    parser.add_argument('zipfile', help='S1 zipped scene archive to be used')
    parser.add_argument('tempdir', help='temporary directory for intermediate files')
    parser.add_argument('outdir', help='output directory')
    parser.add_argument('srtmdir', help='directory containing SRTM hgt tiles (subdirectories possible)')
    return parser


# todo check existence not by file name but by start and stop time; files are sometimes re-published
class OSV(object):
    """
    interface for management of S1 Orbit State Vector (OSV) files

    input is a directory which is to contain, or already contains, OSV files. Two subdirectories are expected and created otherwise:
    one for Precise Orbit Ephemerides (POE) named POEORB and one for Restituted Orbit (RES) files named RESORB

    using method 'match' the corresponding POE (priority) or RES file is returned for a timestamp
    timestamps are always handled in the format YYYYMMDDThhmmss
    """
    def __init__(self, osvdir):
        self.remote_poe = 'https://qc.sentinel1.eo.esa.int/aux_poeorb/'
        self.remote_res = 'https://qc.sentinel1.eo.esa.int/aux_resorb/'
        self.outdir_poe = os.path.join(osvdir, 'POEORB')
        self.outdir_res = os.path.join(osvdir, 'RESORB')
        self.pattern = 'S1[AB]_OPER_AUX_(?:POE|RES)ORB_OPOD_[0-9TV_]{48}\.EOF'
        self.pattern_fine = 'S1[AB]_OPER_AUX_' \
                            '(?P<type>(?:POE|RES)ORB)_OPOD_' \
                            '(?P<publish>[0-9]{8}T[0-9]{6})_V' \
                            '(?P<start>[0-9]{8}T[0-9]{6})_' \
                            '(?P<stop>[0-9]{8}T[0-9]{6})\.EOF'
        if sys.version_info >= (2, 7, 9):
            self.sslcontext = ssl._create_unverified_context()
        else:
            raise RuntimeError('this functionality requires Python Version >=2.7.9')

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        return

    def _init_dir(self):
        """
        create directories if they don't exist yet
        """
        for dir in [self.outdir_poe, self.outdir_res]:
            if not os.path.isdir(dir):
                os.makedirs(dir)

    def _typeEvaluate(self, osvtype):
        """
        evaluate the 'osvtype' method argument and return the corresponding remote repository and local directory
        :param osvtype: the type of orbit files required; either 'POE' or 'RES'
        :return: the remote repository and local directory of the osv type
        """
        if osvtype not in ['POE', 'RES']:
            raise IOError('type must be either "POE" or "RES"')
        if osvtype == 'POE':
            return self.remote_poe, self.outdir_poe
        else:
            return self.remote_res, self.outdir_res

    def catch(self, osvtype='POE', start=None, stop=None):
        """
        check a server for files
{'fraction_non_alphanumeric': 0.1318490012827561, 'fraction_numerical': 0.01154480483782298, 'mean_word_length': 2.6346986346986347, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 18, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4850538', 'n_tokens_mistral': 3288, 'n_tokens_neox': 3144, 'n_words': 907}