Untitled
Class 3unknown
java
a year ago
9.4 kB
2
Indexable
import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.apache.pdfbox.pdmodel.font.PDType1Font; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; import java.awt.*; import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class PDFHandlerApp { private static final double DISPERSION_CONSTANT = 4.148808e3; private static double dm = 350; // Default DM value private static double clipTo = 1.5; // Default clip value private static boolean useSimulator = false; // Use simulator flag private static Color lineColor = Color.BLACK; // Default line color public static void main(String[] args) { SwingUtilities.invokeLater(PDFHandlerApp::createAndShowGUI); } private static void createAndShowGUI() { JFrame frame = new JFrame("Advanced PDF Data Analyzer"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); // Control Panel JPanel controlPanel = new JPanel(); controlPanel.setLayout(new GridLayout(5, 2)); // Observation ID JLabel obsIdLabel = new JLabel("Observation ID:"); JTextField obsIdField = new JTextField("obs1"); controlPanel.add(obsIdLabel); controlPanel.add(obsIdField); // DM Value JLabel dmLabel = new JLabel("DM Value:"); JTextField dmField = new JTextField(Double.toString(dm)); controlPanel.add(dmLabel); controlPanel.add(dmField); // Clip Value JLabel clipLabel = new JLabel("Clip Value:"); JTextField clipField = new JTextField(Double.toString(clipTo)); controlPanel.add(clipLabel); controlPanel.add(clipField); // Line Color Picker JLabel colorLabel = new JLabel("Line Color:"); JButton colorButton = new JButton("Choose Color"); controlPanel.add(colorLabel); controlPanel.add(colorButton); // Simulator Checkbox JCheckBox simulatorCheckBox = new JCheckBox("Use Simulator"); controlPanel.add(simulatorCheckBox); // File Chooser Button JButton fileChooserButton = new JButton("Choose HDF5 File"); controlPanel.add(fileChooserButton); // Plot Button JButton plotButton = new JButton("Generate Plots"); controlPanel.add(plotButton); frame.add(controlPanel, BorderLayout.NORTH); // Chart Panels JPanel chartPanel = new JPanel(); chartPanel.setLayout(new GridLayout(2, 1)); frame.add(chartPanel, BorderLayout.CENTER); // File Chooser JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileFilter(new FileNameExtensionFilter("HDF5 Files", "hdf5")); fileChooserButton.addActionListener(e -> { int returnValue = fileChooser.showOpenDialog(frame); if (returnValue == JFileChooser.APPROVE_OPTION) { String filename = fileChooser.getSelectedFile().getPath(); obsIdField.setText(filename); } }); colorButton.addActionListener(e -> { Color chosenColor = JColorChooser.showDialog(frame, "Choose Line Color", lineColor); if (chosenColor != null) { lineColor = chosenColor; } }); plotButton.addActionListener(e -> { String observationId = obsIdField.getText(); String filename = fileChooser.getSelectedFile().getPath(); dm = Double.parseDouble(dmField.getText()); clipTo = Double.parseDouble(clipField.getText()); useSimulator = simulatorCheckBox.isSelected(); generateAndDisplayPlots(observationId, filename, chartPanel); }); frame.pack(); frame.setVisible(true); } private static void generateAndDisplayPlots(String observationId, String filename, JPanel chartPanel) { try { double[] freq; double[] time; double[][] wf; if (useSimulator) { // Simulate data freq = simulateFrequencies(100); time = simulateTime(1000); wf = new double[time.length][freq.length]; simulateWaterfallData(wf, time, freq); } else { // Load data from HDF5 file H5File file = new H5File(filename, FileFormat.READ); file.open(); Group observation = (Group) file.get("Observation1"); displayHDF5Structure(observation, ""); freq = (double[]) ((Dataset) observation.getMember("Tuning1/freq")).getData(); time = (double[]) ((Dataset) observation.getMember("time")).getData(); wf = new double[time.length][freq.length]; simulateWaterfallData(wf, time, freq); file.close(); } double[] delay = calculateDispersionDelay(freq, dm); double[][] dedispersedWf = applyIncoherentDedispersion(wf, freq, time, delay); SwingUtilities.invokeLater(() -> { chartPanel.removeAll(); chartPanel.add(new ChartPanel(createFreqSpectChart(observationId, freq, time, dedispersedWf))); chartPanel.add(new ChartPanel(createDMSpectChart(observationId, freq, time, dedispersedWf))); chartPanel.revalidate(); chartPanel.repaint(); }); } catch (Exception e) { e.printStackTrace(); } } private static void displayHDF5Structure(Group group, String indent) { try { for (Object memberObj : group.getMemberList()) { if (memberObj instanceof Dataset) { Dataset dataset = (Dataset) memberObj; System.out.println(indent + dataset.getFullName() + ": " + Arrays.toString(dataset.getDims()) + " " + dataset.getDatatype().getDescription()); } else if (memberObj instanceof Group) { Group subgroup = (Group) memberObj; System.out.println(indent + subgroup.getFullName() + ": Group"); displayHDF5Structure(subgroup, indent + " "); } } } catch (Exception e) { e.printStackTrace(); } } private static double[] simulateFrequencies(int size) { double[] freq = new double[size]; for (int i = 0; i < size; i++) { freq[i] = 1000 + i * 10; // Simulated frequencies from 1000 to 2000 Hz } return freq; } private static double[] simulateTime(int size) { double[] time = new double[size]; for (int i = 0; i < size; i++) { time[i] = i * 0.01; // Simulated time from 0 to 10 seconds } return time; } private static void simulateWaterfallData(double[][] wf, double[] time, double[] freq) { ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); for (int i = 0; i < time.length; i++) { final int idx = i; executor.submit(() -> { for (int j = 0; j < freq.length; j++) { wf[idx][j] = Math.sin(time[idx] + freq[j]); } }); } executor.shutdown(); try { executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { e.printStackTrace(); } } private static JFreeChart createFreqSpectChart(String observationId, double[] freq, double[] time, double[][] wf) { XYSeriesCollection dataset = new XYSeriesCollection(); for (int i = 0; i < freq.length; i++) { XYSeries series = new XYSeries("Frequency " + freq[i]); for (int j = 0; j < time.length; j++) { series.add(time[j], wf[j][i]); } dataset.addSeries(series); } JFreeChart chart = ChartFactory.createXYLineChart( "Frequency Spectrogram: " + observationId, "Time", "Intensity", dataset, PlotOrientation.VERTICAL, true, true, false); customizeChart(chart); return chart; } private static JFreeChart createDMSpectChart(String observationId, double[] freq, double[] time, double[][] wf) { XYSeriesCollection dataset = new XYSeriesCollection(); for (int i = 0; i < time.length; i++) { XYSeries series = new XYSeries("Time " + time[i]); for (int j = 0; j < freq.length; j++) { series.add(freq[j], wf[i][j]);
Editor is loading...
Leave a Comment