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

Last change on this file since 1611 was 1611, checked in by maronga, 9 years ago

added new palm watchdog, removed old nc2vdf scripts

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