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.io.File;
032import java.io.IOException;
033
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037public class Main {
038    
039    private static final Logger logger =
040        LoggerFactory.getLogger(Main.class);
041    
042    public static String HistoryFileName;
043    public static String ReturnOutputString;
044
045    public Main() {
046        HistoryFileName = null;
047        ReturnOutputString = null;
048    }
049
050    public int GetInitialPosition() {
051
052        Auto AutoMode = new Auto();
053
054        String ForecastFileName = null;
055        double[] AutoMode1Return = null;
056
057        boolean RunAuto = Env.AutoTF;
058        ForecastFileName = Env.ForecastFileName;
059        int ForecastFileType = Env.ForecastFileType;
060
061        System.out.printf("Run AUTO=%b\n", RunAuto);
062        if (RunAuto) {
063            try {
064                AutoMode1Return = AutoMode.AutoMode1(ForecastFileName, ForecastFileType);
065
066                if (AutoMode1Return == null)
067                    return -1;
068
069                if (((int) AutoMode1Return[0]) < 0) {
070                    System.out.printf("ERROR with interpolation/extrapolation : return code %d\n",
071                            (int) AutoMode1Return[0]);
072                    return -1;
073                }
074            } catch (IOException exception) {
075                System.out.printf("ERROR with reading forecast file\n");
076                return -2;
077            }
078            int ForecastReturnFlag = (int) AutoMode1Return[0];
079            double ForecastLatitude = AutoMode1Return[1];
080            double ForecastLongitude = AutoMode1Return[2];
081            double ForecastIntensity = AutoMode1Return[3];
082            double ForecastMethodID = AutoMode1Return[4];
083
084            Env.SelectedLatitude = ForecastLatitude;
085            Env.SelectedLongitude = ForecastLongitude;
086
087            History.IRCurrentRecord.latitude = ForecastLatitude;
088            History.IRCurrentRecord.longitude = ForecastLongitude;
089            History.IRCurrentRecord.autopos = 1; // forecast interpolation
090            System.out
091                    .printf("AutoMode1 output position info : Latitude=%f Longitude=%f Intensity=%f MethodID=%f Flag=%d\n",
092                            ForecastLatitude, ForecastLongitude, ForecastIntensity,
093                            ForecastMethodID, ForecastReturnFlag);
094
095        } else {
096            System.out.printf("Manual Mode : latitude=%f longitude=%f", Env.SelectedLatitude,
097                    Env.SelectedLongitude);
098            History.IRCurrentRecord.latitude = Env.SelectedLatitude;
099            History.IRCurrentRecord.longitude = Env.SelectedLongitude;
100            History.IRCurrentRecord.autopos = 0; // Manual
101        }
102
103        return 1;
104
105    }
106
107    public void GetARCHERPosition() {
108
109        double[] AutoMode2Return = null;
110        double InputLatitude = Env.SelectedLatitude;
111        double InputLongitude = Env.SelectedLongitude;
112        try {
113            AutoMode2Return = Auto.AutoMode2(InputLatitude, InputLongitude);
114        } catch (IOException exception) {
115            System.out.printf("ERROR with Automode2 routine\n");
116            return;
117        }
118
119        double FinalLatitude = AutoMode2Return[0];
120        double FinalLongitude = AutoMode2Return[1];
121        int FinalPositioningMethod = (int) AutoMode2Return[2];
122        History.IRCurrentRecord.latitude = FinalLatitude;
123        History.IRCurrentRecord.longitude = FinalLongitude;
124        History.IRCurrentRecord.autopos = FinalPositioningMethod;
125
126        Env.SelectedLatitude = History.IRCurrentRecord.latitude;
127        Env.SelectedLongitude = History.IRCurrentRecord.longitude;
128
129        return;
130
131    }
132
133    public String RunADTAnalysis(boolean RunFullAnalysis, String InputHistoryFile)
134            throws IOException {
135
136        String BulletinOutput = null;
137        int HistoryFileRecords;
138
139        History CurrentHistory = new History();
140
141        HistoryFileName = InputHistoryFile;
142
143        boolean OverrideScene = Env.OverSceneTF;
144        int OverrideSceneTypeValue = Env.OverrideSceneTypeIndex;
145
146        /*
147         * System.out.printf("MW Info : Date=%s JulianDate=%d Time=%d Score=%f\n"
148         * ,MWDate,Env.MWJulianDate,MWTime,MWScore);
149         */
150
151        /* READ HISTORY FILE INFORMATION */
152        if (RunFullAnalysis && HistoryFileName != null) {
153            try {
154                CurrentHistory.ReadHistoryFile(HistoryFileName);
155            } catch (IOException exception) {
156                System.out.printf("History file %s not found\n", HistoryFileName);
157            }
158        } else {
159            System.out.printf("Not utilizing a history file\n");
160        }
161        HistoryFileRecords = History.HistoryNumberOfRecords();
162        System.out.printf("Number of records in history file %s is %d\n", HistoryFileName,
163                HistoryFileRecords);
164
165        /* read topography file at center position */
166        double PositionLatitude = History.IRCurrentRecord.latitude;
167        double PositionLongitude = History.IRCurrentRecord.longitude;
168        String topoPath = new File(".").getCanonicalPath();
169        System.err.println("topoPath: " + topoPath);
170        String TopoFileName = "/edu/wisc/ssec/mcidasv/resources/digelev_hires_le.map";
171
172        int TopographyFlag = 0;
173        System.out.printf("TOPO Info : File=%s Lat=%f Lon=%f\n", TopoFileName, PositionLatitude,
174                PositionLongitude);
175        try {
176            TopographyFlag = Topo.ReadTopoFile(TopoFileName, PositionLatitude, PositionLongitude);
177        } catch (IOException e) {
178            logger.error("ERROR reading topography file", e);
179            return null;
180        }
181        /* System.out.printf("after topo read flag=%d\n",TopographyFlag); */
182        History.IRCurrentRecord.land = TopographyFlag;
183
184        /* Calculate Eye and Cloud region temperatures */
185        Data.CalcEyeCloudTemps();
186        /* System.out.printf("after calceyecloudtemps\n"); */
187        /*
188         * double Eye_Temperature = History.IRCurrentRecord.eyet; double
189         * CWCloud_Temperature = History.IRCurrentRecord.cwcloudt; double
190         * Cloud_Temperature = History.IRCurrentRecord.cloudt; double
191         * Cloud2_Temperature = History.IRCurrentRecord.cloudt2; double
192         * Cloud_Symmetry = History.IRCurrentRecord.cloudsymave; double Eye_STDV
193         * = History.IRCurrentRecord.eyestdv; int CWRing_Distance =
194         * History.IRCurrentRecord.cwring;
195         * System.out.printf("Eye Temperature=%f\n",Eye_Temperature);
196         * System.out.printf("CWCloud Temperature=%f\n",CWCloud_Temperature);
197         * System.out.printf("CWRing Distance=%d\n",CWRing_Distance);
198         * System.out.printf("Cloud Temperature=%f\n",Cloud_Temperature);
199         * System.out.printf("Cloud2 Temperature=%f\n",Cloud2_Temperature);
200         * System.out.printf("Cloud Symmetry=%f\n",Cloud_Symmetry);
201         * System.out.printf("Eye STDV=%f\n",Eye_STDV);
202         */
203
204        /* Calculate Eye and Cloud region Scene Type */
205
206        /*
207         * System.out.printf("overridescenetypevalue=%d\n",
208         * OverrideSceneTypeValue);
209         */
210        if (OverrideSceneTypeValue >= 0) {
211            /* System.out.printf("setting old scene types\n"); */
212            History.IRCurrentRecord.cloudsceneold = History.IRCurrentRecord.cloudscene;
213            History.IRCurrentRecord.eyesceneold = History.IRCurrentRecord.eyescene;
214            History.IRCurrentRecord.cloudscene = Math.max(0, (OverrideSceneTypeValue - 3));
215            History.IRCurrentRecord.eyescene = Math.min(3, OverrideSceneTypeValue);
216
217        } else {
218            Scene.DetermineSceneType(RunFullAnalysis);
219            /* System.out.printf("after scene type determination\n"); */
220            /* System.out.printf("OverrideScene=%b\n",OverrideScene); */
221            if (OverrideScene) {
222                /*
223                 * System.out.printf("overriding scene type : eye=%d cloud=%d\n",
224                 * History
225                 * .IRCurrentRecord.eyescene,History.IRCurrentRecord.cloudscene
226                 * );
227                 */
228                if (History.IRCurrentRecord.eyescene < 3) {
229                    Env.OverrideSceneTypeIndex = History.IRCurrentRecord.eyescene;
230                } else {
231                    Env.OverrideSceneTypeIndex = 3 + History.IRCurrentRecord.cloudscene;
232                }
233                /*
234                 * System.out.printf("ADTEnv.overridescenetype=%d\n",
235                 * Env.OverrideSceneType);
236                 */
237                return "override";
238            }
239        }
240
241        /* Calculate Intensity Estimate Values */
242
243        int RedoIntensityFlag = 0;
244        Intensity.CalculateIntensity(RedoIntensityFlag, RunFullAnalysis, HistoryFileName);
245        /* System.out.printf("after calcintensity\n"); */
246        /* Write Bulletin Output */
247
248        BulletinOutput = Output.TextScreenOutput(HistoryFileName);
249        /* System.out.printf("\n *** Bulletin Output ***\n%s\n",BulletinOutput); */
250        /* System.out.printf("after textscreenoutput\n"); */
251        ReturnOutputString = BulletinOutput;
252
253        return ReturnOutputString;
254
255    }
256}