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

From CSDMS
Automated import of articles *** existing text overwritten ***
Automated import of articles *** existing text overwritten ***
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
<syntaxhighlight lang=c>
    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;
</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;