001    package ui.recognizer;
002    
003    import java.awt.*;
004    import java.io.*;
005    import java.net.*;
006    import java.util.StringTokenizer;
007    import java.text.DecimalFormat;
008    
009    /**
010     * Convert the spectra line plot data extracted from a PostScript file extract to
011     * wavelength versus intensity plot using reference coordinates from tics on xy axes.
012     * This text-based class is designed to convert non-bitmapped ADS publications
013     * into spectra data.
014     * <ul>
015     * <li><b>INPUT</b>: A file containing the PostScript file extract and reference
016     *                   coordinates from tics on x-y axes
017     * <li><b>OUTPUT</b>: A spectra file containing wavelength vs. intensity plot
018     * </ul>
019     * The input file format :
020     * <pre>
021     * &lt; directives &gt;
022     * initialLowerLeft.x initialLowerLeft.y finalLowerLeft.x finalLowerLeft.y
023     * initialUpperRight.x initialUpperRight.y finalUpperRight.x finalUpperRight.y
024     * initial starting point (optinal for relative coordinates)
025     * x1 y1
026     * x2 y2
027     * ...
028     * </pre>
029     *
030     * The directives include : noskip, skip, landscape, relative, z 1.234
031     *
032     * @author by John Talbot
033     */
034    public class PostScriptConverter {
035        public static void main(String[] args) {
036    
037            String fileName;
038            if (args.length != 0) fileName = (String) args[0];
039            else return;
040    
041            BufferedReader in;
042            String line, intensities, name;
043            int lineNumber = 0;
044    
045            boolean skip = false;
046            int skipedlines = 1;
047            boolean relative = false;
048            boolean landscape = false;
049            boolean redshift = false;
050            double z = 1.0;
051    
052            boolean newFile = true;
053    
054            DecimalFormat xFormatter = new DecimalFormat("0.00");  // Angstroms
055            DecimalFormat yFormatter = new DecimalFormat("0.#####E0");
056            FileReader reader = null;
057            FileWriter writer = null;
058            FileOutputStream outFile;
059    
060            try {
061                reader = new FileReader(fileName);
062                in = new BufferedReader(reader);
063    
064                writer = new FileWriter(fileName + ".out");
065    
066                line = in.readLine();
067                StringTokenizer st = new StringTokenizer(line);
068                while (st.hasMoreElements()) {
069                    String command = st.nextToken();
070                    if (command.equals("skip") && !relative) {
071                        skip = true;
072                        skipedlines = Integer.parseInt(st.nextToken());
073                    } else if (command.equals("relative")) {
074                        relative = true;
075                        skip = false;
076                    } else if (command.equals("landscape"))
077                        landscape = true;
078                    else if (command.equals("z")) {
079                        redshift = true;
080                        z = Double.parseDouble(st.nextToken());
081                    }
082                }
083    
084                line = in.readLine();
085                st = new StringTokenizer(line);
086    
087                double x1 = Double.parseDouble(st.nextToken());
088                double y1 = Double.parseDouble(st.nextToken());
089    
090                DataCoordinate finalLowerLeft = new DataCoordinate(Double.parseDouble(st.nextToken()),
091                                                                     Double.parseDouble(st.nextToken()));
092                line = in.readLine();
093                st = new StringTokenizer(line);
094    
095                double x2 = Double.parseDouble(st.nextToken());
096                double y2 = Double.parseDouble(st.nextToken());
097    
098                DataCoordinate initialLowerLeft;
099                DataCoordinate initialUpperRight;
100                if (landscape) {
101                    initialLowerLeft = new DataCoordinate(y1, x2);
102                    initialUpperRight = new DataCoordinate(y2, x1);
103                } else {
104                    initialLowerLeft = new DataCoordinate(x1, y1);
105                    initialUpperRight = new DataCoordinate(x2, y2);
106                }
107                DataCoordinate finalUpperRight =  new DataCoordinate(Double.parseDouble(st.nextToken()),
108                                                                     Double.parseDouble(st.nextToken()));
109    
110                Scale scale = new Scale(initialLowerLeft, finalLowerLeft, initialUpperRight, finalUpperRight);
111    
112                DataCoordinate initialCoordinate;
113                DataCoordinate finalCoordinate;
114                double lastx = 0.0;
115                double lasty = 0.0;
116                int linenum = 0;
117                do {
118                    line = in.readLine();
119                    //System.out.print(line + " -> ");
120    
121                    if (line != null) {
122                        if (linenum == 0) {
123                            st = new StringTokenizer(line);
124                            double x = Double.parseDouble(st.nextToken());
125                            double y = Double.parseDouble(st.nextToken());
126                            if (relative) {
127                               x += lastx;
128                               y += lasty;
129                            }
130                            if (landscape) {
131                                initialCoordinate = new DataCoordinate(y, x);
132                            } else {
133                                initialCoordinate = new DataCoordinate(x, y);
134                            }
135                            finalCoordinate = scale.map(initialCoordinate);
136    
137                            if (redshift) {
138                                finalCoordinate.setX(finalCoordinate.getX()*(z+1));
139                            }
140                            String output = xFormatter.format(finalCoordinate.getX()) + " " +
141                                            yFormatter.format(finalCoordinate.getY());
142                            writer.write(output + "\n");
143                            //System.out.println(output);
144                            if (skip) linenum++;
145                            if (relative) {
146                                lastx = x;
147                                lasty = y;
148                            }
149                        } else {
150                            //System.out.println("skipped");
151                            if (line == null) break;  // stop reading file if end of file is reached
152                            linenum++;
153                            if (linenum == skipedlines) linenum = 0;
154                        }
155                    }
156    
157    
158               } while (line != null);
159               if (writer != null) writer.close();
160               if (reader != null) reader.close();
161            } catch (Exception e) {
162                System.out.println("Exception" + e);
163                e.printStackTrace();
164                System.exit(0);
165            } finally {
166    /*
167                try {
168                  if (writer != null) writer.close();
169                  if (reader != null) reader.close();
170                } catch (Exception ex) {
171                  System.out.println("Could not close reader or writer " + ex);
172                }
173    */
174            }
175    
176        }
177    }