Template:CMI Example get grid values C: Difference between revisions

From CSDMS
Automated import of articles
 
Automated import of articles *** existing text overwritten ***
 
(2 intermediate revisions by the same user not shown)
Line 5: Line 5:
       struct edu_csdms_models_Sedflux3D__data *this =
       struct edu_csdms_models_Sedflux3D__data *this =
         edu_csdms_models_Sedflux3D__get_data (self);
         edu_csdms_models_Sedflux3D__get_data (self);
      edu_csdms_tools_Verbose_info (this->log, "Getting values.", _ex);


       if (this && this->state)
       if (this && this->state)
Line 26: Line 28:
             lower[i] = 0;
             lower[i] = 0;
             upper[i] = dimen[i]-1;
             upper[i] = dimen[i]-1;
            //upper[i] = dimen[n_dims-i-1]-1;
           }
           }
          //for (i=1, stride[0]=1; i<n_dims; i++)
          //  stride[i] = stride[i-1]*dimen[i-1];
           for (i=n_dims-2, stride[n_dims-1]=1; i>=0; i--)
           for (i=n_dims-2, stride[n_dims-1]=1; i>=0; i--)
             stride[i] = stride[i+1]*dimen[i+1];
             stride[i] = stride[i+1]*dimen[i+1];


          if (n_dims==2)
          {
            int i, i_0;
            fprintf (stderr, "%s: Getting values
", CMI_COMPONENT_NAME);
            fprintf (stderr, "%s: Shape is %dx%d
", CMI_COMPONENT_NAME,
                dimen[0], dimen[1]);
            fprintf (stderr, "%s: Stride is %dx%d
", CMI_COMPONENT_NAME,
                stride[0], stride[1]);
            fflush (stderr);
          }
          //vals = sidl_double__array_borrow (data, n_dims, lower, upper,
          //    stride);
           {
           {
             const int _n_dims = 1;
             const int _n_dims = 1;
Line 65: Line 48:


       }
       }
      edu_csdms_tools_Verbose_info (this->log, "Got values.", _ex);
     }
     }


     return vals;
     return vals;
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 12:01, 6 February 2012

    struct sidl_double__array* vals = NULL;

    {
      struct edu_csdms_models_Sedflux3D__data *this =
        edu_csdms_models_Sedflux3D__get_data (self);

      edu_csdms_tools_Verbose_info (this->log, "Getting values.", _ex);

      if (this && this->state)
      {
        int n_dims;
        int * dimen;
        double* data = BMI_Get_double (this->state, long_var_name, &n_dims,
            &dimen);

        if (data)
        { /* Create an array that borrows these values. */
          int i;
          int * lower = (int*)malloc (sizeof (int)*n_dims);
          int * upper = (int*)malloc (sizeof (int)*n_dims);
          int * stride = (int*)malloc (sizeof (int)*n_dims);

          /* BMI_get_double returns an array that is compact and has
           * unit stride */
          for (i=0; i<n_dims; i++)
          {
            lower[i] = 0;
            upper[i] = dimen[i]-1;
          }
          for (i=n_dims-2, stride[n_dims-1]=1; i>=0; i--)
            stride[i] = stride[i+1]*dimen[i+1];

          {
            const int _n_dims = 1;
            const int _lower[1] = {0};
            const int _upper[1] = {dimen[0]*dimen[1]-1};
            const int _stride[1] = {1};

            vals = sidl_double__array_borrow (data, _n_dims, _lower,
                _upper, _stride);
          }

          free (stride);
          free (upper);
          free (lower);
        }

      }
      edu_csdms_tools_Verbose_info (this->log, "Got values.", _ex);
    }

    return vals;