#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ninjotiff_example

Created on Wed Jul  3 13:02:48 2013

@author: ras


This example is using a ninjotiff.cfg file. If you prefer to pass all
meta-data by arguments, they can be defined like:

ninjotiff_config = {
      0.6: {'description': 'MSG Channel 1',
            'sat_id': 6200014,
            'chan_id': 100015,
            'data_cat': 'GORN',
            'data_source': 'EUMETCAST',},
     10.8: {'description': 'MSG Channel 9',
            'sat_id': 6200014,
            'chan_id': 900015,
            'data_cat': 'GORN',
            'data_source': 'EUMETCAST',},
    'HRV': {'description': 'MSG Channel 12',
            'sat_id': 6200014,
            'chan_id': 1200015,
            'data_cat': 'GORN',
            'data_source': 'EUMETCAST',}
}

Saving an image for 'chn' will then go like:

    image.save(filename,
               fformat='mpop.imageo.formats.ninjotiff',
               physic_unit=physic_unit,
               **ninjotiff_config[chn])
"""
import sys
import os
from datetime import datetime

# Basic check.
try:
    os.environ['PPP_CONFIG_DIR']
except KeyError:
    print >>sys.stderr, "PPP_CONFIG_DIR is not defined"
    sys.exit(2)

from mpop.satellites import GeostationaryFactory
import mpop.utils

#mpop.utils.debug_on()
LOG = mpop.utils.get_logger(__name__)

# Handle argument.
try:
    filename = sys.argv[1]
except IndexError:
    print >> sys.stderr, "usage: ninjotiff_example <MSG EPI filename>"
    exit(2)

i__ =  os.path.basename(filename).split('-')
TIMESLOT = datetime.strptime(i__[6], '%Y%m%d%H%M')
SATNO = "%02d" % (7 + int(i__[2][3]))

# Areas to be loaded into and to be projected onto.
AREAS = (
    ('visir_full', 'MSGF', 'msg_pc'),
    ('hrv_north', 'MSGHRVN', 'msg_hrvn_pc')
    )

CHANNEL_DICT = {
    #'visir_full': (0.6, 0.8, 1.6, 3.9, 6.2, 7.3, 8.7, 9.7, 10.8, 12.0, 13.4),
    'visir_full': (0.6, 10.8,),
    'hrv_north': ('HRV',)
    }


for area_name, area_in, area_out in AREAS:

    global_data = GeostationaryFactory.create_scene("meteosat",
                                                    SATNO, 
                                                    "seviri", 
                                                    area=area_in,
                                                    time_slot=TIMESLOT)

    # Load channel by channel (to save memory).
    for chn in CHANNEL_DICT[area_name]:
        global_data.load([chn])
        chn_name = global_data[chn].name

        # Save 'unit' ... it seems to be lost somewhere.
        global_data[chn].unit = global_data[chn].info.get('units', 'None')

        # Resample to Plate Caree.
        scene = global_data.project(area_out, mode='quick', precompute=True)

        # Kelvin -> Celsius.
        physic_unit = scene[chn].unit
        if physic_unit == 'K':
            scene[chn].data -= 273.15
            physic_unit = scene[chn].unit = 'C'
    
        # GeoImage without any data scaling or enhancement.
        img = scene.image(chn, mode="L")
        LOG.info("%s (%s, %s) %.2f %.2f %.2f" % (chn_name, physic_unit,
                                                 img.channels[0].dtype,
                                                 img.channels[0].min(),
                                                 img.channels[0].mean(),
                                                 img.channels[0].max()))
    
        #
        # Save it to Ninjo tif format, pass physics unit and Ninjo product name
        # (by "coincidence" product name correspond to MPOP's channel name :).
        #
        # If physics unit is not passed we expect to find it in ninjotiff's
        # config file.
        #
        filename = ('MSG-' + TIMESLOT.strftime("%Y%m%d_%H%M") + '-' +
                    area_name.split('_')[-1] + '-' + chn_name + '.tif')
        LOG.info("Saving to Ninjo tif %s" % filename)
        img.save(filename,
                 fformat='mpop.imageo.formats.ninjotiff',
                 physic_unit=physic_unit,
                 ninjo_product_name=chn_name)

        # Cleanup.
        scene.unload([chn])
        global_data.unload([chn])
