SimpleGridReader::SimpleGridReader(std::string filename, CartesianGrid3DIntegrator* m) 
    : filename(filename), model(m) 
{

    typedef CartesianGrid3D::Point Point;
    typedef CartesianGrid3D::Scalar Scalar;
    typedef CartesianGrid3D::Vector Vector;

    FILE* file=fopen(filename.c_str(), "rt");

    // initialize memory
    Point gridSpacing(1,1,1);
    Point firstPosition(0,0,0);
    std::vector<int> gridShape(3,1); // fill with ones

    // shape is first line, all integers
    fscanf(file, "%d %d %d", &gridShape[0], &gridShape[1], &gridShape[2]);
    std::cout << gridShape[0] << " " << gridShape[1] << " " << gridShape[2] << std::endl;

    boost::array<CartesianGrid3D::index, 3> shape = {{ 200, 200, 200 }};
    model->grid = new CartesianGrid3D::grid_type(shape);


//      Point gridSpacing(1,1,1);
//      Point firstPosition(-50,-50,-50);

      /** lorentz **/

      typedef CartesianGrid3D::index index;
      Scalar x,y,z;
      Scalar sigma = 10;
      Scalar r = 21;
      Scalar b = 2.6;
      for(index i = 0; i != shape[0]; ++i) 
      {
        x = firstPosition[0] + gridSpacing[0] * i;
        for(index j = 0; j != shape[1]; ++j)
        {
          y = firstPosition[1] + gridSpacing[1] * j;
          for(index k = 0; k != shape[2]; ++k)
          {
            z = firstPosition[2] + gridSpacing[2] * k;
            (*model->grid)[i][j][k] = Vector(sigma*(y-x), r*x-y-x*z, x*y-b*z);
          }
        }
      }  

    fclose(file);

      model->dynamics = new CartesianGrid3D(*model->grid, gridSpacing, firstPosition);
      double stepSize = 0.01;
      model->integrator = new RungeKutta4<CartesianGrid3D>(*(model->dynamics), stepSize);
}
void ossimHdfGridModel::setGridNodes(ossimDblGrid& grid, int32 sds_id, const ossimIpt& spacing)
{
   int x=0, y=0;
   ossim_uint32 index = 0;
   if (m_isHdf4)
   {
      int32 dim_sizes[MAX_VAR_DIMS];
      int32 rank, data_type, n_attrs;
      char  name[MAX_NC_NAME];

      int32 status = SDgetinfo(sds_id, name, &rank, dim_sizes, &data_type, &n_attrs);
      if (status == -1)
         return;

      int32 origin[2] = {0, 0}; 
      int32 num_rows = dim_sizes[0]; 
      int32 num_cols = dim_sizes[1]; 

      ossimDpt grid_origin(0,0); // The grid as used in base class, has UV-space always at 0,0 origin
      ossimIpt grid_size (num_cols, num_rows);
      ossimDpt dspacing (spacing);
      grid.initialize(grid_size, grid_origin, dspacing);

      float32* values = new float32 [num_rows * num_cols]; 
      intn statusN = SDreaddata(sds_id, origin, NULL, dim_sizes, (VOIDP)values);
      if (statusN > -1)
      {
         for (y = 0; y < num_rows; y++)
         {
            for (x = 0; x < num_cols; x++)
            {
               grid.setNode(x, y, values[index++]);
            }
         }
      }
      delete values;
   }
   else
   {
      hsize_t dims_out[2]; //dataset dimensions
      hid_t datatype = H5Dget_type(sds_id);
      hid_t dataspace = H5Dget_space(sds_id);    //dataspace handle
      int rank = H5Sget_simple_extent_ndims(dataspace);
      if (rank == 2)
      {
         herr_t status_n  = H5Sget_simple_extent_dims(dataspace, dims_out, NULL);

         ossim_int32 latGridRows = dims_out[0];
         ossim_int32 lonGridCols = dims_out[1];

         ossim_int32 cols = spacing.x;
         ossim_int32 rows = spacing.y;

         ossim_int32 spacingX = 0;
         ossim_int32 spacingY = 0;

         if (rows % latGridRows == 0 && cols % lonGridCols == 0)
         {
            spacingY = rows/latGridRows; //line increment step
            spacingX = cols/lonGridCols; //pixel increment step
         }
         
         ossimIpt gridSpacing(spacingX, spacingY);

         ossimDpt grid_origin(0,0); // The grid as used in base class, has UV-space always at 0,0 origin
         ossimIpt grid_size (cols, rows);
         ossimDpt dspacing (gridSpacing);
         grid.initialize(grid_size, grid_origin, dspacing);

         if( H5Tequal(H5T_NATIVE_FLOAT,  datatype))
         {
            ossim_float32* data_out = new ossim_float32[dims_out[0] * dims_out[1]];
            herr_t status = H5Dread(sds_id, datatype, dataspace, dataspace,
               H5P_DEFAULT, data_out);

            index = 0;
            for (y = 0; y < rows; y++)
            {
               for (x = 0; x < cols; x++)
               {
                  index = x + y * cols;
                  grid.setNode(x, y, data_out[index]);
               }
            }
            delete[] data_out;
         }
         else if( H5Tequal(H5T_NATIVE_DOUBLE,  datatype))
         {
            ossim_float64* data_out = new ossim_float64[dims_out[0] * dims_out[1]];
            herr_t status = H5Dread(sds_id, datatype, dataspace, dataspace,
               H5P_DEFAULT, data_out);

            index = 0;
            for (y = 0; y < rows; y++)
            {
               for (x = 0; x < cols; x++)
               {
                  index = x + y * cols;
                  grid.setNode(x, y, data_out[index]);
               }
            }
            delete[] data_out;
         }
      }
      H5Tclose(datatype);
      H5Sclose(dataspace);
   }
}