/*
This is a proprietary part of DMelt package.
License: the Creative Commons Attribution-Share Alike License; either version 3.0 of the License,
or any later version.
http://creativecommons.org/licenses/by-sa/3.0/
Permission to use, copy and modify this software and its documentation for
NON-COMMERCIAL purposes is granted,
without fee, provided that an acknowledgment to the author, Dr Sergei Chekanov
appears in all copies and associated documentation or publications.
Redistributions of the source code, or parts of the source codes,
must retain the copyright notice, this list of conditions and the following disclaimer
(all at the top of each source code) and requires the written permission of Sergei Chekanov
Redistribution in binary form of all or parts of a class must reproduce the
copyright notice, this list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution and requires the written permission of Sergei Chekanov
Details about license agreement or commercial use are: http://jwork.org/dmelt/
*/
package jhpro.tseries;
import java.awt.BorderLayout;
import java.io.File;
import java.net.URL;
import javax.swing.JFrame;
import jhplot.P0D;
import jhplot.P1D;
import jhplot.PND;
import jhplot.utils.*;
import jhplot.gui.*;
import com.jstatcom.model.*;
import com.jstatcom.ts.*;
import com.jstatcom.util.*;
import com.jstatcom.io.*;
import java.io.*;
/**
* Standard data container to store and read time series. Standard interface to
* read data in different formats (ASCII, Gauss, Matlab, Excel) and present
* results (time series selector, editable data tables, number selectors with
* interval based validation, etc.). Data can be read from URL (if the path
* start from http or ftp). Data can be shown as tables or represented as plots
* (use P1D and P0D conversions and standard canvases, such as HPlot etc). This
* is very useful class for financial analysis and time series.
*
* @author S.Chekanov
*
*/
public class HStatData implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private com.jstatcom.ts.TSTable TSTable = null;
private JSCDate startDate;
private JSCNArray data;
private JSCSArray varNames;
private String description = "undefined";
private DataHandler handler;
/**
* Initialse data for statistical analysis. The data are typically
* represented as a time series. Use use readData() to fill with the data.
* You can also replace time series and save the data.
*
* @param description
* description
*/
public HStatData(String description) {
this.description = description;
}
/**
* Initialise the container
*/
public HStatData() {
this.description = "undefined";
}
/**
* Reads many data formats (in ASCII, Gauss, Matlab, Excel) and converts the
* data (time series) to arrays for further processing. Input file can be on
* WWW.
*
* @param description
* description
*
* @param sfile
* input data file including the full path. If it starts from
* http: or ftp://, it is assumed that data should be read from
* the web location. Otherwise, data will be read from a local
* file. In case of URL, the filer will be copied to the local
* directory.
*/
public HStatData(String description, String sfile) {
description = this.description;
readData(sfile);
}
/**
* Reads many data formats (in ASCII, Gauss, Matlab, Excel) and converts the
* data to arrays for further processing. Input file can url.
*
* @param sfile
* input data file including the full path. If it starts from
* http: or ftp://, it is assumed that data should be read from
* the web location. Otherwise, data will be read from a local
* file. In case of URL, the filer will be copied to the TMP
* directory.
*/
public void readData(String sfile) {
sfile = sfile.trim();
File file = null;
if (sfile.startsWith("http:") || sfile.startsWith("ftp:")) {
try {
URL url = new URL(sfile);
String fileName = sfile.substring(sfile.lastIndexOf('/') + 1,
sfile.length());
String tempDir = System.getProperty("java.io.tmpdir");
file = new File(tempDir + File.separator + fileName);
if (!file.exists()) Util.copyURLToFile(url, file);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
file = new File(sfile);
}
if (file == null) {
Util.ErrorMessage("No such file is found!");
return;
}
if (!file.canRead()) {
Util.ErrorMessage("Cannot read from file \""
+ file.getAbsolutePath() + "\".");
}
String fName = file.getName();
// get the handler from the filename only
DataHandlerTypes type = DataHandlerTypes.getTypeFromFileName(file
.getAbsolutePath());
DataHandler handler = type.getHandler();
boolean a = handler.importData(file);
if (!a)
Util.ErrorMessage("Error in reading the file!");
data = (JSCNArray) handler.getData(TSImportTypes.DATA);
startDate = (JSCDate) handler.getData(TSImportTypes.STARTDATE);
varNames = (JSCSArray) handler.getData(TSImportTypes.VARNAMES);
JSCString des = (JSCString) handler.getData(TSImportTypes.DESCRIPTION);
description = des.string();
}
/**
* Show the time series data as a table
*/
public void toTable() {
JFrame f = new JFrame(description);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setSize(400, 500);
f.setLocation(200, 200);
TS[] tsArray = new TS[data.cols()];
double[][] values = data.doubleArray();
for (int i = 0; i < tsArray.length; i++) {
tsArray[i] = new TS(UMatrix.getDoubleCol(values, i),
varNames.stringAt(i, 0), startDate.getTSDate());
}
getTSTable().setTS(tsArray);
TSTableScrollPane jScrollPane = new TSTableScrollPane();
jScrollPane.setViewportView(getTSTable());
f.getContentPane().add(BorderLayout.CENTER, jScrollPane);
f.setVisible(true);
}
/**
* This method initializes TSTable
*
* @return com.jstatcom.ts.TSTable
*/
private TSTable getTSTable() {
if (TSTable == null) {
TSTable = new com.jstatcom.ts.TSTable();
}
return TSTable;
}
/**
* Return data as array
*
* @return
*/
public JSCNArray getData() {
return data;
}
/**
* Set new data. The time series will be rebuild.
*
* @param d
* input data
*/
public void setData(JSCNArray d) {
data = d;
}
/**
* Set data from PND container
*/
public void setPND(PND d) {
data.clear();
for (int i = 0; i < d.size(); i++) {
double[] tt = (double[]) d.get(i);
JSCNArray r = new JSCNArray("");
data.appendRows(r);
}
}
/**
* Get data as PND object
*
* @return PND keeping this data.
*/
public PND getPND() {
PND d = new PND(data.name());
double[][] values = data.doubleArray();
d.setArray(values);
return d;
}
/**
* Convert to a string
*
* @param string
* represending the data
*/
public String toString() {
String s = description + "\n varNames=" + varNames.display() + "\n";
return s + data.display();
}
/**
* Get names of variables
*
* @return
*/
public JSCSArray getVarNames() {
return varNames;
}
/**
* Set variable names. Time series will be rebult.
*
* @param d
* input variable names
*/
public void setVarNames(JSCSArray d) {
varNames = d;
}
/**
* Get names of variables.
*
* @return
*/
public String getDescription() {
return description;
}
/**
* Set new description for data set
*
* @param d
*/
public void setDescription(String d) {
description = d;
}
/**
* Get data as 1D array for a given column
*
* @param column
* column index
* @return P0D
*/
public P0D getP0D(int column) {
P0D p = new P0D(description + " " + Integer.toString(column));
p.fill(data.getCol(column));
return p;
}
/**
* Return a column as a array
*
* @param column
* index of column
* @return array with the column
*/
public double[] getColumn(int column) {
return data.getCol(column);
}
/**
* Convert 2 columns in P1D for plotting.
*
* @param column1
* column representing X
* @param column2
* column representing Y
* @return P1D
*/
public P1D getP1D(int column1, int column2) {
P1D p = new P1D(description + " " + Integer.toString(column1) + ":"
+ Integer.toString(column2));
p.setArrayX(data.getCol(column1));
p.setArrayY(data.getCol(column2));
return p;
}
/**
* Save data in one of the formats. It can be
* ASCII,Excel,Matlab,GaussDAT,GaussFMT
*
* @param outfile
* Name of the output file
* @param type
* A string representing a format. It can be "ASCII" or "TXT",
* "Excel", "Matlab","GaussDAT","GaussFMT"
*/
public void saveData(String outfile, String type) {
File fout = new File(outfile);
Symbol symbol = new Symbol(data);
boolean t = true;
String trans = t ? ", transposed" : "";
String header = "name = " + symbol.NAME + ", type = NARRAY"
+ ", rows = " + data.rows() + ", cols = " + data.cols() + trans;
// String des = symbol.getDescription();
if (description != null && description.length() > 0)
header = header + "\r\n" + "description " + description;
// String format = "%-s ";
// data.setFormatString(format);
TS[] tsArray = getTS();
if (type.equalsIgnoreCase("ASCII") || type.equalsIgnoreCase("TXT")) {
handler = TSASCIIHandler.getInstance();
handler.exportTS(fout, tsArray, header);
} else if (type.equalsIgnoreCase("Excel")) {
handler = TSExcelHandler.getInstance();
handler.exportTS(fout, tsArray, header);
} else if (type.equalsIgnoreCase("Matlab")) {
handler = TSMatlabHandler.getInstance();
handler.exportTS(fout, tsArray, header);
} else if (type.equalsIgnoreCase("GaussDAT")) {
handler = TSGaussDATHandler.getInstance();
handler.exportTS(fout, tsArray, header);
} else if (type.equalsIgnoreCase("GaussFMT")) {
handler = TSGaussFMTHandler.getInstance();
handler.exportTS(fout, tsArray, header);
} else {
Util.ErrorMessage("Unsupported formt!");
}
/*
*
*
* if (type.equalsIgnoreCase("ASCII") ||type.equalsIgnoreCase("TXT") ) {
* handler = TSASCIIHandler.getInstance(); handler.exportData(fout,
* data, header); } else if (type.equalsIgnoreCase("Excel")) { handler=
* TSExcelHandler.getInstance(); handler.exportData(fout, data, header);
* } else if (type.equalsIgnoreCase("Matlab")) { handler =
* TSMatlabHandler.getInstance(); handler.exportData(fout, data,
* header); } else if (type.equalsIgnoreCase("GaussDAT")) { handler =
* TSGaussDATHandler.getInstance(); handler.exportData(fout, data,
* header); } else if (type.equalsIgnoreCase("GaussFMT")) { handler =
* TSGaussFMTHandler.getInstance(); handler.exportData(fout, data,
* header); } else {
*
* Util.ErrorMessage("Unsupported formt!"); }
*/
}
/**
* Return number of columns in the data
*
* @return Number of columns
*/
public int getNumberColumns() {
return data.cols();
}
/**
* Return number of columns in the data
*
* @return Number of Rows
*/
public int getNumberRows() {
return data.rows();
}
/**
* Return time series as array
*
* @return
*/
public TS[] getTS() {
TS[] tsArray = new TS[data.cols()];
double[][] values = data.doubleArray();
for (int i = 0; i < tsArray.length; i++) {
tsArray[i] = new TS(UMatrix.getDoubleCol(values, i),
varNames.stringAt(i, 0), startDate.getTSDate());
}
return tsArray;
}
/**
* Show online documentation.
*/
public void doc() {
String a = this.getClass().getName();
a = a.replace(".", "/") + ".html";
new HelpBrowser(HelpBrowser.JHPLOT_HTTP + a);
}
/**
* Replace a column with anew array.
*
* @param column
* column index to be replaced.
* @param d
* array which will be used to replace the column.
*/
public void replaceColumn(int column, double[] d) {
if (data.rows() != d.length) {
System.err
.println("The length of array do not match the number of rows");
return;
}
for (int j = 0; j < data.rows(); j++) {
data.setValAt(d[j], j, column);
}
}
/**
* Append a row.
*
* @param hs
* data to be appended.
*/
public void append(HStatData hs) {
data.appendRows(hs.getData());
}
/**
* Get a row
*
* @param index
* of this row.
*/
public double[] getRow(int index) {
return data.getRow(index);
}
}
Ads help maintain this website.