Convert.java [src/java/m/prms/conv] Revision:   Date:
/*
 * $Id$
 * 
 * This file is part of the Object Modeling System (OMS),
 * 2007-2012, Olaf David and others, Colorado State University.
 *
 * OMS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, version 2.1.
 *
 * OMS 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with OMS.  If not, see <http://www.gnu.org/licenses/lgpl.txt>.
 */
package m.prms.conv;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import ngmf.ui.mms.MMSParameterAdapter;
import ngmf.ui.mms.MMSParameterAdapter.MmsParamInfo;
import oms3.io.DataIO;

class Convert {

  static final String SEPARATOR = "####";


  static File fromData(File file) throws FileNotFoundException, IOException {
    File out = getSibling(file, "csv");

    StringBuffer head = new StringBuffer("@H, date");
    StringBuffer type = new StringBuffer(" " + DataIO.KEY_TYPE + ", Date");
    StringBuffer dout = new StringBuffer(",");

    BufferedReader br = new BufferedReader(new FileReader(file));
    String h = br.readLine();
    String statHdr = br.readLine();

    while (!statHdr.startsWith("###")) {
      if (statHdr.startsWith("//") || statHdr.isEmpty()) {
        statHdr = br.readLine();
        continue;
      }
      String[] st = statHdr.split("\\s+");
      int cols = Integer.parseInt(st[1]);
      for (int i = 0; i < cols; i++) {
        head.append(", " + st[0] + "[" + i + "]");
        type.append(", double");
      }
      statHdr = br.readLine();
    }

    PrintWriter w = new PrintWriter(out);
    w.println("@T, \"Table\"");
    w.println(" " + DataIO.KEY_CREATED_AT + ", \"" + new Date() + "\"");
    w.println(" " + DataIO.KEY_CONVERTED_FROM + ", \"" + file + "\"");
    w.println(" " + DataIO.DATE_FORMAT + ", yyyy MM dd H m s");
    w.println(" Title, \"" + h + "\"");
    w.println(head);
    w.println(type);

    String data;
    while ((data = br.readLine()) != null) {
      String[] str = data.split(" ");
      for (int i = 0; i < 6; i++) {
        dout.append(str[i] + " ");
      }
      for (int i = 6; i < str.length; i++) {
        dout.append("," + str[i]);
      }
      w.println(dout);
      dout.delete(1, dout.length());
    }
    br.close();
    w.close();
    System.out.println("  Converted: '" + file + "' -> '" + out + "'\n");
    return out;
  }


  static File fromParam(File file) throws FileNotFoundException, IOException {
    File out = getSibling(file, "csv");
    MmsParamInfo info = MMSParameterAdapter.map(file);
    info.store(new FileOutputStream(out));
    System.out.println("  Converted: '" + file + "' -> '" + out + "'\n");
    return out;
  }


  static File fromStatvar(File file) throws IOException {
    StringBuffer head = new StringBuffer("@H, date");
    StringBuffer type = new StringBuffer(" " + DataIO.KEY_TYPE + ", Date");
    StringBuffer dout = new StringBuffer(",");

    File out = getSibling(file, "csv");

    BufferedReader br = new BufferedReader(new FileReader(file));

    String statHdr = br.readLine();
    int varNum = Integer.parseInt(statHdr);
    for (int i = 0; i < varNum; i++) {
      String s = br.readLine();
      String[] st = s.split("\\s+");
      head.append(", " + st[0]);
      type.append(", double");
    }

    PrintWriter w = new PrintWriter(out);
    w.println("@T, \"Table\"");
    w.println(" " + DataIO.KEY_CREATED_AT + ", \"" + new Date() + "\"");
    w.println(" " + DataIO.KEY_CONVERTED_FROM + ", \"" + file + "\"");
    w.println(" " + DataIO.DATE_FORMAT + ", yyyy MM dd H m s");
    w.println(head);
    w.println(type);

    String data;
    while ((data = br.readLine()) != null) {
      String[] str = data.split(" ");
      for (int i = 1; i < 7; i++) {
        dout.append(str[i] + " ");
      }
      dout.append(",");
      for (int i = 7; i < str.length - 1; i++) {
        dout.append(str[i] + ",");
      }
      dout.append(str[str.length - 1]);
      w.println(dout);
      dout.delete(1, dout.length());
    }
    br.close();
    w.close();
    System.out.println("  Converted: '" + file + "' -> '" + out + "'\n");
    return out;
  }


  static File toParam(File file) throws IOException {
    File out = getSibling(file, "param");
    PrintWriter w = new PrintWriter(out);
    w.println("Written by CSIP service.");
    w.println("version 1.0");
    w.println("** Dimensions **");
    w.println(SEPARATOR);

    w.println("** Parameters **");
    w.println(SEPARATOR);

    List<String> names = DataIO.tables(file);
    for (String name : names) {
      System.out.println(name);
    }
    w.close();
    return out;
  }


  static File getSibling(File file, String ext) {
    String name = file.getName().substring(0, file.getName().indexOf('.'));
    return new File(file.getParentFile(), name + '.' + ext);
  }


  public static void main(String[] args) {

  }

}