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

Last change on this file since 920 was 920, checked in by maronga, 12 years ago

Bugfixes for the parameter file check. New mrun option switches off combine_plot_fields

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