Blame view

app/run_snow_detector.py 1.81 KB
1d97898c   Grizonnet Manuel (CNES)   BUG: add env pyth...
1
#!/usr/bin/env python
a3e74eab   Grizonnet Manuel (CNES)   BUg: fix all pyth...
2
# -*- coding: utf-8 -*-
1d97898c   Grizonnet Manuel (CNES)   BUG: add env pyth...
3

72cdee3d   Tristan Klempka (Stagiaire CNES)   ENH: separate pyt...
4
import sys
12644f82   Tristan Klempka (Stagiaire CNES)   ENH: add stdout a...
5
import os.path as op
72cdee3d   Tristan Klempka (Stagiaire CNES)   ENH: separate pyt...
6
import json
f1725a82   Germain Salgues   ENH: change pytho...
7
import logging
72cdee3d   Tristan Klempka (Stagiaire CNES)   ENH: separate pyt...
8
9
from s2snow import snow_detector

0f20b234   Grizonnet Manuel (CNES)   ENH: update devel...
10
VERSION = "1.5"
0e0fceea   Grizonnet Manuel (CNES)   ENH: run autopep8...
11

72cdee3d   Tristan Klempka (Stagiaire CNES)   ENH: separate pyt...
12
13

def show_help():
918878c2   Tristan Klempka (Stagiaire CNES)   DOC: fix document...
14
    """Show help of the run_snow_detector script"""
3c5b61e3   Germain Salgues   ENH: code formatting
15
16
    print "This script is used to run the snow detector module that compute snow mask" \
          + " using OTB applications on Spot/LandSat/Sentinel-2 products from theia platform"
918878c2   Tristan Klempka (Stagiaire CNES)   DOC: fix document...
17
18
19
    print "Usage: python run_snow_detector.py param.json"
    print "python run_snow_detector.py version to show version"
    print "python run_snow_detector.py help to show help"
72cdee3d   Tristan Klempka (Stagiaire CNES)   ENH: separate pyt...
20

0e0fceea   Grizonnet Manuel (CNES)   ENH: run autopep8...
21

72cdee3d   Tristan Klempka (Stagiaire CNES)   ENH: separate pyt...
22
23
24
def show_version():
    print VERSION

0e0fceea   Grizonnet Manuel (CNES)   ENH: run autopep8...
25
26
# ----------------- MAIN ---------------------------------------------------

72cdee3d   Tristan Klempka (Stagiaire CNES)   ENH: separate pyt...
27
28
29
30

def main(argv):
    """ main script of snow extraction procedure"""

0e0fceea   Grizonnet Manuel (CNES)   ENH: run autopep8...
31
    json_file = argv[1]
72cdee3d   Tristan Klempka (Stagiaire CNES)   ENH: separate pyt...
32

0e0fceea   Grizonnet Manuel (CNES)   ENH: run autopep8...
33
    # Load json_file from json files
72cdee3d   Tristan Klempka (Stagiaire CNES)   ENH: separate pyt...
34
    with open(json_file) as json_data_file:
0e0fceea   Grizonnet Manuel (CNES)   ENH: run autopep8...
35
36
        data = json.load(json_data_file)

1bc7a05b   Tristan Klempka (Stagiaire CNES)   ENH: logging can ...
37
38
    general = data["general"]
    pout = general.get("pout")
1bc7a05b   Tristan Klempka (Stagiaire CNES)   ENH: logging can ...
39
    log = general.get("log", True)
0e0fceea   Grizonnet Manuel (CNES)   ENH: run autopep8...
40

1bc7a05b   Tristan Klempka (Stagiaire CNES)   ENH: logging can ...
41
42
43
    if log:
        sys.stdout = open(op.join(pout, "stdout.log"), 'w')
        sys.stderr = open(op.join(pout, "stderr.log"), 'w')
0e0fceea   Grizonnet Manuel (CNES)   ENH: run autopep8...
44

f1725a82   Germain Salgues   ENH: change pytho...
45
    # Set logging level and format.
3c5b61e3   Germain Salgues   ENH: code formatting
46
47
    logging.basicConfig(stream=sys.stdout, level=logging.INFO, \
        format='%(asctime)s - %(filename)s:%(lineno)s - %(levelname)s - %(message)s')
f1725a82   Germain Salgues   ENH: change pytho...
48
49
    logging.info("Start run_snow_detector.py")
    logging.info("Input args = " + json_file)
464036f0   Germain Salgues   ENH: code and fil...
50

f1725a82   Germain Salgues   ENH: change pytho...
51
    # Run the snow detector
3c5b61e3   Germain Salgues   ENH: code formatting
52
53
    snow_detector_app = snow_detector.snow_detector(data)
    snow_detector_app.detect_snow(2)
c392be60   Germain Salgues   Added
54
    logging.info("End run_snow_detector.py")
0e0fceea   Grizonnet Manuel (CNES)   ENH: run autopep8...
55

72cdee3d   Tristan Klempka (Stagiaire CNES)   ENH: separate pyt...
56
if __name__ == "__main__":
0e0fceea   Grizonnet Manuel (CNES)   ENH: run autopep8...
57
    if len(sys.argv) != 2:
72cdee3d   Tristan Klempka (Stagiaire CNES)   ENH: separate pyt...
58
59
60
61
62
63
64
65
        show_help()
    else:
        if sys.argv[1] == "version":
            show_version()
        elif sys.argv[1] == "help":
            show_help()
        else:
            main(sys.argv)