Warning: Can't use blame annotator:
svn blame failed on trunk/src/tmcsim/utilities/BuildHighwayFile.java: ("Can't find a temporary directory: Internal error", 20014)

source: tmcsimulator/trunk/src/tmcsim/utilities/BuildHighwayFile.java @ 276

Revision 276, 13.0 KB checked in by jdalbey, 7 years ago (diff)

BuildHighwayFile?.java: added some diagnostic messages.

RevLine 
1package tmcsim.utilities;
2
3import java.io.File;
4import java.io.FileNotFoundException;
5import java.io.IOException;
6import java.io.PrintWriter;
7import java.util.ArrayList;
8import java.util.HashMap;
9import java.util.HashSet;
10import java.util.List;
11import java.util.Map;
12import java.util.Scanner;
13import java.util.Set;
14import java.util.logging.Level;
15import java.util.logging.Logger;
16
17/**
18 * This utility program is used to create the highway map file used as part
19 * of the configuration files needed by the simulator.
20 * It merges info from three input files to create highway map file.
21 * Input: 1. VDS file is the district VDS metadata file downloaded from the PeMS
22 *      Data Clearinghouse.  It gives identifying info about each VDS.
23 *      2. Loop detector (lane) file that we found on the ATMS server.
24 *      It lists all the lanes associated with each VDS.       
25 *      3. LDS file that has the fep line number for each LDS.
26 * Output: 1. The highway map file that lists VDS info and the lanes it governs.
27 *      2. A slightly reformatted loop file.  (Not sure what this is for.)
28 * @author jdalbey, jtorres
29 */
30public class BuildHighwayFile
31{
32    final public static String filepath = "config/vds_data/old_vds_data/";
33    final public static String vdsFileName = "d12_vds_meta.txt";
34    final public static String loopFileName = "cleaned_chu_atms_loop_data.txt";
35    final public static String ldsFileName = "cleaned_chu_atms_lds_data.txt";
36    final public static String reformattedLoopFile = "loop.txt";
37    final public static String highwayFile = "highway_stations.txt";
38    /** A dictionary of ldsIDs to VDSids */
39    Map<String,StationAddr> ldsDict;
40    /** A dictionary of FEP line numbers to a list of VDSids */
41    Map<String,List<String>> feplines;
42    /** A dictionary to lookup VDS data given vdsID */
43    Map<String,VehicleDetectionStation> vdsDict;
44    /** A list of used VDS ids - FOR DEBUGGING*/
45    Set<String> vdsUsed = new HashSet<String>();
46    int ignored = 0; // count of ignored VDS ids
47    /** Reported missing */
48    Set<String> missingVDS = new HashSet<String>();
49    Set<String> missingLDS = new HashSet<String>();
50   
51    public void createHighwayFile()
52    {
53        try
54        {
55            PrintWriter hwyWriter = new PrintWriter(new File(filepath+highwayFile));
56            hwyWriter.print(feplines.size()+"\n");
57            for (String fep_line: feplines.keySet()) // for each fep line
58            {
59                hwyWriter.print(fep_line+" 0 ");
60                List<String> vdsList = feplines.get(fep_line);
61                hwyWriter.print(vdsList.size()+"\n");
62                //    output the vds info
63                for (String vds: vdsList)
64                {
65                    VehicleDetectionStation vdsItem = vdsDict.get(vds);
66                    if (vdsItem == null)
67                    {
68                        System.out.println(vds + " not found in vdsDict");
69                    }
70                    else
71                    {
72                        // Output the VDS identifying info
73                        hwyWriter.print(vdsItem.id + " ");
74                        hwyWriter.print(vdsItem.toString() + " ");
75                        hwyWriter.print(vdsItem.getLaneList().size() + " ");
76                        hwyWriter.print(vdsItem.street+"\n");
77                        List<String> laneList = vdsItem.getLaneList();
78                        //    output all the lanes
79                        for (String lane: laneList)
80                        {
81                            hwyWriter.print(lane);
82                            hwyWriter.print("\n");
83                        }
84                    }
85                }
86            }
87            hwyWriter.close();
88        }catch (FileNotFoundException ex)
89        {
90            Logger.getLogger(BuildHighwayFile.class.getName()).log(Level.SEVERE, null, ex);
91        } catch (Exception ex)
92        {
93            ex.printStackTrace();
94            Logger.getLogger(BuildHighwayFile.class.getName()).log(Level.SEVERE, null, ex);
95        }
96
97    }
98   
99    /** Read the loop file to create a dictionary to lookup all the lanes
100     * for a given VDS.  As a byproduct we will write the reformatted Loop file.
101     * (Not sure if this is needed. Ask jtorres.)
102     */
103    public void createLanelookup()
104    {   
105        feplines = new HashMap<String,List<String>> ();
106           
107        try
108        {
109            Scanner loopScanner = new Scanner(new File(filepath+loopFileName));
110            PrintWriter loopWriter = new PrintWriter(new File(filepath+reformattedLoopFile));
111           
112            loopScanner.nextLine();  // Skip the column headers
113
114            // Read all the lines in the loop file
115            while(loopScanner.hasNextLine())
116            {
117                String line = loopScanner.nextLine();
118                Scanner lineScanner = new Scanner(line);
119               
120                Integer fwy = lineScanner.nextInt();
121                String dir = lineScanner.next();
122                String postmile = lineScanner.next();
123                String ldsID = lineScanner.next();
124                String vdsID = lineScanner.next();
125                String loopID = lineScanner.next();
126                String shortLoc = lineScanner.next();
127                Integer laneNum = lineScanner.nextInt();
128                String loop_name = line.substring(73).trim(); // grab rest of line
129                loop_name = loop_name.replace(" ", "_");
130               
131                loopWriter.print(fwy + "\t");
132                loopWriter.print(dir+ "\t");
133                loopWriter.print(postmile + "\t");
134                loopWriter.print(ldsID + "\t");
135                loopWriter.print(vdsID + "\t");
136                loopWriter.print(loopID + "\t");
137                loopWriter.print(shortLoc + "\t");
138                loopWriter.print(laneNum + "\t");
139                loopWriter.print(loop_name + "\t");
140                loopWriter.print("?" + "\t");
141                loopWriter.print("0" + "\t");
142                loopWriter.print('\n');
143               
144                lineScanner.close();
145               
146                // Combine fields for one lane description
147                String laneDesc = loopID + " " + shortLoc + " " + loop_name;
148                // Add the lane to the lookup table
149                if (vdsDict.containsKey(vdsID))
150                {
151                    vdsDict.get(vdsID).addLane(laneDesc);
152                    vdsUsed.add(vdsID);
153                }
154                else
155                {
156                    boolean desiredType = shortLoc.equals("ML") || shortLoc.equals("OS");
157                    if (desiredType && !missingVDS.contains(vdsID))
158                    {
159                        System.out.println("createLaneLookup(): vdsID: "+vdsID+
160                                " of type "+shortLoc+" not found. "+ String.format("%3s %s %5s",fwy,dir,postmile));
161                        missingVDS.add(vdsID);
162                    }
163                }
164
165                // Also Add this vdsID to the list associated with a fepline
166                // lookup which fepline to use for this VDS (using ldsid as
167                //  intermediate key.
168                StationAddr sa = ldsDict.get(ldsID);
169                if (sa == null)
170                {
171                    if (!missingLDS.contains(ldsID))
172                    {
173                        System.out.println("missing ldsID in Station Addr lookup: "+ldsID+" "+ String.format("%3s %s %5s",fwy,dir,postmile));
174                        missingLDS.add(ldsID);
175                    }
176                }
177                else
178                {
179                    String fep_line = sa.line_num;
180                    // Fetch the info about this VDS
181                    VehicleDetectionStation curr = vdsDict.get(vdsID);
182                    // Some VDS were ignored because they weren't Mainline
183                    if (curr != null)
184                    {
185                        // Assign the station address for this VDS
186                        curr.setStaAddr(ldsDict.get(ldsID).station_address);
187
188                        if (fep_line == null)
189                        {
190                            System.out.println("No fepline for ldsID "+ldsID+ " :vdsid "
191                                    +vdsID+"  "+fwy+dir+postmile);
192                        }
193                        else
194                        {
195                            // Add vdsID to list of feplines
196                            if (feplines.containsKey(fep_line))
197                            {
198                                List<String> vdsList = feplines.get(fep_line);
199                                // only add it if it isn't already there
200                                if (!vdsList.contains(vdsID))
201                                {
202                                    feplines.get(fep_line).add(vdsID);
203                                }
204                            }
205                            else // Create a new one
206                            {
207                                List<String> arraylist1 = new ArrayList<String>();
208                                arraylist1.add(vdsID);
209                                feplines.put(fep_line, arraylist1);
210                            }
211                        }
212                    }
213                }
214            }
215           
216            loopScanner.close();
217            loopWriter.close();
218           
219        } catch (FileNotFoundException ex)
220        {
221            Logger.getLogger(BuildHighwayFile.class.getName()).log(Level.SEVERE, null, ex);
222        } 
223    }
224    /** Create a dictionary of VehicleDetectionStations */
225    public void createVDSdict()
226    {   
227        vdsDict = new HashMap<String,VehicleDetectionStation>(); 
228        try
229        {
230            Scanner vdsScanner = new Scanner(new File(filepath+vdsFileName));
231           
232            // Read the tab-delimited vds file
233            while(vdsScanner.hasNextLine())
234            {
235                String line = vdsScanner.nextLine();
236                Scanner lineScanner = new Scanner(line).useDelimiter("\t");
237                VehicleDetectionStation vds = new VehicleDetectionStation(lineScanner);
238                // We only want stations that are mainline
239                if (vds.type.equals("ML"))
240                {
241                    vdsDict.put(vds.id, vds);
242                }
243                else
244                {
245                    ignored++;
246                }
247                lineScanner.close();
248            }
249            vdsScanner.close();
250            System.out.println("Ignored "+ignored+ " non-Mainline VDS's");
251           
252        } catch (FileNotFoundException ex)
253        {
254            Logger.getLogger(BuildHighwayFile.class.getName()).log(Level.SEVERE, null, ex);
255        } 
256    }
257    /** Read the lds file to create a dictionary to lookup the FEP line number.
258     */
259    public void createLDSdict()
260    {   
261        ldsDict = new HashMap<String,StationAddr> ();
262           
263        try
264        {
265            Scanner ldsScanner = new Scanner(new File(filepath+ldsFileName));
266           
267            ldsScanner.nextLine();  // Skip the column headers
268
269            // Read all the lines in the loop file
270            while(ldsScanner.hasNextLine())
271            {
272                String line = ldsScanner.nextLine();
273                Scanner lineScanner = new Scanner(line);
274               
275                String ldsID = lineScanner.next();
276                String line_num = lineScanner.next();
277                String stn_address = lineScanner.next();
278                // Save the fep linenum and station address for this LDS
279                StationAddr sa = new StationAddr(line_num,stn_address);
280                lineScanner.close();
281                // Add the ldsID to the dict
282                ldsDict.put(ldsID, sa);
283            }
284            ldsScanner.close();
285        } catch (FileNotFoundException ex)
286        {
287            Logger.getLogger(BuildHighwayFile.class.getName()).log(Level.SEVERE, null, ex);
288        } 
289    }
290
291    /** Display the set of VDS in the dict that didn't get used (weren't found
292     * in loop file.
293     */
294    private void showLeftoverVDS()
295    {
296        Set<String> vdsItems = vdsDict.keySet(); // get all the VDS ids
297        vdsItems.removeAll(vdsUsed); // remove the ones that were used
298        System.out.println("Here are the unused VDS ids:");
299        // list the remaining items
300        for (String vdsitem: vdsItems)
301        {
302            System.out.print(vdsitem+"  ");
303        }
304        System.out.println("");           
305    }
306    /**
307     * @param args the command line arguments
308     */
309    public static void main(String[] args)
310    {
311        BuildHighwayFile app = new BuildHighwayFile();
312        app.createLDSdict();
313        app.createVDSdict();
314        app.createLanelookup();
315        app.createHighwayFile();
316        app.showLeftoverVDS();
317        System.out.println("Build complete, output file: "+highwayFile);
318    }
319
320    /** A record for a fep line_num  and Station Address pair */
321    final class StationAddr
322    {
323        public final String line_num;
324        public final String station_address;
325        public StationAddr(String line, String addr)
326        {
327            this.line_num = line;
328            this.station_address = addr;
329        }
330    }
331}
332
Note: See TracBrowser for help on using the repository browser.