Script
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import uk.ac.starlink.table.ColumnInfo;
import uk.ac.starlink.table.RowListStarTable;
import uk.ac.starlink.table.RowSequence;
import uk.ac.starlink.table.StarTable;
import uk.ac.starlink.table.StarTableFactory;
import uk.ac.starlink.votable.DataFormat;
import uk.ac.starlink.votable.VOTableWriter;
StarTable loadVOTable( File file ) throws IOException {
return new StarTableFactory().makeStarTable( file.toString(), "votable" );
}
void writeStringToAFile(String filepath, String content) throws IOException{
BufferedWriter out = new BufferedWriter(new FileWriter(filepath));
out.write(content);
out.close();
}
String readFileAsString(String filePath) throws java.io.IOException{
byte[] buffer = new byte[(int) new File(filePath).length()];
BufferedInputStream f = null;
try {
f = new BufferedInputStream(new FileInputStream(filePath));
f.read(buffer);
} finally {
if (f != null) try { f.close(); } catch (IOException ignored) { }
}
return new String(buffer);
}
String starTableToString(StarTable table) throws IOException{
// Obtain a writer for inline TABLEDATA output.
VOTableWriter voWriter = new VOTableWriter( DataFormat.TABLEDATA, true );
// Use it to write the table to a named file.
StringWriter swriter = new StringWriter();
BufferedWriter bwriter = new BufferedWriter(swriter);
voWriter.writeInlineStarTable(table, bwriter);
String result = swriter.toString();
return result;
}
Map mapping = new HashMap();
Vector mappingPos = new Vector();
//Map mappingIds = new HashMap();
int nCol;
resultVotable ="default value";
try{
//open the table
File file = new File(FileVotablePath);
StarTable table = loadVOTable(file);
//get the column info
nCol = table.getColumnCount();
for(int i =0; i< nCol; i++){
ColumnInfo colInfo = table.getColumnInfo(i);
mapping.put(colInfo.getName(), i); //names to positions
mappingPos.add(colInfo.getName()); //positions to names
}
if(mapping.get(columnNameWithConfigFile)==null)
throw new Exception(columnNameWithConfigFile+" is not a column on the votable");
ColumnInfo [] colsInfo = new ColumnInfo [9];
colsInfo[0] = new ColumnInfo ("x_2", Float.class, "Position x");
colsInfo[1] = new ColumnInfo ("y_2", Float.class, "Position y");
colsInfo[2] = new ColumnInfo ("mauto_2", Float.class, "Integrated magnitude");
colsInfo[3] = new ColumnInfo ("flux_rad0_5_2_big", Float.class, "R_s (disk scale-length) * 1.5 [pix]");
colsInfo[4] = new ColumnInfo ("flux_rad0_5_2_small", Float.class, "R_s (disk scale-length) * 0.25 [pix]");
colsInfo[5] = new ColumnInfo ("flux_rad0_5_2_medium", Float.class, "R_s (disk scale-length) * 0.5 [pix]");
colsInfo[6] = new ColumnInfo ("axes_rate_2", Float.class, "Axis ratio (b/a)");
colsInfo[7] = new ColumnInfo ("theta_2", Float.class, "Position angle");
colsInfo[8] = new ColumnInfo ("sky_2", Float.class, "Sky background at center of fitting region");
RowListStarTable newTable = new RowListStarTable(colsInfo);
RowSequence rseq = table.getRowSequence();
while ( rseq.next() ) {
String x="", y="", mauto="", flux_rad0_5="", axes_rate="", theta="", skybackground="";
String fileName = (String) rseq.getCell( (int) mapping.get(columnNameWithConfigFile) );
String content;
File fileTest = new File(fileName);
Float [] values = new Float[9];
if(fileTest.exists()){
content = readFileAsString(fileName);
String [] components = content.split("# Component number:");
//System.out.println("Iteration");
//System.out.println("number of elements: "+components.length);
//System.out.println("---- Component 1:\n"+components[1]);
String number = "([-0123456789.]+)";
//Take data from component 1
String expression = ".*1"+Pattern.quote(")")+"\\s+"+number+"\\s+"+number+".*"; // Position x, y
expression += ".*3"+Pattern.quote(")")+"\\s+"+number+".*"; // Integrated magnitude
expression += ".*4"+Pattern.quote(")")+"\\s+"+number+".*"; // R_s (disk scale-length) [pix]
//expression += "[.\\s]+";
expression += ".*9"+Pattern.quote(")")+"\\s+"+number+".*"; // Axis ratio (b/a)
expression += ".*10"+Pattern.quote(")")+"\\s+"+number+".*"; // Position angle
Pattern pattern1 = Pattern.compile(expression, Pattern.DOTALL);
Matcher matcher1 = pattern1.matcher(components[1]);
//http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html
//array for a row
while (matcher1.find()) {
x = matcher1.group(1);
y = matcher1.group(2);
mauto = matcher1.group(3);
flux_rad0_5 = matcher1.group(4);
axes_rate = matcher1.group(5);
theta = matcher1.group(6);
values[0] = Float.valueOf(x);
values[1] = Float.valueOf(y);
values[2] = Float.valueOf(mauto);
values[3] = Float.valueOf(flux_rad0_5) * new Float(1.5);
values[4] = Float.valueOf(flux_rad0_5) * new Float(0.25);
values[5] = Float.valueOf(flux_rad0_5) * new Float(0.5);
values[6] = Float.valueOf(axes_rate);
values[7] = Float.valueOf(theta);
//System.out.println("-"+x+"-");
//System.out.println(y);
//System.out.println(mauto);
//System.out.println(flux_rad0_5);
//System.out.println(axes_rate);
//System.out.println(theta);
//System.out.println(skybackground);
}
//System.out.println("---- Component 2:\n"+components[2]);
//take data from component 2: sky
expression = ".*1"+Pattern.quote(")")+"\\s+"+number+".*"; // Position x, y
pattern1 = Pattern.compile(expression, Pattern.DOTALL);
matcher1 = pattern1.matcher(components[2]);
while (matcher1.find()) {
skybackground = matcher1.group(1);
values[8] = Float.valueOf(skybackground);
//System.out.println(skybackground);
}
}else{
values[0] = new Float(-999.0);
values[1] = new Float(-999.0);
values[2] = new Float(-999.0);
values[3] = new Float(-999.0);
values[4] = new Float(-999.0);
values[5] = new Float(-999.0);
values[6] = new Float(-999.0);
values[7] = new Float(-999.0);
values[8] = new Float(-999.0);
}
newTable.addRow(values);
//modify the vo-table, but how? --> instead of this, create a new one and then do a join
}
rseq.close();
resultVoTable = starTableToString(newTable);
System.out.println(resultVoTable);
}catch (Exception ex){
String error = "Exception during the process: \n"+ ex.getMessage();
throw new Exception(error);
}
Comments (0)
No comments yet
Log in to make a comment