-
Notifications
You must be signed in to change notification settings - Fork 7
/
OSFWF_Assimilate_c.py
87 lines (75 loc) · 4.7 KB
/
OSFWF_Assimilate_c.py
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
"""
***************************************************************************
OSFWF_Assimilate_c.py
-------------------------------------
Copyright (C) 2014 TIGER-NET (www.tiger-net.org)
***************************************************************************
* This plugin is part of the Water Observation Information System (WOIS) *
* developed under the TIGER-NET project funded by the European Space *
* Agency as part of the long-term TIGER initiative aiming at promoting *
* the use of Earth Observation (EO) for improved Integrated Water *
* Resources Management (IWRM) in Africa. *
* *
* WOIS is a free software i.e. you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published *
* by the Free Software Foundation, either version 3 of the License, *
* or (at your option) any later version. *
* *
* WOIS is distributed in the hope that it will be useful, but WITHOUT ANY *
* WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
* for more details. *
* *
* You should have received a copy of the GNU General Public License along *
* with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************
"""
import os
import csv
import numpy
from PyQt4 import QtGui
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import *
from SWATAlgorithm import SWATAlgorithm
from ASS_utilities import ReadNoSubs
class OSFWF_Assimilate_c(SWATAlgorithm):
BOOLEAN = "BOOLEAN"
MOD_DESC = "MOD_DESC"
ASS_FOLDER = "ASS_FOLDER"
ERR_MOD_FILE = "ERR_MOD_FILE"
NBRCH = "NBRCH"
def __init__(self):
super(OSFWF_Assimilate_c, self).__init__(__file__)
def defineCharacteristics(self):
self.name = "4.3 - Assimilate observations (OSFWF) - update assimilation file"
self.group = "Operational simulation and forecasting workflow (OSFWF)"
self.addParameter(ParameterBoolean(OSFWF_Assimilate_c.BOOLEAN, "Replace with global parameters"))
self.addParameter(ParameterFile(OSFWF_Assimilate_c.MOD_DESC, "Select model description file", False, False))
self.addParameter(ParameterFile(OSFWF_Assimilate_c.ASS_FOLDER, "Select assimilation folder", True))
self.addParameter(ParameterFile(OSFWF_Assimilate_c.ERR_MOD_FILE, "Select file with error model parameters", False, False))
def processAlgorithm(self, progress):
BOOLEAN = self.getParameterValue(OSFWF_Assimilate_c.BOOLEAN)
MOD_DESC = self.getParameterValue(OSFWF_Assimilate_c.MOD_DESC)
ASS_FOLDER = self.getParameterValue(OSFWF_Assimilate_c.ASS_FOLDER)
ERR_MOD_FILE = self.getParameterValue(OSFWF_Assimilate_c.ERR_MOD_FILE)
# Extract total number of subbasins from model description file
NBRCH = ReadNoSubs(MOD_DESC)
if BOOLEAN == 1:
filename = ASS_FOLDER + os.sep + 'Assimilationfile.txt'
if os.path.isfile(filename) & os.path.isfile(ERR_MOD_FILE):
ass_lines = open(filename,'r').readlines()
err_lines = open(ERR_MOD_FILE,'r').readlines()
with open(ASS_FOLDER + os.sep + 'Assimilationfile.txt', 'wb') as csvfile:
file_writer = csv.writer(csvfile, delimiter=' ')
file_writer.writerow(['Reach'] + ['X'] + ['K'] + ['DrainsTo'] + ['Runoff'] +['alphaerr'] + ['Loss_fraction'])
for j in range(0,NBRCH):
l = ass_lines[j+1].split()
file_writer.writerow([l[i] for i in range(0,5)]+[err_lines[1].split()[0]] + [l[6]])
with open(ASS_FOLDER + os.sep + 'Assimilationfile_q.txt', 'wb') as csvfile:
file_writer = csv.writer(csvfile, delimiter=' ')
file_writer.writerow(['q'])
q = numpy.identity(NBRCH)*float(err_lines[1].split()[1])
for k in range(0,NBRCH):
file_writer.writerow(q[k])
else:
raise GeoAlgorithmExecutionException('Assimilationfile.txt, Assimilationfile_q.txt or '+ ERR_MOD_FILE +' not found in assimilation folder '+ ASS_FOLDER +'.')