Gwyscan Library
 All Data Structures Files Functions Enumerations Enumerator Macros Groups
save_gwyddion_xyz.c

Example showing how to save Gwyddion XYZ file.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include "lib/gwyscan.h"
#include <stdio.h>
#include <assert.h>
int main()
{
int xres = 1234;
int yres = 1234;
double **preview_data12;
double *xydata, *zdata, *data_tmp;
double *xydata1, *zdata1;
double *xydata2, *zdata2;
char *desc[] = { "Data" };
char *desc1[] = { "Data 1" };
char *desc2[] = { "Data 2" };
char *desc12[] = { "Data1", "Data2" };
int i;
int n = 160000;
int channels = 8;
// allocate array for two channels
preview_data12 = (double **)malloc(channels*sizeof(double*));
data_tmp = (double *)malloc(xres*yres*sizeof(double));
memset(data_tmp, 0, sizeof(double)*xres*yres);
preview_data12[0] = data_tmp;
data_tmp = (double *)malloc(xres*yres*sizeof(double));
memset(data_tmp, 0, sizeof(double)*xres*yres);
preview_data12[1] = data_tmp;
data_tmp = (double *)malloc(xres*yres*sizeof(double));
memset(data_tmp, 0, sizeof(double)*xres*yres);
preview_data12[2] = data_tmp;
data_tmp = (double *)malloc(xres*yres*sizeof(double));
memset(data_tmp, 0, sizeof(double)*xres*yres);
preview_data12[3] = data_tmp;
data_tmp = (double *)malloc(xres*yres*sizeof(double));
memset(data_tmp, 0, sizeof(double)*xres*yres);
preview_data12[4] = data_tmp;
data_tmp = (double *)malloc(xres*yres*sizeof(double));
memset(data_tmp, 0, sizeof(double)*xres*yres);
preview_data12[5] = data_tmp;
data_tmp = (double *)malloc(xres*yres*sizeof(double));
memset(data_tmp, 0, sizeof(double)*xres*yres);
preview_data12[6] = data_tmp;
data_tmp = (double *)malloc(xres*yres*sizeof(double));
memset(data_tmp, 0, sizeof(double)*xres*yres);
preview_data12[7] = data_tmp;
xydata1 = (double *)malloc(2 * n*sizeof(double));
zdata1 = (double *)malloc(n*sizeof(double));
xydata2 = (double *)malloc(2 * n*sizeof(double));
zdata2 = (double *)malloc(n*sizeof(double));
xydata = (double *)malloc(2 * (2 * n*sizeof(double)));
zdata = (double *)malloc(channels * (n*sizeof(double)));
// generate xyz data; this simulates scanned data
gwyscan_generate_xyz_data(xydata1, zdata1, n, 1.0, 1.0, 0.0, 0.0, 0);
gwyscan_generate_xyz_data(xydata2, zdata2, n, 0.5, 0.5, 0.0, 0.0, 0);
// copy xydata1 and zdata1 to xydata and zdata
for (i = 0; i < 2 * n; i++)
xydata[i] = xydata1[i];
for (i = 0; i < n; i++)
zdata[i] = zdata1[i];
// simulate fine scan:
// append xydata2 and zdata2 to xydata and zdata
for (i = 0; i < 2 * n; i++)
xydata[i + 2 * n] = xydata2[i];
for (i = 0; i < n; i++)
zdata[i + n] = zdata2[i];
for (i = 0; i < n; i++)
zdata[i + 2*n] = zdata2[i];
for (i = 0; i < n; i++)
zdata[i + 3*n] = zdata2[i];
for (i = 0; i < n; i++)
zdata[i + 4*n] = zdata2[i];
for (i = 0; i < n; i++)
zdata[i + 5*n] = zdata2[i];
for (i = 0; i < n; i++)
zdata[i + 6*n] = zdata2[i];
for (i = 0; i < n; i++)
zdata[i + 7*n] = zdata2[i];
memcpy(xydata, xydata1, 2 * n*sizeof(double));
memcpy(xydata + 2 * n, xydata2, 2 * n*sizeof(double));
memcpy(zdata, zdata1, n*sizeof(double));
memcpy(zdata + n, zdata2, n*sizeof(double));
// save xyz data to disk
gwyscan_save_gwyddion_xyz(xydata1, zdata1, n, 1, xres, yres, NULL, "data_xy1_z1_ch1.gxyz");
gwyscan_save_gwyddion_xyz(xydata2, zdata2, n, 1, xres, yres, desc2, "data_xy2_z2_ch1.gxyz");
gwyscan_save_gwyddion_xyz(xydata, zdata, 2 * n, 1, xres, yres, desc, "data_xy_z_ch1.gxyz");
// save multichannel data
gwyscan_save_gwyddion_xyz(xydata1, zdata, n, 2, xres, yres, desc12, "data_xy1_z12_ch2.gxyz");
gwyscan_save_gwyddion_xyz(xydata2, zdata, n, 2, xres, yres, desc12, "data_xy2_z12_ch2.gxyz");
free(xydata1);
free(zdata1);
free(xydata2);
free(zdata2);
free(xydata);
free(zdata);
return 0;
}