source: palm/trunk/UTIL/mrungui/mainwindow.cpp @ 2067

Last change on this file since 2067 was 2067, checked in by maronga, 7 years ago

small bugfix in palm_jm, integration of palm_jm in mrungui

  • Property svn:keywords set to Id
File size: 47.0 KB
Line 
1//------------------------------------------------------------------------------//
2// This file is part of PALM.
3//
4// PALM is free software: you can redistribute it and/or modify it under the terms
5// of the GNU General Public License as published by the Free Software Foundation,
6// either version 3 of the License, or (at your option) any later version.
7//
8// PALM is distributed in the hope that it will be useful, but WITHOUT ANY
9// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
10// A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
11//
12// You should have received a copy of the GNU General Public License along with
13// PALM. If not, see <http://www.gnu.org/licenses/>.
14//
15// Copyright 1997-2012  Leibniz University Hannover
16//--------------------------------------------------------------------------------//
17//
18// Current revisions:
19// -----------------
20// Added support for PALM job manager
21//
22// Former revisions:
23// -----------------
24// $Id: mainwindow.cpp 2067 2016-11-11 21:55:49Z maronga $
25//
26// 1804 2016-04-05 16:30:18Z maronga
27// Removed parameter file check
28// Update for use with qt5
29//
30// 1723 2015-11-16 15:25:51Z boeske
31// Added checkbox for cyclic fill flag in mrungui
32//
33// 1611 2015-07-07 12:23:22Z maronga
34// Added routine start_watchdog.
35//
36// 1046 2012-11-09 14:38:45Z maronga
37// code put under GPL (PALM 3.9)
38//
39// mainwindow.cpp 920 2012-06-05 09:56:53Z maronga
40// Added -Z option (disable combine_plot_fields)
41//
42// 818 2012-02-08 16:11:23Z maronga
43// New: flag parameter -z implemented (used for skipping parameter file check)
44//
45// 811 2012-01-31 09:45:54Z maronga
46// Bugfix: waitForFinished time exceeded limits, adjusted to 1h waiting time
47//
48// 809 2012-01-30 13:32:58Z marong
49// Bugfix: waiting for 20 minutes does not suffice for local runs
50//
51// 793 2011-12-12 15:15:24Z maronga
52// Initial revision
53//
54// Description:
55// ------------
56// All subroutines for mrungui
57//----------------------------------------------------------------------------//
58
59#include <QApplication>
60#include "mainwindow.h"
61#include "ui_mainwindow.h"
62#include "ui_about.h"
63#include "ui_help.h"
64#include <QString>
65#include <QFileDialog>
66#include <QDateTime>
67#include "stdlib.h"
68#include <QProcess>
69#include <QTextStream>
70
71
72// Define username on host as global variable
73QString username = getenv("USER");
74// Define variable branch (location of mrungui, e.g.
75// "/home/user/palm/current_version"
76QString branch;
77
78
79//****************************************************************************//
80//  Initialization of the main window
81MainWindow::MainWindow(QWidget *parent) :
82    QMainWindow(parent),
83    ui(new Ui::MainWindow)
84{
85    ui->setupUi(this);
86
87//  Empty the mrun command line (hereafter mrunline)
88    ui->commandline->setText("");
89
90//  Get path of the program and set default file
91    branch = QCoreApplication::applicationDirPath();
92    branch = branch.left(branch.length() - 14);
93
94
95    QFile file(branch+"/.mrun.gui.default");
96
97//  Read default settings
98    if ( file.exists() == true && file.size() > 10)
99    {
100
101       QString mrunline;
102       if ( file.open(QIODevice::ReadOnly | QIODevice::Text ) )
103       {
104
105//        File opened successfully
106          QTextStream in(&file);
107
108          QString line = in.readLine();
109          while (!line.isNull())
110          {
111             mrunline = line;
112             line = in.readLine();
113          }
114
115          file.close();
116       }
117
118       mrunline = mrunline.right(mrunline.length() - 17);
119       ui->commandline->setText(mrunline);
120
121       setup_gui(mrunline);
122
123    }
124
125//  Load jobs into the recent jobs list
126    recent_jobs(10);
127}
128
129
130//****************************************************************************//
131//  Routine for listing jobs in the recent jobs list
132int MainWindow::recent_jobs(int noj)
133{
134    branch = QCoreApplication::applicationDirPath();
135    branch = branch.left(branch.length() - 14);
136
137    QFile file(branch+"/.mrun.history");
138    QString listitem, timestamp;
139
140//  Read mrun history and select the last used jobs
141    if ( file.exists() == true && file.size() > 10)
142    {
143
144//     Open history
145       QStringList history, tmphistory;
146       if ( file.open(QIODevice::ReadOnly | QIODevice::Text ) )
147       {
148
149//        file opened successfully
150          QTextStream in(&file);
151
152          QString line = in.readLine();
153          while (!line.isNull())
154          {
155             history.append(line);
156             line = in.readLine();
157          }
158
159          file.close();
160       }
161
162       int j = 0;
163
164       ui->list_jobname->clear();
165
166//     Read history entries and append to recent job list
167       for (int i=history.count(); i>=1; i--)
168       {
169           timestamp = history[i-1].left(16);
170           listitem = history[i-1].right(history[i-1].length() - 17);
171           listitem = listitem.split("-d ", QString::SkipEmptyParts)[1];
172           listitem = listitem.split(" -", QString::SkipEmptyParts)[0];
173           listitem = listitem.replace(" ","");
174
175           QList<QListWidgetItem *> matchitems = \
176                   ui->list_jobname->findItems(listitem, Qt::MatchExactly);
177
178           if ( matchitems.count() == 0 )
179           {
180              ui->list_jobname->addItem(listitem);
181              tmphistory.append(listitem+" ("+timestamp+")");
182              j++;
183           }
184           if ( j == noj )
185           {
186               break;
187           }
188       }
189
190//     Send to list
191       ui->list_jobname->clear();
192       for (int i=tmphistory.count(); i>=1; i--)
193       {
194           ui->list_jobname->addItem(tmphistory[i-1]);
195       }
196
197    }
198    return 0;
199}
200
201//****************************************************************************//
202//  Exit program
203MainWindow::~MainWindow()
204{
205
206    delete ui;
207}
208
209
210//****************************************************************************//
211//  Start the mrun command via xterm
212int MainWindow::startmrun()
213{
214
215    QString xtermoutput;
216    QString mrunline_save;
217    QString history_line;
218    QString mrunline = ui->commandline->text();
219    QString userline = ui->line_user->text();
220
221//  Check for empty line
222    mrunline_save = mrunline;
223    if (userline != "")
224    {
225        mrunline = mrunline + " " + userline;
226    }
227    history_line = mrunline;
228
229//  Disable the main window
230    ui->group_job->setEnabled(false);
231    ui->group_execution->setEnabled(false);
232    ui->group_runcontrol->setEnabled(false);
233    ui->group_advanced->setEnabled(false);
234    ui->check_advanced->setEnabled(false);
235    ui->check_verbose->setEnabled(false);
236    ui->button_start->setEnabled(false);
237    ui->button_start->setText("wait...");
238
239    branch = QCoreApplication::applicationDirPath();
240    branch = branch.left(branch.length() - 14);
241
242    ui->commandline->setText("Executing MRUN in xterm...");
243
244//  Wait until all commands have been executed (ugly)
245    for(int i=0; i<20; i++)
246       qApp->processEvents();
247
248//  Start xterm as QProcess
249    QProcess mrun;
250    mrun.setProcessChannelMode(QProcess::MergedChannels);
251    mrun.setWorkingDirectory(branch);
252
253    mrunline = " -title \"Executing MRUN...\" -geometry \"100x55+970+0\" -e \""\
254          +mrunline.replace("\"","\'")\
255          +";echo -n '--> Press Enter to continue...';read yesno\"";
256
257    mrun.start("xterm"+mrunline);
258
259    if(!mrun.waitForStarted())
260        return 0;
261
262//    while(mrun.waitForReadyRead())
263//        xtermoutput = xtermoutput+mrun.readAllStandardOutput();
264
265//  Wait until mrun has finished or wait for 200 minutes
266    mrun.waitForFinished(3600000);
267
268
269//  Jobs has been submitted or aborted. Continuing...
270//  Save the mrun command to history file
271    QString filename = branch+"/.mrun.history";
272
273    QDateTime time = QDateTime::currentDateTime();
274    QString tmptime = time.toString("yyyy/MM/dd hh:mm");
275
276    QFile file(filename);
277    file.open(QIODevice::Append | QIODevice::Text);
278    QTextStream out(&file);
279    out << tmptime + " " + history_line + "\n";
280    file.close();
281
282//  Enable main window again
283    ui->group_job->setEnabled(true);
284    ui->group_execution->setEnabled(true);
285    ui->group_runcontrol->setEnabled(true);
286    if ( ui->check_advanced->isChecked() == true)
287    {
288       ui->group_advanced->setEnabled(true);
289    }
290    ui->check_advanced->setEnabled(true);
291    ui->check_verbose->setEnabled(true);
292    ui->button_start->setEnabled(true);
293    ui->action_save->setEnabled(true);
294    ui->button_start->setText("MRUN Start");
295    ui->commandline->setText(mrunline_save);
296
297//  Reload recent jobs
298    recent_jobs(10);
299
300    return 0;
301}
302
303
304//****************************************************************************//
305//  Disable/Enable advanced settings dialog
306int MainWindow::enable_advanced()
307{
308    bool status;
309
310    status = ui->group_advanced->isEnabled();
311    if (status == true)
312    {
313       ui->group_advanced->setEnabled(false);
314    }
315    else
316    {
317       ui->group_advanced->setEnabled(true);
318    }
319
320    return 0;
321}
322
323
324//****************************************************************************//
325//  Disable/enable dialog for coupled runs
326int MainWindow::enable_coupled()
327{
328    QString status;
329
330    status = ui->drop_job->currentText();
331    if (status == "Coupled restart")
332    {
333       ui->label_coupled1->setEnabled(true);
334       ui->label_coupled2->setEnabled(true);
335       ui->label_coupled3->setEnabled(true);
336       ui->line_PE_atmos->setEnabled(true);
337       ui->line_PE_ocean->setEnabled(true);
338
339       QString pe_total = ui->line_pe->text();
340       QString pe_atmos = ui->line_PE_atmos->text();
341       QString pe_ocean = ui->line_PE_ocean->text();
342       if (pe_total != "")
343       {
344           int PE_split = pe_total.toInt()/2;
345           ui->line_PE_atmos->setText(QString::number(PE_split));
346           ui->line_PE_ocean->setText(QString::number(PE_split));
347       }
348    }
349    else
350    {
351        ui->label_coupled1->setEnabled(false);
352        ui->label_coupled2->setEnabled(false);
353        ui->label_coupled3->setEnabled(false);
354        ui->line_PE_atmos->setEnabled(false);
355        ui->line_PE_ocean->setEnabled(false);
356    }
357
358    return 0;
359}
360
361
362//****************************************************************************//
363//  Choose job from dialog
364int MainWindow::choosejob()
365{
366    QString filename;
367    branch = QCoreApplication::applicationDirPath();
368    branch = branch.left(branch.length() - 14);
369
370    QString jobdir = branch+"/JOBS";
371
372
373//  Pick a job from dialog
374    filename = QFileDialog::getExistingDirectory(this, \
375                            tr("Choose a job directory"), branch+"/JOBS", \
376               QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks | \
377               QFileDialog::DontUseNativeDialog | QFileDialog::ReadOnly | \
378               QFileDialog::DontConfirmOverwrite);
379
380
381
382
383
384//  If a file was selected, load it into mainwindow
385    if ( filename != "")
386    {
387        filename = filename.right(filename.length() - jobdir.length() - 1);
388
389        ui->line_jobname->setText(filename);
390        ui->list_jobname->clearSelection();
391
392//      Change mrunline accordingly
393        change_commandline("d","");
394        change_commandline("r","");
395        return 0;
396    }
397    else
398    {
399        return 1;
400    }
401}
402
403
404//****************************************************************************//
405//  Choose job from the recent job list and load all settings
406int MainWindow::choosejob_list()
407{
408    QString filename, timestamp, jobname;
409
410//  Get selected item from list
411    filename = ui->list_jobname->currentItem()->text();
412    int itemint = ui->list_jobname->currentRow();
413
414//  Reload list
415    ui->setupUi(this);
416    recent_jobs(10);
417
418//  Set selected item to jobname
419    ui->list_jobname->item(itemint)->setSelected(true);
420
421    timestamp = filename.right(17).left(16);
422    jobname = filename.left(filename.length() - 19);
423
424    branch = QCoreApplication::applicationDirPath();
425    branch = branch.left(branch.length() - 14);
426    QFile file(branch+"/.mrun.history");
427    QString listitem;
428
429//  Load history
430    if ( file.exists() == true && file.size() > 10)
431    {
432
433       QStringList history;
434       if ( file.open(QIODevice::ReadOnly | QIODevice::Text ) )
435       {
436
437//        file opened successfully
438          QTextStream in(&file);
439
440          QString line = in.readLine();
441          while (!line.isNull())
442          {
443             history.append(line);
444             line = in.readLine();
445          }
446
447          file.close();
448       }
449
450       for (int i=history.count(); i>=1; i--)
451       {
452           listitem = history[i-1].right(history[i-1].length() - 17);
453           listitem = listitem.split("-d ", QString::SkipEmptyParts)[1];
454           listitem = listitem.split(" -", QString::SkipEmptyParts)[0];
455           listitem = listitem.replace(" ","");
456
457
458//         Select command line with correct timestamp and jobname
459           if (history[i-1].left(16) == timestamp && listitem == jobname)
460           {
461              QString mrunline = history[i-1];
462              mrunline = mrunline.right(mrunline.length() - 17);
463              ui->commandline->setText(mrunline);
464
465              setup_gui(mrunline);
466           }
467       }
468    }
469
470     return 0;
471}
472
473
474//****************************************************************************//
475//  Change run identifer (initial, restart, coupled...)
476int MainWindow::change_rc_list()
477{
478
479
480   QString drop_job = ui->drop_job->currentText();
481
482   change_commandline("r","");
483
484// Enable PE distribution for atmosphere/ocean
485   if ( drop_job == "Coupled restart")
486   {
487       QString drop_atmos = ui->line_PE_atmos->text();
488       QString drop_ocean = ui->line_PE_ocean->text();
489
490       change_commandline("Y",drop_atmos+" "+drop_ocean);
491   }
492
493// Check of ocean runs
494   else
495   {
496      delete_commandline("Y");
497      if (drop_job == "Precursor run (Ocean)")
498      {
499          activate_flag("y");
500      }
501      else
502      {
503          deactivate_flag("y");
504      }
505   }
506
507
508    return 0;
509}
510
511
512//****************************************************************************//
513//  Routine for processing any changes in the mainwindow settings
514int MainWindow::change_commandline(QString id,QString fwstring)
515{
516
517//  First get the mrunline
518    QString newmrunline;
519    bool    initialize=false;
520
521    QString mrunline = ui->commandline->text();
522
523    QStringList splitline = mrunline.split(" -"+id+"", QString::SkipEmptyParts);
524    if ( splitline.count() == 1)
525    {
526        splitline.append(" ");
527    }
528    else if ( splitline.count() == 0 )
529    {
530       splitline.append("mrun");
531       splitline.append(" ");
532       initialize = true;
533    }
534
535    QStringList param = splitline[1].split("-");
536
537//  Change in parameter "d" (jobname)
538    if (id == "d")
539    {
540       QString filename = ui->line_jobname->text();
541
542       param[0] = filename.replace(" ","");
543
544       if ( initialize == true && ui->group_runcontrol->isEnabled() == true )
545       {
546           ui->group_runcontrol->setEnabled(true);
547           ui->group_execution->setEnabled(true);
548           ui->drop_job->setEnabled(true);
549           ui->check_advanced->setEnabled(true);
550           ui->button_start->setEnabled(true);
551           ui->action_save->setEnabled(true);
552       }
553       else if ( param[0] == "")
554       {
555           ui->group_runcontrol->setEnabled(false);
556           ui->group_execution->setEnabled(false);
557           ui->drop_job->setEnabled(false);
558           ui->button_start->setEnabled(false);
559           ui->action_save->setEnabled(false);
560           ui->group_advanced->setEnabled(false);
561           ui->check_advanced->setEnabled(false);
562           delete_commandline("d");
563           change_commandline("r","remove");
564           ui->label_usercode->setText("");
565           return 1;
566       }
567       else
568       {
569
570//         Check if user code is available
571           branch = QCoreApplication::applicationDirPath();
572           branch = branch.left(branch.length() - 14);
573           QDir usercode = branch+"/USER_CODE/"+param[0];
574           if (usercode.exists() == true)
575           {
576               ui->label_usercode->setText("User code found.");
577           }
578           else
579           {
580               ui->label_usercode->setText("Warning: no user code found!");
581           }
582
583//         Check if _pdf file is available, otherwise notice user
584           if (ui->check_restarts->checkState() == 2)
585           {
586              QString jobname = ui->line_jobname->text();
587              QFile restartfile(branch+"/JOBS/"+jobname+"/INPUT/"+jobname+"_p3df");
588              if (restartfile.exists() == true)
589              {
590                 ui->label_restart->setText("");
591              }
592              else
593              {
594                 ui->label_restart->setText("Warning: No p3df file found!");
595              }
596           }
597
598           ui->group_runcontrol->setEnabled(true);
599           ui->group_execution->setEnabled(true);
600           ui->drop_job->setEnabled(true);
601           ui->check_advanced->setEnabled(true);
602
603           if ( ui->check_advanced->isChecked() == true )
604           {
605              ui->group_advanced->setEnabled(true);
606              if ( ui->line_i->text() != "" || ui->line_o->text() != "")
607              {
608                 change_commandline("r","remove");
609                 ui->group_runcontrol->setEnabled(false);
610              }
611           }
612       }
613
614    }
615
616//  Change in parameter "r" (run control list)
617    else if (id == "r")
618    {
619        int status_ts = ui->check_ts->checkState();
620        int status_pr = ui->check_pr->checkState();
621        int status_xy = ui->check_xy->checkState();
622        int status_xz = ui->check_xz->checkState();
623        int status_yz = ui->check_yz->checkState();
624        int status_3d = ui->check_3d->checkState();
625        int status_ma = ui->check_ma->checkState();
626        int status_sp = ui->check_sp->checkState();
627        int status_pts = ui->check_pts->checkState();
628        int status_prt = ui->check_prt->checkState();
629
630        QString drop_job = ui->drop_job->currentText();
631        QString rc_flag = "#";
632
633        if (drop_job == "Initial run")
634        {
635            rc_flag = "#";
636        }
637        else if (drop_job == "Restart run")
638        {
639            rc_flag = "f";
640        }
641        else if (drop_job == "Precursor run (Atmosphere)")
642        {
643            rc_flag = "#";
644        }
645        else if (drop_job == "Precursor run (Ocean)")
646        {
647           rc_flag = "o#";
648        }
649        else if (drop_job == "Coupled restart")
650        {
651           rc_flag = "f";
652        }
653
654        param[0] = "\"d3"+rc_flag;
655
656        if (status_ts == 2)
657        {
658            param[0] = param[0]+" ts"+rc_flag;
659        }
660        if (status_pr == 2)
661        {
662           param[0] = param[0]+" pr"+rc_flag;
663        }
664        if (status_xy == 2)
665        {
666           param[0] = param[0]+" xy"+rc_flag;
667        }
668        if (status_xz == 2)
669        {
670           param[0] = param[0]+" xz"+rc_flag;
671        }
672        if (status_yz == 2)
673        {
674           param[0] = param[0]+" yz"+rc_flag;
675        }
676        if (status_3d == 2)
677        {
678           param[0] = param[0]+" 3d"+rc_flag;
679        }
680        if (status_ma == 2)
681        {
682           param[0] = param[0]+" ma"+rc_flag;
683        }
684        if (status_sp == 2)
685        {
686           param[0] = param[0]+" sp"+rc_flag;
687        }
688        if (status_prt == 2)
689        {
690           param[0] = param[0]+" prt"+rc_flag;
691        }
692        if (status_pts == 2)
693        {
694           param[0] = param[0]+" pts"+rc_flag;
695        }
696
697        if (drop_job == "Coupled restart")
698        {
699            rc_flag = "of";
700            param[0] = param[0]+" d3"+rc_flag;
701
702            if (status_ts == 2)
703            {
704                param[0] = param[0]+" ts"+rc_flag;
705            }
706            if (status_pr == 2)
707            {
708               param[0] = param[0]+" pr"+rc_flag;
709            }
710            if (status_xy == 2)
711            {
712               param[0] = param[0]+" xy"+rc_flag;
713            }
714            if (status_xz == 2)
715            {
716               param[0] = param[0]+" xz"+rc_flag;
717            }
718            if (status_yz == 2)
719            {
720               param[0] = param[0]+" yz"+rc_flag;
721            }
722            if (status_3d == 2)
723            {
724               param[0] = param[0]+" 3d"+rc_flag;
725            }
726            if (status_ma == 2)
727            {
728               param[0] = param[0]+" ma"+rc_flag;
729            }
730            if (status_sp == 2)
731            {
732               param[0] = param[0]+" sp"+rc_flag;
733            }
734            if (status_prt == 2)
735            {
736               param[0] = param[0]+" prt"+rc_flag;
737            }
738            if (status_pts == 2)
739            {
740               param[0] = param[0]+" pts"+rc_flag;
741            }
742        }
743
744        int status_restarts = ui->check_restarts->checkState();
745
746
747
748        if (status_restarts == 2)
749        {
750            param[0]=param[0]+" restart";
751
752//          Check if _pdf file is available, otherwise notice user
753            if (ui->check_restarts->checkState() == 2)
754            {
755               QString jobname = ui->line_jobname->text();
756               branch = QCoreApplication::applicationDirPath();
757               branch = branch.left(branch.length() - 14);
758               QFile restartfile(branch+"/JOBS/"+jobname+"/INPUT/"+jobname+ \
759                                 "_p3df");
760               if (restartfile.exists() == true)
761               {
762                  ui->label_restart->setText("");
763               }
764               else
765               {
766                  ui->label_restart->setText("Warning: No p3df file found!");
767               }
768            }
769
770        }
771        else  {
772          ui->label_restart->setText("");
773        }
774
775       
776       
777        int status_cycfill = ui->check_cycfill->checkState();
778
779        if (status_cycfill == 2)
780        {
781            param[0]=param[0]+" cycfill";
782        }
783
784
785       
786        param[0]=param[0]+"\" ";
787
788        if ( fwstring == "remove")
789        {
790            delete_commandline(id);
791            return 1;
792        }
793        else
794        {
795           ui->button_start->setEnabled(true);
796           ui->action_save->setEnabled(true);
797        }
798    }
799//  Change in any other parameter
800    else
801    {
802        if ( fwstring != "")
803        {
804           param[0] = "\""+fwstring+"\"";
805        }
806        else
807        {
808            delete_commandline(id);
809            return 1;
810        }
811
812    }
813    param[0] = param[0] + " ";
814
815//  Join the new mrunline
816    splitline[1]= param.join("-");
817    newmrunline = splitline.join(" -"+id+" ");
818
819//  Print the new mrunline to mainwindow
820    newmrunline.replace("  "," ");
821    ui->commandline->setText(newmrunline);
822
823    return 0;
824}
825
826
827//****************************************************************************//
828//  Get all signals from mainwindow and perform change via change_commandline()
829int MainWindow::change_lineinput()
830{
831    QString tmptext;
832
833    if ( sender() == ui->line_host )
834    {
835        tmptext = ui->line_host->text();
836        change_commandline("h",tmptext);
837    }
838    else if ( sender() == ui->line_jobname)
839    {
840        tmptext = ui->line_jobname->text();
841        change_commandline("d",tmptext);
842    }
843    else if ( sender() == ui->line_q)
844    {
845        tmptext = ui->line_q->text();
846        change_commandline("q",tmptext);
847    }
848    else if ( sender() == ui->line_account)
849    {
850        tmptext = ui->line_account->text();
851        change_commandline("u",tmptext);
852    }
853    else if ( sender() ==  ui->line_pe)
854    {
855        tmptext = ui->line_pe->text();
856        change_commandline("X",tmptext);
857    }
858    else if ( sender() == ui->line_tpn)
859    {
860        tmptext = ui->line_tpn->text();
861        change_commandline("T",tmptext);
862    }
863    else if ( sender() == ui->line_branch)
864    {
865        tmptext = ui->line_branch->text();
866        change_commandline("K",tmptext);
867    }
868    else if ( sender() == ui->line_time)
869    {
870        tmptext = ui->line_time->text();
871        change_commandline("t",tmptext);
872    }
873    else if ( sender() == ui->line_M)
874    {
875        tmptext = ui->line_M->text();
876        change_commandline("M",tmptext);
877    }
878    else if ( sender() == ui->line_m)
879    {
880        tmptext = ui->line_m->text();
881        change_commandline("m",tmptext);
882    }
883    else if ( sender() == ui->line_a)
884    {
885        tmptext = ui->line_a->text();
886        change_commandline("a",tmptext);
887    }
888    else if ( sender() == ui->line_D)
889    {
890        tmptext = ui->line_D->text();
891        change_commandline("D",tmptext);
892    }
893    else if ( sender() == ui->line_c)
894    {
895        tmptext = ui->line_c->text();
896        if ( tmptext == ".mrun.config")
897        {
898            tmptext = "";
899        }
900        change_commandline("c",tmptext);
901    }
902    else if ( sender() == ui->line_p)
903    {
904        tmptext = ui->line_p->text();
905        change_commandline("p",tmptext);
906    }
907    else if ( sender() == ui->line_s)
908    {
909        tmptext = ui->line_s->text();
910        change_commandline("s",tmptext);
911    }
912    else if ( sender() == ui->line_i)
913    {
914        tmptext = ui->line_i->text();
915        if ( tmptext != "")
916        {
917            change_commandline("r","remove");
918            ui->group_runcontrol->setEnabled(false);
919
920            ui->button_start->setEnabled(true);
921            ui->action_save->setEnabled(true);
922        }
923        else if (ui->line_o->text() == "" )
924        {
925           ui->group_runcontrol->setEnabled(true);
926           change_commandline("r","");
927        }
928
929        change_commandline("i",tmptext);
930    }
931    else if ( sender() == ui->line_o)
932    {
933        tmptext = ui->line_o->text();
934        if ( tmptext != "")
935        {
936            change_commandline("r","remove");
937            ui->button_start->setEnabled(true);
938            ui->action_save->setEnabled(true);
939            ui->group_runcontrol->setEnabled(false);
940        }
941        else if (ui->line_i->text() == "" )
942        {
943           ui->group_runcontrol->setEnabled(true);
944           change_commandline("r","");
945        }
946
947        change_commandline("o",tmptext);
948
949    }
950    else if ( sender() == ui->line_w)
951    {
952        tmptext = ui->line_w->text();
953        change_commandline("w",tmptext);
954    }
955    else if ( sender() == ui->line_PE_atmos || sender() == ui->line_PE_ocean)
956    {
957        tmptext = ui->line_PE_atmos->text() + " "  + ui->line_PE_ocean->text();
958        change_commandline("Y",tmptext);
959        int PE_total = ui->line_PE_atmos->text().toInt() + \
960                        ui->line_PE_ocean->text().toInt();
961        ui->line_pe->setText(QString::number(PE_total));
962        change_commandline("X",QString::number(PE_total));
963    }
964
965    else if ( sender() == ui->combo_n)
966    {
967        tmptext = ui->combo_n->currentText();
968        if ( tmptext == "default")
969        {
970            tmptext = "";
971        }
972        change_commandline("n",tmptext);
973    }
974    else if ( sender() == ui->line_user)
975    {
976       return 0;
977    }
978
979    return 0;
980}
981
982
983//****************************************************************************//
984//  Delete a parameter from mrunline
985int MainWindow::delete_commandline(QString id)
986{
987
988//  Read mrunline
989    QString newmrunline;
990    QString mrunline = ui->commandline->text();
991    QStringList splitline = mrunline.split(" -"+id, QString::SkipEmptyParts);
992    if ( splitline.count() == 1)
993    {
994       return 0;
995    }
996    else
997    {
998       QStringList param = splitline[1].split("-");
999       param[0] = "";
1000       splitline[1]= param.join(" -");
1001       newmrunline = splitline.join("");
1002
1003//     Print new mrunline to screen
1004       ui->commandline->setText(newmrunline);
1005
1006       return 0;
1007    }
1008}
1009
1010
1011//****************************************************************************//
1012//  Check routine for all flag parameters
1013int MainWindow::check_flags()
1014{
1015
1016    int status = ui->check_delete_tmp_files->checkState();
1017
1018    if (status == 2)
1019    {
1020        activate_flag("B");
1021    }
1022    else
1023    {
1024        deactivate_flag("B");
1025    }
1026
1027    status = ui->check_verbose->checkState();
1028
1029    if (status == 2)
1030    {
1031        activate_flag("v");
1032    }
1033    else
1034    {
1035        deactivate_flag("v");
1036    }
1037
1038    status = ui->check_A->checkState();
1039
1040    if (status == 2)
1041    {
1042        activate_flag("A");
1043    }
1044    else
1045    {
1046        deactivate_flag("A");
1047    }
1048
1049    status = ui->check_b->checkState();
1050
1051    if (status == 2)
1052    {
1053        activate_flag("b");
1054    }
1055    else
1056    {
1057        deactivate_flag("b");
1058    }
1059
1060    status = ui->check_F->checkState();
1061
1062    if (status == 2)
1063    {
1064        activate_flag("F");
1065    }
1066    else
1067    {
1068        deactivate_flag("F");
1069    }
1070    status = ui->check_I->checkState();
1071
1072    if (status == 2)
1073    {
1074        activate_flag("I");
1075    }
1076    else
1077    {
1078        deactivate_flag("I");
1079    }
1080    status = ui->check_k->checkState();
1081
1082    if (status == 2)
1083    {
1084        activate_flag("k");
1085    }
1086    else
1087    {
1088        deactivate_flag("k");
1089    }
1090    status = ui->check_O->checkState();
1091
1092    if (status == 2)
1093    {
1094        activate_flag("O");
1095    }
1096    else
1097    {
1098        deactivate_flag("O");
1099    }
1100    status = ui->check_S->checkState();
1101
1102    if (status == 2)
1103    {
1104        activate_flag("S");
1105    }
1106    else
1107    {
1108        deactivate_flag("S");
1109    }
1110    status = ui->check_x->checkState();
1111
1112    if (status == 2)
1113    {
1114        activate_flag("x");
1115    }
1116    else
1117    {
1118        deactivate_flag("x");
1119    }
1120   
1121    status = ui->check_Z->checkState();
1122
1123    if (status == 2)
1124    {
1125        activate_flag("Z");
1126    }
1127    else
1128    {
1129        deactivate_flag("Z");
1130    }
1131    return 0;
1132
1133}
1134
1135
1136//****************************************************************************//
1137//  Activate flag
1138int MainWindow::activate_flag(QString id)
1139{
1140    QString newmrunline;
1141
1142    QString mrunline = ui->commandline->text();
1143
1144    QStringList splitline = mrunline.split(" -"+id, QString::SkipEmptyParts);
1145    if ( splitline.count() == 1)
1146    {
1147        splitline.append("");
1148        newmrunline = splitline.join(" -"+id);
1149
1150 //     print new commandline to screen
1151        newmrunline.replace("  "," ");
1152        ui->commandline->setText(newmrunline);
1153
1154       return 0;
1155    }
1156    else
1157    {
1158       return 0;
1159    }
1160}
1161
1162
1163//****************************************************************************//
1164//  Deactivate flag
1165int MainWindow::deactivate_flag(QString id)
1166{
1167    QString newmrunline;
1168
1169    QString mrunline = ui->commandline->text();
1170
1171    QStringList splitline = mrunline.split(" -"+id, QString::SkipEmptyParts);
1172
1173    newmrunline = splitline.join("");
1174//  print new commandline to screen
1175    newmrunline.replace("  "," ");
1176    ui->commandline->setText(newmrunline);
1177
1178    return 0;
1179}
1180
1181
1182//****************************************************************************//
1183//  Mainwindow reset
1184int MainWindow::reset_window()
1185{
1186    ui->setupUi(this);
1187    recent_jobs(10);
1188    return 0;
1189}
1190
1191
1192//****************************************************************************//
1193//  Save current setting as default
1194int MainWindow::save_default()
1195{
1196
1197//  Get mrunline
1198    QString mrunline_save;
1199    QString mrunline = ui->commandline->text();
1200    QString userline = ui->line_user->text();
1201
1202    mrunline_save = mrunline;
1203    if (userline != "")
1204    {
1205        mrunline = mrunline + " " + userline;
1206    }
1207
1208
1209//  Define filename
1210    branch = QCoreApplication::applicationDirPath();
1211    branch = branch.left(branch.length() - 14);
1212
1213    QString filename = branch+"/.mrun.gui.default";
1214
1215//  Prepare data output
1216    QDateTime time = QDateTime::currentDateTime();
1217    QString tmptime = time.toString("yyyy/MM/dd hh:mm");
1218
1219//  Write to file
1220    QFile file(filename);
1221    file.open(QIODevice::WriteOnly | QIODevice::Text);
1222    QTextStream out(&file);
1223    if ( mrunline == "" || mrunline == " ")
1224    {
1225        out << "";
1226    }
1227    else
1228    {
1229       out << tmptime + " " + mrunline;
1230    }
1231
1232    file.close();
1233
1234    return 0;
1235}
1236
1237
1238//****************************************************************************//
1239//  Save current settings to a file of choice
1240int MainWindow::save_to_file()
1241{
1242
1243//  Get mrunline
1244    QString mrunline_save;
1245    QString mrunline = ui->commandline->text();
1246    QString userline = ui->line_user->text();
1247
1248    mrunline_save = mrunline;
1249    if (userline != "")
1250    {
1251        mrunline = mrunline + " " + userline;
1252    }
1253
1254//  Define a filename
1255    QString filename = QFileDialog::getSaveFileName(this, tr("Save to file"), \
1256                                    "", tr("Save files (*.sav)"));
1257    QString extension = filename.right(4);
1258
1259    if ( extension != ".sav" )
1260    {
1261        filename = filename + ".sav";
1262    }
1263
1264//  Prepare data output
1265    QDateTime time = QDateTime::currentDateTime();
1266    QString tmptime = time.toString("yyyy/MM/dd hh:mm");
1267
1268//  Write to file
1269    QFile file(filename);
1270    file.open(QIODevice::WriteOnly | QIODevice::Text);
1271    QTextStream out(&file);
1272    out << tmptime + " " + mrunline;
1273    file.close();
1274
1275    return 0;
1276}
1277
1278//****************************************************************************//
1279//  Start watchdog (palm_wd)
1280int MainWindow::start_watchdog()
1281{
1282    return system("nohup palm_wd >> /dev/null 2>&1 &");
1283
1284}
1285
1286//****************************************************************************//
1287//  Start job manager (palm_jm)
1288int MainWindow::start_jobmanager()
1289{
1290    return system("nohup palm_jm >> /dev/null 2>&1 &");
1291
1292}
1293
1294
1295//****************************************************************************//
1296//  Open job from file (previously saved by the user)
1297int MainWindow::open_from_file()
1298{
1299
1300//  Select filename and open it
1301    QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), \
1302                                    "", tr("Save files (*.sav)"));
1303
1304    QFile file(filename);
1305    QString mrunline;
1306    if ( filename != "")
1307    {
1308       if ( file.open(QIODevice::ReadOnly | QIODevice::Text ) )
1309       {
1310
1311//        File opened successfully
1312          QTextStream in(&file);
1313
1314          QString line = in.readLine();
1315          while (!line.isNull())
1316          {
1317             mrunline = line;
1318             line = in.readLine();
1319          }
1320          file.close();
1321       }
1322
1323//     In case a mrunline was found, load it to mainwindow
1324       if ( mrunline != "")
1325       {
1326
1327          mrunline = mrunline.right(mrunline.length() - 17);
1328          ui->commandline->setText(mrunline);
1329
1330          setup_gui(mrunline);
1331       }
1332    }
1333    return 0;
1334}
1335
1336
1337//****************************************************************************//
1338//  Open the last submitted job
1339int MainWindow::open_last()
1340{
1341    branch = QCoreApplication::applicationDirPath();
1342    branch = branch.left(branch.length() - 14);
1343
1344    QFile file(branch+"/.mrun.history");
1345
1346//  Load mrun history
1347    QString mrunline;
1348    if ( file.open(QIODevice::ReadOnly | QIODevice::Text ) )
1349    {
1350
1351//     file opened successfully
1352       QTextStream in(&file);
1353
1354       QString line = in.readLine();
1355       while (!line.isNull())
1356       {
1357          mrunline = line;
1358          line = in.readLine();
1359       }
1360
1361       file.close();
1362    }
1363
1364//  Select the last submitted job and print it to mainwindow
1365    if ( mrunline != "" )
1366    {
1367       mrunline = mrunline.right(mrunline.length() - 17);
1368       ui->commandline->setText(mrunline);
1369
1370       setup_gui(mrunline);
1371    }
1372    return 0;
1373}
1374
1375//****************************************************************************//
1376//  Display help window
1377int MainWindow::help()
1378{
1379
1380    QDialog *child_help = new QDialog;
1381    Ui::child_help ui;
1382    ui.setupUi(child_help);
1383    child_help->show();
1384
1385
1386    return 0;
1387}
1388
1389//****************************************************************************//
1390//  Display about window
1391int MainWindow::about_gui()
1392{
1393
1394    QDialog *about = new QDialog;
1395    Ui::about ui;
1396    ui.setupUi(about);
1397    about->show();
1398
1399    return 0;
1400}
1401
1402//****************************************************************************//
1403//  Setup mainwindow according to a given mrunline
1404int MainWindow::setup_gui(QString mrunline)
1405{
1406
1407//  Some initial settings
1408    QString user = "";
1409
1410    bool coupled_run = false;
1411    bool ocean_run   = false;
1412    bool advanced    = false;
1413    bool nojob       = false;
1414    bool rc_manual   = false;
1415
1416//  Split parameters in mrunline
1417    QStringList splitline = mrunline.split(" -", QString::SkipEmptyParts);
1418
1419    if ( splitline[0] != "mrun")
1420    {
1421        return 1;
1422
1423    }
1424    else
1425    {
1426        ui->group_job->setEnabled(true);
1427
1428//      Loop for all parameters in mrunline
1429        for(int i=splitline.count()-1; i>=1; i--)
1430        {
1431
1432//          Determine parameter
1433            QStringList splitparameter = splitline[i].split(" ", \
1434                                                      QString::SkipEmptyParts);
1435
1436            QString parameter = splitparameter[0];
1437            splitparameter.removeFirst();
1438            QString options = splitparameter.join(" ");
1439            options = options.replace("\"","");
1440
1441//          Check for suitable switch
1442            if ( parameter == "d")
1443            {
1444               if ( options != "")
1445               {
1446                  ui->line_jobname->setText(options);
1447                  nojob = false;
1448               }
1449               else
1450               {
1451                  nojob = true;
1452               }
1453            }
1454            else if ( parameter == "h")
1455            {
1456               ui->line_host->setText(options);
1457            }
1458            else if ( parameter == "q")
1459            {
1460               ui->line_q->setText(options);
1461            }
1462            else if ( parameter == "K")
1463            {
1464               ui->line_branch->setText(options);
1465            }
1466            else if ( parameter == "u")
1467            {
1468               ui->line_account->setText(options);
1469            }
1470            else if ( parameter == "X")
1471            {
1472               ui->line_pe->setText(options);
1473            }
1474            else if ( parameter == "T")
1475            {
1476               ui->line_tpn->setText(options);
1477            }
1478            else if ( parameter == "t")
1479            {
1480               ui->line_time->setText(options);
1481            }
1482            else if ( parameter == "B")
1483            {
1484               ui->check_delete_tmp_files->setChecked(true);
1485            }
1486            else if ( parameter == "v")
1487            {
1488               ui->check_verbose->setChecked(true);
1489            }
1490            else if ( parameter == "A")
1491            {
1492               ui->check_A->setChecked(true);
1493            }
1494            else if ( parameter == "b")
1495            {
1496               ui->check_b->setChecked(true);
1497               advanced = true;
1498            }
1499            else if ( parameter == "F")
1500            {
1501               ui->check_F->setChecked(true);
1502               advanced = true;
1503            }
1504            else if ( parameter == "I")
1505            {
1506               ui->check_I->setChecked(true);
1507               advanced = true;
1508            }
1509            else if ( parameter == "k")
1510            {
1511               ui->check_k->setChecked(true);
1512               advanced = true;
1513            }
1514            else if ( parameter == "O")
1515            {
1516               ui->check_O->setChecked(true);
1517               advanced = true;
1518            }
1519            else if ( parameter == "S")
1520            {
1521               ui->check_S->setChecked(true);
1522               advanced = true;
1523            }
1524            else if ( parameter == "x")
1525            {
1526               ui->check_x->setChecked(true);
1527               advanced = true;
1528            }
1529            else if ( parameter == "Z")
1530            {
1531               ui->check_Z->setChecked(true);
1532               advanced = true;
1533            } 
1534            else if ( parameter == "m")
1535            {
1536               ui->line_m->setText(options);
1537               advanced = true;
1538            }
1539            else if ( parameter == "M")
1540            {
1541               ui->line_M->setText(options);
1542               advanced = true;
1543            }
1544            else if ( parameter == "a")
1545            {
1546               ui->line_a->setText(options);
1547               advanced = true;
1548            }
1549            else if ( parameter == "D")
1550            {
1551               ui->line_D->setText(options);
1552               advanced = true;
1553            }
1554            else if ( parameter == "c")
1555            {
1556               ui->line_c->setText(options);
1557               advanced = true;
1558            }
1559            else if ( parameter == "p")
1560            {
1561               ui->line_p->setText(options);
1562               advanced = true;
1563            }
1564            else if ( parameter == "s")
1565            {
1566               ui->line_s->setText(options);
1567               advanced = true;
1568            }
1569            else if ( parameter == "i")
1570            {
1571               ui->line_i->setText(options);
1572               advanced = true;
1573               rc_manual = true;
1574            }
1575            else if ( parameter == "o")
1576            {
1577               ui->line_o->setText(options);
1578               advanced = true;
1579               rc_manual = true;
1580            }
1581            else if ( parameter == "w")
1582            {
1583               ui->line_w->setText(options);
1584               advanced = true;
1585            }
1586
1587//          Determine settings for coupled restart runs
1588            else if ( parameter == "Y")
1589            {
1590                QStringList optionssplit = options.split(" ", \
1591                                                   QString::SkipEmptyParts);
1592
1593                ui->line_PE_atmos->setEnabled(true);
1594                ui->line_PE_ocean->setEnabled(true);
1595                ui->label_coupled1->setEnabled(true);
1596                ui->label_coupled2->setEnabled(true);
1597                ui->label_coupled3->setEnabled(true);
1598                ui->label_coupling->setEnabled(true);
1599
1600                if (optionssplit.count() == 2)
1601                {
1602                   ui->line_PE_atmos->setText(optionssplit[0]);
1603                   ui->line_PE_ocean->setText(optionssplit[1]);
1604                }
1605                else
1606                {
1607                   ui->line_PE_atmos->setText("");
1608                   ui->line_PE_ocean->setText("");
1609                }
1610                coupled_run = true;
1611            }
1612            else if ( parameter == "n")
1613            {
1614                if ( options == "shared")
1615                {
1616                    ui->combo_n->setCurrentIndex(1);
1617                }
1618                else if ( options == "non_shared")
1619                {
1620                    ui->combo_n->setCurrentIndex(2);
1621                }
1622                else
1623                {
1624                    ui->combo_n->setCurrentIndex(0);
1625                }
1626            }
1627            else if ( parameter == "y")
1628            {
1629               ui->drop_job->setCurrentIndex(3);
1630            }
1631
1632//          Determine settings for the run control list
1633            else if ( parameter == "r")
1634            {
1635               QStringList optionssplit = options.split(" ", \
1636                                          QString::SkipEmptyParts);
1637
1638               QString options_2;
1639               QString options_all;
1640               for (int j=0; j<optionssplit.count(); j++)
1641               {
1642                   options_all = optionssplit[j];
1643                   options_2 = optionssplit[j].left(2);
1644                   if (options_2 == "ts")
1645                   {
1646                       ui->check_ts->setChecked(true);
1647                   }
1648                   if (options_2 == "pr" && options_all.left(3) != "prt")
1649                   {
1650                       ui->check_pr->setChecked(true);
1651                   }
1652                   if (options_2 == "xy")
1653                   {
1654                       ui->check_xy->setChecked(true);
1655                   }
1656                   if (options_2 == "xz")
1657                   {
1658                       ui->check_xz->setChecked(true);
1659                   }
1660                   if (options_2 == "yz")
1661                   {
1662                       ui->check_yz->setChecked(true);
1663                   }
1664                   if (options_2 == "3d")
1665                   {
1666                       ui->check_3d->setChecked(true);
1667                   }
1668                   if (options_2 == "ma")
1669                   {
1670                       ui->check_ma->setChecked(true);
1671                   }
1672                   if (options_2 == "sp")
1673                   {
1674                       ui->check_sp->setChecked(true);
1675                   }
1676                   if (options_all.left(3) == "prt")
1677                   {
1678                       ui->check_prt->setChecked(true);
1679                   }
1680                   if (options_all.left(3) == "pts")
1681                   {
1682                       ui->check_pts->setChecked(true);
1683                   }
1684                   if (options_2 == "d3")
1685                   {
1686                       if (options_all.left(3).right(1) == "#")
1687                       {
1688                          ui->drop_job->setCurrentIndex(0);
1689                       }
1690                       else if (options_all.left(3).right(1) == "f")
1691                       {
1692                          ui->drop_job->setCurrentIndex(1);
1693                       }
1694                       else if (options_all.left(3).right(1) == "o")
1695                       {
1696                          ocean_run = true;
1697                       }
1698                   }
1699                   if (options_all == "restart")
1700                   {
1701                      ui->check_restarts->setChecked(true);
1702//                    Check if _pdf file is available, otherwise notice user
1703                      QString jobname = ui->line_jobname->text();
1704                      branch = QCoreApplication::applicationDirPath();
1705                      branch = branch.left(branch.length() - 14);
1706
1707                      QFile restartfile(branch+"/JOBS/"+jobname+"/INPUT/"+ \
1708                                        jobname+"_p3df");
1709                      if (restartfile.exists() == true)
1710                      {
1711                         ui->label_restart->setText("");
1712                      }
1713                      else
1714                      {
1715                         ui->label_restart->setText("Warning: No p3df file \
1716                                                    found!");
1717                      }
1718                   }
1719
1720               }
1721
1722            }
1723
1724//          All unknown parameters are set as extra user parameters
1725            else
1726            {
1727                user = user + "-" + parameter + " \"" + options + "\" ";
1728                splitline.removeAt(i);
1729            }
1730        }
1731
1732//      Change drop box state in case of ocean precursor or coupled restart runs
1733        if ( ocean_run == true )
1734        {
1735           if ( coupled_run == true )
1736           {
1737             ui->drop_job->setCurrentIndex(4);
1738           }
1739           else
1740           {
1741              ui->drop_job->setCurrentIndex(3);
1742           }
1743        }
1744
1745        if ( user != "")
1746        {
1747           ui->line_user->setText(user);
1748        }
1749
1750//      Join mrunline and post it to mainwindow
1751        mrunline = splitline.join(" -");
1752        ui->commandline->setText(mrunline);
1753
1754//      Check if advanced settings are used and enable advanced dialog
1755        if ( advanced == true )
1756        {
1757           ui->check_advanced->setChecked(true);
1758        }
1759
1760//      Disable mainwindow if no job was found, otherwise enable
1761        if ( nojob == true )
1762        {
1763           ui->group_execution->setEnabled(false);
1764           ui->group_runcontrol->setEnabled(false);
1765           ui->button_start->setEnabled(false);
1766           ui->action_save->setEnabled(false);
1767           ui->drop_job->setEnabled(false);
1768           ui->group_advanced->setEnabled(false);
1769           ui->check_advanced->setEnabled(false);
1770        }
1771        else
1772        {
1773           ui->group_execution->setEnabled(true);
1774           ui->group_runcontrol->setEnabled(true);
1775           ui->button_start->setEnabled(true);
1776           ui->action_save->setEnabled(true);
1777           ui->check_advanced->setEnabled(true);
1778           ui->drop_job->setEnabled(true);
1779           if ( advanced == true )
1780           {
1781              ui->group_advanced->setEnabled(true);
1782           }
1783           else
1784           {
1785              ui->group_advanced->setEnabled(false);
1786           }
1787        }
1788
1789//      Disable run control box, if manual settings of -i and -o are used
1790        if ( rc_manual == true )
1791        {
1792           ui->group_runcontrol->setEnabled(false);
1793           change_commandline("r","remove");
1794           ui->button_start->setEnabled(true);
1795           ui->action_save->setEnabled(true);
1796        }
1797
1798    }
1799
1800    return 0;
1801}
Note: See TracBrowser for help on using the repository browser.