File: ninjotiff_example

package info (click to toggle)
python-mpop 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 20,516 kB
  • ctags: 1,877
  • sloc: python: 15,374; xml: 820; makefile: 90; sh: 8
file content (130 lines) | stat: -rwxr-xr-x 4,115 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/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])