package osp.ui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Point2D;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.math.RoundingMode;
import java.net.URL;
import java.nio.file.Paths;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;
import javax.help.CSH;
import javax.help.CSH.DisplayHelpFromSource;
import javax.help.HelpBroker;
import javax.help.HelpSet;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.omg.CosNaming._NamingContextImplBase;
import jsky.coords.WCSTransform;
import jsky.coords.WorldCoords;
import jsky.image.ImageProcessor;
import jsky.image.fits.FITSKeywordProvider;
import jsky.image.fits.codec.FITSImage;
import jsky.util.gui.ExampleFileFilter;
import nom.tam.fits.FitsException;
import osp.FieldOfView;
import osp.Image;
import osp.InstrumentParameters;
import osp.Mask;
import osp.MaskHistory;
import osp.Reader;
import osp.SkyObject;
import osp.Slit;
import osp.Writer;
import osp.ui.OSPE_NavigatorFrame.TypeNavigator;
import osp.utils.CustomJDialog;
import osp.utils.Tools;
/**
* Methods and menu actions.
*
* @author JM Glorian
*
*/
public class OspeControl implements ToolBarActionInterface, Observer{
private HelpBroker hb;
private OSPE_MainFrame mainFrame;
private OSPE_VisuPanel visuPanel;
private OspeMenuBar menuBar;
private FieldOfView fov;
private OSPE_BrowserToolBar toolBar;
private Reader reader;
private Writer writer;
private OspeMaskControl maskControl;
private OspeActionControl actionControl;
private OSPE_SelectPanel selectPanel;
private OSPE_SettingsPanel settingsPanel;
private DisplayHelpFromSource displayHelpFromSource;
private String lastDirectory; /** Last working directory */
public String projectCompletePath;
public String userDirectory;
public String emirHomeDir; /** Directory where the soft is located */
private int nbLoadCat=0;
protected InstrumentParameters instrumentParams;
public OspeControl(OSPE_MainFrame mainFrame, OSPE_VisuPanel ospe_VisuPanel,
OSPE_SelectPanel selectPanel,OSPE_SettingsPanel ospe_SettingsPanel,
FieldOfView fov, OSPE_BrowserToolBar toolBar, Reader reader,
Writer writer, OspeMenuBar menuBar) {
this.selectPanel = selectPanel;
this.settingsPanel = ospe_SettingsPanel;
this.maskControl = new OspeMaskControl();
this.actionControl = new OspeActionControl();
this.fov = fov;
this.toolBar = toolBar;
this.reader = reader;
this.writer = writer;
this.menuBar = menuBar;
menuBar.setControl(this);
this.mainFrame = mainFrame;
this.visuPanel = ospe_VisuPanel;
lastDirectory="";
projectCompletePath="";
}
/**
* Test if there is a project yet opened or not
* To be implemented.
*/
@Override
public void projectNew() {
projectNew(true);
}
/**
* Create a new project
* @param saveOld If there is yet an open project
*
that needs to be saved before opening a new one.
*/
public void projectNew(boolean saveOld) {
boolean cancel = false;
if (saveOld){
int choice = askToSaveIfNeeded();
if (choice != JOptionPane.CANCEL_OPTION) {
projectClose();
} else {
cancel = true;
}
}
if (!cancel) {
resetNavigatorImageDisplay();
getMainFrame().setStatusBarText("");
File theDirectory = getDefaultDirectory();
JFileChooser fileChooser = new JFileChooser();
if (theDirectory != null) {
fileChooser.setCurrentDirectory(theDirectory);
}
fileChooser.setDialogTitle("Select New Workspace");
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (fileChooser.showDialog(getMainFrame(), "New") == JFileChooser.CANCEL_OPTION) {
cancel = true;
}
if (! cancel) {
theDirectory = fileChooser.getSelectedFile();
if (!theDirectory.exists()) {
if (theDirectory.mkdir()){
getMainFrame().setStatusBarText(Color.blue,
"INFO: Directory <" + theDirectory.getPath() + "> created !");
} else {
new CustomJDialog("Error while creating directory !",
CustomJDialog._ERROR_, CustomJDialog._CLOSE_BUTTON_,
getMainFrame());
cancel = true;
}
}
if (!cancel) {
createNewWorkspace(theDirectory);
}
}
}
}
public void createFirstProject() {
//projectClose();
resetNavigatorImageDisplay();
getMainFrame().setStatusBarText("");
File theDirectory = getDefaultDirectory();
createNewWorkspace(theDirectory);
}
private void createNewWorkspace(File theDirectory) {
fov.setPath(theDirectory.getPath());
setLastDir(theDirectory.getAbsolutePath());
getMainFrame().setTitle("OSP EDITOR - New project");
getInstrumentParameters(emirHomeDir);
//getInstrumentParameters(theDirectory.getAbsolutePath());
fov.update();
menuBar.enableForNewProject();
toolBar.enableForNewProject();
}
private File getDefaultDirectory() {
File theDirectory = null;
String fovDirectory = null;
if (fov.getClass() !=null) {
fovDirectory=fov.getPath();
}
if (fovDirectory == null)
{
fovDirectory=System.getProperty("user.dir");
emirHomeDir=fovDirectory;
if (lastDirectory.equals(fovDirectory)){
theDirectory = new File(fovDirectory);
} else {
theDirectory = new File (lastDirectory);
}
}
//System.out.println("Ctrl-getDefDir "+ fovDirectory+" "+theDirectory.getAbsolutePath()+" "+lastDirectory+" "+emirHomeDir);
return theDirectory;
}
@Override
public void moveMask(){
if (fov.getCurrentImage().getSelectedMask() >= 0)
{
if (! fov.getCurrentImage().getCurrentMask().moveEnable )
{
//System.out.println("Control : movEnable false mais bouger");
maskControl.setmMaskFlag(1);
getMainFrame().setStatusBarText(Color.blue, "INFO: Mask - Move ");
fov.getCurrentImage().getCurrentMask().moveEnable = true;
toolBar.moveMask(true);
menuBar.jMenuMaskMove.setSelected(true);
fov.getCurrentImage().getCurrentMask().turnEnable = false;
menuBar.jMenuMaskTurn.setSelected(false);
}
else
{
//System.out.println("Control : movEnable true du coup pas bouger");
fov.getCurrentImage().getCurrentMask().moveEnable = false;
toolBar.moveMask(false);
menuBar.jMenuMaskMove.setSelected(false);
}
fov.getCurrentImage().getCurrentMask().update();
}
else
{
toolBar.getToggleButtonMove().setSelected(false);
menuBar.jMenuMaskMove.setSelected(false);
}
}
@Override
public void projectOpen() {
// getMainFrame().setStatusBarText("Test if work");
int choice = askToSaveIfNeeded();
if (choice == JOptionPane.CANCEL_OPTION)
return;
// To change project to another, the previous must be closed before loading the other one
projectClose();
getMainFrame().setStatusBarText("");
File file = null;
// Choose the file to load
JFileChooser fileChooser = new JFileChooser();
if (lastDirectory!= null)
{
file=new File(lastDirectory);
fileChooser.setCurrentDirectory(file);
}
fileChooser.setDialogTitle("Open Existing Project");
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("xml");
fileChooser.setFileFilter(filter);
int returnVal = fileChooser.showDialog(getMainFrame(), "Open");
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile();
if (file != null) {
fov.setPath(file.getParent());
fov.setFileName(file.getName());
setLastDir(file.getParent());
projectCompletePath=fov.getCompletePath();
//String filePath=getLastDir();
String filePath=getEmirDir();
getInstrumentParameters(filePath);
// Load the project
int err = reader.read(projectCompletePath);
if (err <= 0) {
String mess = reader.getMessageError();
CustomJDialog dialog = new CustomJDialog(mess, CustomJDialog._WARNING_,
CustomJDialog._CLOSE_BUTTON_, getMainFrame());
if (dialog.getValue() == JOptionPane.OK_OPTION) {
projectClose();
}
// System.out.println("DEBUG : MainFrame::ProjectOpenAction :"
// + " Error loading project !");
return;
}
// Display it
getMainFrame().setTitle("OSP EDITOR - " + projectCompletePath);
menuBar.jMenuProjectSave.setEnabled(true);
menuBar.jMenuProjectSaveAs.setEnabled(true);
menuBar.jMenuProjectClose.setEnabled(true);
menuBar.jMenuDisplay.setEnabled(true);
menuBar.jMenuImage.setEnabled(true);
toolBar.getButtonSave().setEnabled(true);
Image img = fov.getCurrentImage();
if (img != null){
int i= fov.getSelectedImage();
if (fov.getCurrentImage().getNbrObjects()>0)
{
menuBar.jMenuMask.setEnabled(true);
}
if (fov.getCurrentImage().getListMasks().size() > 0)
{
visuPanel.isMaskDisplayed = true;
menuBar.jMenuMaskRemove.setEnabled(true);
menuBar.jMenuMaskMove.setEnabled(true);
menuBar.jMenuMaskTurn.setEnabled(true);
menuBar.jMenuSlit.setEnabled(true);
toolBar.getToggleButtonMove().setEnabled(true);
toolBar.getToggleButtonTurn().setEnabled(true);
toolBar.getButtonUndoToolBar().setEnabled(false);
}
if (!visuPanel.getImageCutDialog().hasDisplay()) {
visuPanel.updateCutLevels(i);
visuPanel.updateColors(i);
visuPanel.updateOfImageNavigatorDisplay();
}
img.cutsChanged = false;
//mainFrame.setStatusBarText(Color.blue, "INFO: Project opened");
mainFrame.setTitle("OSP EDITOR - " + projectCompletePath);
getMainFrame().setStatusBarText(Color.blue, "INFO: Project opened");
fov.update();
}
}
}
}
private String getEmirDir() {
// TODO Auto-generated method stub
//System.out.println("ctrl - getEmirDir");
return emirHomeDir;
}
/**
*
*/
public void projectClose() {
getMainFrame().setStatusBarText("");
int choice = askToSaveIfNeeded();
if (choice != JOptionPane.CANCEL_OPTION) {
if (fov.getPath() != null) {
for (int i = fov.getNbrImages(); i > 0; i--) {
fov.setSelectedImage(i - 1);
Image im=fov.getImage(i - 1);
if (im.getNbrMasks() > 0)
im.resetTotalMask();
if (im.getNbrObjects() > 0)
im.removeAllObjects();
removeImage();
}
fov.resetTotalImage();
fov.setPath(null);
fov.setFileName(null);
getMainFrame().setTitle("OSP EDITOR ");
fov.update();
}
if (fov.getSelectedImage() >= 0){
fov.getCurrentImage().setScale(1);
}
visuPanel.validateButton.setEnabled(false);
visuPanel.searchButton.setEnabled(false);
visuPanel.writeButton.setEnabled(false);
visuPanel.eraseVisu();
visuPanel.repaint();
getMainFrame().setStatusBarText(Color.blue, "INFO: Project closed");
menuBar.disableMenuForClosedProject();
toolBar.disableButtonForCloseProject();
projectCompletePath=null;
}
//lastDirectory= System.getProperty("user.dir");
}
/** Reseting NavigatorImageDisplay
* ClearING the window displaying and reseting its paratmeters before loading another image
*
*/
public void resetNavigatorImageDisplay() {
visuPanel.getNavigatorImageDisplay().clear();
visuPanel.getNavigatorImageDisplay().getImageProcessor().setDefaultColormap();
visuPanel.getNavigatorImageDisplay().setOrigin(new Point2D.Double(0, 0));
visuPanel.getNavigatorImageDisplay().setScale(1);
visuPanel.getNavigatorImageDisplay().updateImage();
}
@Override
public void projectSave() {
if ((fov.getFileName() == null) || (projectCompletePath==" ") ||(projectCompletePath==null) ) {
saveAsProject();
} else {
//projectCompletePath = fov.getCompletePath();
writer.write(projectCompletePath);
actionControl.setSaveNeeded(false);
actionControl.setSaveNeeded(false);
getMainFrame().setTitle("OSP EDITOR - " + projectCompletePath);
getMainFrame().setStatusBarText(Color.blue, "INFO: Project saved");
}
}
/**
* Access to the menu bar.
* @return menuBar
*/
public OspeMenuBar getMenuBar() {
return menuBar;
}
/**
* Access to the tools bar.
* @return toolBar
*/
public OSPE_BrowserToolBar getToolBar(){
return toolBar;
}
/**
*
*/
public void joinSlit() {
Mask currentMask = fov.getCurrentImage().getCurrentMask();
JDialog quickJoinSlitsDialog = new OSPE_QuickJoinSlitsPanel(currentMask, this,
currentMask.getSelectedSlit(), currentMask.getSelectedSlit() + 1);
quickJoinSlitsDialog.setLocationRelativeTo(visuPanel);
actionControl.setSaveNeeded(true);
quickJoinSlitsDialog.addWindowListener(new ConflictsPreventer(visuPanel, selectPanel, menuBar, toolBar));
}
/**
*
*/
public void loadImageFileAction() {
JFileChooser choose = new JFileChooser();
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("fits");
filter.setDescription("FITS Images");
choose.setFileFilter(filter);
choose.setCurrentDirectory(new File(fov.getPath()));
int returnVal = choose.showOpenDialog(visuPanel);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
File file = choose.getSelectedFile();
addImageToProject(file);
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
/**
* @param file
* @throws IOException
* @throws FitsException
*/
public void addImageToProject(File file) throws IOException, FitsException {
String pathOfFitsFile = file.getAbsolutePath();
final FITSImage fitsImage = new FITSImage(pathOfFitsFile);
WCSTransform wcs = checkFitsImage(fitsImage);
if (wcs != null)
{
getMainFrame().setStatusBarText("");
OSPE_NavigatorImageDisplay navigatorImageDisplay = visuPanel.getNavigatorImageDisplay();
resetNavigatorImageDisplay();
navigatorImageDisplay.setImage(fitsImage);
/** CCenter in canvas coord
* NavigatorImageDisplay Origin is in canvas coords */
double xc=(navigatorImageDisplay.getWidth() / 2.0 + navigatorImageDisplay
.getOrigin().getX()) / navigatorImageDisplay.getScale();
double yc=(navigatorImageDisplay.getHeight() / 2.0 + navigatorImageDisplay
.getOrigin().getY()) / navigatorImageDisplay.getScale();
// Point2D.Double cCenter = new Point2D.Double((navigatorImageDisplay.getWidth() / 2.0 + navigatorImageDisplay
// .getOrigin().getX()) / navigatorImageDisplay.getScale(), (navigatorImageDisplay.getHeight() / 2.0 + navigatorImageDisplay
// .getOrigin().getY()) / navigatorImageDisplay.getScale());
Point2D.Double cCenter = new Point2D.Double(xc,yc);
//System.out.println("Ctrl - addImage "+xc+" "+yc);
String imName=null;
String imCatName="Local File";
if (visuPanel.getNewImageCatalog() != "Local File")
{
imCatName=visuPanel.getNewImageCatalog();
visuPanel.setNewImageCatalog(imCatName);
if (visuPanel.getNewImageName() != null)
{
imName=visuPanel.getNewImageName();
visuPanel.setNewImageName(null);
}
}
else
{
imName=file.getName();
}
Image image = fov.addImage(fitsImage, cCenter, navigatorImageDisplay.getScale(), imName, imCatName );
/**
* Re-init imagaName and image Catalog
*/
imName=null;
imCatName="Local File";
/**
* Set the image center world coordinates if they are not filled.
*/
if ((image.getAlpha()==null) || (image.getDelta()==null))
{
WorldCoords imCenterHMS=visuPanel.getWorldCoords(cCenter.getX(),cCenter.getY());
image.setAlpha(imCenterHMS.getRA());
image.setDelta(imCenterHMS.getDec());
image.setAlpha(imCenterHMS.getRaDeg());
image.setDelta(imCenterHMS.getDecDeg());
}
/**
* Read the hight and low cuts levels, colors and algorithm
*/
final ImageProcessor imageProcessor = navigatorImageDisplay.getImageProcessor();
image.setCutsLevels(imageProcessor.getLowCut(),imageProcessor.getHighCut());
image.setImageColors(imageProcessor.getColorLookupTableName(),
imageProcessor.getIntensityLookupTableName(),
imageProcessor.getScaleAlgorithm());
image.setAbsPath(pathOfFitsFile);
int pixHgt = fitsImage.getRealHeight(); // Image Height in pixel
double degImage = wcs.getHeightInDeg(); // Image Height in deg
double resol = (degImage * 3600.0) / pixHgt;
navigatorImageDisplay.setState(1);
image.setResol(resol);
image.addObserver(visuPanel);
image.addObserver(selectPanel);
image.addObserver(mainFrame.getTableurPanel());
getMainFrame().setStatusBarText(Color.blue,
"INFO : File " + file.getAbsolutePath() + " loaded !");
actionControl.setSaveNeeded(true);
fov.update();
}
}
/**
* @param fitsImage
* @return
*/
public WCSTransform checkFitsImage(final FITSImage fitsImage) {
boolean ok = true;
WCSTransform wcs;
try {
wcs = new WCSTransform(new FITSKeywordProvider(fitsImage));
} catch (IllegalArgumentException e) {
wcs = null;
ok = false;
}
if (ok && !wcs.isWCS()) {
new CustomJDialog("Image doesn't have WCS !", CustomJDialog._ERROR_,
CustomJDialog._CLOSE_BUTTON_, getMainFrame());
wcs = null;
ok = false;
}
if (ok && fitsImage.getHeader().getStringValue("OBJECT") == null
&& fitsImage.getHeader().getStringValue("POSITNID") == null) {
new CustomJDialog("Image doesn't have the keyword OBJECT !", CustomJDialog._ERROR_,
CustomJDialog._CLOSE_BUTTON_, getMainFrame());
wcs = null;
ok = false;
}
return wcs;
}
/**
* Called with the menu options to update display.
*/
public void displayAction() {
visuPanel.updateByHand();
}
/**
*
*/
public void removeImage() {
int currentImage = fov.getSelectedImage();
getMainFrame().setStatusBarText("");
if (currentImage >= 0)
{
fov.removeImage(currentImage);
int imNb = fov.getNbrImages();
if (imNb > 0)
{
visuPanel.getNavigatorImageDisplay().setState(2); // image used
actionControl.setSaveNeeded(true);
if (currentImage == 0)
currentImage = 1;
//updateCutColorsAndSetImage(currentImage-1);
}
else
{
visuPanel.getImageCutDialog().setDisplay(null);
visuPanel.getNavigatorImageDisplay().setState(0); // empty
visuPanel.updateByHand();
actionControl.setSaveNeeded(false);
}
fov.update();
}
else
{
new CustomJDialog("There is no current image !", CustomJDialog._ERROR_,
CustomJDialog._CLOSE_BUTTON_, visuPanel);
}
}
/**
*
*/
public void editColorsImage() {
getMainFrame().setStatusBarText("");
visuPanel.getImageColorsDialog().setDisplay(fov.getCurrentImage());
}
public void editCutImage() {
getMainFrame().setStatusBarText("");
visuPanel.getImageCutDialog().setDisplay(fov.getCurrentImage());
}
/**
* @param val
*/
public void updateCutColorsAndSetImage(int val) {
visuPanel.updateCutLevels(val);
visuPanel.updateColors(val);
visuPanel.updateOfImageNavigatorDisplay();
fov.setSelectedImage(val);
fov.getCurrentImage().setSelectedMask(-1);
visuPanel.updateByHand();
}
/**
*
*/
public void addObjectcsFromCat() {
getMainFrame().setStatusBarText("");
OSPE_NavigatorFrame navigatorFrame = new OSPE_NavigatorFrame(getMainFrame(), visuPanel);
navigatorFrame.setTypeNavigator(TypeNavigator.OBJECT);
actionControl.setSaveNeeded(true);
fov.update();
}
public void loadImageFromServerAction() {
OSPE_NavigatorFrame navigatorFrame = new OSPE_NavigatorFrame(getMainFrame(), visuPanel);
navigatorFrame.setTypeNavigator(TypeNavigator.IMAGE);
visuPanel.getNavigatorImageDisplay().setState(1); // image loaded - Ben non pas encore !
getMainFrame().setStatusBarText("");
actionControl.setSaveNeeded(true);
fov.setSelectedImage(-1);
fov.update();
}
public void removeAllObjects() {
getMainFrame().setStatusBarText("");
if (fov.getSelectedImage() >= 0)
{
getMainFrame().setStatusBarText(Color.blue, "INFO: All objects are removed !");
fov.getCurrentImage().removeAllObjects();
setNbLoadCat(-1);
mainFrame.updateButtons();
fov.update();
actionControl.setSaveNeeded(true);
} else {
new CustomJDialog("There is no selected image !", CustomJDialog._ERROR_,
CustomJDialog._CLOSE_BUTTON_, getMainFrame());
// setStatusBarText(Color.red,
// "WARNING: There is no selected image !");
}
}
/**
*
*/
public void recalculateCentroid() {
getMainFrame().setStatusBarText("");
if (fov.getSelectedImage() >= 0) {
getMainFrame().setStatusBarText(Color.blue, "INFO: All centroids have been recalculated !");
for (Iterator itr = fov.getCurrentImage().getListObjects().iterator(); itr
.hasNext();) {
visuPanel.recalculateCentroid(itr.next());
}
fov.update();
actionControl.setSaveNeeded(true);
} else {
new CustomJDialog("There is no selected image !", CustomJDialog._ERROR_,
CustomJDialog._CLOSE_BUTTON_, getMainFrame());
// mainFrame.setStatusBarText(Color.red,
// "WARNING: There is no selected image !");
}
}
/**
*
*/
public void revertCentroid() {
getMainFrame().setStatusBarText("");
if (fov.getSelectedImage() >= 0) {
getMainFrame().setStatusBarText(Color.blue,
"INFO: All centroids have been reverted to their original value !");
for (Iterator itr = fov.getCurrentImage().getListObjects().iterator(); itr
.hasNext();) {
itr.next().setPosPixRecalc(null);
}
fov.update();
actionControl.setSaveNeeded(true);
} else {
new CustomJDialog("There is no selected image !", CustomJDialog._ERROR_,
CustomJDialog._CLOSE_BUTTON_, getMainFrame());
// mainFrame.setStatusBarText(Color.red,
// "WARNING: There is no selected image !");
}
}
/**
* Save the project in file to choose
*/
public void saveAsProject() {
File file = null;
getMainFrame().setStatusBarText(" ");
JFileChooser fileChooser = new JFileChooser(fov.getPath());
fileChooser.setDialogTitle("Save Project File");
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("xml");
fileChooser.setFileFilter(filter);
String fileName = "OSPE_project.xml";
int returnVal = fileChooser.showDialog(getMainFrame(), "Save as");
if (returnVal == JFileChooser.APPROVE_OPTION)
{
file = fileChooser.getSelectedFile();
if (file != null)
{
fileName = file.getName();
if (fileName.endsWith(".xml"))
{
fileName = file.getAbsolutePath();
fov.setFileName(file.getName());
}
else
{
fileName = file.getAbsolutePath() + ".xml";
fov.setFileName(file.getName() + ".xml");
}
}
}
writer.write(fileName);
actionControl.setSaveNeeded(false);
projectCompletePath = fov.getCompletePath();
getMainFrame().setTitle("OSP EDITOR - " + projectCompletePath);
getMainFrame().setStatusBarText(Color.blue, "INFO: Project saved");
fov.update();
}
/**
*
*/
public HelpBroker createHb() {
if(hb == null){
// creation des objets HelpSet et HelpBroker
HelpSet hs = getHelpSet("doc/OSPE_help.hs");
hb = hs.createHelpBroker();
hb.enableHelpKey(getMainFrame(), "overview", hs); // for F1 key
}
return hb;
}
public HelpSet getHelpSet(String helpsetfile) {
HelpSet hs = null;
ClassLoader cl = this.getClass().getClassLoader();
getMainFrame().setStatusBarText("");
try {
URL hsURL = HelpSet.findHelpSet(cl, helpsetfile);
hs = new HelpSet(null, hsURL);
} catch (Exception ee) {
// System.out.println("HelpSet: "+ee.getMessage());
// System.out.println("HelpSet: "+ helpsetfile + " not found");
}
return hs;
}
/**
* Load a mask on the selected image
*/
public void loadMask() {
File file = null;
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Load an Existing Mask");
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("xml");
fileChooser.setFileFilter(filter);
int returnVal = fileChooser.showDialog(getMainFrame(), "Load");
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile();
if (file != null) {
int err = reader.readMask(file.toString());
//if (err == 0) {
if (err <= 0) {
if (err==-1)
{
String mess = "The file you try to read is not a mask file.";
CustomJDialog dialog = new CustomJDialog(mess, CustomJDialog._WARNING_,
CustomJDialog._CLOSE_BUTTON_, getMainFrame());
dialog.getValue();
}
return;
}
/*else if (err < 0) {
fov.getCurrentImage().removeMask(fov.getCurrentImage().getSelectedMask());
fov.getCurrentImage().update();
return;
}*/
int numIm = 0;
if (fov.getCurrentImage().getId() >0)
numIm= fov.getCurrentImage().getId();
String fileName = numIm+"_"+file.toString().substring(
file.toString().lastIndexOf(File.separatorChar) + 1);
fov.getCurrentImage().getCurrentMask().setName(fileName);
actionControl.setSaveNeeded(true);
fov.update();
}
}
}
/*
* To add a mask in image center
*/
public void addMask() {
Point2D.Double point = new Point2D.Double();
point.setLocation(fov.getCurrentImage().getCenter());
addMask(point);
}
/**
* To add mask on project at the defined point (user coord) given by the display image center
*/
public void addMask(Point2D.Double point) {
// System.out.println("DEBUG: MainFrame::addMask.");
// A image is needed before add a mask
if (fov.getSelectedImage() < 0) {
getMainFrame().setStatusBarText(Color.red, "WARNING: Add an image first !");
return;
}
// And maybe objects too.
if (fov.getCurrentImage().getNbrObjects() == 0)
{
CustomJDialog dialog = new CustomJDialog(
"You don't have objects defined. Do you want to add a new mask ?",
CustomJDialog._WARNING_, CustomJDialog._YESNO_BUTTON_, visuPanel);
if (dialog.getValue() == JOptionPane.NO_OPTION)
{
return;
}
}
// Set the mask center with the center of the image portion displayed.
Point2D.Double maskCenterDef = new Point2D.Double(point.x,point.y);
maskCenterDef.setLocation(maskCenterDef);
fov.getCurrentImage().addMask(maskCenterDef);
Mask mask = fov.getCurrentImage().getCurrentMask();
// WorldCoords wc = new WorldCoords();
// wc = visuPanel.getWorldCoords(mask.getCenter().getX(), mask.getCenter().getY());
Point2D.Double ptM=maskCenterDef;
visuPanel.getIcc().userToWorldCoords(ptM, false);
WorldCoords wc = new WorldCoords(ptM.x,ptM.y);
//System.out.print("ctrl add Mk "+point.x+","+point.y+" "+wc.getRA()+" "+wc.getDec());//+" ptM "+w1.getRA()+" "+w1.getDec());
// mask.setAlpha(wc.getRA());
// mask.setDelta(wc.getDec());
mask.setCenterWc(wc.getRaDeg(), wc.getDecDeg());
mask.init2Display();
maskCenterDef = new Point2D.Double();
maskCenterDef.x = wc.getRaDeg();
maskCenterDef.y = wc.getDecDeg();
mask.setCenterDeg(maskCenterDef);
// Add left top corner coordinate
double width = mask.getWidth2Display();
double height = mask.getHeight2Display();
//System.out.println("ctrl add Mk Cnt X "+mask.getCenter().x+" "+mask.getCenter().y);
double maskDebutX = mask.getCenter().x - (width / 2.0);
double maskDebutY = mask.getCenter().y + (height / 2.0);
//System.out.println(" MLx "+maskDebutX+" MLy "+maskDebutY);
Point2D.Double tmpCorner = new Point2D.Double();
tmpCorner.setLocation(maskDebutX, maskDebutY);
mask.setLeftCornerPix(tmpCorner);
ptM=tmpCorner;
visuPanel.getIcc().userToWorldCoords(ptM, false);
wc = visuPanel.getWorldCoords(tmpCorner.getX(), tmpCorner.getY());
mask.setLeftCornerWc(wc.getRaDeg(), wc.getDecDeg());
//System.out.print("ctrl add Mk LeftCorn "+wc.getRA()+" "+wc.getDec());
// mask.setAlphaCorner(wc.getRA());
// mask.setDeltaCorner(wc.getDec());
Point2D.Double tmpC = new Point2D.Double();
tmpC.x = wc.getRaDeg();
tmpC.y = wc.getDecDeg();
mask.setLeftCornerDeg(tmpC);
// mess="DEBUG MainFrame::AddMask - centerDeg "+tmp.x+":"+tmp.y;
// System.out.println(mess);
mask.addObserver(this);
mask.addObserver(getMainFrame().getSelectPanel());
mask.addObserver(visuPanel);
mask.getCurrentSlit().addObserver(visuPanel);
//mask.update();
if (!fov.getCurrentImage().searching) {
visuPanel.slitCombo.setSelectedIndex(0);
visuPanel.updateSlides();
}
mask.update();
actionControl.setSaveNeeded(true);
}
/**
*
*/
public void projectExit() {
getMainFrame().setStatusBarText("");
/**
* Test if the image cuts levels were changed when close the project
* to save it. And flag re-init
*/
// If there is an image in the project
if (visuPanel.getNavigatorImageDisplay().getState()== 2){
Image img = fov.getCurrentImage();
if (img.cutsChanged) {
actionControl.setSaveNeeded(true);
img.cutsChanged = false;
}
}
int choice = askToSaveIfNeeded();
if (choice == JOptionPane.CANCEL_OPTION)
return;
if (choice == -1) {
String mess = "Are you sure you want to exit the program ?";
CustomJDialog dialog = new CustomJDialog(mess, CustomJDialog._QUESTION_,
CustomJDialog._YESNO_BUTTON_, getMainFrame());
if (dialog.getValue() == JOptionPane.NO_OPTION) {
return;
}
}
getMainFrame().setVisible(false);
getMainFrame().dispose();
System.exit(0);
}
/**
*
*/
public void displaySettings() {
getMainFrame().setStatusBarText("");
settingsPanel.setVisible(true);
}
/**
*
*/
@Override
public void stop() {
if (fov.getCurrentImage().getSelectedMask() >= 0) {
if (maskControl.getmMaskFlag() == 1) {
// Mask moving
Mask m = fov.getCurrentImage().getCurrentMask();
getMainFrame().setStatusBarText(Color.blue, "INFO: Stop Mask moving.");
fov.getCurrentImage().getCurrentMask().moveEnable = false;
toolBar.moveMask(false);
menuBar.jMenuMaskMove.setSelected(false);
visuPanel.updateMaskCoords(m);
} else if (maskControl.getmMaskFlag() == 2) {
// Mask turning
getMainFrame().setStatusBarText(Color.blue, "INFO: Stop Mask turning.");
fov.getCurrentImage().getCurrentMask().turnEnable = false;
toolBar.turnMask(false);
menuBar.jMenuMaskTurn.setSelected(false);
}
maskControl.setmMaskFlag(0);
fov.getCurrentImage().getCurrentMask().update();
} else {
toolBar.enableButtonDuringMaskEdition(false);
menuBar.jMenuMaskTurn.setSelected(false);
menuBar.jMenuMaskMove.setSelected(false);
}
}
/**
*
*/
@Override
public void turnMask() {
if (fov.getCurrentImage().getSelectedMask() >= 0)
{
//System.out.println("Control TurnMask- turnEnable "+fov.getCurrentImage().getCurrentMask().turnEnable);
if (!fov.getCurrentImage().getCurrentMask().turnEnable)
{
//System.out.println("Control : turnEnable false mais je vais bouger");
maskControl.setmMaskFlag(2);
getMainFrame().setStatusBarText(Color.blue, "INFO: Mask - Turn ");
fov.getCurrentImage().getCurrentMask().turnEnable = true;
toolBar.turnMask(true);
menuBar.jMenuMaskTurn.setSelected(true);
fov.getCurrentImage().getCurrentMask().moveEnable = false;
menuBar.jMenuMaskMove.setSelected(false);
actionControl.setSaveNeeded(true);
}
else
{
//System.out.println("Control : turnEnable true et pas bouger");
fov.getCurrentImage().getCurrentMask().turnEnable = false;
toolBar.turnMask(false);
menuBar.jMenuMaskTurn.setSelected(false);
}
fov.getCurrentImage().getCurrentMask().update();
}
else
{
toolBar.getToggleButtonTurn().setSelected(false);
menuBar.jMenuMaskTurn.setSelected(false);
}
}
public void helpAbout() {
OSPE_AboutBoxPanel dlg = new OSPE_AboutBoxPanel();
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getMainFrame().getContentPanel().getSize();
Point loc = getMainFrame().getContentPanel().getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
(frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.pack();
dlg.setVisible(true);
}
/**
* @param popMess
*
*/
public void removeMask() {
int currentMask = fov.getCurrentImage().getSelectedMask();
if (currentMask >= 0)
{
// System.out.println("id mask " + currentMask);
fov.getCurrentImage().removeMask(currentMask);
getMainFrame().setStatusBarText(Color.blue, "INFO: Current mask is removed !");
actionControl.setSaveNeeded(true);
// if ( fov.getCurrentImage().getNbrMasks() == 0 )
// {
// jMenuSlitJoin.setEnabled(false);
// jMenuSlitSplit.setEnabled(false);
//
// rightPanel.searchButton.setEnabled(false);
// rightPanel.validateButton.setEnabled(false);
// rightPanel.generateButton.setEnabled(false);
// }
// else
// {
// fov.getCurrentImage().setSelectedMask(0);
// }
fov.getCurrentImage().update();
fov.update();
}
else
{
new CustomJDialog("There is no current mask !", CustomJDialog._ERROR_,
CustomJDialog._CLOSE_BUTTON_, getMainFrame());
// mainFrame.setStatusBarText(Color.red,
// "WARNING: There is no current mask !");
}
}
/**
* Slpit joined slits
*
*/
public void splitSlit() {
Mask currentMask = fov.getCurrentImage().getCurrentMask();
int firstSlit = currentMask.getFirstAssociatedSlit();
int lastSlit = (firstSlit >= 0) ? currentMask.getAssociatedMaxSlit(firstSlit) : Mask
.get_NBRSLITS_() - 1;
if (firstSlit < 0)
firstSlit = 0;
javax.swing.JDialog splitSlitsPanel = new OSPE_QuickSplitSlitsPanel(currentMask,
firstSlit, lastSlit);
splitSlitsPanel.setLocationRelativeTo(visuPanel);
actionControl.setSaveNeeded(true);
splitSlitsPanel.addWindowListener(new ConflictsPreventer(visuPanel, selectPanel, menuBar, toolBar));
}
/**
*
*/
public void resetSelectedSlit() {
String mess = "Are you sure you want to reset this slit position ?";
CustomJDialog dialog = new CustomJDialog(mess, CustomJDialog._WARNING_,
CustomJDialog._YESNO_BUTTON_, getMainFrame());
if (dialog.getValue() == JOptionPane.YES_OPTION) {
Mask curMsk = fov.getCurrentImage().getCurrentMask();
int idSelectedSlit = fov.getCurrentImage().getCurrentMask().getSelectedSlit();
Slit sl=curMsk.getSlit(idSelectedSlit);
if (sl.isJoined())
{
/** Get the min slit id and the max of the joined slits */
int mnjSlit=curMsk.getAssociatedMinSlit(sl.getId());
if (mnjSlit==-1) mnjSlit=sl.getId();
int mxjSlit=curMsk.getAssociatedMaxSlit(sl.getId());
if (mxjSlit==-1) mxjSlit=sl.getId();
curMsk.splitSlits(mnjSlit,mxjSlit);
// visuPanel.updateByHand();
for (int i=mnjSlit; i= 0 )
{
sl.setAffectedObject(-1);
}
}
visuPanel.updateByHand();
//visuPanel.updateByHand("Ctrl reset Slit");
actionControl.setSaveNeeded(true);
}
}
/**
*
*/
public void pickObjects() {
getMainFrame().setStatusBarText("");
visuPanel.setPickObjectFrame();
actionControl.setSaveNeeded(true);
}
public void displayPlotNoneSpectra(){
fov.setPlotSpectra(FieldOfView._PLOTNONE_);
fov.getCurrentMaskOfCurrentImage();
int nbSlits = Mask.get_NBRSLITS_();
for (int i = 0; i < nbSlits ; i++)
{
fov.getCurrentMaskOfCurrentImage().setPlotSpectrum(i, false);
}
menuBar.jMenuDisplayPlotNoneSpectra.setSelected(true);
menuBar.jMenuDisplayPlotSelectedSpectra.setSelected(false);
menuBar.jMenuDisplayPlotAllSpectra.setSelected(false);
visuPanel.eraseMask();
visuPanel.updateByHand();
//visuPanel.updateByHand("Ctrl disp No spect");
}
public void displayPlotSelectedSpectra(){
fov.setPlotSpectra(FieldOfView._PLOTSELECTED_);
int idS=fov.getCurrentMaskOfCurrentImage().getCurrentSlit().getId();
fov.getCurrentMaskOfCurrentImage().setPlotSpectrum(idS, true);
menuBar.jMenuDisplayPlotNoneSpectra.setSelected(false);
menuBar.jMenuDisplayPlotSelectedSpectra.setSelected(true);
menuBar.jMenuDisplayPlotAllSpectra.setSelected(false);
visuPanel.eraseMask();
visuPanel.updateByHand();
//visuPanel.updateByHand("Ctrl displ spect");
}
public void displayPlotAllSPectra(){
fov.setPlotSpectra(FieldOfView._PLOTALL_);
menuBar.jMenuDisplayPlotNoneSpectra.setSelected(false);
menuBar.jMenuDisplayPlotSelectedSpectra.setSelected(false);
menuBar.jMenuDisplayPlotAllSpectra.setSelected(true);
visuPanel.eraseMask();
visuPanel.updateByHand();
//visuPanel.updateByHand("Ctrl Displ all Spect");
}
/**
* @param src
*/
public void displayPlotSpectraRange(boolean plot) {
if (fov.getCurrentImage().getSelectedMask() >= 0) {
OSPE_SelectSpectraRangePanel panel = new OSPE_SelectSpectraRangePanel(fov
.getCurrentImage().getCurrentMask(), plot);
panel.setLocationRelativeTo(visuPanel);
panel.addWindowListener(new ConflictsPreventer(visuPanel, selectPanel, menuBar, toolBar));
panel.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
displayPlotSelectedSpectra();
}
});
} else {
String mess = "There is no mask displayed!";
new CustomJDialog(mess, CustomJDialog._ERROR_, CustomJDialog._CLOSE_BUTTON_,
getMainFrame());
}
visuPanel.eraseMask();
visuPanel.updateByHand();
//visuPanel.updateByHand("Ctrl Displ Range spec");
}
/**
*
*/
public void displayUnplotSpectra() {
for (int i = 0; i < Mask._NBRSLITS_; i++){
fov.getCurrentImage().getCurrentMask().setPlotSpectrum(i, false);
}
displayPlotNoneSpectra();
visuPanel.eraseMask();
visuPanel.updateByHand();
//visuPanel.updateByHand("Ctrl unplot spec");
}
//
// public CSH.DisplayHelpFromSource getDisplayHelpAction() {
// if(displayHelpFromSource == null){
// final CSH.DisplayHelpFromSource displayHelpFromSource2 = getDisplayHelpAction();
// displayHelpFromSource = displayHelpFromSource2;
// }
//
// return displayHelpFromSource;
// }
/**
* @return
*/
@Override
public CSH.DisplayHelpFromSource getDisplayHelpAction() {
if(displayHelpFromSource == null){
displayHelpFromSource = new CSH.DisplayHelpFromSource(createHb());
}
return displayHelpFromSource;
}
private int askToSaveIfNeeded() {
if (actionControl.isSaveNeeded()) {
String mess = "All changes made to this project will be lost.\r\nDo you want to save it before quitting? ";
CustomJDialog dialog = new CustomJDialog(mess, CustomJDialog._WARNING_,
CustomJDialog._YESNOCANCEL_BUTTON_, getMainFrame());
if (dialog.getValue() == JOptionPane.YES_OPTION) {
projectSave();
}
actionControl.setSaveNeeded(false);
return dialog.getValue();
}
return -1;
}
/**
* Set the default running directory
*/
public void setLastDir(String newDir)
{
lastDirectory=newDir;
//System.out.println("crtl - set last dir "+newDir);
}
/**
* Get the default running directory
*/
public String getLastDir()
{
return lastDirectory;
}
/**
* Undo the last operation made on the mask
*/
@Override
public void undo() {
if (fov.getCurrentImage().getSelectedMask() >= 0) {
MaskHistory history = fov.getCurrentImage().getCurrentMask().getHistory();
getMainFrame().setStatusBarText(Color.blue, "INFO: " + history.getLastEntry().getName()
+ " undone !");
history.undo();
if (fov.getCurrentMaskOfCurrentImage().getHistory().isEmpty())
actionControl.setSaveNeeded(false);
else
actionControl.setSaveNeeded(true);
fov.update();
visuPanel.updateSlides();
} else {
new CustomJDialog("There is no current mask !", CustomJDialog._ERROR_,
CustomJDialog._CLOSE_BUTTON_, getMainFrame());
// mainFrame.setStatusBarText(Color.red,
// "WARNING: There is no current mask !");
}
}
/**
* Update the menu's options following the user's choices
*
* @param O
* : Observable
* @param arg
* : Object
*/
@Override
public void update(Observable o, Object arg) {
if (Mask.ADD_ENTRY_IN_HISTORY.equals(arg)){
actionControl.setSaveNeeded(true);
} else {
getMainFrame().updateButtons();
if (actionControl.isSaveNeeded() && !getMainFrame().getTitle().endsWith("*")) {
//getMainFrame().setTitle(getMainFrame().getTitle() + " *");
getMainFrame().setTitle(projectCompletePath+ " *");
}
}
}
/*
* Access to maskControl :
* flags positions if the mask is turning or moving or not.
*/
public OspeMaskControl getMaskControl() {
return maskControl;
}
/* Access to actionControl :
* Flag position to ask of saving the project
* if modifications.
*/
public OspeActionControl getActionControl() {
return actionControl;
}
/*
* Access to MainFrame.
*/
public OSPE_MainFrame getMainFrame() {
return mainFrame;
}
/*
* Access to VisuPanel.
*/
public OSPE_VisuPanel getVisuPanel() {
return visuPanel;
}
/**
* Load objects from local catalog
*/
public void addObjectsLocalCat()
{
boolean noError=false;
// TODO Auto-generated method stub
JFileChooser choose = new JFileChooser();
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("txt");
filter.addExtension("ascii");
filter.addExtension("dat");
filter.setDescription("Objects text files");
choose.setFileFilter(filter);
choose.setCurrentDirectory(new File(fov.getPath()));
int returnVal = choose.showOpenDialog(visuPanel);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
try
{
File file = choose.getSelectedFile();
OSPE_LoadLocalObjectsCat catScan=new OSPE_LoadLocalObjectsCat(mainFrame,fov);
catScan.loadLocalObjectsCat(file);
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
public void saveAsLocalCat()
{
File ff = null;
getMainFrame().setStatusBarText(" ");
JFileChooser fileChooser = new JFileChooser(fov.getPath());
fileChooser.setDialogTitle("Choose file to save Objects : ");
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("dat");
filter.addExtension("txt");
filter.addExtension("ascii");
fileChooser.setFileFilter(filter);
String fileName = "localCat.dat";
int returnVal = fileChooser.showDialog(getMainFrame(), "Save");
if (returnVal == JFileChooser.APPROVE_OPTION)
{
ff = fileChooser.getSelectedFile();
if (ff != null)
{
fileName = ff.getName();
if ((fileName.endsWith(".dat")) || (fileName.endsWith(".txt")) || (fileName.endsWith(".ascii")))
{
fileName = ff.getAbsolutePath();
}
else
{
fileName = ff.getAbsolutePath() + ".dat";
}
}
boolean noError = writer.writeLocalCatalog(fov.getCurrentImage(),fileName);
if (noError)
{
getMainFrame().setStatusBarText(Color.blue, "INFO: Local catalog created");
}
}
}
/** Get instruments parameters
* @param projectCompletePath
*
*/
public void getInstrumentParameters(String projectPath) {
// System.out.println("Ctrl- GetInstParam "+projectPath);
if (instrumentParams == null) {
instrumentParams = new InstrumentParameters(projectPath);
}
}
@SuppressWarnings("null")
public String[][] getSlitsdetector(Mask currentMask) {
// TODO Auto-generated method stub
String [][] tab=new String [55][14];
double [][] tabCsuCal;
double offset=InstrumentParameters._OFFSET_X;
double pxsz=InstrumentParameters._PXSZ;
double pltsc=InstrumentParameters._PLTSC;
double safe_param=InstrumentParameters._SAFE_PARAMETER;
int affected=0;
tabCsuCal=instrumentParams.getTabX();
int lg=instrumentParams._nbLiFi2;
/**
* Slit charact
*/
Slit currentSlit;
int idSlit=0, idSlit2=0;
double slitAper=0.0;
double slitPos=0.0;
boolean isJoined=false;
int joinedRefId=0;
String joinedInfo="0";
boolean isRef=false;
double xr_left,xr_right,yr_left,yr_right,xr_slit, yr_slit;
double xv_left,xv_right,xv_slit,yv_left,yv_right,yv_slit;
double csup_left,csup_right;
double objVirtPosX, objVirtPosY;
double xv=0.0;
double xvs=0.0;
double yv=0.0;
double defValue=100.000;
Point2D.Double objPosition;
int max = currentMask._NBRSLITS_;
for (int i=0; i-1)
{
affected=1;
SkyObject obj=fov.getCurrentImage().getObject(idObj);
if (obj.isRef()) affected=2;
objPosition= calculObjectStdPosition(currentMask, obj);
/** Get virtual coord with instrument params */
objVirtPosX=1024.5 + objPosition.getX() / pltsc / pxsz;
objVirtPosY=1024.5 - objPosition.getY() / pltsc / pxsz;
xv=objVirtPosX;
yv=objVirtPosY;
xv_slit=objVirtPosX;
//System.out.println(" crtl getSlitDet obj xv_slit "+xv_slit);
}
else
{
// Slit position calculation (virtual ansd CSU positions)
xv_slit=1024.5 + slitPos / pltsc / pxsz;
}
if (isJoined)
{
Slit jSlit=currentMask.getSlit(currentSlit.getJoinedRefSlit());
int idObjSlit = jSlit.getAffectedObject();
if (idObjSlit > -1)
{
SkyObject obj=fov.getCurrentImage().getObject(idObjSlit);
isRef=obj.isRef();
if (isRef) joinedInfo="2";
else joinedInfo="1";
}
}
if (slitAper > 0.0)
{
xvs=slitAper/pltsc/pxsz/2;
xv_left = xv_slit - xvs;
xv_right = xv_slit + xvs;
}
else
{
slitAper=safe_param;
xv_left = xv_slit - slitAper/2/pltsc/pxsz-offset;
xv_right = xv_slit + slitAper/2/pltsc/pxsz+offset;
}
yv_left=instrumentParams.getTabY(0, idSlit);
yv_right=instrumentParams.getTabY(0, idSlit2);
/** CSU calculation - always with slit position */
csup_left=getValFromCalibArray(tabCsuCal, idSlit, slitAper, xv_left, lg);
csup_right=getValFromCalibArray(tabCsuCal, idSlit2, slitAper, xv_right, lg);
//System.out.println("getSlitDetc xvl "+xv_left+" xvr "+xv_right+"csul "+csup_left+" csur "+csup_right+" ap "+slitAper+" xvS "+xv_slit);
/** real position on detector - with object position */
if (affected>0)
{
double [] arrayCoord=calRealDetectorVal(xv,yv);
xv_left=objVirtPosX;
yv_left=objVirtPosY;
xv_right=objVirtPosX;
yv_right=objVirtPosY;
xr_left=arrayCoord[0];
yr_left=arrayCoord[1];
xr_right=arrayCoord[0];
yr_right=arrayCoord[1];
}
else
{
xv_left=-100.00;
yv_left=-100.00;
xr_left=-100.00;
yr_left=-100.00;
xv_right=-100.00;
yv_right=-100.00;
xr_right=-100.00;
yr_right=-100.00;
}
DecimalFormat nf;
nf = new DecimalFormat("000.000");
nf.setRoundingMode(RoundingMode.HALF_EVEN);
tab[i][0]=String.valueOf((int)idSlit);
tab[i][1]= nf.format(csup_left);
tab[i][2]=String.valueOf((int)idSlit2);
tab[i][3]=nf.format(csup_right);
if (isRef) tab[i][4]=String.valueOf((int)2);
else if (isJoined) tab[i][4]=joinedInfo;
else tab[i][4]=String.valueOf((int)affected);
tab[i][5]=nf.format(xv_left);
tab[i][6]=nf.format(yv_left);
tab[i][7]=nf.format(xv_right);
tab[i][8]=nf.format(yv_right);
tab[i][9]=nf.format(xr_left);
tab[i][10]=nf.format(yr_left);
tab[i][11]=nf.format(xr_right);
tab[i][12]=nf.format(yr_right);
int v=joinedRefId+1;
tab[i][13]=String.valueOf((int)v);
isRef=false;
}
return tab;
}
private double [] calRealDetectorVal(double xv, double yv)
{
double [] tab = {0.0,0.0};
double cf=0.1944*Math.PI/(180*3600);
double ra=Math.sqrt((Math.pow((xv-1024.5),2.0)+Math.pow((yv-1024.5),2.0)));
double r=cf*ra;
double rr1=1+14606.7*(Math.pow(r,2.0))+1739716115.1*(Math.pow(r,4.0));
double thet=Math.atan2((xv-1024.5),(yv-1024.5));
tab[0]=(rr1*ra*Math.sin(thet))+1024.5;
tab[1]=(rr1*ra*Math.cos(thet))+1024.5;
return tab;
}
/**
* Get the val from calibration file which is closest from virtual value.
* @param tabCal2
* @param idSlit
* @param xv
* @param lg
* @return array index of the value. Needed to read the csu corresponding value
*/
private double getValFromCalibArray(double[][] tabCal2, int idSlit, double aper, double xv, int lg)
{
//System.out.println("ctrl getValCal: slit "+idSlit+" xv "+xv);
double val=0.0;
double [] tabValX= new double [lg];
for (int k=0; kv3)) {idx1=idx_l;idx2=idx; }
if ((xv>v1) && (xvv1) && (xvv2)) {idx1=idx_s;idx2=idx; }
}
v1=tabCal2[idSlit][idx1]; // Lowest value
v2=tabCal2[idSlit][idx2]; // biggest value
y1=tabCal2[0][idx1];
y2=tabCal2[0][idx2];
double v=0.0;
if (v1==v2) val=v1;
// if (v1==v2) val=tabCal2[0][idx1];
else
{
// double m = (v1 - v2) / (y1 - y2);
// val = (xv - v2) * m + y2;
v=v2-v1;
val=y1+(((xv-v1)/(v))*(y2-y1));
//System.out.println("slit "+idSlit+" idx1 "+ idx1+" v1 "+v1+" idx2 "+idx2+" v2 "+v2+" xv "+ xv+" y1 "+y1+" y2 "+y2+ " val "+val);
}
return val;
}
// /** Use the visu one.
// * Calculation Slit center Sky position
// * @param currentMask : current mask
// * @param posY : slit position on mask
// * @param idSlit : slit id
// */
// private Point2D.Double calculSlitCenterSkyPosition(Mask currentMask, double posY,
// int idSlit) {
// Point2D.Double p2=new Point2D.Double(0.0,0.0);
// double raDeg = currentMask.getCenterDeg().getX();
// double decDeg= currentMask.getCenterDeg().getY();
// double raMk=raDeg*Math.PI/180.0;
// double decMk=decDeg*Math.PI/180.0;
//
// p2.y=currentMask.getCenterDeg().getY()+(posY/3600.0);
//
// double om=currentMask.getOmega()*Math.PI/180.0;
// double fv=400.0;
// double hs=fv/currentMask._NBRSLITS_;
// double hsr=hs*Math.PI/180.0/3600.0;
//
// double nbslit=(double)27-(idSlit+1);
// double x0=(nbslit*hsr);
// double n0=posY*Math.PI/180.0/3600.0;
//
// double xi=x0*Math.cos(om)+n0*Math.sin(om);
// double ni=-x0*Math.sin(om)+n0*Math.cos(om);
//
// double rai=raMk+ Math.atan(xi/(Math.cos(decMk)-ni*Math.sin(raMk)));
// double dai=Math.asin((Math.sin(decMk)+ ni*Math.cos(decMk))/(Math.sqrt(1+Math.pow(ni, 2)+Math.pow(xi, 2) )));
// p2.x=rai;
// p2.y=dai;
// p2.x=rai*180/Math.PI;
// p2.y=dai*180/Math.PI;
//
// return p2;
// }
// /**
// * Joins the slits from the slit number n to the slit number m (inclusive).
// * The values (position and width) of the slit number k will be kept and set
// * to all the other associated slits.
// * @param control
// *
// * @param n : Number of the first slit to join.
// * @param m : Number of the last slit to join.
// * @param k : Number of the slit to keep values of.
// */
// public void joinSlits(int n, int m, int k) {
// /**
// * Table that defines which slits are associated. If the value is true for
// * two slits number, then the slits are associated.
// */
//
// boolean joining = true;
// Mask mk=fov.getCurrentMaskOfCurrentImage();
//
//
// for (int i = n; i <= m; i++) {
// for (int j = i + 1; j <= m; j++) {
// mk.associatedSlits[i][j] = true;
// mk.associatedSlits[j][i] = true;
// }
// Slit s=mk.getSlit(k);
// mk.getSlit(i).setPosition(s.getPosition());
// mk.getSlit(i).setAperture(s.getAperture());
// mk.getSlit(i).setPosXY(s.getPosX(), s.getPosY());
//
// Point2D.Double p=new Point2D.Double(s.getWcPosCenter().getRaDeg(),s.getWcPosCenter().getDecDeg());
// p=getVisuPanel().calculSlitCenterSkyPosition(mk, s.getPosition(), i);
//
// mk.getSlit(i).setWcPosCenter(p);
// mk.getSlit(i).fSkyCoord=true;
//System.out.println("ctrl joinSlit "+i+" P2 "+p.x+" "+p.y+" "+s.getWcPosCenter().getRA()+" "+s.getWcPosCenter().getDec());
//
// }
// mk.update();
// mk.setjoining(false);
// }
/**
* Calculation Object std position in radians
* @param currentMask : current mask
* @param sk : sky object
*/
public Point2D.Double calculObjectStdPosition(final Mask currentMask, SkyObject sk)
{
double q=0.0;
Point2D.Double p2=new Point2D.Double(0.0,0.0);
double raDeg = currentMask.getCenterDeg().getX();
double decDeg= currentMask.getCenterDeg().getY();
double raMk=raDeg*Math.PI/180.0;
double decMk=decDeg*Math.PI/180.0;
double om=currentMask.getOmega()*Math.PI/180.0;
double raObj=sk.getObjWorldCenter().getX()*Math.PI/180.0;
double decObj=sk.getObjWorldCenter().getY()*Math.PI/180.0;
q=Math.atan(Math.tan(decObj)/Math.cos(raObj-raMk));
double eta0=(Math.cos(q)*Math.tan(raObj-raMk))/(Math.cos(q-decMk));
double xai0=Math.tan(q-decMk);
double xi=xai0*Math.cos(om)+eta0*Math.sin(om);
double etai=-xai0*Math.sin(om)+eta0*Math.cos(om);
p2.y=etai*206265;
p2.x=xi*206265;
return p2;
}
/**
* Keep the nb of objects catalogs loaded in a project
*
* @return actual nb catalogs loaded -int nbLoadCat
*/
public int getNbLoadCat()
{
return nbLoadCat;
}
/**
* Set the nb of loaded objects catalog
* @param i
*/
public void setNbLoadCat(int i)
{
nbLoadCat=i;
}
/**
* Find the good omega of the mask if the image is not on the same orientation that the original image
*/
private double addGoodBaseOmegaByImage(Mask currentMask, WorldCoords wc)
{
// System.out.println("AddGoodBaseOmegaByImage");
int clockwise;
double res;
double width = currentMask.getWidth2Display();
double height = currentMask.getHeight2Display();
double maskDebutX = currentMask.getCenter().x - (width / 2.0);
double maskDebutY = currentMask.getCenter().y - (height / 2.0);
res = 0.0;
//WorldCoords wc = new WorldCoords(mask.getChild("maskCoordLeftCorner").getChild("ra").getValue(), mask.getChild("maskCoordLeftCorner").getChild("dec").getValue());
//System.out.println("Corner RA " + wc.getRA() + " DEC " + wc.getDec());
WorldCoords wc2 = new WorldCoords();
Tools tools = new Tools();
Point2D.Double pCenter = new Point2D.Double();
Point2D.Double pBaseLeftCorner = new Point2D.Double();
Point2D.Double pRealLeftCorner = new Point2D.Double();
wc = new WorldCoords(wc.getRA(),wc.getDec());
wc2 = getVisuPanel().getWorldCoords(maskDebutX, maskDebutY);
//System.out.println("Real Corner RA " + wc2.getRA() + " DEC " + wc2.getDec());
//System.out.println("End AddGoodBaseBaseOmegaByImage");
// return res;
pCenter.setLocation(currentMask.getCenterDeg());
pBaseLeftCorner.setLocation(wc.getRaDeg(), wc.getDecDeg());
pRealLeftCorner.setLocation(wc2.getRaDeg(), wc2.getDecDeg());
clockwise = tools.CounterClockWise(pCenter.getX(), pCenter.getY(),
pRealLeftCorner.getX(), pRealLeftCorner.getY(),
pBaseLeftCorner.getX(), pBaseLeftCorner.getY());
res = tools.AngleOfView(pCenter.getX(), pCenter.getY(),
pBaseLeftCorner.getX(), pBaseLeftCorner.getY(),
pRealLeftCorner.getX(),pRealLeftCorner.getY());
if (clockwise == 1)
return (res);
else if (clockwise == -1)
return (360 - res);
else
return (0);
}
public void setSaveNeed(boolean b)
{
actionControl.setSaveNeeded(b);
}
} // End Control.java