#! /usr/bin/env python # encoding: utf-8 ''' Created on August 14, 2011 @author: nickb Copyright (c) 2011 University of Wisconsin SSEC. All rights reserved. ''' """ """ MODIS_SHAPE = (2030, 1354) VIIRS_SHAPE = (768, 3200) SHAPE = VIIRS_SHAPE from pypre.files.HDF5File import HDF5File import os from optparse import OptionParser import numpy as np ViirsCMbitMasks = ((3,12,16,32,192), (7,8,16,32,64,128), (1,2,4,8,16,32,64,128), (3,4,8,16,32,64,128), (256,), (7,8,16,224)) ViirsCMbitShift = ((0,2,4,5,6), (0,3,4,5,6,7), (0,1,2,3,4,5,6,7), (0,2,3,4,5,6,7), (0,), (0,3,4,5)) ViirsCMbitMaskNames = (('CM Quality','Cloud Mask','DayNight','SnowIce','Sunglint'), ('Land/Water','Shadow','Heavy Aerosol','Fire','Cirrus (RM9)','Cirrus (BTM15-BTM16)'), ('IR Threshold Cloud Test (BTM15)','High Cloud (BTM12-BTM16) Test','IR Temperature Difference Test (BTM14 - BTM15 BTM15 - BTM16)','Temperature Difference Test (BTM15 - BTM12)','Temperature Difference Test (BTM12 - BTM13)','Visible Reflectance Test (RM5)','Visible Reflectance Test (RM7)','Visible Ratio Test (RM7 \ RM5)'), ('Adjacent Pixel Cloud Confident Value','Conifer Boreal Forest','Spatial Uniformity','Dust','Smoke','Dust or Vol Ash','Spare'), ('Spare',), ('Cloud Phase','Thin Cirrus','Ephemeral Water','Spare')) byte2bits = [''.join(['01'[i&(1<0] for b in xrange(7,-1,-1)]) for i in xrange(256)] def enum_cm(infileName): inf = HDF5File(infileName) for byte_index in range(6): bytes = inf.getDataset("All_Data/VIIRS-CM-IP_All/QF%d_VIIRSCMIP" % (byte_index + 1)) for bit_index, mask in enumerate(ViirsCMbitMasks[byte_index]): shift = ViirsCMbitShift[byte_index][bit_index] name = ViirsCMbitMaskNames[byte_index][bit_index] bytes = np.reshape(bytes, SHAPE) flag = bytes & mask flag = flag >> shift print byte_index, bit_index, name if name != 'Spare': yield name, flag def cloudout_convert(infileName, outfileName): print "Converting our cloud out file!" print "Output: ", outfileName inf = HDF5File(infileName) ouf = HDF5File(outfileName) e = enum_cm(infileName) for name, bitfield in e: print "Writing field: ", name ouf.writeData(name, bitfield) def handle_args(): parser = OptionParser() # parser.add_option("-i", "--infile", action="store", type="string", dest="infile", help="Input file.") # parser.add_option("-o", "--outfile", action="store", type="string", dest="outfile", help="Output file.") # parser.add_option("-n", "--navfile", action="store", type="string", dest="navfile", help="Navigation file.") return parser.parse_args() def main(): print "\n\n\n" # Handle arguments (options, args) = handle_args() infile = args[0] outfile = args[1] if infile is not None: infile = infile else: print "No input file provided." exit(1) if outfile is not None: outfile = outfile else: print "No output file provided." exit(1) # Remove output file if it exists (so I can rapidly test!) if os.access(outfile, os.F_OK): os.remove(outfile) # cloudout_convert(infile, navfile, outfile) cloudout_convert(infile, outfile) if __name__ == '__main__': main()