001/*
002 * This file is part of McIDAS-V
003 *
004 * Copyright 2007-2024
005 * Space Science and Engineering Center (SSEC)
006 * University of Wisconsin - Madison
007 * 1225 W. Dayton Street, Madison, WI 53706, USA
008 * https://www.ssec.wisc.edu/mcidas/
009 * 
010 * All Rights Reserved
011 * 
012 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
013 * some McIDAS-V source code is based on IDV and VisAD source code.  
014 * 
015 * McIDAS-V is free software; you can redistribute it and/or modify
016 * it under the terms of the GNU Lesser Public License as published by
017 * the Free Software Foundation; either version 3 of the License, or
018 * (at your option) any later version.
019 * 
020 * McIDAS-V is distributed in the hope that it will be useful,
021 * but WITHOUT ANY WARRANTY; without even the implied warranty of
022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
023 * GNU Lesser Public License for more details.
024 * 
025 * You should have received a copy of the GNU Lesser Public License
026 * along with this program.  If not, see https://www.gnu.org/licenses/.
027 */
028
029package edu.wisc.ssec.mcidasv.adt;
030
031import java.lang.Math;
032import java.lang.String;
033
034public class MWAdj {
035
036    public MWAdj() {
037    }
038
039    public static double[] adt_calcmwadjustment(double MWScoreInput,
040            double IntensityEstimateValueInput) {
041
042        int MWScoreApplicationFlag = 0;
043        int XInc;
044        int NumRecsHistory = History.HistoryNumberOfRecords();
045        int RecDate, RecTime, RecLand;
046        int RecMWDate, RecMWTime;
047        int Rule8Flag = 0;
048        ;
049        int LastCloudSceneValue;
050        int LastRule8Flag = 0;
051        ;
052        int NonEIRSceneCounter = 0;
053        int LastEyeSceneValue = 0;
054        double IntensityEstimateValueReturn = IntensityEstimateValueInput;
055        double InputIntensityEstStore = IntensityEstimateValueInput;
056        double MWScoreThresholdValue1 = 20.0;
057        double MWScoreThresholdValue2 = 60.0;
058        double MWIntensityTnoValue1 = 4.3;
059        double MWIntensityTnoValue1a = 4.8;
060        double MWIntensityTnoValue2 = 5.0;
061        double RecMWScore;
062        double Traw;
063        double PreviousMWScoreValue;
064        double HistoryRecTime = 0.0;
065        double LastCIValue = 1.0;
066        double FirstMWHoldTime = 0.0;
067        double LastValidMWCIValue = 0.0;
068        double LastMWScoreValue = 0.0;
069        double FirstHistoryCIValue = 0.0;
070        double FirstMWadjTime = 0.0;
071        double FirstMWadjTimePlus12hr = 0.0;
072        double LastValidMWadjTime = 0.0;
073        double LastValidMWadjTimePlus6 = 0.0;
074        double HistoryCIValue = 0.0;
075        double HistoryCIValueMinus12hr = 0.0;
076        double HistoryRecTimeMinus12hr = 0.0;
077        double InterpTimePart, IntensityCIMergeValue, NewIntensityEstScore;
078        boolean LandCheckTF = true;
079        boolean FirstMWHoldTF = true;
080        boolean EXIT_Routine = false;
081        boolean MWAdjOFFTF = false;
082        boolean FoundHistoryRecPrev12hrTF = false;
083        boolean First31Record = true;
084        boolean MWAdjCurrentONTF = false;
085        boolean NORecordAfterMWTimeTF = true;
086        String CommentString = "";
087
088        boolean LandFlagTF = Env.LandFlagTF;
089
090        int CurDate = History.IRCurrentRecord.date;
091        int CurTime = History.IRCurrentRecord.time;
092        int CurCloudScene = History.IRCurrentRecord.cloudscene;
093        int CurEyeScene = History.IRCurrentRecord.eyescene;
094        int CurLand = History.IRCurrentRecord.land;
095        double CurrentTime = Functions.calctime(CurDate, CurTime);
096
097        System.out.printf("INPUT current mw env score=%f date=%d time=%d\n", MWScoreInput,
098                Env.MWJulianDate, Env.MWHHMMSSTime);
099        /* check for valid MW eye score value (not equal to -920) */
100        if (MWScoreInput < -900.0) {
101            if (Env.DEBUG == 100) {
102                System.out.printf("MW SCORE=-920 ... will restore to previous valid record");
103            }
104            XInc = 0;
105            while (XInc < NumRecsHistory) {
106                RecDate = History.HistoryFile[XInc].date;
107                RecTime = History.HistoryFile[XInc].time;
108                HistoryRecTime = Functions.calctime(RecDate, RecTime);
109
110                RecLand = History.HistoryFile[XInc].land;
111                Traw = History.HistoryFile[XInc].Traw;
112                LandCheckTF = true;
113                if (((LandFlagTF) && (RecLand == 1)) || (Traw < 1.0)) {
114                    LandCheckTF = false;
115                }
116                if (LandCheckTF) {
117                    RecMWDate = History.HistoryFile[XInc].mwdate;
118                    RecMWTime = History.HistoryFile[XInc].mwtime;
119                    RecMWScore = History.HistoryFile[XInc].mwscore;
120                    MWScoreInput = RecMWScore;
121                    History.IRCurrentRecord.mwdate = RecMWDate;
122                    History.IRCurrentRecord.mwtime = RecMWTime;
123                    History.IRCurrentRecord.mwscore = RecMWScore;
124                    Env.MWJulianDate = RecMWDate;
125                    Env.MWHHMMSSTime = RecMWTime;
126                    Env.MWScore = RecMWScore;
127                }
128                XInc++;
129            }
130        }
131
132        /* determine MW score flag and value */
133        if (MWScoreInput >= MWScoreThresholdValue1) {
134            MWScoreApplicationFlag = 1;
135        }
136        if ((MWScoreInput >= MWScoreThresholdValue1) && (MWScoreInput <= MWScoreThresholdValue2)) {
137            IntensityEstimateValueReturn = MWIntensityTnoValue1 + 0.01;
138        }
139        if (MWScoreInput >= MWScoreThresholdValue2) {
140            IntensityEstimateValueReturn = MWIntensityTnoValue2 + 0.01;
141        }
142
143        int MWJulianDateGlobal = Env.MWJulianDate;
144        int MWTimeGlobal = Env.MWHHMMSSTime;
145        if (Env.DEBUG == 100) {
146            System.out.printf("MW DATE=%d TIME=%d\n", MWJulianDateGlobal, MWTimeGlobal);
147        }
148
149        double CurrentMWTime = Functions.calctime(MWJulianDateGlobal, MWTimeGlobal);
150
151        double CurrentMWTimeMinus12hr = CurrentMWTime - 0.5;
152        double CurrentMWTimePlus12hr = CurrentMWTime + 0.5;
153        double CurrentMWTimePlus6hr = CurrentMWTime + 0.25;
154
155        if (Env.DEBUG == 100) {
156            System.out.printf("MWScoreInput=%f  *IntensityEstimateValue_Return=%f\n", MWScoreInput,
157                    IntensityEstimateValueReturn);
158            System.out.printf("CurrentTime=%f  CurrentMWTime=%f CurrentMWTimePlus12hr=%f\n",
159                    CurrentTime, CurrentMWTime, CurrentMWTimePlus12hr);
160        }
161
162        /* redo MW score logic */
163        if (CurrentTime > CurrentMWTimePlus12hr) {
164            MWScoreApplicationFlag = 0;
165        }
166
167        /*
168         * NEW LOGIC - If last valid MW overpass time is greater than 8 hours
169         * old, then MW HOLD -- If, while in MW HOLD pattern we get three shear
170         * scenes, turn MW OFF -- If in MW HOLD for greater than or equal to 6
171         * hours, turn MW OFF - Will NOT check for three shear scenes if in MW
172         * ON
173         */
174
175        if ((CurrentTime - CurrentMWTime) > .3333) {
176            /* MW record is > 8 hours old... start new Shear/HOLD checking logic */
177            if (Env.DEBUG == 100) {
178                System.out.printf(
179                        "MW record > 8 hours old... new logic  CurrentTime=%f CurrentMWTime=%f\n",
180                        CurrentTime, CurrentMWTime);
181            }
182
183            /* First... loop through history file records */
184            XInc = 0;
185            while (XInc < NumRecsHistory) {
186                RecDate = History.HistoryFile[XInc].date;
187                RecTime = History.HistoryFile[XInc].time;
188                HistoryRecTime = Functions.calctime(RecDate, RecTime);
189
190                RecLand = History.HistoryFile[XInc].land;
191                Traw = History.HistoryFile[XInc].Traw;
192                LandCheckTF = true;
193                if (((LandFlagTF) && (RecLand == 1)) || (Traw < 1.0)) {
194                    LandCheckTF = false;
195                }
196
197                if (LandCheckTF) {
198                    if (HistoryRecTime < CurrentTime) {
199                        LastCloudSceneValue = History.HistoryFile[XInc].cloudscene;
200                        LastRule8Flag = History.HistoryFile[XInc].rule8;
201                        LastValidMWCIValue = History.HistoryFile[XInc].CI;
202                        NonEIRSceneCounter = (LastCloudSceneValue >= 4) ? NonEIRSceneCounter + 1
203                                : 0;
204                        if (Env.DEBUG == 100) {
205                            System.out.printf("time=%f nonEIRscenecounter=%d\n", HistoryRecTime,
206                                    NonEIRSceneCounter);
207                        }
208                        if (LastRule8Flag == 33) {
209                            if (FirstMWHoldTF) {
210                                FirstMWHoldTime = HistoryRecTime;
211                                FirstMWHoldTF = false;
212                            }
213                        } else {
214                            FirstMWHoldTime = HistoryRecTime;
215                            FirstMWHoldTF = true;
216                        }
217                    }
218                }
219                XInc++;
220            }
221
222            /* now check for current shear scene and previous MW ON */
223            if (LastRule8Flag == 32) {
224                EXIT_Routine = true;
225                /* previous MW ON... set to HOLD */
226                History.IRCurrentRecord.rule8 = 33; /* HOLD */
227                if (Env.DEBUG == 100) {
228                    System.out.printf("previous MW on... now MW HOLD: nonEIRscenecounter=%d\n",
229                            NonEIRSceneCounter);
230                }
231                IntensityEstimateValueReturn = LastValidMWCIValue;
232                MWScoreApplicationFlag = 1;
233                /*
234                 * check to see if current record is shear scene and had two
235                 * shears before it... if so... turn MW OFF
236                 */
237                if ((NonEIRSceneCounter >= 2) && (CurCloudScene == 4)) {
238                    if (Env.DEBUG == 100) {
239                        System.out.printf("Third consecutive SHEAR scene... turning OFF!!!\n");
240                    }
241                    /*
242                     * >= three consecutive non-EIR eye scenes and would have
243                     * been MW HOLD... turn OFF
244                     */
245                    IntensityEstimateValueReturn = InputIntensityEstStore;
246                    History.IRCurrentRecord.rule8 = 34; /* OFF */
247                    MWScoreApplicationFlag = 0;
248                }
249                /* end possible remove section */
250            } else if (LastRule8Flag == 33) {
251                EXIT_Routine = true;
252                if (Env.DEBUG == 100) {
253                    System.out.printf("current MW HOLD... nonEIRscene=%d  cloudscene=%d\n",
254                            NonEIRSceneCounter, CurCloudScene);
255                }
256                /*
257                 * was in MW HOLD... now checking for shear scenes to turn MW
258                 * OFF
259                 */
260                if ((NonEIRSceneCounter >= 2) && (CurCloudScene == 4)) {
261                    if (Env.DEBUG == 100) {
262                        System.out
263                                .printf("current MW HOLD... third consecutive SHEAR scene... turning OFF!!!\n");
264                    }
265                    /*
266                     * >= three consecutive non-EIR eye scenes and was in MW
267                     * HOLD... turn OFF
268                     */
269                    IntensityEstimateValueReturn = InputIntensityEstStore;
270                    History.IRCurrentRecord.rule8 = 34; /* OFF */
271                    MWScoreApplicationFlag = 0;
272                } else {
273                    History.IRCurrentRecord.rule8 = 33; /* HOLD */
274                    IntensityEstimateValueReturn = LastValidMWCIValue;
275                    MWScoreApplicationFlag = 1;
276                    if (Env.DEBUG == 100) {
277                        System.out.printf("current MW HOLD... keep HOLD\n");
278                    }
279                    if ((CurrentTime - FirstMWHoldTime) >= .25) {
280                        /* in MW HOLD >= 6 hours... turn OFF */
281                        IntensityEstimateValueReturn = InputIntensityEstStore;
282                        History.IRCurrentRecord.rule8 = 34; /* OFF */
283                        MWScoreApplicationFlag = 0;
284
285                        CommentString = String.format("MW OFF HOLD >=6hrs old", CommentString);
286                        History.IRCurrentRecord.comment = CommentString;
287                        if (Env.DEBUG == 100) {
288                            System.out
289                                    .printf("current MW HOLD... in HOLD >= 6 hours... turning OFF!!!\n");
290                        }
291                    }
292                }
293            } else if (LastRule8Flag == 34) {
294                MWScoreApplicationFlag = 0;
295                IntensityEstimateValueReturn = InputIntensityEstStore;
296                EXIT_Routine = true;
297                if (Env.DEBUG == 100) {
298                    System.out.printf("last record MW OFF.... returning\n");
299                }
300            } else {
301                /* do nothing */
302                MWScoreApplicationFlag = 0;
303                IntensityEstimateValueReturn = InputIntensityEstStore;
304                EXIT_Routine = true;
305                if (Env.DEBUG == 100) {
306                    System.out.printf("old MW record.... returning\n");
307                }
308            }
309            /* END NEW LOGIC */
310        } else {
311            /* check for three consecutive eyes */
312            int EyeSceneCounter = 0;
313            LastCIValue = 1.0;
314            XInc = 0;
315            while (XInc < NumRecsHistory) {
316                RecDate = History.HistoryFile[XInc].date;
317                RecTime = History.HistoryFile[XInc].time;
318                HistoryRecTime = Functions.calctime(RecDate, RecTime);
319
320                RecLand = History.HistoryFile[XInc].land;
321                Traw = History.HistoryFile[XInc].Traw;
322                LandCheckTF = true;
323                if (((LandFlagTF) && (RecLand == 1)) || (Traw < 1.0)) {
324                    LandCheckTF = false;
325                }
326
327                if (LandCheckTF) {
328                    if (HistoryRecTime < CurrentTime) {
329                        /*
330                         * added time check for LastCIValue to make sure MW eye
331                         * adjustment doesn't reset at a time after the MW time
332                         * once the CI falls below 4.0 (after the MW adjustment
333                         * had been on but had turned off for whatever
334                         * reason)... the CI# must be below 4.0 when the current
335                         * MW score is implemented at or before the current MW
336                         * time for this to be reset
337                         */
338                        if (HistoryRecTime < CurrentMWTime) {
339                            LastCIValue = History.HistoryFile[XInc].CI;
340                        }
341                        LastEyeSceneValue = History.HistoryFile[XInc].eyescene;
342                        LastRule8Flag = History.HistoryFile[XInc].rule8;
343                        EyeSceneCounter = (LastEyeSceneValue <= 2) ? EyeSceneCounter + 1 : 0;
344                        if (Env.DEBUG == 100) {
345                            System.out.printf("time=%f eyescenecounter=%d LastCIValue=%f\n",
346                                    HistoryRecTime, EyeSceneCounter, LastCIValue);
347                        }
348                        if (EyeSceneCounter >= 3)
349                            MWAdjOFFTF = true;
350                    }
351                }
352                /*
353                 * turn back on MW adjustment if CI falls below 4.0. had to
354                 * modifiy because if MW adjustment goes on after "reset" and
355                 * CI#s are modified when a new MW adjustment goes on, the eye
356                 * check from a previous period will still tell ADT to turn off
357                 * MW adjustment even though it was reset. Checking for Rule8
358                 * value of 31 will indicate that the MW was turned back on
359                 * correctly and to start new checking for eyes after that
360                 * point.
361                 */
362                if ((LastCIValue < 4.0 || LastRule8Flag == 31)) {
363                    if (Env.DEBUG == 100) {
364                        System.out.printf(
365                                "CI below 4.0... resetting MW adjustment!!!  LastCIValue=%f\n",
366                                LastCIValue);
367                    }
368                    MWAdjOFFTF = false;
369                }
370                XInc++;
371            }
372            if (Env.DEBUG == 100) {
373                System.out.printf("current eye scene=%d\n", CurEyeScene);
374            }
375            if (CurEyeScene <= 2) {
376                EyeSceneCounter++;
377                if (Env.DEBUG == 100) {
378                    System.out.printf("time=%f eyescenecounter=%d\n", CurrentTime, EyeSceneCounter);
379                }
380                if (EyeSceneCounter >= 3)
381                    MWAdjOFFTF = true;
382            }
383            if (MWAdjOFFTF) {
384                /* found three consecutive eyes... return immediately */
385                if (Env.DEBUG == 100) {
386                    System.out.printf("FOUND THREE EYES... EXITING!!!  LastCIValue=%f\n",
387                            LastCIValue);
388                }
389                if ((LastRule8Flag >= 30) && (LastRule8Flag <= 33)) {
390                    History.IRCurrentRecord.rule8 = 34; /* OFF */
391                }
392                IntensityEstimateValueReturn = InputIntensityEstStore;
393                EXIT_Routine = true;
394                MWScoreApplicationFlag = 0;
395            }
396        }
397        if (Env.DEBUG == 100) {
398            System.out.printf("1 EXIT_Routine=%b\n", EXIT_Routine);
399        }
400
401        /*
402         * check for THREE hours of land interaction... turn MW off this occurs
403         * changed from six hours on 7 June 2013 per Velden recommendation
404         */
405        LastRule8Flag = 0;
406        if (Env.DEBUG == 100) {
407            System.out.printf("CHECKING LAND INTERACTION\n");
408        }
409        double LastNonLandTime = 0.0;
410        boolean MWLandCheckTF = false;
411        XInc = 0;
412        while (XInc < NumRecsHistory) {
413            RecDate = History.HistoryFile[XInc].date;
414            RecTime = History.HistoryFile[XInc].time;
415            HistoryRecTime = Functions.calctime(RecDate, RecTime);
416
417            RecLand = History.HistoryFile[XInc].land;
418            Traw = History.HistoryFile[XInc].Traw;
419            LandCheckTF = true;
420            if (((LandFlagTF) && (RecLand == 1)) || (Traw < 1.0)) {
421                LandCheckTF = false;
422            }
423
424            if (!LandCheckTF) { /* TC over land */
425                if (Env.DEBUG == 100) {
426                    System.out.printf("OVER LAND SINCE %f FOR %f DAYS \n", LastNonLandTime,
427                            (HistoryRecTime - LastNonLandTime));
428                }
429                if ((HistoryRecTime - LastNonLandTime) >= 0.125) {
430                    MWLandCheckTF = true;
431                    MWAdjOFFTF = false;
432                    if (Env.DEBUG == 100) {
433                        System.out.printf("RESETTING MWAdjOFFTF Flag\n");
434                    }
435                }
436            } else {
437                LastRule8Flag = History.HistoryFile[XInc].rule8;
438                LastNonLandTime = HistoryRecTime;
439                MWLandCheckTF = false;
440            }
441            if (Env.DEBUG == 100) {
442                System.out.printf("MWLandCheckTF=%b\n", MWLandCheckTF);
443            }
444            XInc++;
445        }
446        if (Env.DEBUG == 100) {
447            System.out.printf("2 EXIT_Routine=%b\n", EXIT_Routine);
448        }
449        if (Env.DEBUG == 100) {
450            System.out.printf("MWLandCheckTF=%b  LandFlag=%d  LastRule8Flag=%d\n", MWLandCheckTF,
451                    CurLand, LastRule8Flag);
452        }
453        if ((MWLandCheckTF) && (CurLand == 2)) {
454            if ((LastRule8Flag >= 30) && (LastRule8Flag <= 33)) {
455                if (Env.DEBUG == 100) {
456                    System.out
457                            .printf("OVER LAND FOR MORE THAN 3 HOURS... TURNING OFF ME AND EXITING!!!\n");
458                }
459                History.IRCurrentRecord.rule8 = 34; /* OFF */
460                IntensityEstimateValueReturn = InputIntensityEstStore;
461                EXIT_Routine = true;
462                MWScoreApplicationFlag = 0;
463            }
464        }
465        /* End Land Check */
466
467        if (Env.DEBUG == 100) {
468            System.out.printf("3 EXIT_Routine=%b\n", EXIT_Routine);
469        }
470
471        if (!EXIT_Routine) {
472            /* check previous history record values */
473            XInc = 0;
474            while (XInc < NumRecsHistory) {
475                RecDate = History.HistoryFile[XInc].date;
476                RecTime = History.HistoryFile[XInc].time;
477                HistoryRecTime = Functions.calctime(RecDate, RecTime);
478
479                if (HistoryRecTime < CurrentMWTime) {
480
481                    RecLand = History.HistoryFile[XInc].land;
482                    Traw = History.HistoryFile[XInc].Traw;
483                    LandCheckTF = true;
484                    if (((LandFlagTF) && (RecLand == 1)) || (Traw < 1.0)) {
485                        LandCheckTF = false;
486                    }
487
488                    if (LandCheckTF) { /* TC over land */
489                        /*
490                         * check to see if current MW adjustment was on previous
491                         * * to current MW entry date/time
492                         */
493                        LastMWScoreValue = History.HistoryFile[XInc].mwscore;
494                        LastValidMWCIValue = History.HistoryFile[XInc].CI;
495                        Rule8Flag = History.HistoryFile[XInc].rule8;
496                        if (Env.DEBUG == 100) {
497                            System.out
498                                    .printf("Time=%f  previous MW record:  lastscore=%f lasteye=%d  rule8=%d\n",
499                                            HistoryRecTime, LastMWScoreValue, LastEyeSceneValue,
500                                            Rule8Flag);
501                        }
502
503                        MWAdjCurrentONTF = false;
504                        if ((Rule8Flag >= 31) && (Rule8Flag <= 33)) {
505                            MWAdjCurrentONTF = true;
506                            if ((Rule8Flag >= 31) && (First31Record)) {
507                                if (Env.DEBUG == 100) {
508                                    System.out.printf("found 31 in previous record search!\n");
509                                }
510                                FirstHistoryCIValue = History.HistoryFile[XInc].CI;
511                                FirstMWadjTime = HistoryRecTime;
512                                FirstMWadjTimePlus12hr = FirstMWadjTime + 0.5;
513                                First31Record = false;
514                            }
515                        } else {
516                            First31Record = true; /*
517                                                   * reset mark for MW score
518                                                   * merging
519                                                   */
520                        }
521                        if ((Rule8Flag >= 30) && (Rule8Flag <= 32)) {
522                            LastValidMWCIValue = History.HistoryFile[XInc].CI;
523                            LastValidMWadjTime = HistoryRecTime;
524                            LastValidMWadjTimePlus6 = HistoryRecTime + 0.25;
525                        }
526                    }
527                }
528                XInc++;
529            }
530
531            LastRule8Flag = Rule8Flag;
532            PreviousMWScoreValue = LastMWScoreValue;
533
534            if (Env.DEBUG == 100) {
535                System.out.printf("FirstHistoryCIValue=%f  First31Record=%b\n",
536                        FirstHistoryCIValue, First31Record);
537                System.out.printf("FirstMWadjTime=%f FirstMWadjTimePlus12hr=%f\n", FirstMWadjTime,
538                        FirstMWadjTimePlus12hr);
539            }
540            /* BEFORE MW TIME */
541            if (Env.DEBUG == 100) {
542                System.out.printf("MWAdjCurrentONTF=%b MWScoreApplicationFlag=%d\n",
543                        MWAdjCurrentONTF, MWScoreApplicationFlag);
544            }
545            if ((!MWAdjCurrentONTF) && (MWScoreApplicationFlag == 0)) {
546                /* MW was OFF and is still OFF */
547                if (Env.DEBUG == 100) {
548                    System.out.printf("OFF - OFF\n");
549                }
550                /*
551                 * I still need to put the mw value into all records after the
552                 * mw input time
553                 */
554                XInc = 0;
555                while (XInc < NumRecsHistory) {
556                    RecDate = History.HistoryFile[XInc].date;
557                    RecTime = History.HistoryFile[XInc].time;
558                    HistoryRecTime = Functions.calctime(RecDate, RecTime);
559                    if (HistoryRecTime >= CurrentMWTime) {
560                        /* determine if there is a record after MW time */
561                        NORecordAfterMWTimeTF = false;
562                    }
563                    XInc++;
564                }
565            } else if ((!MWAdjCurrentONTF) && (MWScoreApplicationFlag == 1)) {
566                /* MW was OFF but is now ON */
567                if (Env.DEBUG == 100) {
568                    System.out.printf("OFF - now ON\n");
569                }
570
571                XInc = 0;
572                while (XInc < NumRecsHistory) {
573                    RecDate = History.HistoryFile[XInc].date;
574                    RecTime = History.HistoryFile[XInc].time;
575                    HistoryRecTime = Functions.calctime(RecDate, RecTime);
576                    if (Env.DEBUG == 100) {
577                        System.out.printf("HistoryRecTime=%f CurrentMWTime=%f\n", HistoryRecTime,
578                                CurrentMWTime);
579                    }
580                    if (HistoryRecTime < CurrentMWTime) {
581                        /* merge backwards 12 hours previous to MW time */
582                        RecLand = History.HistoryFile[XInc].land;
583                        Traw = History.HistoryFile[XInc].Traw;
584                        LandCheckTF = true;
585                        if (((LandFlagTF) && (RecLand == 1)) || (Traw < 1.0)) {
586                            LandCheckTF = false;
587                        }
588
589                        if (LandCheckTF) {
590                            HistoryCIValue = History.HistoryFile[XInc].CI;
591                            Rule8Flag = History.HistoryFile[XInc].rule8;
592                            if (HistoryRecTime >= CurrentMWTimeMinus12hr) {
593                                if (!FoundHistoryRecPrev12hrTF) {
594                                    FoundHistoryRecPrev12hrTF = true;
595                                    HistoryCIValueMinus12hr = HistoryCIValue;
596                                    HistoryRecTimeMinus12hr = HistoryRecTime;
597                                }
598                                /*
599                                 * interpolate value between current value and
600                                 * 12 hour previous value
601                                 */
602                                if (Rule8Flag != 30) {
603                                    CommentString = "";
604                                    CommentString = String.format("MWinit1=%3.1f/%3.1f/%3.1f",
605                                            History.HistoryFile[XInc].Traw,
606                                            History.HistoryFile[XInc].Tfinal,
607                                            History.HistoryFile[XInc].CI);
608                                    InterpTimePart = (HistoryRecTime - HistoryRecTimeMinus12hr)
609                                            / (CurrentMWTime - HistoryRecTimeMinus12hr);
610                                    IntensityCIMergeValue = Math.max(IntensityEstimateValueReturn,
611                                            HistoryCIValueMinus12hr);
612                                    NewIntensityEstScore = ((IntensityCIMergeValue - HistoryCIValueMinus12hr) * InterpTimePart)
613                                            + HistoryCIValueMinus12hr;
614                                    History.HistoryFile[XInc].Traw = NewIntensityEstScore;
615                                    History.HistoryFile[XInc].Tfinal = NewIntensityEstScore;
616                                    History.HistoryFile[XInc].CI = NewIntensityEstScore;
617                                    History.HistoryFile[XInc].rule8 = 30;
618                                    History.HistoryFile[XInc].comment = CommentString;
619                                    if (Env.DEBUG == 100) {
620                                        System.out.printf(
621                                                "rule8=30 Time=%f NewIntensityEstScore=%f\n",
622                                                HistoryRecTime, NewIntensityEstScore);
623                                    }
624                                } else {
625                                    if (Env.DEBUG == 100) {
626                                        System.out.printf("already merged backwards... skipping\n");
627                                    }
628                                }
629                            }
630                        }
631                        LastValidMWCIValue = History.HistoryFile[XInc].CI;
632                        LastRule8Flag = 30;
633                    } else {
634                        /* determine if there is a record after MW time */
635                        NORecordAfterMWTimeTF = false;
636                    }
637                    XInc++;
638                }
639            } else if ((MWAdjCurrentONTF) && (MWScoreApplicationFlag == 1)) {
640                /* MW was ON and is still ON */
641                if (Env.DEBUG == 100) {
642                    System.out.printf("ON - still ON\n");
643                }
644
645                XInc = 0;
646                while (XInc < NumRecsHistory) {
647                    RecDate = History.HistoryFile[XInc].date;
648                    RecTime = History.HistoryFile[XInc].time;
649                    HistoryRecTime = Functions.calctime(RecDate, RecTime);
650                    if (Env.DEBUG == 100) {
651                        System.out.printf(
652                                "PreviousMWScoreValue=%f IntensityEstimateValue_Ret=%f\n",
653                                PreviousMWScoreValue, IntensityEstimateValueReturn);
654                    }
655                    if (HistoryRecTime < CurrentMWTime) {
656                        if (Env.DEBUG == 100) {
657                            System.out
658                                    .printf("HistoryRecTime=%f CurrentMWTime=%f : LastMWScoreValue=%f *IntensityEstimateValue_Ret=%f\n",
659                                            HistoryRecTime, CurrentMWTime, LastMWScoreValue,
660                                            IntensityEstimateValueReturn);
661                        }
662                        /* merge backwards from 4.3/4.8 to 5.0, if necessary */
663                        if ((LastMWScoreValue < MWScoreThresholdValue2)
664                                && (IntensityEstimateValueReturn >= MWIntensityTnoValue2)) {
665                            /* merge backwards 12 hours previous to MW time */
666                            RecLand = History.HistoryFile[XInc].land;
667                            Traw = History.HistoryFile[XInc].Traw;
668                            LandCheckTF = true;
669                            if (((LandFlagTF) && (RecLand == 1)) || (Traw < 1.0)) {
670                                LandCheckTF = false;
671                            }
672                            if (LandCheckTF) {
673                                HistoryCIValue = History.HistoryFile[XInc].CI;
674                                Rule8Flag = History.HistoryFile[XInc].rule8;
675                                CommentString = History.HistoryFile[XInc].comment;
676                                if (Env.DEBUG == 100) {
677                                    System.out.printf("CommentString=%s**%d\n", CommentString,
678                                            CommentString.length());
679                                }
680                                if (HistoryRecTime >= CurrentMWTimeMinus12hr) {
681                                    if (!FoundHistoryRecPrev12hrTF) {
682                                        FoundHistoryRecPrev12hrTF = true;
683                                        HistoryCIValueMinus12hr = HistoryCIValue;
684                                        HistoryRecTimeMinus12hr = HistoryRecTime;
685                                        if (Env.DEBUG == 100) {
686                                            System.out.printf("historyminus12 CI=%f time=%f\n",
687                                                    HistoryCIValueMinus12hr,
688                                                    HistoryRecTimeMinus12hr);
689                                        }
690                                    }
691                                    if (CommentString.length() == 0) {
692                                        /*
693                                         * interpolate value between current
694                                         * value and 12 hour previous value
695                                         */
696                                        CommentString = "";
697                                        CommentString = String.format("MWinit2=%3.1f/%3.1f/%3.1f",
698                                                History.HistoryFile[XInc].Traw,
699                                                History.HistoryFile[XInc].Tfinal,
700                                                History.HistoryFile[XInc].CI);
701                                        History.HistoryFile[XInc].comment = CommentString;
702                                    }
703                                    InterpTimePart = (HistoryRecTime - HistoryRecTimeMinus12hr)
704                                            / (CurrentMWTime - HistoryRecTimeMinus12hr);
705                                    NewIntensityEstScore = ((IntensityEstimateValueReturn - HistoryCIValueMinus12hr) * InterpTimePart)
706                                            + HistoryCIValueMinus12hr;
707                                    History.HistoryFile[XInc].Traw = NewIntensityEstScore;
708                                    History.HistoryFile[XInc].Tfinal = NewIntensityEstScore;
709                                    History.HistoryFile[XInc].CI = NewIntensityEstScore;
710                                    Rule8Flag = History.HistoryFile[XInc].rule8;
711                                    if (Rule8Flag >= 32) {
712                                        History.HistoryFile[XInc].rule8 = 32;
713                                    }
714                                    if (Env.DEBUG == 100) {
715                                        System.out.printf(
716                                                "rule8=32 Time=%f NewIntensityEstScore=%f\n",
717                                                HistoryRecTime, NewIntensityEstScore);
718                                    }
719                                }
720                            }
721                        } else {
722                            /*
723                             * merge backwards from 4.3 to 4.8 AFTER HOLD when
724                             * MW > threshold, if necessary
725                             */
726                            if ((LastMWScoreValue < MWScoreThresholdValue1)
727                                    && (MWScoreInput >= MWScoreThresholdValue1)) {
728                                CommentString = History.HistoryFile[XInc].comment;
729                                Rule8Flag = History.HistoryFile[XInc].rule8;
730                                if (Env.DEBUG == 100) {
731                                    System.out
732                                            .printf("merge backwards - WAS IN HOLD, now back on\n");
733                                }
734                                /* merge backwards 12 hours previous to MW time */
735                                RecLand = History.HistoryFile[XInc].land;
736                                Traw = History.HistoryFile[XInc].Traw;
737                                LandCheckTF = true;
738                                if (((LandFlagTF) && (RecLand == 1)) || (Traw < 1.0)) {
739                                    LandCheckTF = false;
740                                }
741                                if (LandCheckTF) {
742                                    if (HistoryRecTime >= FirstMWadjTime) {
743                                        if (Rule8Flag == 33) {
744                                            /*
745                                             * interpolate value between current
746                                             * value and 12 hour previous value
747                                             */
748                                            if (CommentString.length() == 0) {
749                                                CommentString = "";
750                                                CommentString = String.format(
751                                                        "MWinit3=%3.1f/%3.1f/%3.1f",
752                                                        History.HistoryFile[XInc].Traw,
753                                                        History.HistoryFile[XInc].Tfinal,
754                                                        History.HistoryFile[XInc].CI);
755                                                History.HistoryFile[XInc].comment = CommentString;
756                                            }
757                                            InterpTimePart = (HistoryRecTime - FirstMWadjTime)
758                                                    / (FirstMWadjTimePlus12hr - FirstMWadjTime);
759                                            InterpTimePart = Math.min(1.0, InterpTimePart);
760                                            NewIntensityEstScore = ((MWIntensityTnoValue1a - FirstHistoryCIValue) * InterpTimePart)
761                                                    + FirstHistoryCIValue;
762                                            History.HistoryFile[XInc].Traw = NewIntensityEstScore;
763                                            History.HistoryFile[XInc].Tfinal = NewIntensityEstScore;
764                                            History.HistoryFile[XInc].CI = NewIntensityEstScore;
765                                            Rule8Flag = History.HistoryFile[XInc].rule8;
766                                            if (Rule8Flag >= 32) {
767                                                History.HistoryFile[XInc].rule8 = 32;
768                                            }
769                                            if (Env.DEBUG == 100) {
770                                                System.out
771                                                        .printf("changing to rule8=32 Time=%f NewIntensityEstScore=%f\n",
772                                                                HistoryRecTime,
773                                                                NewIntensityEstScore);
774                                            }
775                                        } else {
776                                            if (Env.DEBUG == 100) {
777                                                System.out
778                                                        .printf("ALREADY INTERPOLATED BACKWARDS... SKIPPING\n");
779                                            }
780                                        }
781                                    }
782                                }
783                            }
784                        }
785                        LastValidMWCIValue = History.HistoryFile[XInc].CI;
786                    } else {
787                        /* determine if there is a record after MW time */
788                        NORecordAfterMWTimeTF = false;
789                    }
790                    XInc++;
791                }
792            } else if ((MWAdjCurrentONTF) && (MWScoreApplicationFlag == 0)) {
793                /* MW was ON and is now OFF */
794                if (Env.DEBUG == 100) {
795                    System.out.printf("ON - now OFF\n");
796                }
797                XInc = 0;
798                while (XInc < NumRecsHistory) {
799                    RecDate = History.HistoryFile[XInc].date;
800                    RecTime = History.HistoryFile[XInc].time;
801                    HistoryRecTime = Functions.calctime(RecDate, RecTime);
802                    if (HistoryRecTime >= CurrentMWTime) {
803                        /* determine if there is a record after MW time */
804                        NORecordAfterMWTimeTF = false;
805                    }
806                    XInc++;
807                }
808            } else {
809                /* nothing */
810                if (Env.DEBUG == 100) {
811                    System.out.printf("nothing\n");
812                }
813            }
814
815            if (Env.DEBUG == 100) {
816                System.out.printf("LastValidMWCIValue=%f\n", LastValidMWCIValue);
817            }
818
819            /* AFTER MW TIME */
820            if (Env.DEBUG == 100) {
821                System.out.printf("NORecordAfterMWTimeTF=%b\n", NORecordAfterMWTimeTF);
822            }
823            if (!NORecordAfterMWTimeTF) {
824                /*
825                 * first record after MW time is not current record... do
826                 * necessary processing to records in between MW time and up to
827                 * current record
828                 */
829                if (Env.DEBUG == 100) {
830                    System.out.printf("CURRENT RECORD IS NOT LAST\n");
831                }
832                XInc = 0;
833                while (XInc < NumRecsHistory) {
834                    /* handle all records after input MW time */
835                    RecDate = History.HistoryFile[XInc].date;
836                    RecTime = History.HistoryFile[XInc].time;
837                    HistoryRecTime = Functions.calctime(RecDate, RecTime);
838                    if (Env.DEBUG == 100) {
839                        System.out.printf("HistoryRecTime=%f CurrentMWTime=%f\n", HistoryRecTime,
840                                CurrentMWTime);
841                    }
842                    Rule8Flag = History.HistoryFile[XInc].rule8;
843                    if (Rule8Flag == 34)
844                        LastRule8Flag = 34;
845                    RecLand = History.HistoryFile[XInc].land;
846                    Traw = History.HistoryFile[XInc].Traw;
847                    LandCheckTF = true;
848                    if (((LandFlagTF) && (RecLand == 1)) || (Traw < 1.0)) {
849                        LandCheckTF = false;
850                    }
851                    if (LandCheckTF) {
852                        if (HistoryRecTime >= CurrentMWTime) {
853                            if (Env.DEBUG == 100) {
854                                System.out.printf("LastRule8Flag=%d\n", LastRule8Flag);
855                            }
856                            switch (LastRule8Flag) {
857                                case 30:
858                                    if (Env.DEBUG == 100) {
859                                        System.out.printf("rule8 was 30 current is 31\n");
860                                    }
861                                    History.HistoryFile[XInc].rule8 = 32;
862                                    /*
863                                     * should set value right after MW time to
864                                     * 4.3/4.8
865                                     */
866                                    LastValidMWCIValue = Math.max(IntensityEstimateValueReturn,
867                                            LastValidMWCIValue);
868                                    History.HistoryFile[XInc].Traw = LastValidMWCIValue;
869                                    History.HistoryFile[XInc].Tfinal = LastValidMWCIValue;
870                                    History.HistoryFile[XInc].CI = LastValidMWCIValue;
871                                    LastValidMWadjTime = HistoryRecTime;
872                                    LastValidMWadjTimePlus6 = LastValidMWadjTime + 0.25;
873                                    FirstHistoryCIValue = History.HistoryFile[XInc].CI;
874                                    FirstMWadjTime = HistoryRecTime;
875                                    FirstMWadjTimePlus12hr = HistoryRecTime + 0.5;
876                                    break;
877                                case 31:
878                                    History.HistoryFile[XInc].rule8 = 32;
879                                    if (Env.DEBUG == 100) {
880                                        System.out.printf("rule8 was 31 current is 32\n");
881                                    }
882                                    if (HistoryRecTime <= FirstMWadjTimePlus12hr) {
883                                        /*
884                                         * merge forward to 4.8 for first 12
885                                         * hours
886                                         */
887                                        InterpTimePart = 1.0 - ((FirstMWadjTimePlus12hr - HistoryRecTime) / (FirstMWadjTimePlus12hr - FirstMWadjTime));
888                                        IntensityCIMergeValue = MWIntensityTnoValue1a;
889                                        NewIntensityEstScore = ((IntensityCIMergeValue - FirstHistoryCIValue) * InterpTimePart)
890                                                + FirstHistoryCIValue;
891                                        if (Env.DEBUG == 100) {
892                                            System.out
893                                                    .printf("InterpTimePart=%f IntensityCIMergeValue=%f NewIntensityEstScore=%f\n",
894                                                            InterpTimePart, IntensityCIMergeValue,
895                                                            NewIntensityEstScore);
896                                        }
897                                        History.HistoryFile[XInc].Traw = NewIntensityEstScore;
898                                        History.HistoryFile[XInc].Tfinal = NewIntensityEstScore;
899                                        History.HistoryFile[XInc].CI = NewIntensityEstScore;
900                                    }
901                                    LastValidMWadjTime = HistoryRecTime;
902                                    LastValidMWadjTimePlus6 = LastValidMWadjTime + 0.25;
903                                    break;
904                                case 32:
905                                    History.HistoryFile[XInc].rule8 = 32;
906                                    if (Env.DEBUG == 100) {
907                                        System.out.printf("rule8 was 32 current is 32\n");
908                                    }
909                                    if (HistoryRecTime <= FirstMWadjTimePlus12hr) {
910                                        if (MWScoreInput >= MWScoreThresholdValue1) {
911                                            if (Env.DEBUG == 100) {
912                                                System.out.printf("merge to 4.8\n");
913                                            }
914                                            /*
915                                             * merge forward to 4.8, if
916                                             * necessary
917                                             *//*
918                                                * check to make sure MW score is
919                                                * not 5.0
920                                                */
921                                            if (MWScoreInput < MWScoreThresholdValue2) {
922                                                InterpTimePart = 1.0 - ((FirstMWadjTimePlus12hr - HistoryRecTime) / (FirstMWadjTimePlus12hr - FirstMWadjTime));
923                                                IntensityCIMergeValue = MWIntensityTnoValue1a;
924                                                NewIntensityEstScore = ((IntensityCIMergeValue - FirstHistoryCIValue) * InterpTimePart)
925                                                        + FirstHistoryCIValue;
926                                                if (Env.DEBUG == 100) {
927                                                    System.out
928                                                            .printf("InterpTimePart=%f IntensityCIMergeValue=%f NewIntensityEstScore=%f\n",
929                                                                    InterpTimePart,
930                                                                    IntensityCIMergeValue,
931                                                                    NewIntensityEstScore);
932                                                }
933                                            } else {
934                                                if (Env.DEBUG == 100) {
935                                                    System.out.printf("holding at 5.0\n");
936                                                }
937                                                NewIntensityEstScore = MWIntensityTnoValue2;
938                                            }
939                                            History.HistoryFile[XInc].Traw = NewIntensityEstScore;
940                                            History.HistoryFile[XInc].Tfinal = NewIntensityEstScore;
941                                            History.HistoryFile[XInc].CI = NewIntensityEstScore;
942                                            LastValidMWCIValue = History.HistoryFile[XInc].CI;
943                                            LastValidMWadjTime = HistoryRecTime;
944                                            LastValidMWadjTimePlus6 = LastValidMWadjTime + 0.25;
945                                        } else {
946                                            if (Env.DEBUG == 100) {
947                                                System.out
948                                                        .printf("MW value less than 20... START hold\n");
949                                            }
950                                            History.HistoryFile[XInc].rule8 = 33;
951                                            IntensityEstimateValueReturn = LastValidMWCIValue;
952                                            History.HistoryFile[XInc].Traw = LastValidMWCIValue;
953                                            History.HistoryFile[XInc].Tfinal = LastValidMWCIValue;
954                                            History.HistoryFile[XInc].CI = LastValidMWCIValue;
955                                        }
956                                    } else {
957                                        if (HistoryRecTime <= CurrentMWTimePlus12hr) {
958                                            if (MWScoreInput >= MWScoreThresholdValue1) {
959                                                if (Env.DEBUG == 100) {
960                                                    System.out
961                                                            .printf("rule8 was 32 keep as 32 (hold at last value of %f)\n",
962                                                                    LastValidMWCIValue);
963                                                }
964                                                History.HistoryFile[XInc].Traw = LastValidMWCIValue;
965                                                History.HistoryFile[XInc].Tfinal = LastValidMWCIValue;
966                                                History.HistoryFile[XInc].CI = LastValidMWCIValue;
967                                                LastValidMWCIValue = History.HistoryFile[XInc].CI;
968                                                LastValidMWadjTime = HistoryRecTime;
969                                                LastValidMWadjTimePlus6 = LastValidMWadjTime + 0.25;
970                                            } else {
971                                                if (Env.DEBUG == 100) {
972                                                    System.out
973                                                            .printf("MW value less than 20... hold\n");
974                                                }
975                                                History.HistoryFile[XInc].rule8 = 33;
976                                                History.HistoryFile[XInc].Traw = LastValidMWCIValue;
977                                                History.HistoryFile[XInc].Tfinal = LastValidMWCIValue;
978                                                History.HistoryFile[XInc].CI = LastValidMWCIValue;
979                                                IntensityEstimateValueReturn = LastValidMWCIValue;
980                                            }
981                                        } else {
982                                            if (Env.DEBUG == 100) {
983                                                System.out
984                                                        .printf("rule8 was 32 greater than 12 hours old... turn off!!!\n");
985                                            }
986                                            History.HistoryFile[XInc].rule8 = 34;
987                                            History.HistoryFile[XInc].Traw = LastValidMWCIValue;
988                                            History.HistoryFile[XInc].Tfinal = LastValidMWCIValue;
989                                            History.HistoryFile[XInc].CI = LastValidMWCIValue;
990                                            MWScoreApplicationFlag = 0;
991                                        }
992                                    }
993                                    break;
994                                case 33:
995                                    if (Env.DEBUG == 100) {
996                                        System.out.printf(
997                                                "HistoryRecTime=%f LastValidMWadjTimeP6=%f\n",
998                                                HistoryRecTime, LastValidMWadjTimePlus6);
999                                    }
1000                                    if (HistoryRecTime <= LastValidMWadjTimePlus6) {
1001                                        if (MWScoreInput >= MWScoreThresholdValue1) {
1002                                            if (Env.DEBUG == 100) {
1003                                                System.out
1004                                                        .printf("rule8 was 33 mwscore>threshold1... turn back ON\n");
1005                                            }
1006                                            History.HistoryFile[XInc].rule8 = 32; /*
1007                                                                                   * Turn
1008                                                                                   * back
1009                                                                                   * ON
1010                                                                                   */
1011                                            LastValidMWadjTime = HistoryRecTime;
1012                                            LastValidMWadjTimePlus6 = LastValidMWadjTime + 0.25;
1013                                        } else {
1014                                            if (Env.DEBUG == 100) {
1015                                                System.out.printf("rule8 was 33 hold at 33\n");
1016                                            }
1017                                            History.HistoryFile[XInc].rule8 = 33; /* hold */
1018                                        }
1019                                        History.HistoryFile[XInc].Traw = LastValidMWCIValue;
1020                                        History.HistoryFile[XInc].Tfinal = LastValidMWCIValue;
1021                                        History.HistoryFile[XInc].CI = LastValidMWCIValue;
1022                                        LastValidMWCIValue = History.HistoryFile[XInc].CI;
1023                                        MWScoreApplicationFlag = 1;
1024                                    } else {
1025                                        if (Env.DEBUG == 100) {
1026                                            System.out
1027                                                    .printf("rule8 was 33 GREATER THAN 6 hours old!   TURNING OFF\n");
1028                                        }
1029                                        History.HistoryFile[XInc].rule8 = 34; /*
1030                                                                               * turn
1031                                                                               * off
1032                                                                               */
1033                                        CommentString = "";
1034                                        CommentString = String.format("MW OFF HOLD >=6hrs old");
1035                                        History.HistoryFile[XInc].comment = CommentString;
1036                                        MWScoreApplicationFlag = 0;
1037                                    }
1038                                    break;
1039                                case 34:
1040                                    /* added */
1041                                    if (MWScoreInput >= MWScoreThresholdValue1) {
1042                                        if (Env.DEBUG == 100) {
1043                                            System.out
1044                                                    .printf("rule8 was 34 mwscore>threshold1... turn back ON\n");
1045                                        }
1046                                        History.HistoryFile[XInc].rule8 = 31;
1047                                        LastValidMWadjTime = HistoryRecTime;
1048                                        LastValidMWadjTimePlus6 = LastValidMWadjTime + 0.25;
1049                                        History.HistoryFile[XInc].Traw = LastValidMWCIValue;
1050                                        History.HistoryFile[XInc].Tfinal = LastValidMWCIValue;
1051                                        History.HistoryFile[XInc].CI = LastValidMWCIValue;
1052                                        LastValidMWCIValue = History.HistoryFile[XInc].CI;
1053                                        MWScoreApplicationFlag = 1;
1054                                        CommentString = "";
1055                                        History.HistoryFile[XInc].comment = CommentString;
1056                                    } else {
1057                                        if (Env.DEBUG == 100) {
1058                                            System.out.printf("rule8 was 34 adjustment is off!\n");
1059                                        }
1060                                        MWScoreApplicationFlag = 0;
1061                                    }
1062                                    /* end added */
1063                                    break;
1064                                default:
1065                                    if (Env.DEBUG == 100) {
1066                                        System.out.printf("DEFAULT : MW OFF at time %f\n",
1067                                                HistoryRecTime);
1068                                    }
1069                            }
1070                            System.out.printf("resetting MWdate/time for record XINC=%d\n", XInc);
1071                            History.HistoryFile[XInc].mwscore = MWScoreInput;
1072                            History.HistoryFile[XInc].mwdate = Env.MWJulianDate;
1073                            History.HistoryFile[XInc].mwtime = Env.MWHHMMSSTime;
1074                        }
1075                        LastRule8Flag = History.HistoryFile[XInc].rule8;
1076                        PreviousMWScoreValue = History.HistoryFile[XInc].mwscore;
1077                    }
1078                    XInc++;
1079                }
1080                /* deal with last/current record */
1081            }
1082
1083            /* deal with last/current record */
1084            if (Env.DEBUG == 100) {
1085                System.out.printf("CURRENT RECORD IS LAST\n");
1086            }
1087            /*
1088             * first record after MW time is current record... mark it with
1089             * appropriate Rule 8 value
1090             */
1091            if (Env.DEBUG == 100) {
1092                System.out.printf("LastRule8Flag=%d\n", LastRule8Flag);
1093            }
1094            switch (LastRule8Flag) {
1095                case 30:
1096                    History.IRCurrentRecord.rule8 = 31;
1097                    if (Env.DEBUG == 100) {
1098                        System.out.printf("rule8 was 30 current is 31\n");
1099                        System.out.printf("LastValidMWCIValue=%f\n", LastValidMWCIValue);
1100                    }
1101                    IntensityEstimateValueReturn = Math.max(LastValidMWCIValue,
1102                            MWIntensityTnoValue1);
1103                    MWScoreApplicationFlag = 1;
1104                    break;
1105                case 31:
1106                    History.IRCurrentRecord.rule8 = 32;
1107                    if (Env.DEBUG == 100) {
1108                        System.out.printf("rule8 was 31 current is 32\n");
1109                        System.out.printf("LastValidMWCIValue=%f\n", LastValidMWCIValue);
1110                    }
1111                    IntensityEstimateValueReturn = Math.max(LastValidMWCIValue,
1112                            MWIntensityTnoValue1);
1113                    MWScoreApplicationFlag = 1;
1114                    break;
1115                case 32:
1116                    History.IRCurrentRecord.rule8 = 32;
1117                    if (Env.DEBUG == 100) {
1118                        System.out.printf("rule8 was 32 current is 32\n");
1119                    }
1120                    MWScoreApplicationFlag = 1;
1121                    /* check for change in MW value from previous record */
1122                    double MWScoreDiffValue = Math.abs(PreviousMWScoreValue - MWScoreInput);
1123                    if (Env.DEBUG == 100) {
1124                        System.out.printf("previousMWvalue=%f  currentMWvalue=%f  diff=%f\n",
1125                                PreviousMWScoreValue, MWScoreInput, MWScoreDiffValue);
1126                    }
1127                    if (MWScoreInput >= MWScoreThresholdValue1) {
1128                        if (Env.DEBUG == 100) {
1129                            System.out.printf("currentTime=%f  MWTimeplus12=%f\n", CurrentTime,
1130                                    FirstMWadjTimePlus12hr);
1131                        }
1132                        if (CurrentTime <= FirstMWadjTimePlus12hr) {
1133                            if (MWScoreInput < MWScoreThresholdValue2) {
1134                                if (Env.DEBUG == 100) {
1135                                    System.out.printf("merging to 4.8\n");
1136                                }
1137                                InterpTimePart = 1.0 - ((FirstMWadjTimePlus12hr - CurrentTime) / (FirstMWadjTimePlus12hr - FirstMWadjTime));
1138                                IntensityCIMergeValue = MWIntensityTnoValue1a;
1139                                NewIntensityEstScore = ((IntensityCIMergeValue - FirstHistoryCIValue) * InterpTimePart)
1140                                        + FirstHistoryCIValue;
1141                                if (Env.DEBUG == 100) {
1142                                    System.out
1143                                            .printf("InterpTimePart=%f IntensityCIMergeValue=%f NewIntensityEstScore=%f\n",
1144                                                    InterpTimePart, IntensityCIMergeValue,
1145                                                    NewIntensityEstScore);
1146                                }
1147                            } else {
1148                                if (Env.DEBUG == 100) {
1149                                    System.out.printf("holding at 5.0\n");
1150                                }
1151                                NewIntensityEstScore = MWIntensityTnoValue2;
1152                            }
1153                            IntensityEstimateValueReturn = NewIntensityEstScore;
1154                        } else {
1155                            if (Env.DEBUG == 100) {
1156                                System.out.printf("CurrentTime=%f  CurrentMWTimePlus12hr=%f\n",
1157                                        CurrentTime, CurrentMWTimePlus12hr);
1158                            }
1159                            if (CurrentTime <= CurrentMWTimePlus12hr) {
1160                                if (Env.DEBUG == 100) {
1161                                    System.out
1162                                            .printf("***rule8 was 32 keep as 32 (hold at last value of %f)\n",
1163                                                    LastValidMWCIValue);
1164                                }
1165                                IntensityEstimateValueReturn = LastValidMWCIValue;
1166                            } else {
1167                                CommentString = "";
1168                                CommentString = String.format("MW OFF  >=12hrs old");
1169                                History.IRCurrentRecord.comment = CommentString;
1170                                if (Env.DEBUG == 100) {
1171                                    System.out
1172                                            .printf("rule8 was 33 GREATER THAN 12 hours old!  TURNING OFF\n");
1173                                }
1174                                History.IRCurrentRecord.rule8 = 34; /* turn off */
1175                                IntensityEstimateValueReturn = LastValidMWCIValue;
1176                                MWScoreApplicationFlag = 0;
1177                            }
1178                        }
1179                    } else {
1180                        if (Env.DEBUG == 100) {
1181                            System.out
1182                                    .printf("MW value < 20: rule8 was 32 current is 33  STARTING HOLD\n");
1183                        }
1184                        History.IRCurrentRecord.rule8 = 33; /*
1185                                                             * start hold for 12
1186                                                             * hrs
1187                                                             */
1188                        IntensityEstimateValueReturn = LastValidMWCIValue;
1189                    }
1190                    break;
1191                case 33:
1192                    if (Env.DEBUG == 100) {
1193                        System.out.printf("CurrentTime=%f  CurrentMWTimePlus6hr=%f\n", CurrentTime,
1194                                CurrentMWTimePlus6hr);
1195                    }
1196                    if (CurrentTime <= CurrentMWTimePlus6hr) {
1197                        if (MWScoreInput >= MWScoreThresholdValue1) {
1198                            if (Env.DEBUG == 100) {
1199                                System.out
1200                                        .printf("rule8 was 33 current is 32   score >threshold1\n");
1201                            }
1202                            History.IRCurrentRecord.rule8 = 32; /* turn back ON */
1203                        } else {
1204                            if (Env.DEBUG == 100) {
1205                                System.out.printf("rule8 was 33 current is 33   STILL HOLDING\n");
1206                            }
1207                            History.IRCurrentRecord.rule8 = 33; /*
1208                                                                 * start hold
1209                                                                 * for 12 hrs
1210                                                                 */
1211                        }
1212                        IntensityEstimateValueReturn = LastValidMWCIValue;
1213                        MWScoreApplicationFlag = 1;
1214                    } else {
1215                        CommentString = "";
1216                        CommentString = String.format("MW OFF HOLD >=6hrs old");
1217                        History.IRCurrentRecord.comment = CommentString;
1218                        if (Env.DEBUG == 100) {
1219                            System.out
1220                                    .printf("rule8 was 33 GREATER THAN 6 hours old!   TURNING OFF\n");
1221                        }
1222                        History.IRCurrentRecord.rule8 = 34; /* turn off */
1223                        IntensityEstimateValueReturn = LastValidMWCIValue;
1224                        MWScoreApplicationFlag = 0;
1225                    }
1226                    break;
1227                /* added */
1228                case 34:
1229                    if (MWScoreInput >= MWScoreThresholdValue1) {
1230                        if (Env.DEBUG == 100) {
1231                            System.out.printf("rule8 was 34 current is 31   score >threshold1\n");
1232                        }
1233                        History.IRCurrentRecord.rule8 = 31; /* turn back ON */
1234                        IntensityEstimateValueReturn = LastValidMWCIValue;
1235                        MWScoreApplicationFlag = 1;
1236                    } else {
1237                        IntensityEstimateValueReturn = InputIntensityEstStore;
1238                        MWScoreApplicationFlag = 0;
1239                    }
1240                    break;
1241                /* end added */
1242                default:
1243                    IntensityEstimateValueReturn = InputIntensityEstStore;
1244                    MWScoreApplicationFlag = 0;
1245                    if (Env.DEBUG == 100) {
1246                        System.out.printf("default!\n");
1247                    }
1248            }
1249            if (Env.DEBUG == 100) {
1250                System.out.printf("Setting current MW score to %f\n", MWScoreInput);
1251            }
1252            History.IRCurrentRecord.mwscore = Env.MWScore;
1253            History.IRCurrentRecord.mwdate = Env.MWJulianDate;
1254            History.IRCurrentRecord.mwtime = Env.MWHHMMSSTime;
1255            IntensityEstimateValueReturn = ((double) (int) ((IntensityEstimateValueReturn + 0.05) * 10.0)) / 10.0;
1256            /* EXIT_Routine */
1257        } else {
1258            /*
1259             * need to put the intensity score into the correct place in the
1260             * history file
1261             */
1262            XInc = 0;
1263            while (XInc < NumRecsHistory) {
1264                RecDate = History.HistoryFile[XInc].date;
1265                RecTime = History.HistoryFile[XInc].time;
1266                HistoryRecTime = Functions.calctime(RecDate, RecTime);
1267                if (HistoryRecTime >= CurrentMWTime) {
1268                    /* determine if there is a record after MW time */
1269                    if (Env.DEBUG == 100) {
1270                        System.out.printf("putting MW value of %f into history file at %f!\n",
1271                                MWScoreInput, HistoryRecTime);
1272                    }
1273                    History.HistoryFile[XInc].mwscore = MWScoreInput;
1274                    History.HistoryFile[XInc].mwdate = Env.MWJulianDate;
1275                    History.HistoryFile[XInc].mwtime = Env.MWHHMMSSTime;
1276                }
1277                XInc++;
1278            }
1279        }
1280
1281        /*
1282         * IntensityEstimateValueReturn = IntensityEstimateValueReturn; compiler
1283         * says this does nothing
1284         */
1285        if (Env.DEBUG == 100) {
1286            System.out.printf("exit *IntensityEstimateValue_Return=%f\n",
1287                    IntensityEstimateValueReturn);
1288            System.out.printf("MWScoreApplicationFlag=%d\n", MWScoreApplicationFlag);
1289        }
1290
1291        return new double[] { (double) MWScoreApplicationFlag, IntensityEstimateValueReturn };
1292    }
1293
1294}