package water.util;

import java.io.File;
import java.util.Arrays;

import com.sun.tools.javac.code.Attribute.Array;

import water.Futures;
import water.Key;
import water.fvec.*;

public class FrameUtils {

  /** Create a frame with single column represented by given vector.
   *
   * @param name  name of the column
   * @param vec  column data
   * @return a new frame
   */
  public static Frame frame(String name, Vec vec)       { return new Frame().add(name, vec); }
  /**
   * Create a new frame based on given column data.
   * @param names  name of frame columns
   * @param vecs  columns data represented by individual data
   * @return a new frame composed of given vectors.
   */
  public static Frame frame(String[] names, Vec[] vecs) { return new Frame(names, vecs); }
  /**
   * Create a new frame based on given row data.
   * @param names  names of frame columns
   * @param rows  data given in the form of rows
   * @return new frame which contains columns named according given names and including given data
   */
  public static Frame frame(String[] names, double[]... rows) {
    assert names == null || names.length == rows[0].length;
    Futures fs = new Futures();
    Vec[] vecs = new Vec[rows[0].length];
    Key keys[] = Vec.VectorGroup.VG_LEN1.addVecs(vecs.length);
    for( int c = 0; c < vecs.length; c++ ) {
      AppendableVec vec = new AppendableVec(keys[c]);
      NewChunk chunk = new NewChunk(vec, 0);
      for( int r = 0; r < rows.length; r++ )
        chunk.addNum(rows[r][c]);
      chunk.close(0, fs);
      vecs[c] = vec.close(fs);
    }
    fs.blockForPending();
    return new Frame(names, vecs);
  }

  /** Parse given file into the form of frame represented by the given key.
   *
   * @param okey  destination key for parsed frame
   * @param file  file to parse
   * @return a new frame
   */
  public static Frame parseFrame(Key okey, File file, File...files) {
    File[] filex = new File[1+files.length];
    System.arraycopy(files, 0, filex, 1, files.length);
    filex[0] = file;
    for (File f : filex)
      if (!file.exists())
        throw new RuntimeException("File not found " + f);
    // Create output key if not specified
    if(okey == null)
      okey = Key.make(file.getName());

    Key[] fkeys = new Key[filex.length];
    int cnt = 0;
    for (File f : filex) fkeys[cnt++] = NFSFileVec.make(f);
    return ParseDataset2.parse(okey,fkeys);
  }


}

@@ -1887,18 +1887,8 @@
 ile 
-file, File
 ...f
@@ -1903,124 +1903,59 @@
     
-File%5B%5D filex = new File%5B1+files.length%5D;%0A    System.arraycopy(files, 0, filex, 1, files.length);%0A    filex%5B0%5D = file
+assert files.length %3E 0 : %22Ups. No files to parse!%22
 ;%0A  
@@ -1974,17 +1974,17 @@
 f : file
-x
+s
 )%0A      
@@ -1989,19 +1989,16 @@
   if (!f
-ile
 .exists(
@@ -2148,16 +2148,20 @@
 ake(file
+s%5B0%5D
 .getName
@@ -2196,17 +2196,17 @@
 Key%5Bfile
-x
+s
 .length%5D
@@ -2246,17 +2246,17 @@
 f : file
-x
+s
 ) fkeys%5B

Fix in a frame helper.