#include <stdio.h>

void data_C_save ( char *fname, unsigned int nx, unsigned int ny, double *x, double *y, double *z )
   {
   /****************************************************************************
   *
   * Function writes a 3d data-file for 3d contour, color, and mesh plots, and
   * 2d contour and color plots.
   *
   * Input
   *    fname: 3d data file-name (including path)
   *    nx:    number of values in x-coordinates array
   *    ny:    number of values in y-coordinates array
   *    x:     array of x coordinates
   *    y:     array of y coordinates
   *    z:     2-dimensional array of z coordinates
   *           z[i][j] = value of z corresponding to point (x[i], y[j])
   *
   * An example of how to call data_C_save is:
   *
   *    data_C_save("./sinc3d.txt", nx, ny, &x[0], &y[0], &z[0][0]);
   *
   * License:
   *    This document is released into the public domain.
   *
   ****************************************************************************/

   /* Declare variables */
   unsigned int i, j;
   FILE *fid;

   /* Write data file */
   fid = fopen(fname, "w");
   fprintf(fid, "%13d %13d", nx, ny);
   fprintf(fid, "\n");
   for ( i=1; i<=nx; i++ )
      fprintf(fid, "%+13.6e ", x[i-1]);
   fprintf(fid, "\n");
   for ( j=1; j<=ny; j++ )
      fprintf(fid, "%+13.6e ", y[j-1]);
   fprintf(fid, "\n");
   for ( i=1; i<=nx; i++ )
      {
      for ( j=1; j<=ny; j++ )
         fprintf(fid, "%+13.6e ", z[ny*(i-1)+j-1]);
      fprintf(fid, "\n");
      }
   fclose(fid);

   return;
   }

