/** * SpeciesFile class * * encapsulates info of a species file * ( many species ) * */ import java.util.Vector; import java.util.StringTokenizer; import java.io.*; import java.text.DecimalFormat; public class SpeciesFile { /** * Description associated with this file */ private String description; /** * Location of the html */ private String directorySufix; /** * Data Sets within this file */ private Vector datasets; public SpeciesFile() { datasets = new Vector(); description = null; directorySufix = null; } /** * Add Species to this file * @param data array containing the original data file (kin.o02) * @return true if data was well formed, false otherwise */ public boolean addSpecies( byte data[] ) { boolean retValue = true; boolean debugMac = false; int start = 0; int end = 0; int k = 0; byte buffer[] = new byte[ 1024 ]; final String speciesNamePrefix = "# "; final String datasetSeparator = "time-dep. species concentrations"; final String datasetNamePrefix = "data set"; String str = ""; String spname = null; String datasetName = null; String val = null; StringTokenizer st = null; Vector speciesname = null; Vector values = null; DatasetSpecies dataset = null; for( int i = 0; i < data.length; i++ ) { if( data[ i ] == 13 ) { // get rid of the \r that ms-dos files have continue; } if( data[ i ] == 10 ) { if( k <= 1 ) { k = 0; continue; } str = new String( buffer, 0, k ); if( str.indexOf( datasetSeparator ) != -1 ) { dataset = new DatasetSpecies( datasetName ); speciesname = dataset.getSpeciesname(); values = dataset.getValues(); datasets.addElement( dataset ); } else if( str.indexOf( datasetNamePrefix ) != -1 ) { datasetName = str.substring( datasetNamePrefix.length() ).trim(); } else if( str.startsWith( speciesNamePrefix ) ) { spname = str.substring( speciesNamePrefix.length() ); if( speciesname != null ) { speciesname.addElement( spname.trim() ); } else { System.err.println( "speciesname Vector is null" ); return false; } } else if( ! str.startsWith( "#" ) ) { st = new StringTokenizer( str ); if( spname != null && st.countTokens() == 3 ) { st.nextToken(); st.nextToken(); val = st.nextToken(); val = val.replace( 'D', 'E' ); if( values != null ) { values.addElement( Double.valueOf( val ) ); } else { System.err.println( "values Vector is null" ); return false; } } } k = 0; } else { // replace / with a _ to avoid problems with file naming buffer[ k ] = ( (byte) '/' ) == data[ i ] ? (byte) '_' : data[ i ]; k++; if( k >= 1023 ) { return false; } } } try { writeToLocation(); } catch( Exception e ) { e.printStackTrace(); return false; } return retValue; } /** * Writes data to a data file organized by columns */ private void writeToLocation() throws IOException, InterruptedException { int numlines = 0; int status = 0; int dirno = 0; Process p = null; Runtime r = Runtime.getRuntime(); Vector speciesname = null; Vector values = null; DatasetSpecies dataset = null; PrintStream htmlFile = null; Vector chmodFiles = new Vector(); DecimalFormat threeDigits = new DecimalFormat( "000" ); String base_directory = Environment.HTML_DIRECTORY; final String HTMLFILE = "index.html"; String PSFILE = null; String GNUFILE = null; String directory = null; File tmpFile = null; int max = 0; Integer tmpInt = null; tmpFile = new File( base_directory ); String files[] = tmpFile.list(); for( int i = 0; files != null & i < files.length; i++ ) { try { tmpInt = Integer.valueOf( files[ i ] ); } catch( NumberFormatException nfe ) { continue; } if( tmpInt.intValue() > dirno ) { dirno = tmpInt.intValue(); } } dirno++; if( dirno == Integer.MAX_VALUE ) { throw new IOException( "Too many existent directories!" + "\n[" + directory + "]" ); } directorySufix = threeDigits.format( dirno ); directory = base_directory + directorySufix; tmpFile = new File( directory ); tmpFile.mkdir(); //do { // dirno++; // directorySufix = threeDigits.format( dirno ); // directory = base_directory + directorySufix; // tmpFile = new File( directory ); // status = tmpFile.mkdir() ? 0 : 1; //} while( status != 0 && dirno < Environment.MAX_DIRECTORIES ); //if( dirno >= Environment.MAX_DIRECTORIES ) { // throw new IOException( "Too many existent directories!" // + "\n[" + directory + "]" ); //} // write out each of the datasets for( int i = 0; i < datasets.size(); i++ ) { PSFILE = "all_vs_time_" + i + ".ps"; GNUFILE = "toplot_" + i + ".gnufile"; dataset = (DatasetSpecies) datasets.elementAt( i ); speciesname = dataset.getSpeciesname(); values = dataset.getValues(); if( speciesname.size() == 0 ) { throw new IOException( "ERROR, dataset " + i + " has no species" ); } numlines = values.size() / speciesname.size(); if( numlines == 0 ) { throw new IOException( "ERROR, dataset " + i + " numlines is 0!" ); } PrintStream fileColumns = new PrintStream( new FileOutputStream( new File( directory + File.separator + Environment.COLUMNS_DATA + i ) ) ); String out = null; for( int lc = 0; lc < numlines; lc++ ) { out = ""; for( int sp = 0; sp < speciesname.size(); sp++ ) { out += ( (Double) values.elementAt( lc + numlines * sp ) ).toString() + " "; } fileColumns.println( out ); } fileColumns.close(); PrintStream gnuFile = new PrintStream( new FileOutputStream( new File( directory + File.separator + GNUFILE ) ) ); // create first a postcript with all species vs. time gnuFile.println( "cd '" + directory + "'" ); gnuFile.println( "set terminal postscript" ); gnuFile.println( "set nokey" ); gnuFile.println( "set output '" + PSFILE + "'" ); gnuFile.println( "set xlabel 'time'" ); String spname = null; for( int sp = 0; sp < speciesname.size(); sp++ ) { spname = (String) speciesname.elementAt( sp ); gnuFile.println( "set title '" + spname + " vs. time'" ); gnuFile.println( "set ylabel '" + spname + "'" ); gnuFile.println( "plot '" + Environment.COLUMNS_DATA + i + "' using 0:" + ( sp + 1 ) ); } gnuFile.println( "quit" ); gnuFile.close(); String[] command = { Environment.GNUPLOT, directory + File.separator + GNUFILE }; r = Runtime.getRuntime(); p = r.exec( command ); p.waitFor(); // create all plots and thumbnails final String SCRIPT = "convert_" + i + ".sh"; PrintStream scriptFile = new PrintStream( new FileOutputStream( new File( directory + File.separator + SCRIPT ) ) ); PrintStream t_gnuFile = new PrintStream( new FileOutputStream( new File( directory + File.separator + "t_" + GNUFILE ) ) ); String plotName = null; t_gnuFile.println( "cd '" + directory + "'" ); t_gnuFile.println( "set terminal pbm" ); t_gnuFile.println( "set nokey" ); scriptFile.println( "cd '" + directory + "'" ); // do one iteration for thumbnails, other for the (big) images for( int iter = 0; iter < 2; iter++ ) { for( int each_sp = 0; each_sp < speciesname.size(); each_sp++ ) { spname = (String) speciesname.elementAt( each_sp ); if( iter == 0 ) { t_gnuFile.println( "set size 0.15, 0.10" ); t_gnuFile.println( "set noxtics" ); t_gnuFile.println( "set noytics" ); t_gnuFile.println( "set xlabel ''" ); t_gnuFile.println( "set ylabel ''" ); t_gnuFile.println( "set title ''" ); plotName = "t_" + spname + "__time_" + i; } else { t_gnuFile.println( "set size 0.8, 0.8" ); t_gnuFile.println( "set xtics" ); t_gnuFile.println( "set ytics" ); t_gnuFile.println( "set ylabel '" + spname + "'" ); t_gnuFile.println( "set xlabel 'time'" ); t_gnuFile.println( "set title '" + spname + " vs. time'" ); plotName = spname + "__time_" + i; } t_gnuFile.println( "set output '" + plotName + ".pbm'" ); t_gnuFile.println( "plot '" + Environment.COLUMNS_DATA + i + "' using 0:" + ( each_sp + 1 ) ); scriptFile.println( Environment.PBM2GIF + " < '" + plotName + ".pbm' > '" + plotName + ".gif'" ); chmodFiles.addElement( plotName + ".gif" ); scriptFile.println( "rm '" + plotName + ".pbm'" ); } } t_gnuFile.println( "quit" ); t_gnuFile.close(); scriptFile.close(); String[] plot2 = { Environment.GNUPLOT, directory + File.separator + "t_" + GNUFILE }; r = Runtime.getRuntime(); p = r.exec( plot2 ); p.waitFor(); r = Runtime.getRuntime(); p = r.exec( "chmod 755 " + directory + File.separator + SCRIPT ); p.waitFor(); r = Runtime.getRuntime(); p = r.exec( directory + File.separator + SCRIPT ); p.waitFor(); // write out the html file if( i == 0 ) { htmlFile = new PrintStream( new FileOutputStream( new File( directory + File.separator + HTMLFILE ) ) ); htmlFile.println(); htmlFile.println( "" ); htmlFile.println( " " ); htmlFile.println( " Plots For '" + description + "'" ); htmlFile.println( " " ); htmlFile.println( " " ); htmlFile.println( "

Plots For '" + description + "'

" ); } htmlFile.println( "

" ); htmlFile.println( " " ); htmlFile.println( " " ); htmlFile.println( " " ); htmlFile.println( " " ); htmlFile.println( " " ); for( int sp = 0; sp < speciesname.size(); sp++ ) { spname = (String) speciesname.elementAt( sp ); htmlFile.println( " " ); htmlFile.println( " " ); htmlFile.println( " " ); htmlFile.println( " " ); } htmlFile.println( "
" ); htmlFile.println( " all vs. time
" + "(postscript)
time
" + spname + "" ); htmlFile.println( "
" ); htmlFile.println( "

" ); htmlFile.println( " Plot especific species against each other
" ); htmlFile.println( "

" ); htmlFile.println( " " ); htmlFile.println( " VS." ); htmlFile.println( " " ); htmlFile.println( " " ); htmlFile.println( " " ); htmlFile.println( " " ); htmlFile.println( "
" ); chmodFiles.addElement( PSFILE ); } htmlFile.println( "

Go back to the Upload Page" ); htmlFile.println( " " ); htmlFile.println( "" ); htmlFile.close(); chmodFiles.addElement( "" ); // the actual directory chmodFiles.addElement( HTMLFILE ); String tmp = null; r = Runtime.getRuntime(); String chmod = "chmod 755 "; for( int i = 0; i < chmodFiles.size(); i++ ) { tmp = (String) chmodFiles.elementAt( i ); chmod += directory + File.separator + tmp + " "; } p = r.exec( chmod ); p.waitFor(); p.destroy(); } /** * Sets the description for this file * @paran description this file description */ public void setDescription( String description ) { this.description = description; } /** * Gets the description for this file * @return this file description */ public String getDescription() { return description; } /** * Gets the directorySufix */ public String getDirectorySufix() { return directorySufix; } }