2222import static org .jgrasstools .gears .libs .modules .JGTConstants .isNovalue ;
2323
2424import java .util .HashMap ;
25- import java .util .Set ;
26- import java .util .Map .Entry ;
25+ import java .util .Map ;
2726
2827import oms3 .annotations .Description ;
2928import oms3 .annotations .Execute ;
29+ import oms3 .annotations .Finalize ;
3030import oms3 .annotations .In ;
31+ import oms3 .annotations .Initialize ;
3132import oms3 .annotations .Out ;
3233
33- import org .geotools .feature .SchemaException ;
3434import org .jgrasstools .gears .libs .modules .JGTModel ;
35- import java .io .IOException ;
3635
37-
38-
39-
40- // TODO: Auto-generated Javadoc
4136/**
4237 * The Class SumSeries compute the sum of two time series
38+ *
39+ * @author sidereus <francesco.serafin.3@gmail.com>
40+ * @date Nov 2nd, 2016
4341 */
44- public class SumSeries extends JGTModel {
45-
42+ public class SumSeries extends JGTModel {
4643
4744 @ Description ("Input first discharge Hashmap" )
4845 @ In
@@ -56,9 +53,18 @@ public class SumSeries extends JGTModel{
5653 @ Description ("The output HashMap with the sum"
5754 + "for the considered layer " )
5855 @ Out
59- public HashMap < Integer , double []> outHMQtot = new HashMap <Integer , double []>() ;
56+ public static HashMap <Integer , double []> outHMQtot ;
6057
58+ @ In
59+ public static Integer id ;
6160
61+ private static int timeSeriesCounter = 0 ;
62+
63+ @ Initialize
64+ public void init () {
65+ outHMQtot = new HashMap <>();
66+ timeSeriesCounter = 0 ;
67+ }
6268
6369 /**
6470 * Process.
@@ -68,61 +74,37 @@ public class SumSeries extends JGTModel{
6874 @ Execute
6975 public void process () throws Exception {
7076
71- //checkNull(inHMDischarge);
72-
73- Set <Entry <Integer , double []>> entrySet = inHMDischarge .entrySet ();
74-
75- Set <Entry <Integer , double []>> entrySet2 = inHMDischarge2 .entrySet ();
77+ System .out .println (id + ": processing sum series" );
7678
79+ checkNull (inHMDischarge );
80+ double [] sum = new double [1 ];
7781
78- for ( Entry < Integer , double []> entry : entrySet ){
79- for ( Entry < Integer , double []> entry2 : entrySet2 ){
82+ sumInHMDischarge ( sum , inHMDischarge , timeSeriesCounter );
83+ sumInHMDischarge ( sum , inHMDischarge2 , 0 );
8084
81- Integer ID = entry . getKey ( );
82- Integer ID2 = entry2 . getKey () ;
85+ outHMQtot . put ( id , sum );
86+ timeSeriesCounter += 1 ;
8387
84- double Q1 =inHMDischarge .get (ID )[0 ];
85- if (isNovalue (Q1 )) Q1 = 0 ;
88+ }
8689
87- double Q2 =inHMDischarge2 .get (ID2 )[0 ];
88- if (isNovalue (Q2 )) Q2 = 0 ;
90+ @ Finalize
91+ public void finalize () {
92+ timeSeriesCounter = 0 ;
93+ outHMQtot .clear ();
94+ }
8995
90- /** sum of the given quantities */
91- double sum = sum ( Q1 , Q2 );
96+ private void sumInHMDischarge ( double [] sum , final HashMap < Integer ,
97+ double []> discharge , int index ) {
9298
93- /** Save the result in hashmap output */
94- storeResult_series (ID ,sum );
99+ if (discharge != null ) {
100+ for (Map .Entry <Integer , double []> me : discharge .entrySet ()) {
101+ double tmpDischarge = me .getValue ()[index ];
102+ if (isNovalue (tmpDischarge )) tmpDischarge = 0.0 ;
103+ sum [0 ] += tmpDischarge ;
95104 }
96105 }
97106
98107 }
99108
109+ }
100110
101-
102-
103- /**
104- * Sum: computation of the sum
105- *
106- * @param Q1: the first quantity
107- * @param Q2 : the second quantity
108- * @return the double value of the sum
109- * @throws IOException Signals that an I/O exception has occurred.
110- */
111- public double sum (double Q1 , double Q2 ) throws IOException {
112- double Qtot =Q1 +Q2 ;
113- return Qtot ;
114- }
115-
116-
117- /**
118- * Store result_series
119- *
120- * @param Qtot: the vector with the sums
121- * @throws SchemaException the schema exception
122- */
123- private void storeResult_series (int ID ,double Qtot ) throws SchemaException {
124- outHMQtot .put (ID , new double []{Qtot });
125-
126-
127- }
128- }
0 commit comments