Example showing how to write a simple Gwyddion GWY file with one channel a graph with a couple of curves.
#include <stdlib.h>
#include <math.h>
#include <errno.h>
#include <string.h>
#define rng() (rand()/(double)RAND_MAX - 0.5)
static double* create_image_data(int xres, int yres);
static double* create_curve_xdata(int xres, double xreal);
static double* create_curve_ydata(const double *imagedata,
int xres, int row);
int
main(void)
{
GwyfileObject *gwyf, *datafield, **curves, *graph, *selection;
double *imagedata, *xdata, *ydata;
double seldata[8];
int xres = 256, yres = 256;
imagedata = create_image_data(xres, yres);
"data", imagedata,
"si_unit_xy", "m",
"si_unit_z", "A",
NULL);
curves = (GwyfileObject**)malloc(2*sizeof(GwyfileObject*));
xdata = create_curve_xdata(xres, 1.6e-9);
ydata = create_curve_ydata(imagedata, xres, yres/4);
"xdata", xdata,
"ydata", ydata,
"type", 1,
"color.red", 0.9,
"description", "Profile 1",
NULL);
xdata = create_curve_xdata(xres, 1.6e-9);
ydata = create_curve_ydata(imagedata, xres, 3*yres/4);
"xdata", xdata,
"ydata", ydata,
"type", 2,
"color.green", 0.8,
"description", "Profile 2",
NULL);
"curves", curves,
"title", "Profiles",
"x_unit", "m",
"y_unit", "A",
NULL);
seldata[0] = 0.1e-9;
seldata[1] = 0.2e-9;
seldata[2] = 1.5e-9;
seldata[3] = 0.3e-9;
seldata[4] = 0.3e-9;
seldata[5] = 0.4e-9;
seldata[6] = 0.5e-9;
seldata[7] = 1.1e-9;
NULL);
fprintf(stderr,
"Cannot write test.gwy: %s\n", error->
message);
exit(EXIT_FAILURE);
}
return 0;
}
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] = rng() + 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;
}
static double*
create_curve_xdata(int xres, double xreal)
{
double *xdata = (double*)malloc(xres*sizeof(double));
int i;
for (i = 0; i < xres; i++)
xdata[i] = (i + 0.5)/xres*xreal;
return xdata;
}
static double*
create_curve_ydata(const double *imagedata, int xres, int row)
{
double *ydata = (double*)malloc(xres*sizeof(double));
memcpy(ydata, imagedata + xres*row, xres*sizeof(double));
return ydata;
}
bool gwyfile_write_file(GwyfileObject *object, const char *filename, GwyfileError **error)
Writes a GWY file to a named file.
Definition: gwyfile.c:331
GwyfileObject * gwyfile_object_new_datafield(int xres, int yres, double xreal, double yreal,...)
Creates a new GWY file GwyDataField object.
Definition: gwyfile.c:831
GwyfileObject * gwyfile_object_new_selectionline(int nsel,...)
Creates a new GWY file GwySelectionLine object.
Definition: gwyfile.c:1813
GwyfileObject * gwyfile_object_new_graphcurvemodel(int ndata,...)
Creates a new GWY file GwyGraphCurveModel object.
Definition: gwyfile.c:1487
GwyfileObject * gwyfile_object_new_graphmodel(int ncurves,...)
Creates a new GWY file GwyGraphModel object.
Definition: gwyfile.c:1608
void gwyfile_error_clear(GwyfileError **error)
Clears a GwyfileError.
Definition: gwyfile.c:8990
GwyfileItem * gwyfile_item_new_object(const char *name, GwyfileObject *value)
Creates a new object GWY file item.
Definition: gwyfile.c:5856
GwyfileObject * gwyfile_object_new(const char *name,...)
Creates a new GWY file object.
Definition: gwyfile.c:607
void gwyfile_object_free(GwyfileObject *object)
Frees a GWY file data object.
Definition: gwyfile.c:4577
Detailed information about an error.
Definition: gwyfile.h:131
char * message
Text message describing the error. It is allocated when the error struct is created and freed with gw...
Definition: gwyfile.h:134