Example #1
0
def createHDF(allvars):
    varinfo = {
        0:
        'nametimestamp',
        1:
        'calipso fname',
        2:
        'mod21km fname',
        3:
        'mod03 fname',
        4:
        'myd35 fnames',
        5: ['Latitude', {
            'units': 'deg',
            'valid_range': '-90.0...90.0'
        }],
        6: ['Longitude', {
            'units': 'deg',
            'valid_range': '-90.0...90.0'
        }],
        7:
        ['IGBP_Surface_Type', {
            'units': 'no units',
            'valid_range': '1... 18'
        }],
        8: ['DEM_Surface_Elevation', {
            'units': 'km'
        }],
        9: ['Layer_Top_Altitude', {
            'units': 'km',
            'fill_value': '-9999'
        }],
        10: ['Feature_Classification_Flags', {}],
        11: [
            'SCM_classification', {
                'valid_range': '0,1',
                'description': '1:Layered\n0:clear'
            }
        ],
        12: [
            'Clear_Layered_Mask', {
                'valid_range':
                '0...4',
                'description':
                '4:Invalid\n3:Cloud+Aerosol\n2:Aerosol\n1:Cloud\n0:Clear'
            }
        ],
        13: ['Cloud_Mask', {}],
        14: [
            'Confusion_Matrix_SCM', {
                'valid_range':
                '-2,-1,1,2',
                'description':
                '2:True Layered\n1:True Clear\n-1:False Clear\n-2:False Layered'
            }
        ],
        15: [
            'Confusion_Matrix_CM', {
                'valid_range':
                '-2,-1,1,2',
                'description':
                '2:True Layered\n1:True Clear\n-1:False Clear\n-2:False Layered'
            }
        ],
        16: ['Number_Layers_Found', {}],
        17: ['SensorZenith', {
            'units': 'deg',
            'valid_range': '0.0...180.0'
        }],
        18: ['SensorAzimuth', {
            'units': 'deg',
            'valid_range': '0.0...180.0'
        }],
        19:
        ['Solar_Zenith_Angle', {
            'units': 'deg',
            'valid_range': '0.0...180.0'
        }],
        20: [
            'Solar_Azimuth_Angle', {
                'units': 'deg',
                'valid_range': '0.0...180.0'
            }
        ],
        21: ['Layer_Base_Altitude', {
            'units': 'km'
        }],
        22: ['Layer_Top_Pressure', {}],
        23: ['Midlayer_Pressure', {}],
        24: ['Layer_Base_Pressure', {}],
        25: ['Layer_Top_Temperature', {}],
        26: ['Layer_Centroid_Temperature', {}],
        27: ['Midlayer_Temperature', {}],
        28: ['Layer_Base_Temperature', {}],
        29: ['CAD_Score', {
            'fill_value': '-127'
        }],
        30: ['Initial_CAD_Score', {
            'fill_value': '-127'
        }],
        31: [
            'Profile_UTC_Time', {
                'units': 'no units',
                'valid_range': '60,426.0...261,231.0'
            }
        ],
        32: [
            'Snow_Ice_Surface_Type', {
                'units': 'no units',
                'valid_range': '0.0...255.0'
            }
        ],
        33:
        ['Scattering_Angle', {
            'units': 'deg',
            'valid_range': '0.0...180.0'
        }],
        34: ['Skill_Score', {}],
        35: ['Hit_Rate', {}]
    }

    data_types = {
        'float64': SDC.FLOAT64,
        'float32': SDC.FLOAT32,
        'int32': SDC.INT32,
        'uint32': SDC.UINT32,
        'int16': SDC.INT16,
        'uint16': SDC.UINT16,
        'int8': SDC.INT8,
        'uint8': SDC.UINT8,
        '<U11': SDC.UCHAR
    }

    filename = 'CALTRACK-333m_SCM_V1-1_' + allvars[
        0] + '.hdf'  # Create HDF file.
    print(filename)
    path = 'E:\\Custom_HDFs\\'
    hdfFile = SD(path + filename, SDC.WRITE | SDC.CREATE)
    # Assign a few attributes at the file level
    hdfFile.File_Name = filename
    hdfFile.Timestamp = allvars[0]
    hdfFile.Fill_Value = -9999
    hdfFile.Description = 'SCM, VFM, MYD35 cloud classifications coincident with the CALIOP 333m track'
    fused = f'{allvars[1]}\n{allvars[2]}\n{allvars[3]}\n' + '\n'.join(
        str(s) for s in allvars[4])
    hdfFile.Files_Used = fused

    #Parametirize this
    print('SKILL SCORES:', allvars[34])
    hdfFile.Skill_Scores = f'MYD35:{allvars[34][0]}\nSCM:{allvars[34][1]}'
    hdfFile.Hit_Rates = f'MYD35:{allvars[35][0]}\nSCM:{allvars[35][1]}'
    hdfFile.Processing_Time = datetime.datetime.now().strftime(
        "%Y-%m-%d %H:%M:%S")

    for i in range(5, len(allvars) - 2):
        #Check if array is 2D
        try:
            allvars[i].shape
            arr = allvars[i]
        except:
            arr = np.array(allvars[i])

        #Get data type from data_types dictionary
        data_type = data_types.get(str(arr.dtype))

        #Different x value for 2D arrays
        x = 1
        if arr.ndim > 1: x = arr.shape[1]
        v1 = hdfFile.create(varinfo.get(i)[0], data_type, (len(arr), x))

        #GZIP compression
        v1.setcompress(SDC.COMP_DEFLATE, value=1)

        # Set some attributts on 'd1'
        for key in varinfo.get(i)[1]:
            setattr(v1, key, varinfo.get(i)[1][key])

        v1[0:] = arr
        v1.endaccess()  # Close file

    hdfFile.end()