Files
x11ma/plugins/ShiftsIO.java
2021-06-30 16:15:12 +02:00

109 lines
4.1 KiB
Java

import ij.io.OpenDialog;
import ij.io.SaveDialog;
import java.util.ArrayList;
import java.io.IOException;
import java.io.File;
import jmatio.types.*;
import jmatio.io.*;
import ij.gui.GenericDialog;
import ij.IJ;
import ij.Prefs;
public class ShiftsIO {
public ShiftsIO () {}
public double[][] load(String filename, String varname) {
if (filename==null){
String analysis_dir = Prefs.get("peem.analysis_dir", "");
OpenDialog od = new OpenDialog("Open_shifts .mat file:", analysis_dir, "shifts.mat");
String dir = od.getDirectory();
if (null == dir) return null; // dialog was canceled
if (!dir.endsWith(File.separator)) dir += File.separator;
filename = dir + od.getFileName();
Prefs.set("peem.analysis_dir", dir);
}
return low_load(filename, varname);
}
private double[][] low_load(String filename, String varname) {
MatFileReader mfr = null;
try {
mfr = new MatFileReader(filename);
} catch (IOException e) {
System.err.println("Caught IOException: "
+ e.getMessage());
}
if (mfr != null) {
MLDouble array = (MLDouble)mfr.getMLArray(varname);
if (array != null){
return array.getArray();
} else {
return null;
}
} else {
return null;
}
}
public void save(String filename, double[][] shifts, String varname) {
boolean update = false;
if (filename==null){
String analysis_dir = Prefs.get("peem.analysis_dir", "");
SaveDialog sd = new SaveDialog("Save_shifts .mat file:", analysis_dir, "shifts.mat", ".mat");
String dir = sd.getDirectory();
String fname = sd.getFileName();
if (null == fname) return; // user canceled dialog
if (!dir.endsWith(File.separator)) dir += File.separator;
Prefs.set("peem.analysis_dir", dir);
if ((new File(dir + fname)).exists()) { // if file exist, overwrite or update shifts ?
GenericDialog gd = new GenericDialog("Update");
String[] options = {"overwrite","update"};
gd.addChoice("Mode of operation with existing file. You want to: ", options, options[0]);
gd.showDialog();
if (gd.wasCanceled()) return;
if (gd.getNextChoiceIndex() == 1) update = true;
}
filename = dir + fname;
}
double[][] final_shifts;
if (update) {
double[][] prev_shifts = low_load(filename, "directshifts");
if (prev_shifts == null) {
IJ.error("Can't read the direct shifts in this file");
return;
}
if (prev_shifts.length == shifts.length) {
for (int j = 0; j < prev_shifts.length; j++) {
prev_shifts[j][3] += shifts[j][3];
prev_shifts[j][4] += shifts[j][4];
}
} else if (shifts.length == 2) {// 2 shifts, like between two slices of different
// sequences
for (int j = 0; j < prev_shifts.length; j++) {
prev_shifts[j][3] += shifts[1][3];
prev_shifts[j][4] += shifts[1][4];
}
} else {
IJ.error("Old and new shifts have incompatible length !");
return;
}
final_shifts = prev_shifts;
} else {
final_shifts = shifts;
}
MLDouble mlDouble = new MLDouble(varname, final_shifts);
ArrayList<MLArray> list = new ArrayList<MLArray>();
list.add(mlDouble);
try {
new MatFileWriter(filename, list);
} catch (IOException e) {
System.err.println("Caught IOException: "
+ e.getMessage());
}
}
}