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

becker at geodynamics.org becker at geodynamics.org
Fri May 30 11:42:57 PDT 2008


Author: becker
Date: 2008-05-30 11:42:57 -0700 (Fri, 30 May 2008)
New Revision: 12062

Modified:
   mc/3D/CitcomS/trunk/visual/project_geoid.c
Log:
Fixed typo in memory allocation procedure that led to core dump.



Modified: mc/3D/CitcomS/trunk/visual/project_geoid.c
===================================================================
--- mc/3D/CitcomS/trunk/visual/project_geoid.c	2008-05-30 17:35:59 UTC (rev 12061)
+++ mc/3D/CitcomS/trunk/visual/project_geoid.c	2008-05-30 18:42:57 UTC (rev 12062)
@@ -1,8 +1,8 @@
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <math.h>
 
+
 /**/
 struct sph_harm {
     int len;
@@ -35,12 +35,22 @@
 typedef struct field field;
 
 
+void allocate_field(field *);
+void project_sph_harm_to_mesh(sph_harm *, field *);
+void allocate_sph_harm(sph_harm *, int );
+void get_sph_harm_coeff(char *, sph_harm* );
+void get_mesh(mesh *, int , int );
+double modified_plgndr_a(int , int , double );
+void project_sph_harm_to_mesh(sph_harm *, field *);
+void write_projected_field(char *, field *);
+
+
 void print_help()
 {
     const char msg[] = ""
         "Project the spherical harmonic coefficients to a regular mesh\n"
         "\n"
-        "Usage: project_geoid infile outfile n_longitude n_latitude\n"
+        "Usage: project_geoid infile outfile n_latitude n_longitude\n"
         "\n"
         "infile: CitcomS geoid file\n"
         "outfile: file will contain 3 columns (longitude, latitude, geoid)\n"
@@ -103,7 +113,7 @@
                 coeff->clm[index], coeff->slm[index]);
         */
     }
-
+    
     return;
 }
 
@@ -151,16 +161,24 @@
 }
 
 
-void allocate_field(field* geoid)
+void allocate_field(field *geoid)
 {
     int i;
     mesh *grid = geoid->grid;
 
     /* allocate memory */
-    geoid->data = malloc(grid->ntheta * sizeof(float));
-    for(i=0; i<grid->ntheta; i++)
-        geoid->data[i] = calloc(grid->nphi, sizeof(float));
-
+    geoid->data = (float **)malloc(grid->ntheta * sizeof(float *));
+    if(!geoid->data){
+      fprintf(stderr,"mem error\n");
+      exit(-1);
+    }
+    for(i=0; i < grid->ntheta; i++){
+      geoid->data[i] = (float *)calloc(grid->nphi, sizeof(float));
+      if(!geoid->data[i]){
+	fprintf(stderr,"mem error\n");
+	exit(-1);
+      }
+    }
     return;
 }
 
@@ -229,11 +247,11 @@
 
     allocate_field(geoid);
 
-    cosm = malloc(grid->nphi * sizeof(float));
-    sinm = malloc(grid->nphi * sizeof(float));
+    cosm = (float *)malloc(grid->nphi * sizeof(float));
+    sinm = (float *)malloc(grid->nphi * sizeof(float));
 
     /* projecting */
-    for(index=0; index<coeff->len; index++) {
+    for(index=0; index < coeff->len; index++) {
         ll = coeff->ll[index];
         mm = coeff->mm[index];
 
@@ -242,15 +260,15 @@
             sinm[j] = sin(mm * grid->phi[j]);
         }
 
-        for(i=0; i<grid->ntheta; i++) {
+        for(i=0; i < grid->ntheta; i++) {
             /* val = Plm(theta) */
             val = modified_plgndr_a(ll, mm, grid->theta[i]);
 
-            for(j=0; j<grid->nphi; j++) {
+            for(j=0; j < grid->nphi; j++) {
                 /* data = val*(Clm*cos(m*phi) + Slm*sin(m*phi)) */
-                geoid->data[i][j] += val *
-                    (coeff->clm[index] * cosm[j] +
-                     coeff->slm[index] * sinm[j]);
+	      geoid->data[i][j] += val *
+		(coeff->clm[index] * cosm[j] +
+		 coeff->slm[index] * sinm[j]);
             }
         }
     }



More information about the cig-commits mailing list