source: trunk/MESOSCALE/PLOT/PYTHON/pywrf.example.r39/wrf/pydown/pydown.py @ 183

Last change on this file since 183 was 183, checked in by aslmd, 14 years ago

MESOSCALE: PYTHON: added pywrf r39 to act as a reference for futher developments

File size: 25.9 KB
Line 
1#!/usr/bin/python
2
3import os
4import sys
5import cPickle
6import subprocess as sp
7
8import pywrf.wrf.utils as wu
9import pywrf.wrf.pydown.utils as pdu
10
11
12def phase_description(phase_idx,pp):
13    '''
14    Returns a string that describes the phase that we are at
15
16    The 'phases' are:
17    phase 0: from wps to d01 real
18    phase 1: from d01 real to d01 wrf
19    phase 2: from d01 wrf to ppcheck
20    phase 3 ie (2 + 4*(pp-2) + 1): from ppcheck to d02 (dpp) real
21    phase 4 ie (2 + 4*(pp-2) + 2): from d02 (dpp) real to d02 (dpp) ndown
22    phase 5 ie (2 + 4*(pp-2) + 3): from d02 (dpp) ndown to d02 (dpp) wrf
23    phase 6 ie (2 + 4*(pp-2) + 4): from d02 (dpp) wrf to dppcheck
24    ...
25    for pp <= max_dom
26    '''
27    # if we are dealing we the outermost domain...
28    if phase_idx < 3:
29        if phase_idx == 0:
30            return 'Phase ' + str(phase_idx) + ': ' \
31              + 'We start by running the wps binaries and doing the housekeeping ' \
32              + 'needed to run real.exe for d' + str(pp).zfill(2) + '.'
33        elif phase_idx == 1:
34            return 'Phase ' + str(phase_idx) + ': ' \
35              + 'real.exe has completed for d' + str(pp).zfill(2) + '. ' \
36              + 'we can now move on to prepare for wrf.exe.'
37        elif phase_idx == 2:
38            return 'Phase ' + str(phase_idx) + ': ' \
39              + 'wrf.exe completed for d' + str(pp).zfill(2) + '. '\
40              + 'We are now going to archive the results and check if there ' \
41              + 'are more nests to run.'
42    # if we are dealing with one of the nests
43    else:
44        # reduce to 1-4
45        loop_idx = phase_idx - 4*(pp-2) - 2
46        if loop_idx == 1:
47            return 'Phase ' + str(phase_idx) + ': ' \
48              + 'Yup, we will now work on d' + str(pp).zfill(2) + '. '\
49              + 'We start by running real.exe for this grid by itself.'
50        elif loop_idx == 2:
51            return 'Phase ' + str(phase_idx) + ': ' \
52              + 'real.exe is done for d' + str(pp).zfill(2) + '. '\
53              + 'We now prepare to run ndown.exe for this grid and its parent.'
54        elif loop_idx == 3:
55            return 'Phase ' + str(phase_idx) + ': ' \
56              + 'ndown.exe is done for d' + str(pp).zfill(2) + '. '\
57              + 'We now prepare to run wrf.exe for this grid.'
58        elif loop_idx == 4:
59            return 'Phase ' + str(phase_idx) + ': ' \
60              + 'wrf.exe completed for d' + str(pp).zfill(2) + '. '\
61              + 'We are now going to archive the results and check if there ' \
62              + 'are more nests to run.'
63       
64def this_is_what_you_should_do_next(phase_idx):
65    '''
66    Returns a string that describes the mpi job that should be submitted and
67    checked after the previous phase
68
69    The 'phases' are:
70    phase 0: from wps to d01 real
71    phase 1: from d01 real to d01 wrf
72    phase 2: from d01 wrf to ppcheck
73    phase 3 ie (2 + 4*(pp-2) + 1): from ppcheck to d02 (dpp) real
74    phase 4 ie (2 + 4*(pp-2) + 2): from d02 (dpp) real to d02 (dpp) ndown
75    phase 5 ie (2 + 4*(pp-2) + 3): from d02 (dpp) ndown to d02 (dpp) wrf
76    phase 6 ie (2 + 4*(pp-2) + 4): from d02 (dpp) wrf to dppcheck
77    ...
78    for pp <= max_dom
79    '''
80    # if we are dealing withe the outermost domain...
81    if phase_idx < 3:
82        if phase_idx == 0:
83            return '\nCompleted phase ' + str(phase_idx) + '! ' \
84              + 'Now submit real.exe as an mpi job. Do not forget to check ' \
85              + 'the resuts ;)'
86        elif phase_idx == 1:
87            return 'Completed phase ' + str(phase_idx) + '! ' \
88              + 'Now submit wrf.exe as an mpi job. Do not forget to check ' \
89              + 'the resuts ;)'
90        elif phase_idx == 2:
91            return 'Completed phase ' + str(phase_idx) + '! ' \
92              + 'Just keep going to see if there is any more to do ;]'
93    # if we are dealing with one of the nests
94    else:
95        # reduce to 1-4
96        loop_idx = phase_idx - 4*(pp-2) - 2
97        if loop_idx == 1:
98            return 'Completed phase ' + str(phase_idx) + '! ' \
99              + 'Now submit real.exe as an mpi job. Do not forget to check ' \
100              + 'the resuts ;)'
101        elif loop_idx == 2:
102            return 'Completed phase ' + str(phase_idx) + '! ' \
103              + 'Now submit ndown.exe as an mpi job. Do not forget to check ' \
104              + 'the resuts ;)'
105        elif loop_idx == 3:
106            return 'Completed phase ' + str(phase_idx) + '! ' \
107              + 'Now submit wrf.exe as an mpi job. Do not forget to check ' \
108              + 'the resuts ;)'
109        elif loop_idx == 4:
110            return 'Completed phase ' + str(phase_idx) + '! ' \
111              + 'Just keep going to see if there is any more to do ;]'
112
113def check_with_user():
114    answer = raw_input('\nSowhaddoyouwonnadonow? Should we go ahead? [y/n]: ')
115    if answer not in ['y','n']:
116        check_with_user()
117    if answer == 'n':
118        dump_status()
119        sys.exit('Status saved: see yaa!')
120    else:
121        print
122
123def dump_status():
124    cPickle.dump((phase_completed),open(pydown_status,'w'))
125    return
126
127def load_status():
128    return cPickle.load(open(pydown_status,'r'))
129   
130
131
132# TODO make the pydown_dir a command_line argument (?)
133pydown_dir = os.getcwd()
134pydown_status = os.path.join(pydown_dir,'pydown.status')
135wps_dir = os.path.join(pydown_dir,'WPS')
136run_dir = os.path.join(pydown_dir,'WRFV2','run')
137archive_dir = os.path.join(pydown_dir,'archive')
138namelist_wps = os.path.join(wps_dir,'namelist.wps')
139namelist_input_master = os.path.join(run_dir,'namelist.input.master')
140
141#TODO consistency checks between namelist.wps and namelist.input
142# for now let's take it from namelist.input and assume the two namelists are
143# compatible
144namelist_input_master_dict = wu.read_namelist(namelist_input_master)
145max_dom = namelist_input_master_dict['&domains']['max_dom'][0]
146no_of_phases = (2 + 4*(max_dom-2) + 4) + 1
147phase_idx = 0
148pp = 1
149
150# control tower     
151# is this a brand new run or are we continuing a previous one?
152if os.path.isfile(pydown_status):
153    print '\nThis is what has been already done:\n'
154    phase_completed = load_status()
155    ready_to_roll = False
156else:
157    # Initial checks to protect fragile operations
158    fix_before_we_start = ''
159
160    if os.path.isdir(wps_dir):
161        wps_files = os.listdir(wps_dir)
162        if not ('GRIBFILE.AAA' in wps_files and 'GRIBFILE.AAB' in wps_files):
163            fix_before_we_start += "\t- There is no way for me to know which grib " \
164              + "files you want linked to generate the WRF's ICs and BCs so " \
165              + " please go back and link the ones you want.\n"
166        if 'Vtable' not in wps_files:
167            fix_before_we_start += "\t- There is no way for me to know which " \
168              + "Vtable goes with your grib files of choice so please go back " \
169              + "and link the one you want.\n"
170    else:
171        fix_before_we_start += "I couldn't find the WPS directory " \
172          + wps_dir + '\n'
173
174    if os.path.isdir(run_dir):
175        run_files = os.listdir(run_dir)
176        if 'namelist.input' in run_files:
177            fix_before_we_start += '\t- namelist.input in your ' + run_dir \
178            + ' will bother me so please move/delete it.\n'
179        if 'namelist.input.master' not in run_files:
180            fix_before_we_start += '\t- I expected to find a namelist.input.master ' \
181              + 'in your ' + run_dir + ' please put one (possibly sensible) ' \
182              + 'there for me to know what to do.\n'
183        if True in ['met_em' in file for file in run_files]:
184            fix_before_we_start += "\t- It looks like you've met_em files (or dir) " \
185              + "in your " + run_dir + ', please move/delete them.\n'
186    else:
187        fix_before_we_start += "\t- I couldn't find the run directory\n" \
188          + run_dir
189
190    if os.path.isdir(archive_dir):
191        archive_files = os.listdir(archive_dir)
192        if not (archive_files == ['README'] 
193          or archive_files == ['README','README~']):
194            # TODO this request might just be me being lazy so make it more general
195            # if are so inclined
196            fix_before_we_start += "\t- All I would like to see in the archive " \
197              + "directory is a README file.\n" \
198              + " \t\tDon't be lazy: jot down a couple of sentences in" \
199              + " archive/README" \
200              + " describing what you set out to achieve with this run... " \
201              + " Yeah, I know it's boring, but you will thank me later ;]\n" \
202              + " \t\tIf you've done this already, then get rid of any other" \
203              + " file in the archive folder."
204    else:
205        fix_before_we_start += "\t- I couldn't find the archive directory " \
206          + archive_dir + '\n'
207
208    if fix_before_we_start != '':
209        # We've got a problem
210        print 'Please address the following and restart the script:'
211        print fix_before_we_start
212        sys.exit()
213    else:
214        # All is good (or the error is semantyc).
215        print "Welcome to pydown. I have found all I need in order to start," \
216         + "so let's begin with:"
217        print phase_description(phase_idx,pp)
218   
219    phase_completed = [False for idx in range(no_of_phases)]
220    check_with_user()
221    ready_to_roll = True
222
223if not ready_to_roll and not phase_completed[phase_idx]:
224    print '\nThis is what has to be done next:\n'
225    print phase_description(phase_idx,pp)
226    check_with_user()   
227    ready_to_roll = True
228print phase_description(phase_idx,pp)
229if ready_to_roll:
230    os.chdir(wps_dir)
231    print '\tMoved to ' + wps_dir + '.'
232    # TODO I am assuming the user has already linked the correct Vtable and
233    # grib files as I cannot extract that information from the namelists...
234
235    # Let's run the wps components
236    print '\tRunning geogrid.exe:'
237    if 'Successful completion of geogrid' not in \
238      sp.Popen('./geogrid.exe', stdout=sp.PIPE).communicate()[0]:
239        # TODO handle the error in a more useful way.
240        print "Houston we've got a problem... with geogrid.exe!"
241    else:
242        print '\t\tDone!'
243    print '\tRunning ungrib.exe:'
244    if 'Successful completion of ungrib' not in \
245      sp.Popen('./ungrib.exe', stdout=sp.PIPE).communicate()[0]:
246        # TODO handle the error in a more useful way.
247        print "Houston we've got a problem... with ungrib.exe!"
248    else:
249        print '\t\tDone!'
250    print '\tRunning metgrid.exe:'
251    if 'Successful completion of metgrid' not in \
252      sp.Popen('./metgrid.exe', stdout=sp.PIPE).communicate()[0]:
253        # TODO handle the error in a more useful way.
254        print "Houston we've got a problem... with metgrid.exe!"
255    else:
256        print '\t\tDone!'
257
258    # By this stage the wps components should have executed successfully so we
259    # move on to archive the appropriate metadata for future reference
260    print '\tZipping metadata archive:'
261    wps_metadata = 'wps_metadata.zip'
262    cmd = r'zip ' + wps_metadata + ' configure.wps Vtable namelist.wps '
263    # TODO I am assuming standard name for the ungrib intermediate files ->
264    # this should be made more general by reading it from namelist.wps
265    for file in os.listdir(os.getcwd()):
266        if 'met_em' in file \
267          or '.log' in file \
268          or 'geo_em' in file \
269          or 'GRIBFILE' in file \
270          or 'FILE' in file :
271            cmd += file + ' '
272    # The following is to ignore both stdout and stderr from the zip command
273    # TODO check if the following syntax represents a portability issue
274    if sp.call(cmd.split(), stdout=open('/dev/null','w'), stderr = sp.STDOUT) == 0:
275        print '\t\tDone!'
276
277    # let's archive the metadata and move the met_em files to a met_en dir in
278    # the run_dir moving them out of the wps directory will free it up for
279    # other uses
280    # TODO none of the possible exceptions are caught... beware!
281    print '\tArchiving metadata:'
282    os.rename(wps_metadata,os.path.join(archive_dir,wps_metadata))
283    print '\t\tDone!'
284
285    print '\tMoving met_em* files to the run directory:'
286    os.mkdir(os.path.join(run_dir,'met_em'))
287    for file in os.listdir(os.getcwd()):
288        if 'met_em' in file:
289            os.rename(file,os.path.join(run_dir,'met_em',file))
290    print '\t\tDone!'
291   
292    # let's now clean the wps_dir
293    # TODO I am just being lazy here but the user
294    # should be forced to regenerate Vtable and GRIBFILE.* links every time to
295    # make sure they are the right thing... beware!
296          #or 'GRIBFILE' in file \
297          #or 'Vtable' == file \
298    print '\tCleaning the wps directory:'
299    for file in os.listdir(os.getcwd()):
300        if 'geo_em' in file \
301          or 'FILE:' in file \
302          or 'PFILE:' in file:
303            os.remove(file)
304    print '\t\tDone!'
305
306    print '\tGenerating the namelist.input.d01.real:'
307    current_namelist_input = \
308      pdu.generate_namelist_input_d01_real(namelist_input_master,run_dir)
309    print '\t\tDone!'
310
311    print '\tLinking the just generated namelist.input:'
312    os.symlink(current_namelist_input,os.path.join(run_dir,'namelist.input'))
313    print '\t\tDone!'
314   
315    print '\tLinking the previously generated met_em files:'
316    for file in os.listdir(os.path.join(run_dir,'met_em')):
317        if 'd' + str(pp).zfill(2) in file:
318            os.symlink(os.path.join(run_dir,'met_em',file),
319              os.path.join(run_dir,file[:9] + '1' + file[10:]))
320    print '\t\tDone!'
321
322    phase_completed[phase_idx] = True
323    # let's be patronizing...
324    print this_is_what_you_should_do_next(phase_idx)
325    check_with_user()
326# we are finished let's move on to the next phase
327phase_idx += 1
328
329if not ready_to_roll and not phase_completed[phase_idx]:
330    print '\nThis is what has to be done next:\n'
331    print phase_description(phase_idx,pp)
332    check_with_user()   
333    ready_to_roll = True
334print phase_description(phase_idx,pp)
335if ready_to_roll:
336    # let's start with a little housekeeping to clean after the real.exe
337    os.chdir(run_dir)
338    print '\tMoved to ' + run_dir + '.'
339    print '\tMoving real log files out of the way:'
340    os.mkdir(os.path.join(run_dir,'real_logs'))
341    for file in os.listdir(os.getcwd()):
342        if 'rsl' in file \
343          or 'std' in file:
344            os.rename(file,os.path.join(run_dir,'real_logs',file))
345    print '\t\tDone!'
346   
347    print '\tGenerating the namelist.input.d01.wrf:'
348    current_namelist_input = \
349      pdu.generate_namelist_input_d01_wrf(namelist_input_master,run_dir)
350    print '\t\tDone!'
351
352    print '\tLinking the just generated namelist.input:'
353    os.remove(os.path.join(run_dir,'namelist.input'))
354    os.symlink(current_namelist_input,os.path.join(run_dir,'namelist.input'))
355    print '\t\tDone!'
356
357    phase_completed[phase_idx] = True
358    print this_is_what_you_should_do_next(phase_idx)
359    check_with_user()
360    # we are finished let's move on to the next phase
361phase_idx += 1
362
363if not ready_to_roll and not phase_completed[phase_idx]:
364    print '\nThis is what has to be done next:\n'
365    print phase_description(phase_idx,pp)
366    check_with_user()   
367    ready_to_roll = True
368print phase_description(phase_idx,pp)
369if ready_to_roll:
370    # let's start with a little housekeeping to clean after the wrf.exe
371    os.chdir(run_dir)
372    print '\tMoved to ' + run_dir + '.'
373    print '\tMoving wrf log files out of the way:'
374    os.mkdir(os.path.join(run_dir,'wrf_logs'))
375    for file in os.listdir(os.getcwd()):
376        if 'rsl' in file \
377          or 'std' in file:
378            os.rename(file,os.path.join(run_dir,'wrf_logs',file))
379    print '\t\tDone!'
380
381    # then move on to generating and archiving the metadata and wrfout_d01*
382    print '\tZipping metadata archive:'
383    wrf_metadata = 'wrf_metadata_d' + str(pp).zfill(2) + '.zip'
384    cmd = r'zip -r ' + wrf_metadata + ' wrfndi_d02 ../compile.log ' \
385      + '../configure.wrf wrfinput_d01 wrfbdy_d01 submit_wrf.sh ' \
386      + 'submit_real.sh submit_ndown.sh namelist.input ' \
387      + 'wrf_logs real_logs ndown_logs'
388    # The following is to ignore both stdout and stderr from the zip command
389    # TODO check if the following syntax represents a portability issue
390    if sp.call(cmd.split(), stdout=open('/dev/null','w'), stderr = sp.STDOUT) == 0:
391        print '\t\tDone!'
392
393    # TODO check that the appropriate directory structure exist for the archive
394    # and create it if it doesn't   
395    # TODO none of the possible exceptions are caught... beware!
396    print '\tArchiving metadata:'
397    os.rename(wrf_metadata,os.path.join(archive_dir,wrf_metadata))
398    print '\t\tDone!'
399
400    print '\tMoving wrfout_d01* files to the archive directory:'
401    for file in os.listdir(os.getcwd()):
402        if 'wrfout' in file:
403            os.rename(file,os.path.join(archive_dir,file))
404    print '\t\tDone!'
405   
406    # let's now clean the wrf_dir
407    print '\tCleaning the wrf directory:'
408    for file_or_dir in os.listdir(os.getcwd()):
409        if 'met_em.' in file_or_dir \
410          or 'wrfinput' in file_or_dir \
411          or 'wrfndi' in file_or_dir \
412          or 'namelist.input' == file_or_dir \
413          or 'wrfbdy' in file_or_dir:
414            os.remove(file_or_dir)
415        # only my logs directories will match 'logs'
416        # TODO make this more general/robust or warn the user they should not
417        # have anything that matches this check in their run_dir
418        if 'logs' in file_or_dir:
419            for nested_file in os.listdir(file_or_dir):
420                os.remove(os.path.join(file_or_dir,nested_file))
421            os.rmdir(file_or_dir)
422    print '\t\tDone!'
423
424    phase_completed[phase_idx] = True
425    print this_is_what_you_should_do_next(phase_idx)
426    check_with_user()
427# we are finished let's move on to the next phase
428phase_idx += 1
429
430while pp < max_dom:
431    pp += 1
432
433    if not ready_to_roll and not phase_completed[phase_idx]:
434        print '\nThis is what has to be done next:\n'
435        print phase_description(phase_idx,pp)
436        check_with_user()   
437        ready_to_roll = True
438    print phase_description(phase_idx,pp)
439    if ready_to_roll:
440        print '\tGenerating the namelist.input.d' + str(pp).zfill(2) + '.real:'
441        current_namelist_input = \
442          pdu.generate_namelist_input_dpp_real(pp,namelist_input_master,run_dir)
443        print '\t\tDone!'
444
445        print '\tLinking the just generated namelist.input:'
446        os.symlink(current_namelist_input,os.path.join(run_dir,'namelist.input'))
447        print '\t\tDone!'
448       
449        print '\tLinking the previously generated met_em files:'
450        for file in os.listdir(os.path.join(run_dir,'met_em')):
451            if 'd' + str(pp).zfill(2) in file:
452                os.symlink(os.path.join(run_dir,'met_em',file),
453                  os.path.join(run_dir,file[:9] + '1' + file[10:]))
454        print '\t\tDone!'
455       
456        phase_completed[phase_idx] = True
457        print this_is_what_you_should_do_next(phase_idx)
458        check_with_user()
459    # we are finished let's move on to the next phase
460    phase_idx += 1
461   
462    if not ready_to_roll and not phase_completed[phase_idx]:
463        print '\nThis is what has to be done next:\n'
464        print phase_description(phase_idx,pp)
465        check_with_user()   
466        ready_to_roll = True
467    print phase_description(phase_idx,pp)
468    if ready_to_roll:
469        # let's start with a little housekeeping to clean after the real.exe
470        os.chdir(run_dir)
471        print '\tMoved to ' + run_dir + '.'
472
473        print '\tMoving real log files out of the way:'
474        os.mkdir(os.path.join(run_dir,'real_logs'))
475        for file in os.listdir(os.getcwd()):
476            if 'rsl' in file \
477              or 'std' in file:
478                os.rename(file,os.path.join(run_dir,'real_logs',file))
479        print '\t\tDone!'
480
481        #let's prepare for ndown
482        print '\tRename wrfinput_d01 wrfndi_02 and get rid of wrfbdy_d01:'
483        os.rename('wrfinput_d01','wrfndi_d02')
484        os.remove('wrfbdy_d01')
485        print '\t\tDone!'
486
487        print '\tGenerating the namelist.input.d' + str(pp).zfill(2) + '.ndown:'
488        current_namelist_input = \
489          pdu.generate_namelist_input_dpp_ndown(pp,namelist_input_master,run_dir)
490        print '\t\tDone!'
491
492        print '\tLinking the just generated namelist.input:'
493        if os.path.isfile(os.path.join(run_dir,'namelist.input')):
494            os.remove(os.path.join(run_dir,'namelist.input'))
495        os.symlink(current_namelist_input,os.path.join(run_dir,'namelist.input'))
496        print '\t\tDone!'
497
498        # TODO this statement is untested
499        print '\tLinking the (previously calculated) wrfout_d' \
500          + str(pp-1).zfill(2)
501        for file in os.listdir(archive_dir):
502            if 'wrfout_d' + str(pp-1).zfill(2) in file:
503                os.symlink(os.path.join(archive_dir,file),
504                file[:9] + '1' + file[10:])
505        print '\t\tDone!' 
506
507        phase_completed[phase_idx] = True
508        print this_is_what_you_should_do_next(phase_idx)
509        check_with_user()
510    # we are finished let's move on to the next phase
511    phase_idx += 1
512           
513    if not ready_to_roll and not phase_completed[phase_idx]:
514        print '\nThis is what has to be done next:\n'
515        print phase_description(phase_idx,pp)
516        check_with_user()   
517        ready_to_roll = True
518    print phase_description(phase_idx,pp)
519    if ready_to_roll:
520        # let's start with a little housekeeping to clean after the ndown.exe
521        os.chdir(run_dir)
522        print '\tMoved to ' + run_dir + '.'
523
524        print '\tMoving ndown log files out of the way:'
525        os.mkdir(os.path.join(run_dir,'ndown_logs'))
526        for file in os.listdir(os.getcwd()):
527            if 'rsl' in file \
528              or 'std' in file:
529                os.rename(file,os.path.join(run_dir,'ndown_logs',file))
530        print '\t\tDone!'
531
532        #let's prepare for wrf
533        print '\tRename wrfinput_d02 and wrfbdy_02 to wrfinput_d01 and wrfbdy_d01:'
534        os.rename('wrfinput_d02','wrfinput_d01')
535        os.rename('wrfbdy_d02','wrfbdy_d01')
536        print '\t\tDone!'
537
538        print '\tGenerating the namelist.input.d' + str(pp).zfill(2) + '.wrf:'
539        current_namelist_input = \
540          pdu.generate_namelist_input_dpp_wrf(pp,namelist_input_master,run_dir)
541        print '\t\tDone!'
542
543        print '\tLinking the just generated namelist.input:'
544        os.remove(os.path.join(run_dir,'namelist.input'))
545        os.symlink(current_namelist_input,os.path.join(run_dir,'namelist.input'))
546        print '\t\tDone!'
547
548        print '\tRemove the links to  wrfout_d' + str(pp-1).zfill(2) + '*'
549        for file in os.listdir(run_dir):
550            #if 'wrfout_d' + str(pp-1).zfill(2) in file:
551            if 'wrfout_d01' in file:
552                os.remove(file)
553        print '\t\tDone!' 
554
555        phase_completed[phase_idx] = True
556        print this_is_what_you_should_do_next(phase_idx)
557        check_with_user()
558    # we are finished let's move on to the next phase
559    phase_idx += 1
560           
561    if not ready_to_roll and not phase_completed[phase_idx]:
562        print '\nThis is what has to be done next:\n'
563        print phase_description(phase_idx,pp)
564        check_with_user()   
565        ready_to_roll = True
566    print phase_description(phase_idx,pp)
567    if ready_to_roll:
568        # let's start with a little housekeeping to clean after the wrf.exe
569        os.chdir(run_dir)
570        print '\tMoved to ' + run_dir + '.'
571        print '\tMoving wrf log files out of the way:'
572        os.mkdir(os.path.join(run_dir,'wrf_logs'))
573        for file in os.listdir(os.getcwd()):
574            if 'rsl' in file \
575              or 'std' in file:
576                os.rename(file,os.path.join(run_dir,'wrf_logs',file))
577        print '\t\tDone!'
578
579        # then move on to generating and archiving the metadata and wrfout_d01*
580        print '\tZipping metadata archive:'
581        wrf_metadata = 'wrf_metadata_d' + str(pp).zfill(2) + '.zip'
582        cmd = r'zip -r ' + wrf_metadata + ' wrfndi_d02 ../compile.log ' \
583          + '../configure.wrf wrfinput_d01 wrfbdy_d01 submit_wrf.sh ' \
584          + 'submit_real.sh submit_ndown.sh namelist.input ' \
585          + 'wrf_logs real_logs ndown_logs'
586        # The following is to ignore both stdout and stderr from the zip command
587        # TODO check if the following syntax represents a portability issue
588        if sp.call(cmd.split(), stdout=open('/dev/null','w'), stderr = sp.STDOUT) == 0:
589            print '\t\tDone!'
590   
591        # TODO check that the appropriate directory structure exist for the archive
592        # and create it if it doesn't   
593        # TODO none of the possible exceptions are caught... beware!
594        print '\tArchiving metadata:'
595        os.rename(wrf_metadata,os.path.join(archive_dir,wrf_metadata))
596        print '\t\tDone!'
597   
598        print '\tMoving wrfout_d01* files to the archive directory:'
599        for file in os.listdir(os.getcwd()):
600            if 'wrfout' in file:
601                os.rename(file,os.path.join(archive_dir,file[:8] \
602                  + str(pp).zfill(2) + file[10:]))
603        print '\t\tDone!'
604       
605        # let's now clean the wrf_dir
606        print '\tCleaning the wrf directory:'
607        for file_or_dir in os.listdir(os.getcwd()):
608            if 'met_em.' in file_or_dir \
609              or 'wrfinput' in file_or_dir \
610              or 'wrfndi' in file_or_dir \
611              or 'wrfbdy' in file_or_dir:
612                os.remove(file_or_dir)
613            if os.path.isfile(os.path.join(run_dir,'namelist.input')):
614                os.remove(os.path.join(run_dir,'namelist.input'))
615            # only my logs directories will match 'logs'
616            # TODO make this more general/robust or warn the user they should not
617            # have anything that matches this check in their run_dir
618            if 'logs' in file_or_dir:
619                for nested_file in os.listdir(file_or_dir):
620                    os.remove(os.path.join(file_or_dir,nested_file))
621                os.rmdir(file_or_dir)
622        print '\t\tDone!'
623   
624        phase_completed[phase_idx] = True
625        print this_is_what_you_should_do_next(phase_idx)
626        check_with_user()
627    # we are finished let's move on to the next phase
628    phase_idx += 1
629
630# the presence of a pydown.status file will be assumed to mean that we want to
631# continue a partially executed run. We are now finished so we need to clean up
632# the directory not to confuse the program next time it is run.
633# TODO check that this exists before erasing it
634os.remove(pydown_status)
635print 'No more nests to run... good night and good luck.'
Note: See TracBrowser for help on using the repository browser.