Gwyscan Library
 All Files Functions Enumerations Enumerator Modules
preview.c

Example simulating creating preview which can be used during adaptive scanning path. First XYZ data are generated, they are interpolated using the preview function, then next XYZ data are appended to the first set (a detail in the scan) and whole set is interpolated again using the preview function. Three images are stored - interpolated first set, interpolated detail and final interpolated image from all the data available.

#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;
int npos = xres*yres;
double* xypos = (double *)malloc(2 * npos * sizeof(double));
double* vdata = (double *)malloc(8 * npos * sizeof(double));
//memset(vdata, 0, 8 * npos * sizeof(double));
npos = gwyscan_create_path_regular(xypos, xres, yres,
100, 100,
0, 0,
int result = gwyscan_save_gwyddion_xyz(xypos, vdata, npos, 8, xres, yres, NULL, "data_brutus.gxyz");
return 0;
double **preview_data12;
double *xydata, *zdata, *preview_data, *preview_data1, *preview_data2, *data_tmp;
double *preview_data3, *preview_data4;
double *xydata1, *zdata1;
double *xydata2, *zdata2;
char *desc[] = { "Data" };
char *desc1[] = { "Data 1" };
char *desc2[] = { "Data 2" };
char *desc12[] = { "Data1", "Data2" };
int i, ch;
//int n = 400;
int n = 160000;
// int xres = 400;
// int yres = 400;
int channels = 8;
preview_data = (double *)malloc(xres*yres*sizeof(double));
preview_data1 = (double *)malloc(xres*yres*sizeof(double));
preview_data2 = (double *)malloc(xres*yres*sizeof(double));
preview_data3 = (double *)malloc(xres*yres*sizeof(double));
preview_data4 = (double *)malloc(xres*yres*sizeof(double));
// 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));
gwyscan_preview_average(xydata, zdata, 2 * n, 0, preview_data, NULL, xres, yres, 1.0, 1.0, 0.0, 0.0, 0.0);
gwyscan_preview_average(xydata1, zdata1, n, 0, preview_data1, NULL, xres, yres, 1.0, 1.0, 0.0, 0.0, 0.0);
gwyscan_preview_average(xydata2, zdata2, n, 0, preview_data2, NULL, xres, yres, 0.5, 0.5, 0.0, 0.0, 0.0);
gwyscan_preview_average(xydata, zdata, n, 0, preview_data3, NULL, xres, yres, 1.0, 1.0, 0.0, 0.0, 0.0);
gwyscan_preview_average(xydata, zdata, n, 1, preview_data4, NULL, xres, yres, 0.5, 0.5, 0.0, 0.0, 0.0);
gwyscan_preview_subset_all_channels(xydata, zdata, n, 0, n, channels, preview_data12, NULL, xres, yres, 1.0, 1.0, 0.0, 0.0, 5.0);
//gwyscan_preview_subset_average_all_channels(xydata, zdata, n, 0, n, channels, preview_data12, NULL, xres, yres, 1.0, 1.0, 0.0, 0.0, 0.0);
// save raw data to disk
FILE* file1 = fopen("xyz_data.txt", "wt");
if (file1 == NULL)
return 0;
for (i = 0; i < n; i++)
{
fprintf(file1, "%g %g %g\n", xydata[2 * i], xydata[2 * i + 1], zdata[i]);
}
fclose(file1);
FILE* file2 = fopen("xyz_data1.txt", "wt");
if (file2 == NULL)
return 0;
for (i = 0; i < n; i++)
{
fprintf(file2, "%g %g %g\n", xydata1[2 * i], xydata1[2 * i + 1], zdata1[i]);
}
fclose(file2);
FILE* file3 = fopen("xyz_data2.txt", "wt");
if (file3 == NULL)
return 0;
for (i = 0; i < n; i++)
{
fprintf(file3, "%g %g %g\n", xydata2[2 * i], xydata2[2 * i + 1], zdata2[i]);
}
fclose(file3);
FILE* file4 = fopen("preview_data.txt", "wt");
if (file4 == NULL)
return 0;
for (i = 0; i < xres*yres; i++)
{
fprintf(file4, "%g\n", preview_data[i]);
}
fclose(file4);
FILE* file5 = fopen("preview_data1.txt", "wt");
if (file5 == NULL)
return 0;
for (i = 0; i < xres*yres; i++)
{
fprintf(file5, "%g\n", preview_data1[i]);
}
fclose(file5);
FILE* file6 = fopen("preview_data2.txt", "wt");
if (file6 == NULL)
return 0;
for (i = 0; i < xres*yres; i++)
{
fprintf(file6, "%g\n", preview_data2[i]);
}
fclose(file6);
FILE* file7 = fopen("preview_data3.txt", "wt");
if (file7 == NULL)
return 0;
for (i = 0; i < xres*yres; i++)
{
fprintf(file7, "%g\n", preview_data3[i]);
}
fclose(file7);
FILE* file8 = fopen("preview_data4.txt", "wt");
if (file8 == NULL)
return 0;
for (i = 0; i < xres*yres; i++)
{
fprintf(file8, "%g\n", preview_data4[i]);
}
fclose(file8);
for (ch = 0; ch < channels; ch++) {
char filename[256];
snprintf(filename, 256, "preview_data12_%d.txt", ch);
FILE* file9 = fopen(filename, "wt");
if (file9 == NULL)
return 0;
for (i = 0; i < xres*yres; i++)
{
fprintf(file9, "%g\n", preview_data12[ch][i]);
}
fclose(file9);
}
// 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);
free(preview_data);
free(preview_data1);
free(preview_data2);
free(preview_data3);
free(preview_data4);
return 0;
}