Example #1
0
class Radiance(dict, _V):
    """
    The Radiance class
    Radiance object is Read-Only and contains minimum information
    """
    def __init__(self):
        dict.__init__(self)
        for key in radiance_list:
            self[key] = None
            self[key + '_ATTRIBUTE'] = {'COMMENT': 'none', 'UNITS': 'n/a'}
        self.misc = None

    def loadh5(self, h5):
        self.loadh5liste(h5, radiance_list)

    def read(self, filename):
        f = h5py.File(filename, 'r')

        # get the Dataset
        h5 = f['/RADIANCE/']
        self.loadh5(h5)

        # Load
        self.loadh5(h5)

        h5 = f['/MISC/']
        self.misc = Misc()
        self.misc.loadh5(h5)
        f.close()
class Kmatrix(object):
    """
    The Kmatrix class
    This class contains all K Matrix related information
    Its members are
    - profile: the profile considered for the K calculation
    - option: calculation options
    - kmatrix: a profile class containing the K results
    - chanprof: input channels/profiles
    - emissivity: K result for emissivity
    - reflectance: K result for reflectance
    - misc: important coef file names, list of wavenumbers...
    """
    def __init__(self):
        self.profile = Profile()
        self.option = Option()
        self.kmatrix = Profile()
        self.chanprof = Chanprof()
        self.emissivity = Emissivity()
        self.reflectance = Reflectance()
        self.misc = Misc()
        self.scaled = False
        self.scalecoef = 1.0
        self.scaledfirst = True

    def display(self):
        self.profile.display()
        self.option.display()
        self.kmatrix.display()
        self.chanprof.display()
        self.emissivity.display()
        self.reflectance.display()
        self.misc.display()

    def loadh5(self, h5):
        h5p = h5['PROFILES/0001']
        self.profile.loadh5(h5p)
        h5o = h5['OPTIONS']
        self.option.loadh5(h5o)
        if 'KMATRIX' in h5.keys():
            h5k = h5['KMATRIX']
            self.kmatrix.loadh5(h5k)
        h5c = h5['CHANPROF']
        self.chanprof.loadh5(h5c)
        if 'EMISSIVITY_K' in h5.keys():
            h5e = h5['EMISSIVITY_K']
            self.emissivity.loadh5(h5e)
        if 'REFLECTANCE_K' in h5.keys():
            h5r = h5['REFLECTANCE_K']
            self.reflectance.loadh5(h5r)
        h5m = h5['MISC']
        self.misc.loadh5(h5m)

    def saveh5(self, h5, path):
        self.profile.saveh5(h5, path + '/PROFILES/0001')
        self.option.saveh5(h5, path + '/OPTIONS/')
        self.kmatrix.saveh5(h5, path + '/KMATRIX/')
        self.chanprof.saveh5(h5, path + '/CHANPROF/')
        self.emissivity.saveh5(h5, path + '/EMISSIVITY_K/')
        self.reflectance.saveh5(h5, path + '/REFLECTANCE_K/')
        self.misc.saveh5(h5, path + '/MISC/')

    def getchanprof(self, ip):
        "Extracts a jacobian for a given channel"
        "A jacobian for a channel is a profile structure"
        "Channel should start from 0"
        # take care dimension order is opposite as Fortran"
        chanprof = Profile()
        # a cause des predicteurs on peut avoir nchannel different
        nchan = self.kmatrix['T'].shape[1]
        if (ip < 0 or ip >= nchan):
            print("ERROR Invalid channel index", ip, " allowed is [0,",
                  nchan - 1, "]")
            chanprof.__init__
            return chanprof
        for key in (self.kmatrix.profile_list + self.kmatrix.aerosol_list +
                    self.kmatrix.cloud_list):
            if (self.kmatrix[key] is not None):
                if (key == "DATE" or key == "TIME" or key == "ID"):
                    chanprof[key] = self.kmatrix[key]
                else:
                    if np.isscalar(self.kmatrix[key]):
                        chanprof[key] = self.kmatrix[key]
                    elif (self.kmatrix[key].ndim == 0):
                        chanprof[key] = self.kmatrix[key]
                    elif (self.kmatrix[key].ndim == 1):
                        chanprof[key] = self.kmatrix[key][ip]
                    elif (self.kmatrix[key].ndim == 2):
                        chanprof[key] = self.kmatrix[key][:, ip]
                    elif (self.kmatrix[key].ndim == 3):
                        chanprof[key] = self.kmatrix[key][:, :, ip]
                    else:
                        print "ERROR kmatrix getchanprof"
                chanprof[key + '_ATTRIBUTE'] = self.kmatrix[key + '_ATTRIBUTE']
                if ('LBOUND' in chanprof[key + '_ATTRIBUTE'].keys()):
                    del chanprof[key + '_ATTRIBUTE']['LBOUND']
                    del chanprof[key + '_ATTRIBUTE']['UBOUND']

        for key in self.kmatrix.s2m_list:
            if (self.kmatrix['S2M'][key] is not None):
                if (self.kmatrix['S2M'][key].ndim == 0):
                    chanprof['S2M'][key] = self.kmatrix['S2M'][key]
                elif (self.kmatrix['S2M'][key].ndim == 1):
                    chanprof['S2M'][key] = self.kmatrix['S2M'][key][ip]
                else:
                    print "ERROR kmatrix getchanprof"
                chanprof['S2M'][key + '_ATTRIBUTE'] = self.kmatrix['S2M'][
                    key + '_ATTRIBUTE']
                if ('LBOUND' in chanprof['S2M'][key + '_ATTRIBUTE'].keys()):
                    del chanprof['S2M'][key + '_ATTRIBUTE']['LBOUND']
                    del chanprof['S2M'][key + '_ATTRIBUTE']['UBOUND']

        for key in self.kmatrix.sskin_list:
            if (self.kmatrix['SKIN'][key] is not None):
                if (key == "FASTEM"):
                    chanprof['SKIN'][key] = self.kmatrix['SKIN'][key]
                else:
                    if (self.kmatrix['SKIN'][key].ndim == 0):
                        chanprof['SKIN'][key] = self.kmatrix['SKIN'][key]
                    elif (self.kmatrix['SKIN'][key].ndim == 1):
                        chanprof['SKIN'][key] = self.kmatrix['SKIN'][key][ip]
                    else:
                        print "ERROR kmatrix getchanprof"
                chanprof['SKIN'][key + '_ATTRIBUTE'] = self.kmatrix['SKIN'][
                    key + '_ATTRIBUTE']
                if ('LBOUND' in chanprof['SKIN'][key + '_ATTRIBUTE'].keys()):
                    del chanprof['SKIN'][key + '_ATTRIBUTE']['LBOUND']
                    del chanprof['SKIN'][key + '_ATTRIBUTE']['UBOUND']
        return chanprof

    def kreset(self):
        self.kscale(1.0)

    def kscale(self, coef):
        """Scale KMatrix by input profile * coef
        Temperature arrays are not modified
        This method could be called several times, scaling coefficient is
        always considered relative to the original matrix
        Could be called with coef = 1.0 even for the first call
        in that case the kMatrix is reset to the original values and units"""

        if (self.scaled):
            old = self.scalecoef
            val = coef / old
        else:
            val = coef

        if (coef == 1.0):
            print "KMatrix reset"
            reset = True
        else:
            print "KMatrix scaling by coef ", coef
            reset = False

        coefstr = '{}'.format(coef)
        try:
            nchan = len(self.chanprof['CHANNELS'])
        except:
            nchan = 1

        for key in (self.kmatrix.profile_list + self.kmatrix.aerosol_list +
                    self.kmatrix.cloud_list):
            if (self.kmatrix[key] is not None):
                if (key == "DATE" or key == "TIME" or key == "ID"
                        or key == "T"):
                    pass
                else:
                    if (self.kmatrix[key].ndim == 0):
                        pass
                    if (self.kmatrix[key].ndim == 1):
                        self.kmatrix[key][:] = self.kmatrix[
                            key][:] * self.profile[key][None] * val
                    elif (self.kmatrix[key].ndim == 2):
                        self.kmatrix[key][:, :] = self.kmatrix[
                            key][:, :] * self.profile[key][:, None] * val
                    elif (self.kmatrix[key].ndim == 3):
                        print self.kmatrix[key].shape
                        self.profile[key] = self.kmatrix[key][:, :, ip]
                        self.kmatrix[key][:, :, :] = self.kmatrix[
                            key][:, :, :] * self.profile[key][:, :, None] * val
                    if (self.scaledfirst):
                        self.kmatrix[key + '_ATTRIBUTE_SAVED'] = {
                            'UNITS': 'n/a'
                        }
                        self.kmatrix[
                            key + '_ATTRIBUTE_SAVED']['UNITS'] = self.kmatrix[
                                key + '_ATTRIBUTE']['UNITS']
                    if (reset):
                        self.kmatrix[key +
                                     '_ATTRIBUTE']['UNITS'] = self.kmatrix[
                                         key + '_ATTRIBUTE_SAVED']['UNITS']
                    else:
                        self.kmatrix[key + '_ATTRIBUTE']['UNITS'] = (
                            self.kmatrix[key + '_ATTRIBUTE_SAVED']['UNITS'] +
                            " scaled by " + coefstr + " input profile")

        for key in self.kmatrix.s2m_list:
            if (self.kmatrix['S2M'][key] is not None):
                if (self.kmatrix['S2M'][key].ndim == 0):
                    pass
                elif (key == "T"):
                    pass
                elif (self.kmatrix['S2M'][key].ndim == 1):
                    for i in range(nchan):
                        self.kmatrix['S2M'][key][i] = self.kmatrix['S2M'][key][
                            i] * self.profile['S2M'][key] * val
                    if (self.scaledfirst):
                        self.kmatrix['S2M'][key + '_ATTRIBUTE_SAVED'] = {
                            'UNITS': 'n/a'
                        }
                        self.kmatrix['S2M'][
                            key +
                            '_ATTRIBUTE_SAVED']['UNITS'] = self.kmatrix['S2M'][
                                key + '_ATTRIBUTE']['UNITS']
                    if (reset):
                        self.kmatrix['S2M'][
                            key + '_ATTRIBUTE']['UNITS'] = self.kmatrix['S2M'][
                                key + '_ATTRIBUTE_SAVED']['UNITS']
                    else:
                        self.kmatrix['S2M'][key + '_ATTRIBUTE']['UNITS'] = (
                            self.kmatrix['S2M'][key +
                                                '_ATTRIBUTE_SAVED']['UNITS'] +
                            " scaled by " + coefstr + " input profile")

        for key in self.kmatrix.sskin_list:
            if (self.kmatrix['SKIN'][key] is not None):
                if (key == "FASTEM" or key == "T"):
                    pass
                else:
                    if (self.kmatrix['SKIN'][key].ndim == 0):
                        pass
                    elif (self.kmatrix['SKIN'][key].ndim == 1):
                        for i in range(nchan):
                            self.kmatrix['SKIN'][key][
                                i] = self.kmatrix['SKIN'][key][
                                    i] * self.profile['SKIN'][key] * val
                        if (self.scaledfirst):
                            self.kmatrix['SKIN'][key + '_ATTRIBUTE_SAVED'] = {
                                'UNITS': 'n/a'
                            }
                            self.kmatrix['SKIN'][key + '_ATTRIBUTE_SAVED'][
                                'UNITS'] = self.kmatrix['SKIN'][
                                    key + '_ATTRIBUTE']['UNITS']
                        if (reset):
                            self.kmatrix['SKIN'][
                                key +
                                '_ATTRIBUTE']['UNITS'] = self.kmatrix['SKIN'][
                                    key + '_ATTRIBUTE_SAVED']['UNITS']
                        else:
                            self.kmatrix['SKIN'][
                                key +
                                '_ATTRIBUTE']['UNITS'] = (self.kmatrix['SKIN'][
                                    key + '_ATTRIBUTE_SAVED']['UNITS'] +
                                                          " scaled by " +
                                                          coefstr +
                                                          " input profile")

        if (reset):
            self.scaled = False
        else:
            self.scaled = True
        self.scalecoef = coef
        self.scaledfirst = False

    def read(self, filename):
        f = h5py.File(filename, 'r')
        # get the Dataset
        h5 = f['/']
        # Load kmatrix
        self.loadh5(h5)
        print 'loaded'
        # Close HDF file
        f.close()