-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathconvert_CAMS_DBL.py
More file actions
129 lines (94 loc) · 4.54 KB
/
Copy pathconvert_CAMS_DBL.py
File metadata and controls
129 lines (94 loc) · 4.54 KB
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
#! /usr/bin/env python
# -*- coding: iso-8859-1 -*-
"""
Processes a Sentinel-2 time series for a tile using MAJA processor for atmospheric correction and cloud screening.
MAJA was developped by CS-SI, under a CNES contract, using a multi-temporal method developped at CESBIO, for the MACCS processor and including methods developped by DLR for ATCOR.
This tool, developped by O.Hagolle (CNES:CESBIO) is a very basic one to show how to use MAJA to process a time series. If anything does not go as anticipated, the tool will probably crash
"""
import glob
import tempfile
import optparse
import os
import os.path
import shutil
import sys
import logging
logger = logging.getLogger('Start-Maja')
logger.setLevel(logging.DEBUG)
if not logger.handlers:
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
START_MAJA_VERSION = 3.1
# #########################################################################
class OptionParser(optparse.OptionParser):
def check_required(self, opt):
option = self.get_option(opt)
# Assumes the option's 'default' is set to None!
if getattr(self.values, option.dest) is None:
self.error("%s option not supplied" % option)
# #################################### Lecture de fichier de parametres "Key=Value"
def read_folders(fic_txt):
repCode = repWork = repL1= repL2 = repMaja = repCAMS = repCAMS_raw = None
with file(fic_txt, 'r') as f:
for ligne in f.readlines():
if ligne.find('repCAMS') == 0:
repCAMS = (ligne.split('=')[1]).strip()
if ligne.find('repCAMS_raw') == 0:
repCAMS_raw = (ligne.split('=')[1]).strip()
missing = False
if repCAMS is None:
logger.debug("repCAMS is missing from configuration file. Needed : repCode, repWork, repL1, repL2, repMaja")
if repCAMS_raw is None:
logger.debug("repCAMS_raw is missing from configuration file. Needed : repCode, repWork, repL1, repL2, repMaja")
if missing:
raise Exception("Configuration file is not complete. See log file for more information.")
directory_missing = False
if repCAMS is not None and not os.path.isdir(repCAMS):
logger.error("repCAMS %s is missing", repCAMS)
if repCAMS_raw is not None and not os.path.isdir(repCAMS_raw):
logger.error("repCAMS %s is missing", repCAMS_raw)
if directory_missing:
raise Exception("One or more directories are missing. See log file for more information.")
return repCAMS, repCAMS_raw
def manage_rep_cams(repCams, repCamsRaw, working_dir):
exocam_creation(repCamsRaw, out_dir=repCams, working_dir=repCams)
return repCams
def exocam_creation(input_dir, out_dir=None, working_dir="/tmp"):
processed_dates = []
nb_files = len(myGlob(input_dir, "*.nc"))
compteur = 1
for file_cams in myGlob(input_dir, "*.nc"):
date_file = get_date(file_cams)
if date_file in processed_dates:
continue
#print("Processing {}/{} : {} ".format(compteur, nb_files/3,date_file), end='\r')
date_written_in_file = back_to_filename_date(date_file)
aot_file = searchOneFile(input_dir, "*AOT_{}*".format(date_written_in_file))
mr_file = searchOneFile(input_dir, "*MR_{}*".format(date_written_in_file))
rh_file = searchOneFile(input_dir, "*RH_{}*".format(date_written_in_file))
process_one_file(aot_file, mr_file, rh_file, out_dir, working_dir)
processed_dates.append(date_file)
compteur += 1
if __name__ == '__main__':
# ========== command line
if len(sys.argv) == 1:
prog = os.path.basename(sys.argv[0])
print ' ' + sys.argv[0] + ' [options]'
print " Aide : ", prog, " --help"
print " ou : ", prog, " -h"
print "exemple : "
print "\t python %s -f folders.txt -c nominal -t 40KCB -s Reunion -d 20160401 " % sys.argv[0]
sys.exit(-1)
else:
usage = "usage: %prog [options] "
parser = OptionParser(usage=usage, version='%prog {}'.format(START_MAJA_VERSION))
parser.add_option("-f", "--folder", dest="folder_file", action="store", type="string",
help="folder definition file", default=None)
(options, args) = parser.parse_args()
# =================directories
folder_file = options.folder_file
(repCams, repCamsRaw) = read_folders(folder_file)
repCams = manage_rep_cams(repCams, repCamsRaw, repCams)