View Javadoc
1 /* 2 * STFDataSet.java 3 * 4 * Created on 15 April 2002 5 * by Frank Hardisty 6 * based on STFile 7 * 8 * 9 * Licensed under GNU General Public License (GPL). 10 * See http://www.gnu.org/copyleft/gpl.html 11 */ 12 13 package edu.psu.geovista.app.parvis.file; 14 15 import edu.psu.geovista.app.parvis.model.*; 16 import edu.psu.geovista.app.parvis.gui.ProgressEvent; 17 import edu.psu.geovista.app.parvis.gui.ProgressListener; 18 19 //import java.io.*; 20 import java.net.*; 21 import java.util.*; 22 23 import edu.psu.geovista.io.*; 24 25 /*** 26 * A Simple file parser for reading STF (Simple Table Fomrat) files from URLs. 27 * 28 * The STF File format is defined as follows: 29 <pre> 30 # test.stf 31 # Comments have # in the first column. 32 # Type the number of fields, on a line by itself. 33 3 34 # Then type field names and types. Field names must not contain 35 # spaces. 36 # 37 PersonName String 38 Age Integer 39 HourlyWage Real 40 # 41 # Data type is case-insensitive. 42 # Default data delimiters are tabs and spaces. 43 # Here's the data, tab-delimited. Notice that the data columns are 44 # in the order they are listed above. 45 # 46 Joe 23 5.75 47 Mary 18 4.75 48 Fred 54 100.00 49 Ginger 48 100.00 50 # 51 # Nothing special is required to end the file. 52 53 </pre> 54 * 55 * Once the file is read and parsed, the data can be accessed with the methods 56 * defined in the ParallelSpaceModel interface. 57 * 58 * @author Flo Ledermann flo@subnet.at 59 * @version 0.1 60 */ 61 public class STFDataSet extends SimpleParallelSpaceModel { 62 63 /*** The url of the file. */ 64 URL url; 65 66 private int tempNumDimensions; 67 68 private int bytesRead = 0; 69 private int filesize = 0; 70 71 private Vector stringLabels = new Vector(); 72 private boolean isStringLabel[]; 73 74 /*** 75 * Creates a new STFDataSet with the given url. The content is not read until 76 * readContents() is called. 77 * 78 * @param url The url of the file to read. 79 */ 80 public STFDataSet(Object[] dataSet) { 81 Object[] dataIn = dataSet; 82 Object[] dataToShow = null; 83 int[] orignalToNew = new int[dataIn.length]; 84 String[] varNames = (String[])dataIn[0]; 85 boolean[] isNumeric = new boolean[dataIn.length]; 86 int count = 0; 87 for (int i = 0; i < dataIn.length; i++) { 88 Object obj = dataIn[i]; 89 if (obj instanceof double[] || obj instanceof int[]) { 90 isNumeric[i] = true; 91 count++; 92 } else { 93 isNumeric[i] = false; 94 } 95 } 96 if (count == 0) return; 97 98 String[] numericVarNames = new String[count]; 99 dataToShow = new Object[count + 3]; //one for variable names 100 //one for observation names 101 //one for spatial data; 102 103 count = 0; 104 for (int i = 1; i < isNumeric.length; i++) { 105 if (isNumeric[i]){ 106 dataToShow[count+1] = dataIn[i]; 107 numericVarNames[count] = varNames[i-1]; // -1 because of the varNames themselves 108 orignalToNew[i] = count;//so we can get back if need be 109 count++; 110 } 111 } 112 dataToShow[0] = numericVarNames; 113 for (int i = 0; i < varNames.length; i++) { 114 String lower = varNames[i].toLowerCase(); 115 if (lower.endsWith("name") && (dataIn[i + 1] instanceof String[])){ 116 dataToShow[numericVarNames.length +1] = (String[])dataIn[i+1]; 117 //this.spat.setObservationNames((String[])dataIn[i+1]);//+1 to skip varNames 118 } 119 } 120 121 this.initNumDimensions(numericVarNames.length); 122 this.setAxisLabels(numericVarNames); 123 Object obj = dataToShow[1]; 124 double[] someDoubles = null; 125 int[] someInts = null; 126 int numObservations = -1; 127 if (obj instanceof double[]) { 128 someDoubles = (double[])obj; 129 numObservations = someDoubles.length; 130 } else if (obj instanceof int[]) { 131 someInts = (int[])obj; 132 numObservations = someInts.length; 133 } 134 135 136 String name = null; 137 for (int row = 0; row < numObservations; row++) { 138 float[] dataVals = new float[numericVarNames.length]; 139 for (int column = 1; column < dataVals.length + 1; column++) { 140 obj = dataToShow[column]; 141 if (obj instanceof double[]) { 142 someDoubles = (double[])obj; 143 dataVals[column-1] = (float)someDoubles[row]; 144 } else if (obj instanceof int[]) { 145 someInts = (int[])obj; 146 dataVals[column-1] = (float)someInts[row]; 147 }//end if 148 }//next column 149 if (dataToShow[numericVarNames.length + 1] != null) { 150 String[] names = (String[])dataToShow[numericVarNames.length +1]; 151 name = names[row]; 152 } 153 this.addRecord(dataVals, name); 154 }//next row 155 156 157 } 158 159 160 private Vector progressListeners = new Vector(); 161 162 public void addProgressListener(ProgressListener l){ 163 progressListeners.add(l); 164 } 165 166 public void removeProgressListener(ProgressListener l){ 167 progressListeners.remove(l); 168 } 169 170 public void fireProgressEvent(ProgressEvent e){ 171 Vector list = (Vector)progressListeners.clone(); 172 for (int i=0; i<list.size(); i++){ 173 ProgressListener l = (ProgressListener)list.elementAt(i); 174 l.processProgressEvent(e); 175 } 176 } 177 178 /*** 179 * Main method for testing purposes. 180 */ 181 public static void main(String args[]){ 182 183 String fileName = "C://geovista_old//data//test6.csv"; 184 185 Object[] dataSet = new Object[4]; 186 String[] labels = new String[] {"0","1","Name"}; 187 dataSet[0] = labels; 188 try { 189 FileIO fio = new FileIO(fileName, "r"); 190 for (int col = 0; col < 2; col++){ 191 double[] doubleData = new double[7]; 192 for (int row = 0; row < 7; row++){ 193 doubleData[row] = fio.readDouble(); 194 } 195 dataSet[col+1] = doubleData; 196 } 197 } catch (Exception ex) { 198 ex.printStackTrace(); 199 } 200 String[] names = new String[7]; 201 for (int row = 0; row < 7; row++){ 202 names[row] = "Obs " + row; 203 } 204 dataSet[3] = names; 205 206 STFDataSet f = new STFDataSet(dataSet); 207 int i = f.filesize; 208 } 209 }

This page was automatically generated by Maven