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>
41 int main(
int argc,
char **argv) {
44 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");
45 Optionpk<std::string> method_opt(
"f",
"filter",
"filter function (to be implemented: dwt, dwti,dwt_cut)");
46 Optionpk<std::string> wavelet_type_opt(
"wt",
"wavelet",
"wavelet type: daubechies,daubechies_centered, haar, haar_centered, bspline, bspline_centered",
"daubechies");
47 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);
48 Optionpk<double> threshold_opt(
"cut",
"cut",
"threshold to cut dwt coefficients. Use 0 to keep all.", 0);
49 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");
50 Optionpk<double> tapZ_opt(
"tapz",
"tapz",
"taps used for spectral filtering");
51 Optionpk<double> fwhm_opt(
"fwhm",
"fwhm",
"list of full width half to apply spectral filtering (-fwhm band1 -fwhm band2 ...)");
52 Optionpk<std::string> srf_opt(
"srf",
"srf",
"list of ASCII files containing spectral response functions (two columns: wavelength response)");
53 Optionpk<int> wavelengthIn_opt(
"win",
"wavelengthIn",
"column number of input ASCII file containing wavelengths");
54 Optionpk<double> wavelengthOut_opt(
"wout",
"wavelengthOut",
"list of wavelengths in output spectrum (-wout band1 -wout band2 ...)");
55 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");
56 Optionpk<bool> transpose_opt(
"t",
"transpose",
"transpose output with samples in rows and wavelengths in cols",
false);
57 Optionpk<short> verbose_opt(
"v",
"verbose",
"verbose mode if > 0", 0,2);
62 wavelengthIn_opt.setHide(1);
63 wavelengthOut_opt.setHide(1);
64 interpolationType_opt.setHide(1);
65 transpose_opt.setHide(1);
66 wavelet_type_opt.setHide(1);
67 family_opt.setHide(1);
68 threshold_opt.setHide(1);
72 doProcess=input_opt.retrieveOption(argc,argv);
73 output_opt.retrieveOption(argc,argv);
74 inputCols_opt.retrieveOption(argc,argv);
75 method_opt.retrieveOption(argc,argv);
76 dimZ_opt.retrieveOption(argc,argv);
77 tapZ_opt.retrieveOption(argc,argv);
78 fwhm_opt.retrieveOption(argc,argv);
79 srf_opt.retrieveOption(argc,argv);
80 wavelengthIn_opt.retrieveOption(argc,argv);
81 wavelengthOut_opt.retrieveOption(argc,argv);
82 interpolationType_opt.retrieveOption(argc,argv);
83 transpose_opt.retrieveOption(argc,argv);
84 wavelet_type_opt.retrieveOption(argc,argv);
85 family_opt.retrieveOption(argc,argv);
86 threshold_opt.retrieveOption(argc,argv);
87 verbose_opt.retrieveOption(argc,argv);
89 catch(
string predefinedString){
90 std::cout << predefinedString << std::endl;
95 cout <<
"Usage: pkfilterascii -i input.txt [-ic column]*" << endl;
97 std::cout <<
"short option -h shows basic options only, use long option --help to show all options" << std::endl;
103 std::vector<double> wavelengthIn;
104 std::vector<double> wavelengthOut;
105 assert(input_opt.size());
107 if(wavelengthIn_opt.size())
108 asciiReader.readData(wavelengthIn,wavelengthIn_opt[0]);
109 assert(inputCols_opt.size());
110 asciiReader.readData(inputData,inputCols_opt);
112 std::cout <<
"wavelengthIn.size(): " << wavelengthIn.size() << std::endl;
113 std::cout <<
"inputData[0].size(): " << inputData[0].size() << std::endl;
115 if(wavelengthIn.size())
116 assert(wavelengthIn.size()==inputData[0].size());
120 filteredData.resize(inputCols_opt.size(),wavelengthOut_opt.size());
121 assert(wavelengthIn_opt.size());
123 std::cout <<
"spectral filtering to " << fwhm_opt.size() <<
" bands with provided fwhm " << std::endl;
124 assert(wavelengthOut_opt.size()==fwhm_opt.size());
125 std::vector<double> fwhmData(wavelengthOut_opt.size());
126 for(
int icol=0;icol<inputCols_opt.size();++icol)
127 filter1d.applyFwhm<
double>(wavelengthIn,inputData[icol], wavelengthOut_opt,fwhm_opt, interpolationType_opt[0], filteredData[icol],verbose_opt[0]);
129 std::cout <<
"spectra filtered to " << wavelengthOut_opt.size() <<
" bands" << std::endl;
130 wavelengthOut=wavelengthOut_opt;
132 else if(srf_opt.size()){
133 wavelengthOut.resize(srf_opt.size());
134 filteredData.resize(inputCols_opt.size(),srf_opt.size());
137 std::cout <<
"spectral filtering to " << srf_opt.size() <<
" bands with provided SRF " << std::endl;
138 std::vector< Vector2d<double> > srf(srf_opt.size());
140 for(
int isrf=0;isrf<srf_opt.size();++isrf){
142 srfFile.open(srf_opt[isrf].c_str());
145 srf[isrf][0].push_back(0);
146 srf[isrf][1].push_back(0);
147 srf[isrf][0].push_back(1);
148 srf[isrf][1].push_back(0);
150 srf[isrf][0].push_back(v);
152 srf[isrf][1].push_back(v);
156 srf[isrf][0].push_back(srf[isrf][0].back()+1);
157 srf[isrf][1].push_back(0);
158 srf[isrf][0].push_back(srf[isrf][0].back()+1);
159 srf[isrf][1].push_back(0);
161 cout <<
"srf file details: " << srf[isrf][0].size() <<
" wavelengths defined" << endl;
162 if(verbose_opt[0]>1){
163 for(
int iw=0;iw<srf[isrf][0].size();++iw)
164 std::cout << srf[isrf][0][iw] <<
" " << srf[isrf][1][iw] << std::endl;
167 double centreWavelength=0;
168 for(
int icol=0;icol<inputCols_opt.size();++icol)
169 filteredData[icol].resize(srf.size());
171 for(
int isrf=0;isrf<srf.size();++isrf){
174 centreWavelength=filter1d.applySrf<
double>(wavelengthIn,inputData,srf[isrf], interpolationType_opt[0], srfData[isrf], delta, normalize,1,
true, verbose_opt[0]);
176 std::cout <<
"centre wavelength srf " << isrf <<
": " << centreWavelength << std::endl;
177 wavelengthOut[isrf]=
static_cast<int>(centreWavelength+0.5);
179 srfData.transpose(filteredData);
181 std::cout <<
"spectra filtered to " << srf.size() <<
" bands" << std::endl;
185 std::cout <<
"no filtering selected" << std::endl;
186 for(
int icol=0;icol<inputCols_opt.size();++icol)
187 filteredData[icol]=inputData[icol];
190 if(method_opt.size()){
191 wavelengthOut=wavelengthIn;
192 for(
int icol=0;icol<inputCols_opt.size();++icol){
193 switch(filter::Filter::getFilterType(method_opt[0])){
195 filter1d.dwtForward(filteredData[icol],wavelet_type_opt[0],family_opt[0]);
198 filter1d.dwtInverse(filteredData[icol],wavelet_type_opt[0],family_opt[0]);
200 case(filter::dwt_cut):
201 filter1d.dwtCut(filteredData[icol],wavelet_type_opt[0],family_opt[0],threshold_opt[0]);
203 case(filter::smooth):
205 filter1d.setTaps(tapZ_opt);
206 filter1d.filter(inputData[icol],filteredData[icol]);
209 assert(dimZ_opt.size());
210 filter1d.smooth(inputData[icol],filteredData[icol],dimZ_opt[0]);
214 assert(tapZ_opt.size());
215 filter1d.filter(inputData[icol],filteredData[icol]);
223 ofstream outputStream;
224 if(!output_opt.empty())
225 outputStream.open(output_opt[0].c_str(),ios::out);
228 std::cout <<
"stream to output" << std::endl;
229 if(transpose_opt[0]){
230 for(
int icol=0;icol<inputCols_opt.size();++icol){
231 for(
int iband=0;iband<filteredData[icol].size();++iband){
232 if(!output_opt.empty()){
233 outputStream << filteredData[icol][iband];
234 if(iband<filteredData[icol].size()-1)
237 outputStream << std::endl;
240 std::cout << filteredData[icol][iband];
241 if(iband<filteredData[icol].size()-1)
244 std::cout << std::endl;
252 if(method_opt.size()){
253 switch(filter::Filter::getFilterType(method_opt[0])){
255 nband=filteredData[0].size();
258 nband=filteredData[0].size();
260 case(filter::dwt_cut):
261 nband=filteredData[0].size();
264 nband=wavelengthOut.size();
269 nband=wavelengthOut.size();
271 std::cout <<
"number of bands: " << nband << std::endl;
272 std::cout <<
"wavelengthOut.size(): " << wavelengthOut.size() << std::endl;
274 for(
int iband=0;iband<nband;++iband){
275 if(!output_opt.empty()){
276 if(wavelengthOut.size())
277 outputStream << wavelengthOut[iband] <<
" ";
278 else if(wavelengthIn_opt.size())
279 outputStream << iband <<
" ";
282 if(wavelengthOut.size())
283 std::cout << wavelengthOut[iband] <<
" ";
284 else if(wavelengthIn_opt.size())
285 std::cout << iband <<
" ";
287 for(
int icol=0;icol<inputCols_opt.size();++icol){
288 if(!output_opt.empty()){
289 outputStream << filteredData[icol][iband];
290 if(icol<inputCols_opt.size()-1)
293 outputStream << std::endl;
296 std::cout << filteredData[icol][iband];
297 if(icol<inputCols_opt.size()-1)
300 std::cout << std::endl;
305 if(!output_opt.empty())
306 outputStream.close();