[cig-commits] r4456 - mc/3D/CitcomS/trunk/visual

luis at geodynamics.org luis at geodynamics.org
Thu Aug 31 07:56:29 PDT 2006


Author: luis
Date: 2006-08-31 07:56:29 -0700 (Thu, 31 Aug 2006)
New Revision: 4456

Modified:
   mc/3D/CitcomS/trunk/visual/h5tocombined.c
Log:
Added ability to output multiple timesteps (specified on command-line).


Modified: mc/3D/CitcomS/trunk/visual/h5tocombined.c
===================================================================
--- mc/3D/CitcomS/trunk/visual/h5tocombined.c	2006-08-31 13:28:33 UTC (rev 4455)
+++ mc/3D/CitcomS/trunk/visual/h5tocombined.c	2006-08-31 14:56:29 UTC (rev 4456)
@@ -84,6 +84,12 @@
     int n, i, j, k;
     int nodex, nodey, nodez;
 
+    int t;
+    int timesteps;
+
+    int *steps;
+    char *endptr;
+
     field_t *coord;
     field_t *velocity;
     field_t *temperature;
@@ -94,21 +100,30 @@
      * Parse command-line parameters.                                       *
      ************************************************************************/
 
+    /*
+     * HDF5 file must be specified as first argument.
+     */
+    
     if (argc < 2)
     {
-        fprintf(stderr, "Usage: %s file.h5 step1 [step2 [...] ] \n", argv[0]);
+        fprintf(stderr, "Usage: %s file.h5 [step1 [step2 [...]]]\n", argv[0]);
         return EXIT_FAILURE;
     }
 
-    /* TODO: Read step(s) from argv[1:] */
+    /*
+     * Recognize help arguments.
+     */
 
-    step = 0;   /* step zero for now.. */
+    if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)
+    {
+        fprintf(stderr, "Usage: %s file.h5 [step1 [step2 [...]]] \n", argv[0]);
+        return EXIT_FAILURE;
+    }
 
+    /*
+     * Open HDF5 file (read-only). Complain if invalid.
+     */
 
-    /************************************************************************
-     * Open HDF5 file (read-only).                                          *
-     ************************************************************************/
-
     h5file = H5Fopen(argv[1], H5F_ACC_RDONLY, H5P_DEFAULT);
     if (h5file < 0)
     {
@@ -116,7 +131,29 @@
         return EXIT_FAILURE;
     }
 
+    /*
+     * Read step(s) from argv[2:]
+     */
+    
+    /* Allocate at least one step (we know argc >= 2) */
+    timesteps = (argc == 2) ? 1 : (argc-2);
+    steps = (int *)malloc(timesteps * sizeof(int));
 
+    /* By default, use zeroth step (might be overwritten) */
+    steps[0] = 0;
+
+    /* Convert argv[2:] into int array */
+    for(n = 2; n < argc; n++)
+    {
+        steps[n-2] = (int)strtol(argv[n], &endptr, 10);
+        if (!(argv[n][0] != '\0' && *endptr == '\0'))
+        {
+            fprintf(stderr, "Error: Could not parse step \"%s\"\n", argv[n]);
+            return EXIT_FAILURE;
+        }
+    }
+
+
     /************************************************************************
      * Get mesh parameters.                                                 *
      ************************************************************************/
@@ -153,61 +190,70 @@
 
 
     /************************************************************************
-     * Iterate over timesteps(TODO) and caps.                               *
+     * Output requested data.                                               *
      ************************************************************************/
 
-    for(capid = 0; capid < caps; capid++)
+    /* Iterate over timesteps */
+    for(t = 0; t < timesteps; t++)
     {
-        cap = open_cap(h5file, capid);
+        /* Determine step */
+        step = steps[t];
 
-        snprintf(filename, (size_t)99, "%s.cap%02d.%d", datafile, capid, step);
-        fprintf(stderr, "Writing %s\n", filename);
+        /* Iterate over caps */
+        for(capid = 0; capid < caps; capid++)
+        {
+            cap = open_cap(h5file, capid);
 
-        file = fopen(filename, "w");
-        fprintf(file, "%d x %d x %d\n", nodex, nodey, nodez);
+            snprintf(filename, (size_t)99, "%s.cap%02d.%d", datafile, capid, step);
+            fprintf(stderr, "Writing %s\n", filename);
 
-        /* Read data from HDF5 file. */
-        read_field(cap, coord, 0);
-        read_field(cap, velocity, step);
-        read_field(cap, temperature, step);
-        read_field(cap, viscosity, step);
+            file = fopen(filename, "w");
+            fprintf(file, "%d x %d x %d\n", nodex, nodey, nodez);
 
-        /* Traverse data in Citcom order */
-        n = 0;
-        for(j = 0; j < nodey; j++)
-        {
-            for(i = 0; i < nodex; i++)
+            /* Read data from HDF5 file. */
+            read_field(cap, coord, 0);
+            read_field(cap, velocity, step);
+            read_field(cap, temperature, step);
+            read_field(cap, viscosity, step);
+
+            /* Traverse data in Citcom order */
+            n = 0;
+            for(j = 0; j < nodey; j++)
             {
-                for(k = 0; k < nodez; k++)
+                for(i = 0; i < nodex; i++)
                 {
-                    fprintf(file, "%g %g %g %g %g %g %g %g\n",
-                            coord->data[3*n+0],
-                            coord->data[3*n+1],
-                            coord->data[3*n+2],
-                            velocity->data[3*n+0],
-                            velocity->data[3*n+1],
-                            velocity->data[3*n+2],
-                            temperature->data[n],
-                            viscosity->data[n]);
+                    for(k = 0; k < nodez; k++)
+                    {
+                        fprintf(file, "%g %g %g %g %g %g %g %g\n",
+                                coord->data[3*n+0],
+                                coord->data[3*n+1],
+                                coord->data[3*n+2],
+                                velocity->data[3*n+0],
+                                velocity->data[3*n+1],
+                                velocity->data[3*n+2],
+                                temperature->data[n],
+                                viscosity->data[n]);
 
-                    n++;    /* n = k + i*nodez + j*nodez*nodex */
+                        n++;    /* n = k + i*nodez + j*nodez*nodex */
+                    }
                 }
             }
+
+            fclose(file);
+            close_cap(cap);
         }
-
-        fclose(file);
-        close_cap(cap);
     }
 
     /* Release resources. */
 
-    close_field(coord);
-    close_field(velocity);
-    close_field(temperature);
-    close_field(viscosity);
-
+    status = close_field(coord);
+    status = close_field(velocity);
+    status = close_field(temperature);
+    status = close_field(viscosity);
     status = H5Fclose(h5file);
 
+    free(steps);
+
     return EXIT_SUCCESS;
 }
 



More information about the cig-commits mailing list