[cig-commits] r13339 - cs/portal/trunk/northridge/backend

leif at geodynamics.org leif at geodynamics.org
Tue Nov 18 17:28:18 PST 2008


Author: leif
Date: 2008-11-18 17:28:18 -0800 (Tue, 18 Nov 2008)
New Revision: 13339

Modified:
   cs/portal/trunk/northridge/backend/specfem3D.py
Log:
Enhanced the backend script 'specfem3D.py'.  It now extracts the
downloaded 3D earth model and generates a makefile for it.  It also
preprocesses the Fortran source in order to expand the @THIS_DIR@
macro (my stopgap solution to the problem of opening data files).


Modified: cs/portal/trunk/northridge/backend/specfem3D.py
===================================================================
--- cs/portal/trunk/northridge/backend/specfem3D.py	2008-11-19 00:11:18 UTC (rev 13338)
+++ cs/portal/trunk/northridge/backend/specfem3D.py	2008-11-19 01:28:18 UTC (rev 13339)
@@ -24,6 +24,7 @@
     parFile        = pyre.inventory.str("par-file")
     cmtSolution    = pyre.inventory.str("cmt-solution")
     stations       = pyre.inventory.str("stations")
+    model          = pyre.inventory.str("model")
     configureArgs  = pyre.inventory.str("configure-args", default="FC=mpif90")
 
     scratchDir     = pyre.inventory.str("scratch-dir", default="/scratch")
@@ -164,10 +165,75 @@
             if exists(dest):
                 continue
             os.symlink(src, dest)
+
+        self.prepareModel()
         
         return
 
 
+    def prepareModel(self):
+        import tarfile
+        from os.path import basename, dirname, splitext
+        
+        tgz = tarfile.open(self.model, 'r:gz')
+        path = "model"
+
+        directories = []
+        fortranSourceFiles = []
+
+        for tarinfo in tgz:
+            if tarinfo.isdir():
+                # Extract directory with a safe mode, so that
+                # all files below can be extracted as well.
+                try:
+                    os.makedirs(os.path.join(path, tarinfo.name), 0777)
+                except EnvironmentError:
+                    pass
+                directories.append(tarinfo)
+            elif tarinfo.name.endswith(".f90"):
+                pathname = os.path.join(path, tarinfo.name)
+                fortranSourceFiles.append(pathname)
+                thisDir = dirname(pathname)
+                s = tgz.extractfile(tarinfo)
+                f = open(pathname, "w")
+                # Preprocess.
+                for line in s.readlines():
+                    line = line.replace('@THIS_DIR@', thisDir)
+                    f.write(line)
+            else:
+                tgz.extract(tarinfo, path)
+
+        # Reverse sort directories.
+        directories.sort(lambda a, b: cmp(a.name, b.name))
+        directories.reverse()
+
+        # Set correct owner, mtime and filemode on directories.
+        for tarinfo in directories:
+            path = os.path.join(path, tarinfo.name)
+            try:
+                tgz.chown(tarinfo, path)
+                tgz.utime(tarinfo, path)
+                tgz.chmod(tarinfo, path)
+            except tarfile.ExtractError, e:
+                pass
+
+        # Generate the make include file.
+        s = open("model.mk", "w")
+        print >>s
+        print >>s, "model_OBJECTS = \\"
+        for sourceFile in fortranSourceFiles:
+            base = splitext(basename(sourceFile))[0]
+            print >>s, "\t$O/%s.o \\" % base
+        print >>s, "\t$(empty)"
+        print >>s
+        for sourceFile in fortranSourceFiles:
+            base = splitext(basename(sourceFile))[0]
+            print >>s, "$O/%s.o: constants.h %s" % (base, sourceFile)
+            print >>s, "\t${MPIFCCOMPILE_CHECK} -c -o $O/%s.o ${FCFLAGS_f90} %s" % (base, sourceFile)
+            print >>s
+        return
+
+
     def readAndCopyParFile(self):
         s = open(self.parFile, "r")
         d = open("DATA/Par_file", "w")
@@ -202,7 +268,7 @@
 
         # make
         self.mkdir("obj")
-        argv = ['make']
+        argv = ['make', 'xmeshfem3D', 'xspecfem3D']
         print ' '.join(argv)
         status = os.spawnvp(os.P_WAIT, argv[0], argv)
         if status != 0:



More information about the CIG-COMMITS mailing list