title: Wrong initialized values showing in netcdf file via ncview
body: <p>I'm trying to test simple leapfrog 2D advection scheme. What I am doing is writing values of each timestep as netCDF4 file. Initial state is set as value of 5.0 in the shape of square. When I print the initial state of the array it looks fine, but when it is written in netCDF4 and look through <code>ncview</code> it looks slanted. I can't figure out why this is happening. I am suspecting that it could be related to the part where I am writing each timestep value as a netCDF4 data (passing <code>p_tf[0][0]</code> in the function <code>nc_put_vara_double</code>). I used the function <code>nc_put_vara_double</code> just how it was used in their coding samples that is on Unidata website.</p>
  <p>Does this problem have something to do with <code>p_tf[0][0]</code> and it's indices? Why do you use <code>[0][0]</code> for this function?</p>
  <p>Below is the code</p>
  <pre><code>#include &lt;netcdf.h&gt;
  
  
  // netCDF constants
  #define err(e) {printf(&quot;Error: %s\n&quot;, nc_strerror(e)); return(2);}
  
  #define fname &quot;leap2d.nc&quot;
  
  // Variable sizes and dimensions (constants)
  #define Nx 10
  #define Ny 10
  #define Nt 2000
  #define ndims 3
  
  int main()
  {
  int printf ( const char * format, ... );
  int    i,j,t;
  double u = 5.0,
         v = 0.0,
         C = 0.01;
  
  // p_tf : p at future
  // p_tn : p at now
  // p_tp : p at past
  double q_tf[Ny+2][Nx+2];
  double q_tn[Ny+2][Nx+2];
  double q_tp[Ny+2][Nx+2];
  
  double (*p_tf)[Nx+2] = q_tf;
  double (*p_tn)[Nx+2] = q_tn;
  double (*p_tp)[Nx+2] = q_tp;
  
  
  // netCDF variables
  int    ncid, retval, varid, x_dimid, y_dimid, t_dimid;
  int    dimids[ndims];
  size_t start[ndims], count[ndims];
  
  // netCDF file operation
  // Creating netCDF file
  if ((retval = nc_create(fname, NC_CLOBBER, &amp;ncid)))
     err(retval);
  
  // Define dimensions
  if ((retval = nc_def_dim(ncid, &quot;y&quot;, Ny, &amp;y_dimid)))
     err(retval);
  if ((retval = nc_def_dim(ncid, &quot;x&quot;, Nx, &amp;x_dimid)))
     err(retval);
  if ((retval = nc_def_dim(ncid, &quot;t&quot;, NC_UNLIMITED, &amp;t_dimid)))
     err(retval);
  
  // Dimension ids
  dimids[0] = t_dimid;
  dimids[1] = y_dimid;
  dimids[2] = x_dimid;
  
  
  // Variable for writing netCDF data one timestep at a time
  count[0] = 1;     // For time dimension : 1 timestep
  count[1] = Ny;    // For y              : write everything
  count[2] = Nx;    // For x              : write everything
  
  start[1] = 0;     // For y              : don't do anything
  start[2] = 0;     // For x              : don't do anything
  
  if ((retval = nc_def_var(ncid, &quot;data&quot;, NC_DOUBLE, ndims, dimids, &amp;varid)))
     err(retval);
  
  if ((retval = nc_enddef(ncid)))
     err(retval);
  
  
  
  // Array initialization
  for(j=3;j&lt;7;j++)
  {
     for(i=3;i&lt;7;i++)
     {
        p_tf[j][i] = 5.0;
        p_tn[j][i] = 5.0;
        p_tp[j][i] = 5.0;
     }
  }
  
  
  for(j=0;j&lt;10;j++)
  {
     for(i=0;i&lt;10;i++)
     {
        printf(&quot;%1.0f   &quot;,p_tf[j][i]);
     }
  
     printf(&quot;\n&quot;);
  
  }
  
  start[0] = 0;
  
  if (retval = nc_put_vara_double(ncid, varid, start, count, &amp;p_tf[0][0]))
     err(retval);
  
  
  
  // Euler scheme for the first time step
  for(j=1;j&lt;Ny+1;j++)
  {
     for(i=1;i&lt;Nx+1;i++)
     {
        p_tf[j][i] = p_tn[j][i] - u * C * (p_tn[j][i] - p_tn[j][i-1])
                                - v * C * (p_tn[j][i] - p_tn[j-1][i]);
     
        // Periodic boundary condition for x
        p_tf[0][i]    = p_tf[Ny][i];
        p_tf[Ny+1][i] = p_tf[1][i];
     }
  
     // Periodic boundary condition for y
     p_tf[j][0]    = p_tf[j][Nx];
     p_tf[j][Nx+1] = p_tf[j][1];
  }
  
  p_tp = p_tn;
  p_tn = p_tf;
  
  
  start[0] = 1;
  
  if (retval = nc_put_vara_double(ncid, varid, start, count, &amp;p_tf[0][0]))
     err(retval);
  
  
  // Leapfrog scheme
  for(t=2;t&lt;Nt;t++)
  {
     for (j=0;j&lt;Ny+2;j++)
     {
        for(i=0;i&lt;Nx+2;i++)
        {
           p_tf[j][i] = p_tp[j][i] - u * C * (p_tn[j][i+1] - p_tn[j][i-1])
                                   - v * C * (p_tn[j+1][i] - p_tn[j-1][i]);
  
              
           // Periodic boundary condition for x
           p_tf[0][i]    = p_tf[Ny][i];
           p_tf[Ny+1][i] = p_tf[1][i];
        }
  
        // Periodic boundary condition for y
        p_tf[j][0]    = p_tf[j][Nx];
        p_tf[j][Nx+1] = p_tf[j][1];
     }
  
     p_tp = p_tn;
     p_tn = p_tf;
  
     start[0] = t;
     if ((retval = nc_put_vara_double(ncid, varid, start, count, &amp;p_tf[0][0])))
        err(retval);
  
  }
  
  if ((retval = nc_close(ncid)))
     err(retval);
  
  
  printf(&quot;\nDone\n&quot;);
  
  
  return 0;
  }
  </code></pre>
  <p>Thank you.</p>