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

Example showing how to save single array (one channel) to a Gwyddion GWY file.

Arrays is passed as one-dimensional array which is a flat representation of a 3d array of xres*yres size.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include "lib/gwyscan.h"
#include <windows.h>
/* Create a ‘simulated’ image of dimensions xres×yres. */
static double*
create_image_data(int xres, int yres)
{
double *data = (double*)malloc(xres*yres * sizeof(double));
int i, j, k;
k = 0;
for (i = 0; i < yres; i++) {
for (j = 0; j < xres; j++, k++) {
data[k] = 0.08*sin(i / 10.0)*cos(j / 5.0)
+ ((i ? data[k - xres] : 0.0)
+ (j ? data[k - 1] : 0.0))*(i && j ? 0.48 : 0.8);
}
}
for (k = 0; k < xres*yres; k++)
data[k] *= 1.2e-11;
return data;
}
int main()
{
int i, j, idx;
int xres[2], yres[2];
double width[2], height[2];
double **data;
bool res;
const char *xyunit[] = { "m", "V" };
const char *zunit[] = { "m", "A" };
const char *desc[] = { "One channel", "Another channel" };
xres[0] = yres[0] = 100;
xres[1] = 300;
yres[1] = 200;
width[0] = height[0] = 1;
width[1] = height[1] = 0.5;
/********************************** 1d arrays version ****************************/
data = (double **)malloc(2 * sizeof(double *));
for (i = 0; i < 2; i++)
data[i] = (double *)malloc(xres[i] * yres[i] * sizeof(double));
idx = 0;
for (j = 0; j < yres[0]; j++)
for (i = 0; i < xres[0]; i++)
{
data[0][idx] = sin(i / 20.0);
idx++;
}
idx = 0;
for (j = 0; j < yres[1]; j++)
for (i = 0; i < xres[1]; i++)
{
data[1][idx] = sin(j / 20.0);
idx++;
}
res = gwyscan_save_gwyddion_array(data[0], xres[0], yres[0], width[0], height[0], xyunit[0], zunit[0], desc[0], "gwyscan_single0.gwy");
res = gwyscan_save_gwyddion_array(data[1], xres[1], yres[1], width[1], height[1], xyunit[1], zunit[1], desc[1], "gwyscan_single1.gwy");
/* save array */
res = gwyscan_save_gwyddion_array(data[0], xres[0], yres[0], width[0], height[0], xyunit[0], zunit[0], desc[0], "gwyscan_single2.gwy");
/* add array to existing file */
res = gwyscan_add_gwyddion_array(data[1], xres[1], yres[1], width[1], height[1], xyunit[1], zunit[1], desc[1], "gwyscan_single2.gwy");
/* add data to non-existing file -> save array*/
res = gwyscan_add_gwyddion_array(data[1], xres[1], yres[1], width[1], height[1], xyunit[1], zunit[1], desc[1], "gwyscan_single3.gwy");
for (i = 0; i < 2; i++)
free(data[i]);
free(data);
return res;
}