package nxm.sys.test;

import nxm.sys.lib.Data;
import nxm.sys.lib.DataFile;
import nxm.sys.lib.Midas;
import nxm.sys.lib.NeXtMidas;

/** A class to show basic usage (example) of DataFile from a standalone Java program.
    This program writes ramp data to specified file in first argument.<br>
    The option second argument specified the number of elements to write (defaults to 10).
   @author Neon Ngo
   @since NeXtMidas 3.3.4
*/
public class TestCreateDataFile {

  /** This constructor was automatically provided by Java,
   *  but it is unneeded as all the methods are static methods.
   *  @deprecated now that Javadocs are required, it is temporarily
   *              being explicitely provided but deprecated
   */
  @Deprecated
  public TestCreateDataFile () {}

  /** main method to run stand alone.
      @param args command line arguments
   */
  public static void main (String[] args) {
    int numElements = 10;
    String filename;
    if (args.length < 1) {
      System.out.println("Usage: java -cp $NMROOT " + TestCreateDataFile.class.getName() + " <out_filename> [num elements; DEF=10]");
      System.out.println("  -or- java -cp path/to/nxm-sys.jar ...");
      System.out.println("  e.g. java -cp $NMROOT/jars/nxm-sys.jar  "   + TestCreateDataFile.class.getName() + " test.tmp 256");
      System.out.println("  e.g. java -cp %NMROOT%\\jars\\nxm-sys.jar " + TestCreateDataFile.class.getName() + " test.tmp 256");
      return;
    } else {
      filename = args[0];
      System.out.println("--- ramp output filename = '" + filename + "'");
    }
    if (args.length > 1) {
      numElements = Integer.parseInt(args[1]);
    }
    // end command line argument parsing code

    NeXtMidas nxm = NeXtMidas.getGlobalInstance();        // get reference to Global NeXtMidas (singleton) instance
    Midas midas = nxm.getMidasContext();                  // get it's midas context

    DataFile df = new DataFile(midas, filename);          // create DataFile instance
    df.open(DataFile.OUTPUT);                             // open file for output

    int[] dataArray = new int[numElements];
    for (int ii = 0; ii < dataArray.length; ii++) {       // generate ramp data
      dataArray[ii] = ii;
    }
    Data data = new Data(dataArray);
    int wrote = df.write(data);                           // write data to file

    df.getKeywordsObject().put("TESTKEYWORD", "Example"); // write test keyword to file

    df.close();                                           // close (cleanup)
    System.out.println("--- number of elements written = " + wrote);
  }
}
