[137] | 1 | #include <iostream> |
---|
| 2 | #include <iomanip> |
---|
| 3 | #include <string> |
---|
| 4 | #include <wcs.h> |
---|
| 5 | #include <wcshdr.h> |
---|
| 6 | #include <fitshdr.h> |
---|
| 7 | #include <wcsfix.h> |
---|
| 8 | #include <wcsunits.h> |
---|
| 9 | #define WCSLIB_GETWCSTAB // define this so that we don't try and redefine wtbarr |
---|
| 10 | // (this is a problem when using gcc v.4+ |
---|
| 11 | #include <fitsio.h> |
---|
| 12 | #include <math.h> |
---|
| 13 | #include <duchamp.hh> |
---|
| 14 | #include <param.hh> |
---|
| 15 | #include <Cubes/cubes.hh> |
---|
| 16 | #include <Detection/detection.hh> |
---|
| 17 | #include <ATrous/atrous.hh> |
---|
| 18 | #include <Utils/utils.hh> |
---|
| 19 | |
---|
| 20 | string imageType[4] = {"point", "spectrum", "image", "cube"}; |
---|
| 21 | |
---|
| 22 | vector<Detection> readAndSearch(Param &par) |
---|
| 23 | { |
---|
| 24 | vector<Detection> outputList; |
---|
| 25 | |
---|
| 26 | short int maxdim=3; |
---|
| 27 | long *dimAxes = new long[maxdim]; |
---|
| 28 | for(int i=0;i<maxdim;i++) dimAxes[i]=1; |
---|
| 29 | long nelements; |
---|
| 30 | int bitpix,numAxes; |
---|
| 31 | int status = 0, nkeys; /* MUST initialize status */ |
---|
| 32 | fitsfile *fptr; |
---|
| 33 | |
---|
| 34 | string fname = par.getImageFile(); |
---|
| 35 | if(par.getFlagSubsection()){ |
---|
| 36 | par.parseSubsection(); |
---|
| 37 | fname+=par.getSubsection(); |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){ |
---|
| 41 | fits_report_error(stderr, status); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | status = 0; |
---|
| 45 | fits_get_img_param(fptr, maxdim, &bitpix, &numAxes, dimAxes, &status); |
---|
| 46 | if(status){ |
---|
| 47 | fits_report_error(stderr, status); |
---|
| 48 | } |
---|
| 49 | status = 0; |
---|
| 50 | fits_close_file(fptr, &status); |
---|
| 51 | if (status){ |
---|
| 52 | duchampWarning("readAndSearch","Error closing file: "); |
---|
| 53 | fits_report_error(stderr, status); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | if(numAxes<=3) |
---|
| 57 | std::cout << "Dimensions of " << imageType[numAxes] << ": " << dimAxes[0]; |
---|
| 58 | else std::cout << "Dimensions of " << imageType[3] << ": " << dimAxes[0]; |
---|
| 59 | if(numAxes>1) std::cout << "x" << dimAxes[1]; |
---|
| 60 | if(numAxes>2) std::cout << "x" << dimAxes[2]; |
---|
| 61 | std::cout << std::endl; |
---|
| 62 | |
---|
| 63 | int anynul; |
---|
| 64 | long *fpixel = new long[numAxes]; |
---|
| 65 | long *lpixel = new long[numAxes]; |
---|
[160] | 66 | long *inc = new long[numAxes]; // the data-sampling increment |
---|
[137] | 67 | for(int i=0;i<numAxes;i++) inc[i]=1; |
---|
| 68 | |
---|
| 69 | //--------------------------------------------------------- |
---|
| 70 | // Read in spectra one at a time, reconstruct if necessary |
---|
| 71 | // and then search, adding found objects to cube's object list. |
---|
| 72 | |
---|
| 73 | fpixel[0] = 1; |
---|
| 74 | for(int i=2;i<numAxes;i++) fpixel[i] = 1; |
---|
| 75 | |
---|
| 76 | lpixel[0] = dimAxes[0]; |
---|
| 77 | lpixel[2] = dimAxes[2]; |
---|
| 78 | for(int i=3;i<numAxes;i++) lpixel[i] = 1; |
---|
| 79 | |
---|
| 80 | int numDetected = 0; |
---|
| 81 | |
---|
| 82 | std::cout << " 0: |"; |
---|
[175] | 83 | printSpace(50); |
---|
[137] | 84 | std::cout << "| " << std::setw(5) << numDetected << std::flush; |
---|
[175] | 85 | printBackSpace(64); |
---|
| 86 | std::cout << std::flush; |
---|
[137] | 87 | |
---|
| 88 | float *bigarray = new float[dimAxes[0] * dimAxes[2]]; |
---|
| 89 | float *array = new float[dimAxes[2]]; |
---|
| 90 | long *specdim = new long[2]; |
---|
| 91 | specdim[0] = dimAxes[2]; specdim[1]=1; |
---|
| 92 | |
---|
| 93 | float median,sigma; |
---|
| 94 | int pos,frac; |
---|
| 95 | |
---|
| 96 | Image *spectrum = new Image(specdim); |
---|
| 97 | spectrum->saveParam(par); |
---|
| 98 | spectrum->pars().setBeamSize(2.); // only neighbouring channels correlated |
---|
| 99 | |
---|
| 100 | for(int y=0;y<dimAxes[1];y++){ |
---|
| 101 | |
---|
| 102 | std::cout << std::setw(4) << y << ": " << std::flush; |
---|
| 103 | |
---|
| 104 | fpixel[1] = lpixel[1] = y+1; |
---|
| 105 | |
---|
| 106 | status = 0; |
---|
| 107 | if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){ |
---|
| 108 | duchampWarning("readAndSearch","Error opening file: "); |
---|
| 109 | fits_report_error(stderr, status); |
---|
| 110 | } |
---|
| 111 | status = 0; |
---|
| 112 | fits_read_subset(fptr, TFLOAT, fpixel, lpixel, inc, NULL, bigarray, &anynul, &status); |
---|
| 113 | if(status){ |
---|
| 114 | duchampError("readAndSearch","There was an error reading in the data array:"); |
---|
| 115 | fits_report_error(stderr, status); |
---|
| 116 | } |
---|
| 117 | status = 0; |
---|
| 118 | fits_close_file(fptr, &status); |
---|
| 119 | if (status){ |
---|
| 120 | duchampWarning("readAndSearch","Error closing file: "); |
---|
| 121 | fits_report_error(stderr, status); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | for(int x=0;x<dimAxes[0];x++){ |
---|
| 125 | |
---|
| 126 | pos = y*dimAxes[0]+x; |
---|
| 127 | frac = 100*(pos+1)/(dimAxes[1]*dimAxes[0]); |
---|
| 128 | if(frac%2 == 0){ |
---|
| 129 | std::cout << "|"; |
---|
[175] | 130 | printHash(frac/2); |
---|
| 131 | printSpace(50-frac/2); |
---|
[137] | 132 | std::cout << "| " << std::setw(5) << numDetected; |
---|
[175] | 133 | printBackSpace(58); |
---|
[137] | 134 | std::cout << std::flush; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | for(int z=0;z<dimAxes[2];z++) array[z] = bigarray[z*dimAxes[0] + x]; |
---|
| 138 | |
---|
| 139 | median = findMedian(array,dimAxes[2]); |
---|
| 140 | spectrum->clearDetectionList(); |
---|
| 141 | if(par.getFlagATrous()){ |
---|
| 142 | float *recon = new float[dimAxes[2]]; |
---|
| 143 | atrous1DReconstruct(dimAxes[2],array,recon,par); |
---|
| 144 | float *resid = new float[dimAxes[2]]; |
---|
| 145 | for(int i=0;i<dimAxes[2];i++) resid[i] = array[i] - recon[i]; |
---|
| 146 | sigma = findMADFM(recon,dimAxes[2])/correctionFactor; |
---|
| 147 | spectrum->saveArray(recon,dimAxes[2]); |
---|
[205] | 148 | delete [] recon; |
---|
| 149 | delete [] resid; |
---|
[137] | 150 | spectrum->removeMW(); // only works if flagMW is true |
---|
| 151 | spectrum->setStats(median,sigma,par.getCut()); |
---|
| 152 | if(par.getFlagFDR()) spectrum->setupFDR(); |
---|
| 153 | spectrum->setMinSize(par.getMinChannels()); |
---|
| 154 | spectrum->spectrumDetect(); |
---|
| 155 | } |
---|
| 156 | else{ |
---|
| 157 | sigma = findMADFM(array,dimAxes[2])/correctionFactor; |
---|
| 158 | spectrum->saveArray(array,dimAxes[2]); |
---|
| 159 | spectrum->removeMW(); // only works if flagMW is true |
---|
| 160 | spectrum->setStats(median,sigma,par.getCut()); |
---|
| 161 | if(par.getFlagFDR()) spectrum->setupFDR(); |
---|
| 162 | spectrum->setMinSize(par.getMinChannels()); |
---|
| 163 | spectrum->spectrumDetect(); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | numDetected += spectrum->getNumObj(); |
---|
| 167 | for(int obj=0;obj<spectrum->getNumObj();obj++){ |
---|
| 168 | Detection *object = new Detection; |
---|
| 169 | *object = spectrum->getObject(obj); |
---|
| 170 | for(int pix=0;pix<object->getSize();pix++) { |
---|
| 171 | // Fix up coordinates of each pixel to match original array |
---|
| 172 | object->setZ(pix, object->getX(pix)); |
---|
| 173 | object->setX(pix, x); |
---|
| 174 | object->setY(pix, y); |
---|
| 175 | object->setF(pix, array[object->getZ(pix)]); |
---|
| 176 | // NB: set F to the original value, not the recon value. |
---|
| 177 | } |
---|
| 178 | object->addOffsets(par); |
---|
| 179 | object->calcParams(); |
---|
| 180 | mergeIntoList(*object,outputList,par); |
---|
| 181 | delete object; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | |
---|
| 185 | } // end of loop over y |
---|
| 186 | |
---|
[175] | 187 | printBackSpace(6); |
---|
| 188 | std::cout << std::flush; |
---|
[137] | 189 | } // end of loop over x |
---|
| 190 | |
---|
| 191 | delete spectrum; |
---|
[205] | 192 | delete [] bigarray; |
---|
| 193 | delete [] array; |
---|
| 194 | delete [] fpixel; |
---|
| 195 | delete [] lpixel; |
---|
| 196 | delete [] inc; |
---|
| 197 | delete [] dimAxes; |
---|
| 198 | delete [] specdim; |
---|
[137] | 199 | |
---|
| 200 | return outputList; |
---|
| 201 | |
---|
| 202 | } |
---|