//
// ShowNCEPModel.java
//

/*

The software in this file is Copyright(C) 2017 by Tom Whittaker.
It is designed to be used with the VisAD system for interactive
analysis and visualization of numerical data.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License in file NOTICE for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

package visad.jmet;

import visad.java3d.*;

import visad.*;
import visad.util.*;
import visad.VisADException;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;
import javax.swing.event.*;

import java.io.*;

import visad.data.mcidas.*;

import java.rmi.RemoteException;

public class ShowNCEPModel
       extends JFrame implements ActionListener, ChangeListener,
       DisplayListener {

  private BaseMapAdapter baseMap;
  private ColorControl ccmap;
  private float[][] colorTable;
  private Container cf;
  private JSlider speedSlider;
  private JLabel speedSliderLabel;
  private int speedValue, frameValue;
  private int maxFrames;
  private JButton start_stop, snapButton, forward, backward, mapColor;
  private boolean isLooping;
  private ContourControl ci;
  private ProjectionControl pc;
  private AnimationControl ca;
  private double[] pcMatrix;
  private GraphicsModeControl gmc;
  private LocalDisplay display;
  private NetcdfGrids ng;
  private JPanel vdisplay;
  private RealType x,y,level,time_type,pres;
  private RealType Values, SfcValues;
  private boolean firstFile;
  private boolean gotSfcGrids, gotAloftGrids;
  private JLabel statLabel;
  private String cmd;
  private NCEPPanel[] ncp;
  private JTabbedPane tabby;
  private FieldImpl mapField;
  private ValueControl mapControl;
  private DataReference mapRef;
  private RealType enableMap;
  private ScalarMap mapMap, xAxis, yAxis;
  private JCheckBox showMap;
  private String directory;
  private String MapFile;

  private boolean isServer = false;
  private boolean isClient = false;
  private String clientHost = null;

  public static void main(String args[]) {

    int num=1;
    boolean srvr = false;
    boolean clnt = false;
    String fileName = null;
    String host = null;
    if (args != null && args.length > 0) {
      boolean killMe = false;
      boolean gotNum = false;
      for (int i = 0; i < args.length; i++) {
        if (args[i].charAt(0) == '-' && args[i].length() > 1) {
          switch (args[i].charAt(1)) {
          case 'c':
            if (srvr) {
              System.out.println("Cannot specify both '-c' and '-s'");
              killMe = true;
            }
            clnt = true;
            if (args[i].length() > 2) host = args[i].substring(2).trim();
            break;
          case 's':
            if (clnt) {
              System.out.println("Cannot specify both '-c' and '-s'");
              killMe = true;
            }
            srvr = true;
            break;
          default:
            System.out.println("Unknown argument \"" + args[i] + "\"");
            killMe = true;
          }
        } else {
          boolean triedNum = false;
          if (!gotNum) {
            try {
              num = Integer.parseInt(args[i]);
              triedNum = true;
            } catch (NumberFormatException nex) {
            }

            if (triedNum) {
              if (num >= 1 && num <= 9) {
                gotNum = true;
              } else {
                System.out.println("invalid number of tabs (1-9) = "+num);
                killMe = true;
              }
              continue;
            }
          }

          if (fileName == null) {
            fileName = args[i];
          } else {
            System.out.println("Ignoring extra argument \"" + args[i] + "\"");
          }
        }
      }

      if (killMe) {
        System.out.println("Usage: ShowNCEPModel [-c|-s] # [fileName]");
        System.out.println("Usage: ShowNCEPModel [-chostname|-s] # [fileName]");
        System.exit(1);
      }
    }

    try {
      new ShowNCEPModel(num, srvr, clnt, host, fileName);
    } catch (Exception e) {
      e.printStackTrace(System.out);
      System.exit(1);
    }
  }

  public ShowNCEPModel(int numPanels)
    throws RemoteException, VisADException
  {
    this(numPanels, false, false, null, null);
  }

  public ShowNCEPModel(int numPanels, boolean srvr, boolean clnt)
    throws RemoteException, VisADException
  {
    this(numPanels, srvr, clnt, null, null);
  }

  public ShowNCEPModel (int numPanels, String fileName)
    throws RemoteException, VisADException
  {
    this(numPanels, false, false, null, fileName);
  }

  public ShowNCEPModel(int numPanels, boolean srvr, boolean clnt,
                       String host, String fileName)
    throws RemoteException, VisADException
  {
    super("Show NCEP Model Data");

    isServer = srvr;
    isClient = clnt;
    clientHost = host;
    if (clientHost == null) clientHost = "localhost";

    addWindowListener( new WindowAdapter() {
      public void windowClosing(WindowEvent e) {System.exit(0); }
    } );

    frameValue = 0;
    maxFrames = -1;
    firstFile = true;
    gotSfcGrids = false;
    gotAloftGrids = false;
    directory = ".";
    ng = null;

    //MapFile = "../data/mcidas/OUTLAUST";
    MapFile = "../data/mcidas/OUTLUSAM";

    String serviceName = getClass().getName();

    if (!isClient) {
      DisplayImpl di = buildData(numPanels);
      if (isServer) {
        RemoteServerImpl server = ClientServer.startServer(serviceName);

        server.addDisplay(new RemoteDisplayImpl(di));
      }
      display = di;
    } else {
      RemoteServer client;
      client = ClientServer.connectToServer(clientHost, serviceName);

      LocalDisplay[] lh = ClientServer.getClientDisplays(client);
      display = lh[0];
    }

    buildUI();

    if (fileName != null) {
      try { setLooping(false); } catch (Throwable t) { }
      getNewFile(".", fileName);
    }
  }

  private DisplayImpl buildData(int numPanels)
    throws RemoteException, VisADException
  {
    // define the VisAD mappings for the Data and Display
    DisplayImpl di = new DisplayImplJ3D("display1");
    di.addDisplayListener(this);
    pc = di.getProjectionControl();

    // make the cube the size of the window...for 'snapping'
    pcMatrix = pc.getMatrix();
    pcMatrix[0] = .95;
    pcMatrix[5] = .95;
    pcMatrix[10] = .95;
    pc.setMatrix(pcMatrix);

    x = RealType.getRealType("x");
    y = RealType.getRealType("y");
    level = RealType.getRealType("level");
    time_type = RealType.Time;
    //time_type = RealType.getRealType("Valid_time", CommonUnit.secondsSinceTheEpoch);
    pres = RealType.getRealType("pres");

    gmc = di.getGraphicsModeControl();
    gmc.setProjectionPolicy(0);

    xAxis = new ScalarMap(x, Display.XAxis);
    yAxis = new ScalarMap(y, Display.YAxis);
    di.addMap(xAxis);
    di.addMap(yAxis);
    ScalarMap lvl = new ScalarMap(pres, Display.ZAxis);
    lvl.setRange(1020., 10.);
    di.addMap(lvl);

    ScalarMap ani = new ScalarMap(time_type, Display.Animation);
    di.addMap(ani);
    ca = (AnimationControl) ani.getControl();
    ca.setOn(false);

    statLabel = new JLabel("Please choose a data file...");
    statLabel.setForeground(Color.black);

    // make the ncep Panel(s) here because they do 'addMaps'

    tabby = new JTabbedPane();

    ncp = new NCEPPanel[numPanels+1];

    ncp[0] = new NCEPPanel(false, di, statLabel, tabby,  "Single-level Data");
    for (int i=1; i ");
    forward.setAlignmentX(Component.CENTER_ALIGNMENT);
    forward.setMaximumSize(forward.getMaximumSize() );
    forward.setMinimumSize(forward.getMaximumSize() );
    forward.setPreferredSize(forward.getMaximumSize() );
    forward.addActionListener(this);
    forward.setActionCommand("forward");

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS) );
    panel.setAlignmentX(Component.CENTER_ALIGNMENT);

    panel.add(backward);
    panel.add(start_stop);
    panel.add(forward);
    panel.add(buildSpeedControl());

    return panel;
  }

  private Component buildControlPanel()
  {
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS) );
    panel.setAlignmentX(Component.CENTER_ALIGNMENT);
    panel.setAlignmentY(Component.TOP_ALIGNMENT);
    panel.setMaximumSize(new Dimension(300,Short.MAX_VALUE) );
    panel.setMinimumSize(new Dimension(300,400) );
    panel.setPreferredSize(new Dimension(300,400) );

    mapColor = new JButton("Map Color");
    mapColor.addActionListener(this);
    mapColor.setActionCommand("mapColor");
    mapColor.setBackground(new Color(154,0,154));

    showMap = new JCheckBox("Make map visible");
    showMap.setAlignmentX(Component.CENTER_ALIGNMENT);
    showMap.addActionListener(this);
    showMap.setActionCommand("showmap");
    showMap.setSelected(true);

    snapButton = new JButton("Snap!");
    snapButton.setAlignmentX(Component.CENTER_ALIGNMENT);
    snapButton.setMaximumSize(snapButton.getMaximumSize() );
    snapButton.setMinimumSize(snapButton.getMaximumSize() );
    snapButton.setPreferredSize(snapButton.getMaximumSize() );
    snapButton.addActionListener(this);
    snapButton.setActionCommand("snapButton");

    panel.add(Box.createRigidArea(new Dimension(10,10) ) );
    panel.add(snapButton);
    panel.add(Box.createRigidArea(new Dimension(10,10) ) );

    panel.add(buildMapControls());
    panel.add(buildAnimationControls());

    for (int i=0; i= 1.0) {
        pcMatrix[0] = .95;
        pcMatrix[5] = .95/aspect;
      } else {
        pcMatrix[0] = .95 * aspect;
        pcMatrix[5] = .95;
      }

      pc.setMatrix(pcMatrix);

      for (int i=0; i 

Ads help maintain this website.