Ejemplo n.º 1
0
void Polar::setPolarName(QString fname)
{
    isCsv=true;
    loaded=false;
    clearPolar();
    if(this->mainWindow->getSelectedBoat() && this->mainWindow->getSelectedBoat()->get_boatType()==BOAT_REAL)
        coeffPolar=Settings::getSetting("polarEfficiency",100).toInt()/100.0;
    else
        coeffPolar=1.0;

    //qWarning() << "Opening polar" << fname<<"with coeff"<<coeffPolar;

    name=fname;
    QString nameF = appFolder.value("polar")+fname+".csv";
    QFile file(nameF);
    if (fname.endsWith(".csv",Qt::CaseInsensitive) || fname.endsWith(".pol",Qt::CaseInsensitive))
    {
        nameF=appFolder.value("polar")+fname;
        file.setFileName(nameF);
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text ))
        {
            QMessageBox::warning(0,QObject::tr("Lecture de polaire"),
                QString(QObject::tr("Impossible d'ouvrir le fichier %1")).arg(name));
            return;
        }

        isCsv=fname.endsWith("csv",Qt::CaseInsensitive);
    }
    else
    {
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text ))
        {
            isCsv=false;
            nameF = appFolder.value("polar")+fname+".pol";
            file.setFileName(nameF);
            if (!file.open(QIODevice::ReadOnly | QIODevice::Text ))
            {
                 QMessageBox::warning(0,QObject::tr("Lecture de polaire"),
                     QString(QObject::tr("Impossible d'ouvrir le fichier %1 (ni en .csv ni en .pol)")).arg(name));
                 return;
            }
        }
    }
    QTextStream stream(&file);
    QString line;
    QStringList list;
    /* read first line to see line length */
    line=stream.readLine();
    line.remove("\"");
    if(line.isNull())
    {
        QMessageBox::warning(0,QObject::tr("Lecture de polaire"),
             QString(QObject::tr("Fichier %1 vide")).arg(fname));
        file.close();
        return;
    }
    if(isCsv)
        list = line.split(';');
    else
        list = line.split('\t');
    if(list[0].toUpper() != "TWA\\TWS" && list[0].toUpper() != "TWA/TWS" && list[0].toUpper() != "TWA")
    {
        QMessageBox::warning(0,QObject::tr("Lecture de polaire"),
             QString(QObject::tr("Fichier %1 invalide (doit commencer par TWA\\TWS et non '%2')"))
                        .arg(fname)
                        .arg(list[0]));
        file.close();
        return;
    }
    int i;
    for(i=1;i<list.count();++i)
    {
        if(!tws.isEmpty() && list[i].toDouble()<=tws.last()) break;
        tws.append(list[i].toDouble());
    }
    bool missingTws0=false;
    if(tws.first()!=0.0)
    {
        missingTws0=true;
    }
    bool firstTWA=true;
    while(true)
    {
        line=stream.readLine();
        if(line.isNull()) break;
        line.remove("\"");
        if (isCsv)
            list = line.split(";");
        else
            list = line.split("\t");
        if(firstTWA)
        {
            firstTWA=false;
            if(list.first().toDouble()!=0.0)
            {
                for(int t=0;t<tws.count();++t)
                {
                    polar_data.append(0);
                }
                if(missingTws0)
                    polar_data.append(0);
                twa.append(0.0);
            }
        }
        twa.append(list[0].toDouble());
        if(missingTws0)
            polar_data.append(0);
        for(i=1;i<list.count();++i)
        {
            if(i>tws.count()) break;
            polar_data.append(list[i].toDouble()*this->coeffPolar);
        }
        while(i<=tws.count())
            polar_data.append(0);
    }
    if(missingTws0)
        tws.prepend(0.0);
#if 0
    qWarning()<<"polar data for"<<nameF;
    QString debug="xxx.x ";
    foreach(double dd,tws)
        debug+=QString().sprintf("%04.1f ",dd);
    qWarning()<<debug;
    QListIterator<double> i1(polar_data);
    QListIterator<double> i2(twa);
    while(i2.hasNext())
    {
        debug=QString().sprintf("%05.1f ",i2.next());
        for(int nn=0;nn<tws.count();++nn)
            debug+=QString().sprintf("%04.1f ",i1.next());
        qWarning()<<debug;
    }
#endif

    mid_twa=qRound(twa.count()/2.0);
    mid_tws=qRound(tws.count()/2.0);
    /* polaire chargee */

/* pre-calculate B-VMG for every tws at 0.1 precision with a twa step of 1 and then .1 */

    double ws=0.0;
    double wa=0.0;
    double bvmg,bvmg_d,bvmg_u,wa_u,wa_d,wa_limit;
    maxSpeed=0.0;
    do
    {
        wa_u=0.0;
        wa_d=180.0;
        bvmg_u=bvmg_d=0;
        double speed;
        do
        {
            speed=myGetSpeed(ws,wa,true);
            if(speed>maxSpeed) maxSpeed=speed;
            bvmg=speed*cos(degToRad(wa));
            if(bvmg_u<bvmg) //bvmg is positive here
            {
                bvmg_u=bvmg;
                wa_u=wa;
            }
            if(bvmg_d>bvmg) //bvmg is negative here
            {
                bvmg_d=bvmg;
                wa_d=wa;
            }
            wa=qRound(wa+1);
        } while(wa<181.00);
        wa=wa_u-1;
        wa_limit=wa_u+1;
        if(wa<0) wa=0.0;
        if(wa_limit<1) wa_limit=1.0;
        if(wa_limit>180) wa_limit=180.0;
        bvmg_u=0.0;
        do
        {
            speed=myGetSpeed(ws,wa,true);
            if(speed>maxSpeed) maxSpeed=speed;
            bvmg=speed*cos(degToRad(wa));
            if(bvmg_u<bvmg)
            {
                bvmg_u=bvmg;
                wa_u=wa;
            }
            wa=wa+0.1;
        } while(wa<(wa_limit+0.1000));
        wa=wa_d-1;
        wa_limit=wa_d+1;
        if(wa<0) wa=0.0;
        if(wa_limit<1) wa_limit=1.0;
        if(wa_limit>180) wa_limit=180.0;
        bvmg_d=0.0;
        do
        {
            speed=myGetSpeed(ws,wa,true);
            if(speed>maxSpeed) maxSpeed=speed;
            bvmg=speed*cos(degToRad(wa));
            if(bvmg_d>bvmg)
            {
                bvmg_d=bvmg;
                wa_d=wa;
            }
            wa=wa+0.1;
        }while(wa<wa_limit+0.1);
        best_vmg_up.append(wa_u);
        best_vmg_down.append(wa_d);
        wa=0.0;
        ws=ws+.1;
    }while(ws<60.1);
    loaded=true;
    QFileInfo fi(file.fileName());
    QString nameFVmg = appFolder.value("polar")+fi.baseName()+".vmg";
    fileVMG.setFileName(nameFVmg);
    if (fileVMG.open(QIODevice::ReadOnly | QIODevice::Text ))
    {
        if(fileVMG.size()<4329500 || fileVMG.size()>4329700)
            fileVMG.remove();
        else
        {
            QFileInfo info1(fileVMG);
            QFileInfo info2(file);
            if(this->mainWindow->isStartingUp && info1.lastModified()<info2.lastModified())
                fileVMG.remove();
            else
            {
                file.close();
                return;
            }
        }
    }
    file.close();
//precalculate regular VMGs
    //qWarning() << "Start computing vmg";
    fileVMG.open(QIODevice::WriteOnly | QIODevice::Text );
    double vmg=0;
    QTextStream sVmg(&fileVMG);
    QString ssVmg;
    QProgressDialog * progress;
    progress=new QProgressDialog((QWidget*)mainWindow);
    progress->setWindowModality(Qt::ApplicationModal);
    progress->setLabelText(tr("Pre-calcul des valeurs de VMG pour ")+fname);
    progress->setMaximum(600);
    progress->setMinimum(0);
    progress->setCancelButton(NULL);
    progress->setMaximumHeight(100);
    progress->show();
    for (int tws=0;tws<601;tws++)
    {
        progress->setValue(tws);
        for (int twa=0;twa<1801;twa++)
        {
            bvmgWind((double) twa/10.0,(double) tws/10.0,&vmg);
            ssVmg.sprintf("%.4d",qRound(A360(vmg)*10.0));
            sVmg<<ssVmg;
        }
    }
    fileVMG.close();
    if(!fileVMG.open(QIODevice::ReadOnly | QIODevice::Text ))
        qWarning()<<"fileVMG could not be re-opened!";
    progress->close();
    delete progress;
}