# Path: cis/data_io/hdf.py
# def _read_hdf4(filename, variables):
#     """
#         A wrapper method for reading raw data from hdf4 files. This returns a dictionary of io handles
#          for each VD and SD data types.
# 
#         :param filename:     A name of a file to read
#         :param variables:    List of variables to read from the files
# 
#         :return: (sds_dict, vds_dict) A tuple of dictionaries, one for sds objects and another for vds
#     """
#     from cis.exceptions import InvalidVariableError
#     from pyhdf.error import HDF4Error
# 
#     variables = utils.listify(variables)
# 
#     # I'd rather not have to make this check but for pyhdf 0.9.0 and hdf 4.2.9 on OS X the c-level read routine will at
#     # some point call exit(138) when reading valid netcdf files (rather than returning a negative status).
#     if not filename.endswith('.hdf'):
#         raise IOError("Tried to read non HDF file: {}".format(filename))
# 
#     try:
#         sds_dict = hdf_sd.read(filename, variables)
# 
#         # remove the variables identified as SD (i.e. the keys in sds_dict)
#         # no need to try looking for them as VD variable
#         # AND this can cause a crash in some version/implementations of the core HDF4 libraries!
# 
#         # First create a copy of the list in order for the original list to be left intact when elements are removed
#         # from it, this enables the original list to be used when many files are read
#         vdvariables = list(variables)
#         for sds_dict_key in sds_dict:
#             vdvariables.remove(sds_dict_key)
# 
#         vds_dict = hdf_vd.read(filename, vdvariables)
#     except HDF4Error as e:
#         raise IOError(str(e))
# 
#     for variable in variables:
#         if variable not in sds_dict and variable not in vds_dict:
#             raise InvalidVariableError("Could not find " + variable + " in file: " + filename)
# 
#     return sds_dict, vds_dict
# 
# Path: cis/exceptions.py
# class InvalidVariableError(CISError):
#     pass


from nose.tools import eq_, istest, raises
from cis.data_io.hdf import _read_hdf4
from cis.exceptions import InvalidVariableError
from cis.test.integration_test_data import *
    _read_hdf4(valid_cloud_cci_filename, valid_cloud_cci_variable)


@istest
@skip_pyhdf
def test_read_hdf4():
    filename = escape_colons(valid_hdf_sd_file)
    sds, vds = _read_hdf4(filename, ['Solution_Ocean', 'Path_Radiance_Land', 'Mean_Reflectance_Land'])

    # VD variable are listed in the VD part of the tuple, but not in the SD part
    eq_(True, 'Solution_Ocean' in vds)
    eq_(False, 'Solution_Ocean' in sds)

    # SD variable are listed in the SD part of the tuple, but not in the VD part
    eq_(True, 'Path_Radiance_Land' in sds)
    eq_(False, 'Path_Radiance_Land' in vds)
    eq_(True, 'Mean_Reflectance_Land' in sds)
    eq_(False, 'Mean_Reflectance_Land' in vds)


@istest
@skip_pyhdf
@raises(InvalidVariableError)
def test_that_cannot_read_unknown_variables():
    filename = escape_colons(valid_hdf_sd_file)
    sds, vds = _read_hdf4(filename, ['athing', 'unechose', 'einding'])


@istest
@skip_pyhdf

# Path: cis/data_io/hdf.py
# def _read_hdf4(filename, variables):
#     """
#         A wrapper method for reading raw data from hdf4 files. This returns a dictionary of io handles
#          for each VD and SD data types.
# 
#         :param filename:     A name of a file to read
#         :param variables:    List of variables to read from the files
# 
#         :return: (sds_dict, vds_dict) A tuple of dictionaries, one for sds objects and another for vds
#     """
#     from cis.exceptions import InvalidVariableError
#     from pyhdf.error import HDF4Error
# 
#     variables = utils.listify(variables)
# 
#     # I'd rather not have to make this check but for pyhdf 0.9.0 and hdf 4.2.9 on OS X the c-level read routine will at
#     # some point call exit(138) when reading valid netcdf files (rather than returning a negative status).
#     if not filename.endswith('.hdf'):
#         raise IOError("Tried to read non HDF file: {}".format(filename))
# 
#     try:
#         sds_dict = hdf_sd.read(filename, variables)
# 
#         # remove the variables identified as SD (i.e. the keys in sds_dict)
#         # no need to try looking for them as VD variable
#         # AND this can cause a crash in some version/implementations of the core HDF4 libraries!
# 
#         # First create a copy of the list in order for the original list to be left intact when elements are removed
#         # from it, this enables the original list to be used when many files are read
#         vdvariables = list(variables)
#         for sds_dict_key in sds_dict:
#             vdvariables.remove(sds_dict_key)
# 
#         vds_dict = hdf_vd.read(filename, vdvariables)
#     except HDF4Error as e:
#         raise IOError(str(e))
# 
#     for variable in variables:
#         if variable not in sds_dict and variable not in vds_dict:
#             raise InvalidVariableError("Could not find " + variable + " in file: " + filename)
# 
#     return sds_dict, vds_dict
# 
# Path: cis/exceptions.py
# class InvalidVariableError(CISError):
#     pass


# Path: cis/test/integration/test_io/test_hdf.py
from nose.tools import eq_, istest, raises
from cis.data_io.hdf import _read_hdf4
from cis.exceptions import InvalidVariableError
from cis.test.integration_test_data import *

    _read_hdf4(valid_cloud_cci_filename, valid_cloud_cci_variable)


@istest
@skip_pyhdf
def test_read_hdf4():
    filename = escape_colons(valid_hdf_sd_file)
    sds, vds = _read_hdf4(filename, ['Solution_Ocean', 'Path_Radiance_Land', 'Mean_Reflectance_Land'])

    # VD variable are listed in the VD part of the tuple, but not in the SD part
    eq_(True, 'Solution_Ocean' in vds)
    eq_(False, 'Solution_Ocean' in sds)

    # SD variable are listed in the SD part of the tuple, but not in the VD part
    eq_(True, 'Path_Radiance_Land' in sds)
    eq_(False, 'Path_Radiance_Land' in vds)
    eq_(True, 'Mean_Reflectance_Land' in sds)
    eq_(False, 'Mean_Reflectance_Land' in vds)


@istest
@skip_pyhdf
@raises(InvalidVariableError)
def test_that_cannot_read_unknown_variables():
    filename = escape_colons(valid_hdf_sd_file)
    sds, vds = _read_hdf4(filename, ['athing', 'unechose', 'einding'])


@istest
@skip_pyhdf

@raises(InvalidVariableError)