Ctunes.cpp 6.9 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5 6 7 8 9
/*****************************************************************************/
/*                                                                           */
/* Class TUNE                                                                */
/* ==============                                                            */
/*                                                                           */
/* ASM, September 2001                                                       */
/*****************************************************************************/
#include <algorithm>
#include <vector>
kraus's avatar
kraus committed
10
#include <cstring>
gsell's avatar
gsell committed
11

kraus's avatar
kraus committed
12 13 14 15
#include "Utility/Inform.h"

#include "Algorithms/Ctunes.h"
#include "Algorithms/lomb.h"
gsell's avatar
gsell committed
16 17 18 19 20 21

extern Inform *gmsg;

//RANLIB_class rndm(265314159,4);


kraus's avatar
kraus committed
22 23 24 25 26
TUNE_class::TUNE_class():
        ofac(0.0),
        hifac(0.0),
        Qmin(0.0),
        Qmax(0.0)
gsell's avatar
gsell committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/*---------------------------------------------------------------------------*
 * constructor
 * ===========
 *
 *---------------------------------------------------------------------------*/
{
}

TUNE_class::~TUNE_class(void)
/*---------------------------------------------------------------------------*
 * destructor
 * ==========
 *
 *---------------------------------------------------------------------------*/
{

    // Do nothing......

}

47
int TUNE_class::lombAnalysis(std::vector<double> &x, std::vector<double> &y, int /*nhis*/, double Norm)
gsell's avatar
gsell committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
/*-----------------------------------------------------------------------------
 *  Launch Lomb analysis and plot results
 *  =======================================
 *
 *---------------------------------------------------------------------------*/
{

    int Ndat = x.size();
    int    i, nout, jmax;
    int    pairc;
    int    datcnt = 0;
    int stat = 0;
    double prob, probi;
    double tofac = 0.8;

    LOMB_TYPE tlom;

    CI_lt p, q, r, s, tp;

    char   mess[80];
    LOMB_class *la;
69
    std::vector<LOMB_TYPE> lodata, lodata2;
gsell's avatar
gsell committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    /*---------------------------------------------------------------------------*/

    /*
     * Do Lomb analysis
     * ================
     */

    for(int j = 0; j < Ndat; j++) {
        tlom.x = x[j];
        tlom.y = y[j];
        lodata.push_back(tlom);
    }

    p = lodata.begin();
    q = lodata.end();

    datcnt = (int) count_if(p, q, Lomb_eq(0.));

    if(datcnt > (q - p - 10)) {
        memset(mess, '\0', sizeof(mess));
        sprintf(mess, "Just found %d data points that are == 0!", datcnt);
        *gmsg << "* " << mess << endl;
        return(-1);
    }

    // this parameterset works ok in most cases.....
    ofac  = 4.0;
    hifac = 0.8;
    Qmin  = 0.2;
    Qmax  = 0.4;

    la = new LOMB_class(1);

    stat = 0;
    stat = la->period(&lodata, &lodata2, ofac, hifac, &nout, &jmax, &prob, 0);
    if(stat != 0) {
        memset(mess, '\0', sizeof(mess));
        sprintf(mess, "@C3ERROR: Lomb analysis failed!");
        *gmsg << "* " << mess << endl;
kraus's avatar
kraus committed
109

gsell's avatar
gsell committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
        delete la;
        la = NULL;
        return(-1);
    }

    double pairx[nout];
    double pairy[nout];

    pairc = 0;
    for(i = 0; i < nout; i++) {
        if(lodata2[i].y > 2.) {
            pairx[pairc] = lodata2[i].x;
            pairy[pairc] = lodata2[i].y;
            if((pairy[pairc] > pairy[pairc-1]) &&
               (pairy[pairc] > lodata2[i+1].y)) {
125
                probi = la->signi(&pairy[pairc], &nout, &tofac);
gsell's avatar
gsell committed
126 127 128
                if(pairy[pairc] > 4.) {
                    memset(mess, '\0', sizeof(mess));
                    sprintf(mess, "%12.8f %8.2f %8.3f %d", pairx[pairc]*Norm, pairy[pairc], probi, i);
kraus's avatar
kraus committed
129
                    *gmsg << "* " << mess << endl;
gsell's avatar
gsell committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
                }
            }
            pairc++;
        }
    }

    memset(mess, '\0', sizeof(mess));
    sprintf(mess, " ===> Max: %12.8f %8.3f\n", lodata2[jmax].x * Norm, lodata2[jmax].y);
    *gmsg << "* " << mess << endl;

    delete la;
    la = NULL;
    return(0);
}


146
int TUNE_class::lombAnalysis(double *x, double *y, int Ndat, int /*nhis*/)
gsell's avatar
gsell committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
/*-----------------------------------------------------------------------------
 *  Launch Lomb analysis and plot results
 *  =======================================
 *
 *---------------------------------------------------------------------------*/
{
    int    i, nout, jmax;
    int    pairc;
    int    datcnt = 0;
    int stat = 0;
    double prob, probi;
    double tofac = 0.8;

    LOMB_TYPE tlom;

    CI_lt p, q, r, s, tp;

    char   mess[80];
    LOMB_class *la;
166
    std::vector<LOMB_TYPE> lodata, lodata2;
gsell's avatar
gsell committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    /*---------------------------------------------------------------------------*/

    sprintf(mess, "TUNE_class LombAnalysis requested");
    *gmsg << "* " << mess << endl;

    /*
     * Do Lomb analysis
     * ================
     */

    for(int j = 0; j < Ndat; j++) {
        tlom.x = x[j];
        tlom.y = y[j];
        lodata.push_back(tlom);
    }

    p = lodata.begin();
    q = lodata.end();

    datcnt = count_if(p, q, Lomb_eq(0.));

    if(datcnt > (q - p - 10)) {
        memset(mess, '\0', sizeof(mess));
        sprintf(mess, "Just found %d data points that are == 0!", datcnt);
kraus's avatar
kraus committed
191
        *gmsg << "* " << mess << endl;
gsell's avatar
gsell committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
        return(-1);
    }

    // this parameterset works ok in most cases.....
    ofac  = 4.0;
    hifac = 0.8;
    Qmin  = 0.2;
    Qmax  = 0.4;

    la = new LOMB_class(1);

    stat = 0;
    stat = la->period(&lodata, &lodata2, ofac, hifac, &nout, &jmax, &prob, 0);
    if(stat != 0) {
        memset(mess, '\0', sizeof(mess));
        sprintf(mess, "@C3ERROR: Lomb analysis failed!");
kraus's avatar
kraus committed
208
        *gmsg << "* " << mess << endl;
gsell's avatar
gsell committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

        delete la;
        la = NULL;
        return(-1);
    }

    memset(mess, '\0', sizeof(mess));
    sprintf(mess, "=====> jmax = %d", jmax);
    *gmsg << "* " << mess << endl;

    double pairx[nout];
    double pairy[nout];

    memset(mess, '\0', sizeof(mess));
    sprintf(mess, "\n********** Peaks in Data:       **************");
    *gmsg << "* " << mess << endl;

    /*
    ada make histogram
    hbook1(nhis,"Lomb data",nout,
       (float)lodata2[0].x,
       (float)lodata2[nout-1].x);

    */
    pairc = 0;
    for(i = 0; i < nout; i++) {
        /* ada book histogram
        Hf1(nhis,(float)lodata2[i].x,(float)lodata2[i].y);
        */
        if(lodata2[i].y > 2.) {
            pairx[pairc] = lodata2[i].x;
            pairy[pairc] = lodata2[i].y;
            if((pairy[pairc] > pairy[pairc-1]) &&
               (pairy[pairc] > lodata2[i+1].y)) {
243
                probi = la->signi(&pairy[pairc], &nout, &tofac);
gsell's avatar
gsell committed
244 245 246 247
                if(pairy[pairc] > 4.) {
                    memset(mess, '\0', sizeof(mess));
                    sprintf(mess, "%12.8f %8.2f %8.3f %d", pairx[pairc], pairy[pairc],
                            probi, i);
kraus's avatar
kraus committed
248
                    *gmsg << "* " << mess << endl;
gsell's avatar
gsell committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
                }
            }
            pairc++;
        }
    }

    memset(mess, '\0', sizeof(mess));
    sprintf(mess, "\n===> Max: %12.8f %8.3f\n", lodata2[jmax].x, lodata2[jmax].y);
    *gmsg << "* " << mess << endl;

    delete la;
    la = NULL;

    return(0);

kraus's avatar
kraus committed
264
}