25 #include <sys/types.h> 27 #include "base/Optionpk.h" 28 #include "base/Vector2d.h" 29 #include "algorithms/Filter.h" 30 #include "fileclasses/FileReaderAscii.h" 33 #include <gsl/gsl_sort.h> 86 int main(
int argc,
char **argv) {
89 Optionpk<int> inputCols_opt(
"ic",
"inputCols",
"input columns (e.g., for three dimensional input data in first three columns use: -ic 0 -ic 1 -ic 2");
90 Optionpk<std::string> method_opt(
"f",
"filter",
"filter function (to be implemented: dwt, dwti,dwt_cut)");
91 Optionpk<std::string> wavelet_type_opt(
"wt",
"wavelet",
"wavelet type: daubechies,daubechies_centered, haar, haar_centered, bspline, bspline_centered",
"daubechies");
92 Optionpk<int> family_opt(
"wf",
"family",
"wavelet family (vanishing moment, see also http://www.gnu.org/software/gsl/manual/html_node/DWT-Initialization.html)", 4);
93 Optionpk<double> threshold_opt(
"cut",
"cut",
"threshold to cut dwt coefficients. Use 0 to keep all.", 0);
94 Optionpk<int> dimZ_opt(
"dz",
"dz",
"filter kernel size in z (band or spectral dimension), must be odd (example: 3).. Set dz>0 if 1-D filter must be used in band domain");
95 Optionpk<double> tapZ_opt(
"tapz",
"tapz",
"taps used for spectral filtering");
96 Optionpk<double> fwhm_opt(
"fwhm",
"fwhm",
"list of full width half to apply spectral filtering (-fwhm band1 -fwhm band2 ...)");
97 Optionpk<std::string> srf_opt(
"srf",
"srf",
"list of ASCII files containing spectral response functions (two columns: wavelength response)");
98 Optionpk<int> wavelengthIn_opt(
"win",
"wavelengthIn",
"column number of input ASCII file containing wavelengths");
99 Optionpk<double> wavelengthOut_opt(
"wout",
"wavelengthOut",
"list of wavelengths in output spectrum (-wout band1 -wout band2 ...)");
100 Optionpk<std::string> interpolationType_opt(
"interp",
"interp",
"type of interpolation for spectral filtering (see http://www.gnu.org/software/gsl/manual/html_node/Interpolation-Types.html)",
"akima");
101 Optionpk<bool> transpose_opt(
"t",
"transpose",
"transpose output with samples in rows and wavelengths in cols",
false);
102 Optionpk<short> verbose_opt(
"v",
"verbose",
"verbose mode if > 0", 0,2);
107 wavelengthIn_opt.setHide(1);
108 wavelengthOut_opt.setHide(1);
109 interpolationType_opt.setHide(1);
110 transpose_opt.setHide(1);
111 wavelet_type_opt.setHide(1);
112 family_opt.setHide(1);
113 threshold_opt.setHide(1);
117 doProcess=input_opt.retrieveOption(argc,argv);
118 output_opt.retrieveOption(argc,argv);
119 inputCols_opt.retrieveOption(argc,argv);
120 method_opt.retrieveOption(argc,argv);
121 dimZ_opt.retrieveOption(argc,argv);
122 tapZ_opt.retrieveOption(argc,argv);
123 fwhm_opt.retrieveOption(argc,argv);
124 srf_opt.retrieveOption(argc,argv);
125 wavelengthIn_opt.retrieveOption(argc,argv);
126 wavelengthOut_opt.retrieveOption(argc,argv);
127 interpolationType_opt.retrieveOption(argc,argv);
128 transpose_opt.retrieveOption(argc,argv);
129 wavelet_type_opt.retrieveOption(argc,argv);
130 family_opt.retrieveOption(argc,argv);
131 threshold_opt.retrieveOption(argc,argv);
132 verbose_opt.retrieveOption(argc,argv);
134 catch(
string predefinedString){
135 std::cout << predefinedString << std::endl;
140 cout <<
"Usage: pkfilterascii -i input.txt [-ic column]*" << endl;
142 std::cout <<
"short option -h shows basic options only, use long option --help to show all options" << std::endl;
148 std::vector<double> wavelengthIn;
149 std::vector<double> wavelengthOut;
150 assert(input_opt.size());
152 if(wavelengthIn_opt.size())
153 asciiReader.readData(wavelengthIn,wavelengthIn_opt[0]);
154 assert(inputCols_opt.size());
155 asciiReader.readData(inputData,inputCols_opt);
157 std::cout <<
"wavelengthIn.size(): " << wavelengthIn.size() << std::endl;
158 std::cout <<
"inputData[0].size(): " << inputData[0].size() << std::endl;
160 if(wavelengthIn.size())
161 assert(wavelengthIn.size()==inputData[0].size());
165 filteredData.resize(inputCols_opt.size(),wavelengthOut_opt.size());
166 assert(wavelengthIn_opt.size());
168 std::cout <<
"spectral filtering to " << fwhm_opt.size() <<
" bands with provided fwhm " << std::endl;
169 assert(wavelengthOut_opt.size()==fwhm_opt.size());
170 std::vector<double> fwhmData(wavelengthOut_opt.size());
171 for(
int icol=0;icol<inputCols_opt.size();++icol)
172 filter1d.applyFwhm<
double>(wavelengthIn,inputData[icol], wavelengthOut_opt,fwhm_opt, interpolationType_opt[0], filteredData[icol],verbose_opt[0]);
174 std::cout <<
"spectra filtered to " << wavelengthOut_opt.size() <<
" bands" << std::endl;
175 wavelengthOut=wavelengthOut_opt;
177 else if(srf_opt.size()){
178 wavelengthOut.resize(srf_opt.size());
179 filteredData.resize(inputCols_opt.size(),srf_opt.size());
182 std::cout <<
"spectral filtering to " << srf_opt.size() <<
" bands with provided SRF " << std::endl;
183 std::vector< Vector2d<double> > srf(srf_opt.size());
185 for(
int isrf=0;isrf<srf_opt.size();++isrf){
187 srfFile.open(srf_opt[isrf].c_str());
190 srf[isrf][0].push_back(0);
191 srf[isrf][1].push_back(0);
192 srf[isrf][0].push_back(1);
193 srf[isrf][1].push_back(0);
195 srf[isrf][0].push_back(v);
197 srf[isrf][1].push_back(v);
201 srf[isrf][0].push_back(srf[isrf][0].back()+1);
202 srf[isrf][1].push_back(0);
203 srf[isrf][0].push_back(srf[isrf][0].back()+1);
204 srf[isrf][1].push_back(0);
206 cout <<
"srf file details: " << srf[isrf][0].size() <<
" wavelengths defined" << endl;
207 if(verbose_opt[0]>1){
208 for(
int iw=0;iw<srf[isrf][0].size();++iw)
209 std::cout << srf[isrf][0][iw] <<
" " << srf[isrf][1][iw] << std::endl;
212 double centreWavelength=0;
213 for(
int icol=0;icol<inputCols_opt.size();++icol)
214 filteredData[icol].resize(srf.size());
216 for(
int isrf=0;isrf<srf.size();++isrf){
219 centreWavelength=filter1d.applySrf<
double>(wavelengthIn,inputData,srf[isrf], interpolationType_opt[0], srfData[isrf], delta, normalize,1,
true, verbose_opt[0]);
221 std::cout <<
"centre wavelength srf " << isrf <<
": " << centreWavelength << std::endl;
222 wavelengthOut[isrf]=
static_cast<int>(centreWavelength+0.5);
224 srfData.transpose(filteredData);
226 std::cout <<
"spectra filtered to " << srf.size() <<
" bands" << std::endl;
230 std::cout <<
"no filtering selected" << std::endl;
231 for(
int icol=0;icol<inputCols_opt.size();++icol)
232 filteredData[icol]=inputData[icol];
235 if(method_opt.size()){
236 wavelengthOut=wavelengthIn;
237 for(
int icol=0;icol<inputCols_opt.size();++icol){
238 switch(filter::Filter::getFilterType(method_opt[0])){
240 filter1d.dwtForward(filteredData[icol],wavelet_type_opt[0],family_opt[0]);
243 filter1d.dwtInverse(filteredData[icol],wavelet_type_opt[0],family_opt[0]);
245 case(filter::dwt_cut):
246 filter1d.dwtCut(filteredData[icol],wavelet_type_opt[0],family_opt[0],threshold_opt[0]);
248 case(filter::smooth):
250 filter1d.setTaps(tapZ_opt);
251 filter1d.filter(inputData[icol],filteredData[icol]);
254 assert(dimZ_opt.size());
255 filter1d.smooth(inputData[icol],filteredData[icol],dimZ_opt[0]);
259 assert(tapZ_opt.size());
260 filter1d.filter(inputData[icol],filteredData[icol]);
268 ofstream outputStream;
269 if(!output_opt.empty())
270 outputStream.open(output_opt[0].c_str(),ios::out);
273 std::cout <<
"stream to output" << std::endl;
274 if(transpose_opt[0]){
275 for(
int icol=0;icol<inputCols_opt.size();++icol){
276 for(
int iband=0;iband<filteredData[icol].size();++iband){
277 if(!output_opt.empty()){
278 outputStream << filteredData[icol][iband];
279 if(iband<filteredData[icol].size()-1)
282 outputStream << std::endl;
285 std::cout << filteredData[icol][iband];
286 if(iband<filteredData[icol].size()-1)
289 std::cout << std::endl;
297 if(method_opt.size()){
298 switch(filter::Filter::getFilterType(method_opt[0])){
300 nband=filteredData[0].size();
303 nband=filteredData[0].size();
305 case(filter::dwt_cut):
306 nband=filteredData[0].size();
309 nband=wavelengthOut.size();
314 nband=wavelengthOut.size();
316 std::cout <<
"number of bands: " << nband << std::endl;
317 std::cout <<
"wavelengthOut.size(): " << wavelengthOut.size() << std::endl;
319 for(
int iband=0;iband<nband;++iband){
320 if(!output_opt.empty()){
321 if(wavelengthOut.size())
322 outputStream << wavelengthOut[iband] <<
" ";
323 else if(wavelengthIn_opt.size())
324 outputStream << iband <<
" ";
327 if(wavelengthOut.size())
328 std::cout << wavelengthOut[iband] <<
" ";
329 else if(wavelengthIn_opt.size())
330 std::cout << iband <<
" ";
332 for(
int icol=0;icol<inputCols_opt.size();++icol){
333 if(!output_opt.empty()){
334 outputStream << filteredData[icol][iband];
335 if(icol<inputCols_opt.size()-1)
338 outputStream << std::endl;
341 std::cout << filteredData[icol][iband];
342 if(icol<inputCols_opt.size()-1)
345 std::cout << std::endl;
350 if(!output_opt.empty())
351 outputStream.close();