1 package org.astrogrid.ace.sextractor;
2
3
4 import org.astrogrid.common.wrapper.*;
5 import java.io.*;
6
7 import org.astrogrid.ace.service.*;
8 import org.astrogrid.common.ucd.*;
9 import org.astrogrid.tools.ascii.*;
10
11 import org.astrogrid.log.*;
12
13 /***
14 * File Writer that creates the native SExtractor files from the dictionary
15 * and parameter bundles
16 *
17 * @author M Hill
18 */
19
20 public class SexNativeFileWriter
21 {
22
23 public static void writeNativeFiles(AceParameterBundle bundle, String configFilename, String paramFilename) throws IOException
24 {
25 writeNativeConfigFile(bundle, configFilename);
26 writeNativeParamFile(bundle, paramFilename);
27 }
28
29 public static void writeNativeConfigFile(AceParameterBundle bundle, String filename) throws IOException
30 {
31 bundle.setConfigFile(filename);
32
33 AsciiOutputStream out = new AsciiOutputStream(new FileOutputStream(filename));
34 writeNativeConfigFile(bundle, out);
35 }
36
37 /***
38 Writes out the parameters in SExtractor config file format
39 <p>
40 */
41 public static void writeNativeConfigFile(AceParameterBundle bundle, AsciiOutputStream out) throws IOException
42 {
43 AceDictionary dictionary = AceDictionary.getInstance();
44
45 String[] configTerms = dictionary.getLocalConfigFileDictionary();
46
47 for (int i=0;i<configTerms.length;i++)
48 {
49 Parameter parameter = bundle.getChild(configTerms[i]);
50
51
52 if ( dictionary.isLocalTermRequired(configTerms[i])
53 && (parameter == null))
54 {
55 throw new RuntimeException("Required term "+configTerms[i]+" is missing from parameter bundle");
56 }
57
58 if (parameter != null)
59 {
60 writeNativeConfigParameter(parameter, out);
61 }
62
63 }
64
65 }
66
67
68 public static void writeNativeConfigParameter(Parameter parameter, OutputStream out) throws IOException
69 {
70 String value = "";
71
72 if (parameter instanceof SingleParameter)
73 {
74 value = ((SingleParameter) parameter).getValue().trim();
75
76 if (value.equals("true"))
77 {
78 value = "Y";
79 }
80 else if (value.equals("false"))
81 {
82 value = "N";
83 }
84 }
85 else if (parameter instanceof IndexedParameter)
86 {
87 String[] allValues = ((IndexedParameter) parameter).getValues();
88 for (int j=0;j<allValues.length;j++)
89 {
90 value = value + allValues[j]+" ";
91 }
92 }
93 else
94 {
95 throw new RuntimeException("AceParameterBundle contains child bundles, and it should not");
96 }
97
98 String line = parameter.getId() + " " + value + '\n';
99 out.write(line.getBytes());
100 }
101
102 /***
103 * Writes out the SExtractor native Param file - the list of columns that
104 * will be included in the output file
105 <p>
106 */
107 public static void writeNativeParamFile(AceParameterBundle bundle, String filename) throws IOException
108 {
109 Log.trace("Writing native param (output cols) file '"+filename+"...");
110
111 bundle.setParamFile(filename);
112
113 String[] outputCols = bundle.getOutputColumns();
114 Log.affirm(outputCols != null, "No output columns given - this should have been validated earlier");
115
116 AsciiOutputStream out = new AsciiOutputStream(new FileOutputStream(filename));
117
118 out.writeLine("NUMBER");
119 for (int i=0;i<outputCols.length;i++)
120 {
121 if (outputCols[i].equalsIgnoreCase("MAG_APER") ||
122 outputCols[i].equalsIgnoreCase("MAG_APERERR") ||
123 outputCols[i].equalsIgnoreCase("FLUX_APER") ||
124 outputCols[i].equalsIgnoreCase("FLUX_APERERR"))
125 {
126
127
128 IndexedParameter p = (IndexedParameter) bundle.getChild("PHOT_APERTURES");
129 out.writeLine(outputCols[i]+"("+p.getChildCount()+")");
130 }
131 else
132 {
133 out.writeLine(outputCols[i]);
134 }
135 }
136 out.close();
137 }
138
139
140 }
141