<p><b>mhoffman@lanl.gov</b> 2013-03-11 08:47:30 -0600 (Mon, 11 Mar 2013)</p><p>BRANCH COMMIT - land ice -ocean coupler<br>
<br>
Progress on the coupler script.<br>
Commented out ocean-related code.  Still working on getting it working properly with the ice sheet model only.<br>
</p><hr noshade><pre><font color="gray">Modified: branches/landice_ocean_coupler/landice-ocean-coupler.py
===================================================================
--- branches/landice_ocean_coupler/landice-ocean-coupler.py        2013-03-09 00:48:01 UTC (rev 2578)
+++ branches/landice_ocean_coupler/landice-ocean-coupler.py        2013-03-11 14:47:30 UTC (rev 2579)
@@ -4,6 +4,7 @@
 
 import numpy
 import os, sys, subprocess
+import time
 #import matplotlib.pyplot as plt
 try:
   from Scientific.IO.NetCDF import NetCDFFile
@@ -28,10 +29,7 @@
 # =======================================
 # Setup needed files
 ocnInput = 'grid.nc'
-ocnOutput = 'output.nc'
-
 liceInput = 'land_ice_grid.nc'
-liceOutput = 'output.nc'
 
 # Setup needed executables
 runLICE = 'mpirun -np 4 land_ice_model.exe'
@@ -39,7 +37,7 @@
 
 # Define number of coupling intervals (assumes you have set dt for each model accordingly, and ocean dt is evenly divisble in land ice dt)
 # (We may want to do more sophisticated time-keeping of the coupling if there is a need.)
-numCoupleIntervals = 3
+numCoupleIntervals = 2
 
 # Some parameters
 rhoi = 900.0  # land ice model's ice density (kg m^-3)
@@ -47,26 +45,34 @@
 
 # =======================================
 
+# clean up previous runs
+os.system('rm -rf land_ice/output*')
+os.system('rm -rf land_ice/restart*')
+os.system('rm -rf ocean/output*')
+os.system('rm -rf ocean/restart*')
 
-
-
-for t in range(coupleIntervals):   
+for t in range(numCoupleIntervals):   
+    print '============================================================'
     print 'Starting coupling number ', t
 
     # Get the appropriate source and destination files for the input needed by the coupler 
     if t==0:
         # Use I.C. from input files at initial time
         liceSourceFile = NetCDFFile('./land_ice/'+liceInput,'r')
-        ocnSourceFile =  NetCDFFile('./ocean/'+ocnInput,'r')
+#        ocnSourceFile =  NetCDFFile('./ocean/'+ocnInput,'r')
 
         # At initial time, we want to write to the input files
         liceDestFile = NetCDFFile('./land_ice/'+liceInput,'r+')
-        ocnDestFile =  NetCDFFile('./ocean/'+ocnInput,'r+')
+#        ocnDestFile =  NetCDFFile('./ocean/'+ocnInput,'r+')
 
     else:
         # Use values from output files at other times.
-        liceSourceFile = NetCDFFile('./land_ice/'+liceOutput,'r')
-        ocnSourceFile =  NetCDFFile('./ocean/'+ocnOutput,'r')
+        p = os.popen('ls -1t ./land_ice/output.*.nc | head -n 1')  # get the most recent output file
+        liceOutput = p.read().rstrip() # get stdout
+        liceSourceFile = NetCDFFile(liceOutput, 'r')
+#        p = os.popen('ls -1t ./ocean/output.*.nc | head -n 1')  # get the most recent output file
+#        ocnOutput = p.read().rstrip() # get stdout
+#        ocnSourceFile =  NetCDFFile(ocnOutput, 'r')
 
         # At other times we want to write to the restart files
         # Need to choose the appropriate restart file - there may be smarter ways to do this, but an easy way is to take the most recent
@@ -74,27 +80,27 @@
         p = os.popen('ls -1t ./land_ice/restart.*.nc | head -n 1')
         liceRestart = p.read().rstrip()  # get stdout
         liceDestFile = NetCDFFile(liceRestart,'r+')
-        p = os.popen('ls -1t ./ocean/restart.*.nc | head -n 1')
-        ocnRestart = p.read().rstrip()  # get stdout
-        ocnDestFile = NetCDFFile(ocnRestart,'r+')
+#        p = os.popen('ls -1t ./ocean/restart.*.nc | head -n 1')
+#        ocnRestart = p.read().rstrip()  # get stdout
+#        ocnDestFile = NetCDFFile(ocnRestart,'r+')
         
 
     # Get the source fields needed by the coupler
     # (Get the -1 time level for all variables - this will be the last whether this is from input or output
     liceThickness = liceSourceFile.variables['thickness'][-1,:]  
     liceBasalTemp = liceSourceFile.variables['temperature'][-1,:,-1]
-    ocnSurfTemp = ocnSourceFile.variables['temperature'][-1,:,0]  
-    ocnSurfSalin = ocnSourceFile.variables['salinity'][-1,:,0]  
+#    ocnSurfTemp = ocnSourceFile.variables['temperature'][-1,:,0]  
+#    ocnSurfSalin = ocnSourceFile.variables['salinity'][-1,:,0]  
 
     # Get the destination fields needed by the coupler
-    liceBMB = liceDestFile.variables['marineBasalMassBalanceTimeSeries'][:,0]  # There should only be on time level  
+    liceBMB = liceDestFile.variables['marineBasalMassBalTimeSeries'][:,0]  # There should only be on time level  
     #liceBHF = liceDestFile.variables['basalHeatFluxTimeSeries'][:,0]  # NEED TO RESTRICT THIS TO CELLS WITH ICE
-    ocnSurfPressure = ocnDestFile.variables['ssh'][0,:]  # TODO WHAT IS THE PROPER VARIABLE HERE?
+#    ocnSurfPressure = ocnDestFile.variables['ssh'][0,:]  # TODO WHAT IS THE PROPER VARIABLE HERE?
     # TODO WHAT ARE THE PROPER VARIABLE FOR MASS, HEAT, AND SALINITY FLUXES?
 
     # Calculate the ocean surface pressure - this could be a function call
     # TODO WHAT TO DO ABOUT NON-ICE SHELF OCEAN CELLS?
-    ocnSurfPressure = rhoi * grav * liceThickness  # TODO ARE THESE THE PROPER UNITS?
+#    ocnSurfPressure = rhoi * grav * liceThickness  # TODO ARE THESE THE PROPER UNITS?
 
     # Calculate boundary layer fluxes - this could be an internal or external function call
     #calculateBdyLyrFluxes(liceBasalTemp, ocnSurfTemp, ocnSurfSalin,   liceBHF, ocnMassFlux, ocnHeatFlux, ocnSalinFlux) #TODO
@@ -104,60 +110,71 @@
 
     # =======================================
     # Run the models  # TODO Do we want to do this in parallel?
-    try:
-        os.chdir('ocean')
-        # Set the namelist file properly for a restart or no
-        print 'Setting up ocean namelist file'
-        if t==0:
-            subprocess.check_call('sed -i.SEDBACKUP &quot;s/^.*config_do_restart.*/   config_do_restart = .false./&quot; namelist.input&quot; ' , shell=True, executable='/bin/bash')  # no restart
-            subprocess.check_call('sed -i.SEDBACKUP &quot;s/^.*config_write_output_on_startup.*/   config_write_output_on_startup = .true./&quot; namelist.input&quot; ' , shell=True, executable='/bin/bash')  # write the initial time
-            subprocess.check_call('sed -i.SEDBACKUP &quot;s/^.*config_start_time.*/   config_start_time = '0000-01-01_00:00:00'/&quot; namelist.input&quot; ' , shell=True, executable='/bin/bash')  # set the start time
-        else:
-            subprocess.check_call('sed -i.SEDBACKUP &quot;s/^.*config_do_restart.*/   config_do_restart = .true./&quot; namelist.input&quot; ' , shell=True, executable='/bin/bash') # do a restart
-            subprocess.check_call('sed -i.SEDBACKUP &quot;s/^.*config_write_output_on_startup.*/   config_write_output_on_startup = .false./&quot; namelist.input&quot; ' , shell=True, executable='/bin/bash')  # don't write the initial time
-            # Get the old stop time so we can set it to be the new start time.
-            p = os.popen('ls -1t output*nc | head -n 1')
-            oldoutputfile=p.read().rstrip()
-            p = os.popen('ncks -v xtime -H -s &quot;%c&quot; ' + oldoutputfile + ' | tail -r -c 65')
-            oldstoptime=p.read().rstrip()
-            subprocess.check_call('sed -i.SEDBACKUP &quot;s/^.*config_start_time.*/   config_start_time = &quot;' + oldstoptime '&quot;/&quot; namelist.input&quot; ' , shell=True, executable='/bin/bash')  # set the start time to the old stop time
+#    try:
+#        os.chdir('ocean')
+#        # Set the namelist file properly for a restart or no
+#        print 'Setting up ocean namelist file'
+#        if t==0:
+#            subprocess.check_call('sed -i.SEDBACKUP &quot;s/^.*config_do_restart.*/   config_do_restart = .false./&quot; namelist.input' , shell=True, executable='/bin/bash')  # no restart
+#            subprocess.check_call('sed -i.SEDBACKUP &quot;s/^.*config_write_output_on_startup.*/   config_write_output_on_startup = .true./&quot; namelist.input' , shell=True, executable='/bin/bash')  # write the initial time
+#            subprocess.check_call('sed -i.SEDBACKUP &quot;s/^.*config_start_time.*/   config_start_time = \'0000-01-01_00:00:00\'/&quot; namelist.input' , shell=True, executable='/bin/bash')  # set the start time
+#        else:
+#            subprocess.check_call('sed -i.SEDBACKUP &quot;s/^.*config_do_restart.*/   config_do_restart = .true./&quot; namelist.input' , shell=True, executable='/bin/bash') # do a restart
+#            subprocess.check_call('sed -i.SEDBACKUP &quot;s/^.*config_write_output_on_startup.*/   config_write_output_on_startup = .false./&quot; namelist.input' , shell=True, executable='/bin/bash')  # don't write the initial time
+#            # Get the old stop time so we can set it to be the new start time.
+#            p = os.popen('ls -1t output*nc | head -n 1')
+#            oldoutputfile=p.read().rstrip()
+#            p = os.popen('ncks -v xtime -H -s &quot;%c&quot; ' + oldoutputfile + ' | tail -r -c 65')
+#            oldstoptime=p.read().rstrip()
+#            subprocess.check_call('sed -i.SEDBACKUP &quot;s/^.*config_start_time.*/   config_start_time = ' + oldstoptime + '/&quot; namelist.input ' , shell=True, executable='/bin/bash')  # set the start time to the old stop time
 
-        print 'Starting ocean model.'
-        subprocess.check_call(runOCN, shell=True, executable='/bin/bash')
-        print 'Ocean model completed.'
-        # Copy output file so we don't lose it when the model restarts - could use Python's shutil module.  TODO Also may want to change format of the string conversion of t
-        subprocess.check_call('cp ' + ocnOutput + ' output.' + str(t) + '.nc', shell=True, executable='/bin/bash')
-        os.chdir('..')
-    except:
-        sys.exit('Ocean model failed!')
 
+#        print 'Starting ocean model.'
+#        subprocess.check_call(runOCN, shell=True, executable='/bin/bash')
+#        print 'Ocean model completed.'
+#        # Don't need to copy output file if the namelist is setup properly
+#        ### Copy output file so we don't lose it when the model restarts - could use Python's shutil module.  TODO Also may want to change format of the string conversion of t
+#        ##subprocess.check_call('cp ' + ocnOutput + ' output.' + str(t) + '.nc', shell=True, executable='/bin/bash')
+#        os.chdir('..')
+#    except:
+#        sys.exit('Ocean model failed!')
+
     try:
         os.chdir('land_ice')
         # Set the namelist file properly for a restart or no and set the start and stop times
         print 'Setting up land ice namelist file'
         if t==0:
-            subprocess.check_call('sed -i.SEDBACKUP &quot;s/^.*config_do_restart.*/   config_do_restart = .false./&quot; namelist.input&quot; ' , shell=True, executable='/bin/bash')  # no restart
-            subprocess.check_call('sed -i.SEDBACKUP &quot;s/^.*config_write_output_on_startup.*/   config_write_output_on_startup = .true./&quot; namelist.input&quot; ' , shell=True, executable='/bin/bash')  # write the initial time
-            subprocess.check_call('sed -i.SEDBACKUP &quot;s/^.*config_start_time.*/   config_start_time = '0000-01-01_00:00:00'/&quot; namelist.input&quot; ' , shell=True, executable='/bin/bash')  # set the start time
+            subprocess.check_call('sed -i.SEDBACKUP &quot;s/^.*config_do_restart.*/   config_do_restart = .false./&quot; namelist.input' , shell=True, executable='/bin/bash')  # no restart
+            print 'set no restart'
+            subprocess.check_call('sed -i.SEDBACKUP &quot;s/^.*config_write_output_on_startup.*/   config_write_output_on_startup = .true./&quot; namelist.input' , shell=True, executable='/bin/bash')  # write the initial time
+            print 'set write initial time'
+            subprocess.check_call('sed -i.SEDBACKUP &quot;s/^.*config_start_time.*/   config_start_time = \'0000-01-01_00:00:00\'/&quot; namelist.input' , shell=True, executable='/bin/bash')  # set the start time
+            print 'set start time'
         else:
-            subprocess.check_call('sed -i.SEDBACKUP &quot;s/^.*config_do_restart.*/   config_do_restart = .true./&quot; namelist.input&quot; ' , shell=True, executable='/bin/bash') # do a restart
-            subprocess.check_call('sed -i.SEDBACKUP &quot;s/^.*config_write_output_on_startup.*/   config_write_output_on_startup = .false./&quot; namelist.input&quot; ' , shell=True, executable='/bin/bash')  # don't write the initial time
-            # Get the old stop time so we can set it to be the new start time.
-            p = os.popen('ls -1t output*nc | head -n 1')
-            oldoutputfile=p.read().rstrip()
-            p = os.popen('ncks -v xtime -H -s &quot;%c&quot; ' + oldoutputfile + ' | tail -r -c 65')
+            subprocess.check_call('sed -i.SEDBACKUP &quot;s/^.*config_do_restart.*/   config_do_restart = .true./&quot; namelist.input' , shell=True, executable='/bin/bash') # do a restart
+            print 'set restart'
+            subprocess.check_call('sed -i.SEDBACKUP &quot;s/^.*config_write_output_on_startup.*/   config_write_output_on_startup = .true./&quot; namelist.input' , shell=True, executable='/bin/bash')  # don't write the initial time
+            print 'disabled initial write'
+            # Get the old stop time so we can set it to be the new start time. - it seems more reliable to search for the most recent restart file rather than the most recent output file.
+            p = os.popen('ls -1t restart*nc | head -n 1')
+            lastrestartfile=p.read().rstrip()
+            p = os.popen('ncks -v xtime -H -s &quot;%c&quot; ' + lastrestartfile + ' | tail -r -c 65')
             oldstoptime=p.read().rstrip()
-            subprocess.check_call('sed -i.SEDBACKUP &quot;s/^.*config_start_time.*/   config_start_time = &quot;' + oldstoptime '&quot;/&quot; namelist.input&quot; ' , shell=True, executable='/bin/bash')  # set the start time to the old stop time
+            subprocess.check_call('sed -i.SEDBACKUP &quot;s/^.*config_start_time.*/   config_start_time = ' + oldstoptime + '/&quot; namelist.input ' , shell=True, executable='/bin/bash')  # set the start time to the old stop time
+            print 'set start time'
 
         print 'Starting land ice model.'
         subprocess.check_call(runLICE, shell=True, executable='/bin/bash')
         print 'Land ice model completed.'
-        # Copy output file so we don't lose it when the model restarts - could use Python's shutil module.  TODO Also may want to change format of the string conversion of t
-        subprocess.check_call('cp ' + liceOutput + ' output.' + str(t) + '.nc', shell=True, executable='/bin/bash')
+        # Don't need to copy output file if the namelist is setup properly
+        ### Copy output file so we don't lose it when the model restarts - could use Python's shutil module.  TODO Also may want to change format of the string conversion of t
+        ##subprocess.check_call('cp ' + liceOutput + ' output.' + str(t) + '.nc', shell=True, executable='/bin/bash')
         os.chdir('..')
     except:
         sys.exit('Land Ice model failed!')
 
+    #time.sleep(62)
+
 # =======================================
 
 print 'Coupled model run complete!'

</font>
</pre>