#!/usr/bin/python
#
import sys
import os
import getopt
import glob
from datetime import datetime, timedelta
import numpy

from mipp import xrit, log

log.logging_on()
logger = log.get_logger('fsd')

max_age = {'MET7': timedelta(seconds=25*60),
           'GOES11' : timedelta(seconds=5*60),
           'GOES12' : timedelta(seconds=5*60),
           'GOES13' : timedelta(seconds=5*60),
           'MTSAT1R' : timedelta(seconds=5*60)}
supported_platforms = max_age.keys()

#-----------------------------------------------------------------------------

def usage():
    print >>sys.stderr, """\
    process_fsd --check-satellite <prologue-file>
        check if we handle this satellite
        
    process_fsd --check [-l] <prologue-file>
        check if number of image segments are as planned
        -l, list corresponding image segment files
        
    process_fsd --decompress [-o<output-dir>] <file> ... <file>
        decompress files to output-dir (default is working directory)
        -l, list decompressed files
        
    process_fsd --metadata <prologue-file> <image-segment> ... <image-segment>
        print meta-data
        
    process_fsd [-h] [-o<output-dir>] <prologue-file> <image-segment> ... <image-segment>
        -h, save image data to a HDF5 file (default is binary dump of image-data and ascii dump of meta-data)\
    """
    sys.exit(2)

#-----------------------------------------------------------------------------

def check_platform(prologue, verbose=False):
    if prologue.platform not in supported_platforms:
        if verbose:
            print >> sys.stderr, "Platform " + str(prologue.platform) + \
                " not supported by the Foreign Satellite Data script!"
        return False
    return True

def check_segments(prologue, listit=False):
    dname, fname = os.path.split(prologue.file_name)
    fname = fname.replace('-PRO______-', '-0????????-')
    fname = fname.replace('-_________-', '-?????????-')
    fname = fname[:-2] + '??'
    image_files = glob.glob(dname + '/' + fname)
    if not image_files:
        return False
    image_files.sort()
    if listit:
        for f in image_files:
            print f
    ctime = datetime.fromtimestamp(os.path.getctime(prologue.file_name))
    now = datetime.now()
    im = xrit.read_imagedata(image_files[-1])
    if im.segment.planned_end_seg_no == im.segment.seg_no:
        return True
    elif now - ctime > max_age[im.platform]:
        return True
    return False

def decompress(image_files, outdir='.', listit=False):
    for f in sorted(image_files):
        outfile = xrit.decompress(f, outdir)
        if listit:
            print outfile

def process(prologue, image_files, outdir='.', saveashdf5=False):
    im = xrit.read_imagedata(image_files[-1])
    logger.info('Processing: %s, %d image data files', prologue.product_id, len(image_files))
    mda, img = xrit.sat.load_files(prologue, image_files, mask=False, calibrate=False)()
    logger.info("Image data min, max: %.2f, %.2f %s", img.min(), img.max(), mda.calibration_unit)
    fname = outdir + '/' + prologue.product_id
    if saveashdf5:
        import hdfdmi
        fname += '.H5'
        logger.info("Writing: '%s'", fname)
        hdfdmi.save(mda, img, fname)
    else:
        fname_mda = fname + '.mda'
        fp = open(fname_mda, 'w')
        logger.info("Writing: '%s'", fname_mda)
        fp.write(str(mda) + '\n')
        fp.close()
        fname += '.dat'
        logger.info("Writing: '%s'", fname)
        if type(img) == numpy.ma.MaskedArray:
            img = img.filled(mda.no_data_value)
        img.tofile(fname)
            
    return True

#-----------------------------------------------------------------------------

long_options = ['check', 'check-satellite', 'decompress', 'metadata']
nlopt = 0
outdir = '.'
check = False
check_satellite = False
decomp = False
metadata = False
listit = False
saveashdf5 = False
opts, args = getopt.getopt(sys.argv[1:], 'o:lh', long_options)
for k, v in opts:
    if k == '--decompress':
        decomp = True
        nlopt += 1
    elif k == '--check':
        check = True
        nlopt += 1
    elif k == '--check-satellite':
        check_satellite = True
        nlopt += 1
    elif k == '--metadata':
        nlopt += 1
        metadata = True
    elif k == '-o':
        outdir = v
    elif k == '-l':
        listit = True
    elif k == '-h':
        saveashdf5 = True

if nlopt > 1:
    logger.error("Please specify only one of these: %s", ', '.join(['--' + s for s in long_options]))
    sys.exit(2)
    
pro_file = ''
image_files = []
try:
    if check or check_satellite:
        pro_file = args[0]
    elif decomp:
        image_files = args
    else:
        pro_file = args[0]
        image_files = args[1:]
except IndexError:
    usage()

if pro_file:
    prologue = xrit.read_prologue(pro_file)
    
    
#-----------------------------------------------------------------------------
return_code = 0

if check:
    if not (check_platform(prologue, verbose=True) and 
            check_segments(prologue, listit)):
        return_code = 1
        
elif check_satellite:
    if not check_platform(prologue):
        return_code = 1
        
elif decomp:
    decompress(image_files, outdir, listit)
    
elif metadata:
    if check_platform(prologue, verbose=True):
        print xrit.sat.load_files(prologue, image_files, only_metadata=True)

else:
    if check_platform(prologue, verbose=True):
        process(prologue, image_files, outdir, saveashdf5)

sys.exit(return_code)
