hdf5
unknown
java
a year ago
10 kB
11
Indexable
import ncsa.hdf.object.FileFormat; import ncsa.hdf.object.h5.H5File; import ncsa.hdf.object.Dataset; import ncsa.hdf.object.Group; 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.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Arrays; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class AdvancedHDF5Plot { private static final double DISPERSION_CONSTANT = 4.148808e3; private static boolean verbose = false; 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 public static void main(String[] args) { SwingUtilities.invokeLater(() -> createAndShowGUI()); } private static void createAndShowGUI() { JFrame frame = new JFrame("Advanced HDF5 Data Analyzer"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); // Control Panel JPanel controlPanel = new JPanel(); controlPanel.setLayout(new GridLayout(4, 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); // 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); } }); 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); applyIncoherentDedispersion(wf, delay); SwingUtilities.invokeLater(() -> { chartPanel.removeAll(); chartPanel.add(new ChartPanel(createFreqSpectChart(observationId, freq, time, wf))); chartPanel.add(new ChartPanel(createDMSpectChart(observationId, freq, time, wf))); 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]); } dataset.addSeries(series); } JFreeChart chart = ChartFactory.createXYLineChart( "DM Spectrogram: " + observationId, "Frequency", "Intensity", dataset, PlotOrientation.VERTICAL, true, true, false); customizeChart(chart); return chart; } private static void customizeChart(JFreeChart chart) { XYPlot plot = chart.getXYPlot(); XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(); renderer.setSeriesShapesVisible(0, false); plot.setRenderer(renderer); plot.getDomainAxis().setLabelFont(new Font("Dialog", Font.BOLD, 14)); plot.getRangeAxis().setLabelFont(new Font("Dialog", Font.BOLD, 14)); } private static double[] calculateDispersionDelay(double[] freq, double dm) { double[] delay = new double[freq.length]; for (int i = 0; i < freq.length; i++) { delay[i] = dm * DISPERSION_CONSTANT * (Math.pow(1e6 / freq[i], 2) - Math.pow(1e6 / freq[freq.length - 1], 2)); } return delay; } private static void applyIncoherentDedispersion(double[][] wf, double[] delay) { for (int i = 0; i < wf.length; i++) { for (int j = 0; j < wf[i].length; j++) { wf[i][j] += delay[j]; } } } }
Editor is loading...
Leave a Comment