# /usr/bin/env python # encoding: utf-8 ''' Created on Mar 16, 2011 Note: forked from make_ancil.py to be for more modis-specific stuff @author: nickb ''' # FIXME: clean up includes post-split from files_inc import * from files.HDF4File import HDF4File from files.HDF5File import HDF5File from files.GribFile import GribFile import misc from datetime import datetime, timedelta from optparse import OptionParser import os import string import re import numpy as np def handle_args(): parser = OptionParser() parser.add_option("-g", "--geofile", action="store", type="string", dest="geoFile", help="geolocation file to use") parser.add_option("-m", "--modisfile", action="store", type="string", dest="modisFile", help="modis granule to use") parser.add_option("-o", "--outFile", action="store", type="string", dest="outFile", help="resulting output hdf file") return parser.parse_args() def main(): # Space out our real output from any import issues. print "\n\n" startTime = datetime.now() print "***********************************" print "****** PREPP MODIS CLOUDMASK ******" print startTime print "\n" # Handle arguments (options, args) = handle_args() if options.modisFile is None: print "Missing MODIS file, use -h for details!" return modisFilename = os.path.abspath(options.modisFile) if options.geoFile is None: print "Missing geolocation file, use -h for details!" return geolocFilename = os.path.abspath(options.geoFile) geolocBasename, geolocExtension = os.path.splitext(geolocFilename) if options.outFile is None: # make the output file based on the geolocation filename. outputFilename = geolocBasename + '.anc' + geolocExtension else: outputFilename = options.outFile # Remove it if it exists (so I can rapidly test!) if os.access(outputFilename, os.F_OK): os.remove(outputFilename) # Create file object for output file: outputFile = HDF5File(outputFilename) # Get the file's date: # TODO: can this come from somewhere other than the filename? kinda hacky atm. yearSearch = re.search('(?<=MYD03.A)\d\d\d\d', geolocFilename) year = int(yearSearch.group(0)) daySearch = re.search('(?<=MYD03.A\d\d\d\d)\d\d\d', geolocFilename) day = int(daySearch.group(0)) hourSearch = re.search('(?<=MYD03.A\d\d\d\d\d\d\d.)\d\d', geolocFilename) hour = int(hourSearch.group(0)) minuteSearch = re.search('(?<=MYD03.A\d\d\d\d\d\d\d.\d\d)\d\d', geolocFilename) minute = int(minuteSearch.group(0)) wantedDatetime = datetime(year=year, month=1, day=1, hour=hour, minute=minute) + timedelta(days=day-1) print "GeoLocation File: " + geolocFilename print "MODIS imgr granule: " + modisFilename print "Out: " + outputFilename geolocFile = HDF4File(geolocFilename) # MODIS # REFLECTANCE: bands = '1,2,3,4,5,6,7,8,9,17,18,19,26' bandList = bands.split(',') imgrFile = HDF4File(modisFilename) # BT: bands = '20,21,22,27,28,29,31,32,33,35' bandList = bands.split(',') imgrFile = HDF4File(modisFilename) # MODIS CLOUDMASK SPECIFIC: sfct_flag, interp_sfctemp = misc.fitSurfaceTemp_sfct_flag(sfctemp, latitude.data, longitude.data, lsm, landseamask.data) output_file.writeData(writeVarName="sfct_flag", data=sfct_flag) output_file.writeData(writeVarName="Surface Temperature sfct_flag", data=interp_sfctemp)