Skip to content
Snippets Groups Projects
iff.py 3.75 KiB
#!/usr/bin/env python
# encoding: utf-8
"""
Author: Nick Bearson, nickb@ssec.wisc.edu
Copyright (c) 2012 University of Wisconsin SSEC. All rights reserved.
"""

import aggregator

from optparse import OptionParser
import glob

from datetime import datetime, timedelta

import logging
log = logging.getLogger(__name__)


def _usage():
  return """usage: %prog [options] [platform] [sensor] [tag] [start day] [start time] [end time] [file ...]

ACCEPTED PARAMETERS:
[platform]: npp, aqua, terra
[sensor]: modis, viirs
[tag]: (modis) 1km, qkm, hkm, (viirs) svm, svi, dnb, (avhrr) nss
[start day]: Start day of the granule we want, in "yyyymmdd" format
[start time]: Start time of the granule we want, includes the full second, in "hhMMss" format
[end time]: End time of the granule we want, includes the full second, in "hhMMss" format

Note that files can either be provided with the full filename, or with a globbing string (e.g: asterisks "*" are wildcards,
but you may need to encase them in " " to avoid your shell expanding the wildcard and making the command go over your shell's character limit.)
"""	


def _handle_args():
  parser = OptionParser(usage=_usage())
  parser.add_option('-v', '--verbose', dest='verbosity', action="count", default=0,
                    help='each occurrence increases verbosity 1 level through ERROR-WARNING-INFO-DEBUG')
  parser.add_option('-b', '--bowtie-replacement', dest='bowtie', action='store_false', default=True,
  	                help='DISABLE bowtie replacement (only applicable to VIIRS)')
#  parser.add_option("-g", "--geofile", action="store", type="string", dest="geofile", help="geolocation file to use")
#  parser.add_option("-t", "--tag", action="store", type="string", dest="tag", help="file type tag")
#  parser.add_option("-c", "--cmoFile", action="store", type="string", dest="cmoFile", help="cloudmask output h5 file")

  return parser


def main():
  parser = _handle_args()
  options, args = parser.parse_args()

  levels = [logging.ERROR, logging.WARN, logging.INFO, logging.DEBUG]
  verbosity = min(options.verbosity, len(levels))
  logging.basicConfig(level = levels[options.verbosity])


  log.debug("args: %s" % (args))
  if not len(args) >= 7:
		print "Missing arguments!"
		parser.print_help()
		exit(1)

  platform, sensor, tag, start_day, start_time, end_time = args[:6]
  files = args[6:]

  FILELIST = []
  for fstr in files:
  	FILELIST.extend(glob.glob(fstr))

  START = datetime.strptime(start_day+start_time, "%Y%m%d%H%M%S")
  END   = datetime.strptime(start_day+end_time,   "%Y%m%d%H%M%S")

  if START > END:
  	END = END + timedelta(days=1)
  if START > END:
  	print "Invalid dates provided..."
  	print "Start Day: ", start_day
  	print "Start Time: ", start_time
  	print "End Time:", end_time
  	exit(2)

  creation_time = datetime.utcnow().strftime("%Y%m%d%H%M%S")

  out_name = "IFF_%(platform)s_%(sensor)s_%(tag)s_d%(start_day)s_t%(start_time)s_e%(end_time)s_c%(creation_time)s.hdf"
  out_name = out_name % {"platform"      : platform, 
  											 "sensor"        : sensor, 
  											 "tag"           : tag, 
  											 "start_day"     : start_day,
  											 "start_time"    : start_time,
  											 "end_time"      : end_time,
  											 "creation_time" : creation_time}

  BOWTIE = options.bowtie

  if sensor == "viirs":
  	from viirs import VIIRS as SENSOR
  elif sensor == "modis":
  	from modis import MODIS as SENSOR
  	BOWTIE = False  # We can't do bowtie replacement on MODIS
  elif sensor == "avhrr":
    from avhrr import AVHRR as SENSOR
    BOWTIE = False  # We can't do bowtie replacement on AVHRR
  else:
  	print "Invalid sensor: ", sensor
  	exit(3)

  agg = aggregator.Aggregator(platform, SENSOR, tag, FILELIST, START, END, BOWTIE)
  aggregator.write_agg_IFF_h4(agg, out_name)


if __name__ == "__main__":
  main()