# Path: atrain_match/utils/common.py
# def write_match_objects(filename, datasets, groups, group_attrs_dict, SETTINGS=None):
#     """Write match objects to HDF5 file *filename*.
# 
#     Arguments:
# 
#         *diff_sec_1970*: `numpy.ndarray`
#             time diff between matched satellites
#         *groups*: dict
#             each key/value pair should hold a list of `numpy.ndarray` instances
#             to be written under HDF5 group with the same name as the key
# 
#     E.g. to write a calipso match:
# 
#     >>> groups = {'calipso': ca_obj.calipso.all_arrays,
#     ...           'imager': ca_obj.imager.all_arrays}
#     >>> write_match_objects('match.h5', ca_obj.diff_sec_1970, groups)
# 
#     The match object data can then be read using `read_match_objects`:
# 
#     >>> diff_sec_1970, groups = read_match_objects('match.h5')
# 
#     """
#     from atrain_match.config import COMPRESS_LVL
#     import h5py
#     from atrain_match.matchobject_io import the_used_variables
#     with h5py.File(filename, 'w') as f:
#         for name in datasets.keys():
#             f.create_dataset(name, data=datasets[name],
#                              compression=COMPRESS_LVL)
# 
#         for group_name, group_object in groups.items():
#             if SETTINGS is not None and group_name in ['calipso',
#                                                        'calipso_aerosol'
#                                                        'cloudsat',
#                                                        'iss',
#                                                        'mora',
#                                                        'synop']:
#                 skip_some = SETTINGS["WRITE_ONLY_THE_MOST_IMPORTANT_STUFF_TO_FILE"]
#             else:
#                 #modis_lvl2, pps, oca, patmosx, or maia
#                 skip_some = False 
# 
#             try:
#                 attrs_dict = group_attrs_dict[group_name]
#             except KeyError:
#                 attrs_dict = {}
#             g = f.create_group(group_name)
#             for key in attrs_dict:
#                 g.attrs[key] = attrs_dict[key]
#             for array_name, array in group_object.items():
#                 if array is None:
#                     continue
#                 if (skip_some and
#                         array_name not in the_used_variables):
#                     logger.debug("Not writing unimportant %s to file",
#                                  array_name)
#                     continue
# 
#                 if len(array) == 0:
#                     continue
#                     # Scalar data can't be compressed
#                     # TODO: Write it as and attribute instead?
#                     # g.create_dataset(array_name, data=array)
#                 else:
#                     # print("writing", array_name)
#                     g.create_dataset(array_name, data=array,
#                                      compression=COMPRESS_LVL)


import numpy as np
import h5py
    import os.path
from atrain_match.utils.common import write_match_objects
    from scipy.ndimage.filters import uniform_filter
                if atrain_match_name in ["snow_ice_surface_type"]:
                    atrain_match_name = "nsidc_surface_type"
                setattr(data_obj, atrain_match_name, group[dataset][...])
    retv.diff_sec_1970 = h5file['diff_sec_1970'][...]
    h5file.close()
    return retv


def read_files(files, truth='calipso', read_all=True, read_var=[], skip_var=[]):
    my_files = files.copy()
    tObj = read_truth_imager_match_obj(my_files.pop(), truth=truth, read_all=read_all, read_var=read_var, skip_var=skip_var)
    if len(my_files) > 0:
        for filename in my_files:
            tObj += read_truth_imager_match_obj(filename, truth=truth, read_all=read_all, read_var=read_var, skip_var=skip_var)
    return tObj


# write matchup files


def write_truth_imager_match_obj(filename, match_obj, SETTINGS=None, imager_obj_name='pps'):
    """Write *match_obj* to *filename*."""
    datasets = {'diff_sec_1970': match_obj.diff_sec_1970}
    groups = {imager_obj_name: match_obj.imager.all_arrays}
    imager_attrs = {'imager_instrument': match_obj.imager_instrument}
    groups_attrs = {imager_obj_name: imager_attrs}
    for name in ['calipso', 'calipso_aerosol', 'iss', 'modis_lvl2',
                 'amsr', 'synop', 'mora', 'cloudsat', 'extra']:
        if hasattr(match_obj, name):
            groups[name] = getattr(match_obj, name).all_arrays

# Path: atrain_match/utils/common.py
# def write_match_objects(filename, datasets, groups, group_attrs_dict, SETTINGS=None):
#     """Write match objects to HDF5 file *filename*.
# 
#     Arguments:
# 
#         *diff_sec_1970*: `numpy.ndarray`
#             time diff between matched satellites
#         *groups*: dict
#             each key/value pair should hold a list of `numpy.ndarray` instances
#             to be written under HDF5 group with the same name as the key
# 
#     E.g. to write a calipso match:
# 
#     >>> groups = {'calipso': ca_obj.calipso.all_arrays,
#     ...           'imager': ca_obj.imager.all_arrays}
#     >>> write_match_objects('match.h5', ca_obj.diff_sec_1970, groups)
# 
#     The match object data can then be read using `read_match_objects`:
# 
#     >>> diff_sec_1970, groups = read_match_objects('match.h5')
# 
#     """
#     from atrain_match.config import COMPRESS_LVL
#     import h5py
#     from atrain_match.matchobject_io import the_used_variables
#     with h5py.File(filename, 'w') as f:
#         for name in datasets.keys():
#             f.create_dataset(name, data=datasets[name],
#                              compression=COMPRESS_LVL)
# 
#         for group_name, group_object in groups.items():
#             if SETTINGS is not None and group_name in ['calipso',
#                                                        'calipso_aerosol'
#                                                        'cloudsat',
#                                                        'iss',
#                                                        'mora',
#                                                        'synop']:
#                 skip_some = SETTINGS["WRITE_ONLY_THE_MOST_IMPORTANT_STUFF_TO_FILE"]
#             else:
#                 #modis_lvl2, pps, oca, patmosx, or maia
#                 skip_some = False 
# 
#             try:
#                 attrs_dict = group_attrs_dict[group_name]
#             except KeyError:
#                 attrs_dict = {}
#             g = f.create_group(group_name)
#             for key in attrs_dict:
#                 g.attrs[key] = attrs_dict[key]
#             for array_name, array in group_object.items():
#                 if array is None:
#                     continue
#                 if (skip_some and
#                         array_name not in the_used_variables):
#                     logger.debug("Not writing unimportant %s to file",
#                                  array_name)
#                     continue
# 
#                 if len(array) == 0:
#                     continue
#                     # Scalar data can't be compressed
#                     # TODO: Write it as and attribute instead?
#                     # g.create_dataset(array_name, data=array)
#                 else:
#                     # print("writing", array_name)
#                     g.create_dataset(array_name, data=array,
#                                      compression=COMPRESS_LVL)


# Path: atrain_match/matchobject_io.py
import numpy as np
import h5py
    import os.path
from atrain_match.utils.common import write_match_objects
    from scipy.ndimage.filters import uniform_filter

                if atrain_match_name in ["snow_ice_surface_type"]:
                    atrain_match_name = "nsidc_surface_type"
                setattr(data_obj, atrain_match_name, group[dataset][...])
    retv.diff_sec_1970 = h5file['diff_sec_1970'][...]
    h5file.close()
    return retv


def read_files(files, truth='calipso', read_all=True, read_var=[], skip_var=[]):
    my_files = files.copy()
    tObj = read_truth_imager_match_obj(my_files.pop(), truth=truth, read_all=read_all, read_var=read_var, skip_var=skip_var)
    if len(my_files) > 0:
        for filename in my_files:
            tObj += read_truth_imager_match_obj(filename, truth=truth, read_all=read_all, read_var=read_var, skip_var=skip_var)
    return tObj


# write matchup files


def write_truth_imager_match_obj(filename, match_obj, SETTINGS=None, imager_obj_name='pps'):
    """Write *match_obj* to *filename*."""
    datasets = {'diff_sec_1970': match_obj.diff_sec_1970}
    groups = {imager_obj_name: match_obj.imager.all_arrays}
    imager_attrs = {'imager_instrument': match_obj.imager_instrument}
    groups_attrs = {imager_obj_name: imager_attrs}
    for name in ['calipso', 'calipso_aerosol', 'iss', 'modis_lvl2',
                 'amsr', 'synop', 'mora', 'cloudsat', 'extra']:
        if hasattr(match_obj, name):
            groups[name] = getattr(match_obj, name).all_arrays

    write_match_objects(filename, datasets, groups, groups_attrs, SETTINGS=SETTINGS)