# Path: pyroSAR/ancillary.py
# def parse_datasetname(name, parse_date=False):
#     """
#     Parse the name of a pyroSAR processing product and extract its metadata components as dictionary
#     
#     Parameters
#     ----------
#     name: str
#         the name of the file to be parsed
#     parse_date: bool
#         parse the start date to a :class:`~datetime.datetime` object or just return the string?
#     
#     Returns
#     -------
#     dict
#         the metadata attributes
#     
#     Examples
#     --------
#     >>> meta = parse_datasetname('S1A__IW___A_20150309T173017_VV_grd_mli_geo_norm_db.tif')
#     >>> print(sorted(meta.keys()))
#     ['acquisition_mode', 'extensions', 'filename', 'orbit',
#     'outname_base', 'polarization', 'proc_steps', 'sensor', 'start']
#     """
#     
#     filename = os.path.abspath(name) if os.path.isfile(name) else name
#     
#     match = re.match(re.compile(product_pattern), filename)
#     if not match:
#         return
#     out = match.groupdict()
#     if out['extensions'] == '':
#         out['extensions'] = None
#     if out['proc_steps'] is not None:
#         out['proc_steps'] = out['proc_steps'].split('_')
#     if parse_date:
#         out['start'] = datetime.strptime(out['start'], '%Y%m%dT%H%M%S')
#     out['filename'] = filename
#     out['outname_base'] = out['outname_base'].strip('_')
#     return out


import os
import re
import yaml
import uuid
import logging
from time import strftime, strptime
from spatialist.raster import Raster, Dtype
from spatialist.ancillary import union
from .ancillary import parse_datasetname
    ----------
    filename: str, list, Dataset
        the product to be used; either an existing :class:`Dataset` object or a (list of) file(s) matching the pyroSAR
        naming pattern, i.e. that can be parsed by :func:`pyroSAR.ancillary.parse_datasetname`
    units: str or dict
        the units of the product measurement
    """
    def __init__(self, filename, units='DN'):
        
        if isinstance(filename, list):
            combined = sum([Dataset(x, units) for x in filename])
            self.__init__(combined)
        
        elif isinstance(filename, Dataset):
            for attr, value in vars(filename).items():
                setattr(self, attr, value)
        
        elif isinstance(filename, str):
            # map pyroSAR sensor identifiers to platform and instrument codes
            sensor_lookup = {'ASAR': ('ENVISAT', 'ASAR'),
                             'ERS1': ('ERS-1', 'SAR'),
                             'ERS2': ('ERS-2', 'SAR'),
                             'PSR1': ('ALOS-1', 'PALSAR'),
                             'PSR2': ('ALOS-2', 'PALSAR-2'),
                             'S1A': ('SENTINEL-1', 'C-SAR'),
                             'S1B': ('SENTINEL-1', 'C-SAR'),
                             'TSX1': ('TERRASAR-X_1', 'SAR'),
                             'TDX1': ('TANDEM-X_1', 'SAR')}
            
            # extract basic metadata attributes from the filename and register them to the object

# Path: pyroSAR/ancillary.py
# def parse_datasetname(name, parse_date=False):
#     """
#     Parse the name of a pyroSAR processing product and extract its metadata components as dictionary
#     
#     Parameters
#     ----------
#     name: str
#         the name of the file to be parsed
#     parse_date: bool
#         parse the start date to a :class:`~datetime.datetime` object or just return the string?
#     
#     Returns
#     -------
#     dict
#         the metadata attributes
#     
#     Examples
#     --------
#     >>> meta = parse_datasetname('S1A__IW___A_20150309T173017_VV_grd_mli_geo_norm_db.tif')
#     >>> print(sorted(meta.keys()))
#     ['acquisition_mode', 'extensions', 'filename', 'orbit',
#     'outname_base', 'polarization', 'proc_steps', 'sensor', 'start']
#     """
#     
#     filename = os.path.abspath(name) if os.path.isfile(name) else name
#     
#     match = re.match(re.compile(product_pattern), filename)
#     if not match:
#         return
#     out = match.groupdict()
#     if out['extensions'] == '':
#         out['extensions'] = None
#     if out['proc_steps'] is not None:
#         out['proc_steps'] = out['proc_steps'].split('_')
#     if parse_date:
#         out['start'] = datetime.strptime(out['start'], '%Y%m%dT%H%M%S')
#     out['filename'] = filename
#     out['outname_base'] = out['outname_base'].strip('_')
#     return out


# Path: pyroSAR/datacube_util.py
import os
import re
import yaml
import uuid
import logging
from time import strftime, strptime
from spatialist.raster import Raster, Dtype
from spatialist.ancillary import union
from .ancillary import parse_datasetname

    ----------
    filename: str, list, Dataset
        the product to be used; either an existing :class:`Dataset` object or a (list of) file(s) matching the pyroSAR
        naming pattern, i.e. that can be parsed by :func:`pyroSAR.ancillary.parse_datasetname`
    units: str or dict
        the units of the product measurement
    """
    def __init__(self, filename, units='DN'):
        
        if isinstance(filename, list):
            combined = sum([Dataset(x, units) for x in filename])
            self.__init__(combined)
        
        elif isinstance(filename, Dataset):
            for attr, value in vars(filename).items():
                setattr(self, attr, value)
        
        elif isinstance(filename, str):
            # map pyroSAR sensor identifiers to platform and instrument codes
            sensor_lookup = {'ASAR': ('ENVISAT', 'ASAR'),
                             'ERS1': ('ERS-1', 'SAR'),
                             'ERS2': ('ERS-2', 'SAR'),
                             'PSR1': ('ALOS-1', 'PALSAR'),
                             'PSR2': ('ALOS-2', 'PALSAR-2'),
                             'S1A': ('SENTINEL-1', 'C-SAR'),
                             'S1B': ('SENTINEL-1', 'C-SAR'),
                             'TSX1': ('TERRASAR-X_1', 'SAR'),
                             'TDX1': ('TANDEM-X_1', 'SAR')}
            
            # extract basic metadata attributes from the filename and register them to the object

            meta = parse_datasetname(filename)