File: volume_asciiexport.c

package info (click to toggle)
gwyddion 2.52-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 46,588 kB
  • sloc: ansic: 367,740; python: 7,788; sh: 5,245; makefile: 4,317; xml: 3,631; cpp: 2,550; pascal: 418; perl: 154; ruby: 130
file content (425 lines) | stat: -rw-r--r-- 14,685 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
 *  $Id: volume_asciiexport.c 20813 2018-03-08 17:04:35Z yeti-dn $
 *  Copyright (C) 2018 David Necas (Yeti).
 *  E-mail: yeti@gwyddion.net.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include <locale.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <glib/gstdio.h>
#include <gtk/gtk.h>
#include <libgwyddion/gwymacros.h>
#include <libgwyddion/gwyutils.h>
#include <libgwymodule/gwymodule-volume.h>
#include <libprocess/brick.h>
#include <libgwydgets/gwydgetutils.h>
#include <libgwydgets/gwycombobox.h>
#include <app/gwyapp.h>
#include <app/gwymoduleutils.h>

#define VOLASCEXP_RUN_MODES (GWY_RUN_IMMEDIATE | GWY_RUN_INTERACTIVE)

typedef enum {
    VOLUME_EXPORT_VTK      = 0,
    VOLUME_EXPORT_ZLINES   = 1,
    VOLUME_EXPORT_LAYERS   = 2,
    VOLUME_EXPORT_MATRICES = 3,
    VOLUME_EXPORT_NTYPES,
} VolumeExportStyle;

typedef struct {
    VolumeExportStyle style;
    gboolean decimal_dot;
    guint precision;
} VolumeASCIIExportArgs;

typedef struct {
    gboolean needs_decimal_dot;
    guint decimal_dot_len;
    gchar *decimal_dot;
} DecimalDotInfo;

typedef struct {
    VolumeASCIIExportArgs *args;
    DecimalDotInfo *decinfo;
    GwyBrick *brick;
    gchar *title;
} VolumeASCIIExportData;

static gboolean module_register        (void);
static void     volume_ascii_export    (GwyContainer *data,
                                        GwyRunType run);
static gboolean volascexp_export_dialog(VolumeASCIIExportArgs *args,
                                        const DecimalDotInfo *decinfo);
static void     decimal_dot_changed    (GtkToggleButton *toggle,
                                        VolumeASCIIExportArgs *args);
static void     precision_changed      (GtkAdjustment *adj,
                                        VolumeASCIIExportArgs *args);
static gchar*   export_brick           (gpointer user_data,
                                        gssize *data_len);
static void     destroy_brick          (gchar *data,
                                        gpointer user_data);
static void     fill_decimal_dot_info  (DecimalDotInfo *info);
static void     volascexp_load_args    (GwyContainer *settings,
                                        VolumeASCIIExportArgs *args);
static void     volascexp_save_args    (GwyContainer *settings,
                                        VolumeASCIIExportArgs *args);

static const VolumeASCIIExportArgs volascexp_defaults = {
    VOLUME_EXPORT_MATRICES, TRUE, 5,
};

static GwyModuleInfo module_info = {
    GWY_MODULE_ABI_VERSION,
    &module_register,
    N_("Exports volume data in simple ASCII formats."),
    "Yeti <yeti@gwyddion.net>",
    "1.0",
    "David Nečas (Yeti)",
    "2018",
};

GWY_MODULE_QUERY2(module_info, volume_asciiexport)

static gboolean
module_register(void)
{
    gwy_volume_func_register("volume_asciiexport",
                             (GwyVolumeFunc)&volume_ascii_export,
                             N_("/Export _Text..."),
                             NULL,
                             VOLASCEXP_RUN_MODES,
                             GWY_MENU_FLAG_VOLUME,
                             N_("Export volume data to a text file"));

    return TRUE;
}

static void
volume_ascii_export(GwyContainer *data, GwyRunType run)
{
    VolumeASCIIExportData expdata;
    VolumeASCIIExportArgs args;
    DecimalDotInfo decinfo;
    GwyBrick *brick;
    GQuark quark;
    const guchar *title;
    gint id;

    g_return_if_fail(run & VOLASCEXP_RUN_MODES);

    gwy_app_data_browser_get_current(GWY_APP_BRICK, &brick,
                                     GWY_APP_BRICK_ID, &id,
                                     0);
    g_return_if_fail(GWY_IS_BRICK(brick));

    volascexp_load_args(gwy_app_settings_get(), &args);
    fill_decimal_dot_info(&decinfo);

    if (run == GWY_RUN_INTERACTIVE) {
        if (!volascexp_export_dialog(&args, &decinfo))
            goto finish;
    }

    expdata.args = &args;
    expdata.decinfo = &decinfo;
    expdata.brick = brick;

    quark = gwy_app_get_brick_title_key_for_id(id);
    if (gwy_container_gis_string(data, quark, &title))
        expdata.title = g_strdup(title);
    else
        expdata.title = g_strdup("Volume data");

    gwy_save_auxiliary_with_callback(_("Export to Text File"), NULL,
                                     export_brick, destroy_brick, &expdata);
    g_free(expdata.title);

finish:
    g_free(decinfo.decimal_dot);
}

static gboolean
volascexp_export_dialog(VolumeASCIIExportArgs *args,
                        const DecimalDotInfo *decinfo)
{
    static const GwyEnum style_types[] = {
        { N_("VTK structured grid"),           VOLUME_EXPORT_VTK,      },
        { N_("One Z-profile per line"),        VOLUME_EXPORT_ZLINES,   },
        { N_("One XY-layer per line"),         VOLUME_EXPORT_LAYERS,   },
        { N_("Blank-line separated matrices"), VOLUME_EXPORT_MATRICES, },
    };

    GtkWidget *dialog, *vbox, *hbox, *label, *decimal_dot, *combo,
              *precision;
    gint response;

    dialog = gtk_dialog_new_with_buttons(_("Export Text"), NULL, 0,
                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                         GTK_STOCK_OK, GTK_RESPONSE_OK,
                                         NULL);
    gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
    gwy_help_add_to_volume_dialog(GTK_DIALOG(dialog), GWY_HELP_DEFAULT);

    vbox = gtk_vbox_new(FALSE, 2);
    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox, TRUE, TRUE, 0);
    gtk_container_set_border_width(GTK_CONTAINER(vbox), 4);

    gtk_box_pack_start(GTK_BOX(vbox), gwy_label_new_header(_("Options")),
                       FALSE, FALSE, 0);

    combo = gwy_enum_combo_box_new(style_types, G_N_ELEMENTS(style_types),
                                   G_CALLBACK(gwy_enum_combo_box_update_int),
                                   &args->style, args->style, TRUE);
    gtk_box_pack_start(GTK_BOX(vbox), combo, FALSE, FALSE, 0);

    decimal_dot = gtk_check_button_new_with_mnemonic(_("Use _dot "
                                                       "as decimal separator"));
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(decimal_dot),
                                 args->decimal_dot
                                 || !decinfo->needs_decimal_dot);
    gtk_widget_set_sensitive(decimal_dot, decinfo->needs_decimal_dot);
    gtk_box_pack_start(GTK_BOX(vbox), decimal_dot, FALSE, FALSE, 0);
    g_signal_connect(decimal_dot, "toggled",
                     G_CALLBACK(decimal_dot_changed), args);

    hbox = gtk_hbox_new(FALSE, 6);
    gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);

    label = gtk_label_new_with_mnemonic(_("_Precision:"));
    gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
    precision = gtk_spin_button_new_with_range(0, 16, 1);
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(precision), args->precision);
    gtk_label_set_mnemonic_widget(GTK_LABEL(label), precision);
    gtk_box_pack_start(GTK_BOX(hbox), precision, FALSE, FALSE, 0);
    g_signal_connect(gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(precision)),
                     "value-changed", G_CALLBACK(precision_changed), args);

    gtk_widget_show_all(dialog);
    response = gtk_dialog_run(GTK_DIALOG(dialog));
    if (response != GTK_RESPONSE_NONE) {
        if (decinfo->needs_decimal_dot)
            args->decimal_dot
                = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(decimal_dot));
        args->precision
            = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(precision));
        volascexp_save_args(gwy_app_settings_get(), args);
        gtk_widget_destroy(dialog);
    }

    return response == GTK_RESPONSE_OK;
}

static void
decimal_dot_changed(GtkToggleButton *toggle, VolumeASCIIExportArgs *args)
{
    args->decimal_dot = gtk_toggle_button_get_active(toggle);
}

static void
precision_changed(GtkAdjustment *adj, VolumeASCIIExportArgs *args)
{
    args->precision = gwy_adjustment_get_int(adj);
}

static inline void
append_formatted_number(GString *str,
                        gdouble v,
                        gint precision,
                        gboolean must_fix_decimal_dot,
                        const gchar *decimal_dot,
                        guint decimal_dot_len)
{
    gchar *pos;
    gchar buf[40];

    g_snprintf(buf, sizeof(buf), "%.*g", precision, v);
    if (!must_fix_decimal_dot) {
        g_string_append(str, buf);
        return;
    }

    pos = strstr(buf, decimal_dot);
    if (!pos) {
        g_string_append(str, buf);
        return;
    }

    pos[0] = '.';
    if (decimal_dot_len == 1) {
        g_string_append(str, buf);
        return;
    }

    pos[1] = '\0';
    g_string_append(str, buf);
    g_string_append(str, pos + decimal_dot_len);
}

static gchar*
export_brick(gpointer user_data, gssize *data_len)
{
    static const gchar vtk_header[] =
        "# vtk DataFile Version 2.0\n"
        "%s\n"
        "ASCII\n"
        "DATASET STRUCTURED_POINTS\n"
        "DIMENSIONS %u %u %u\n"
        "ASPECT_RATIO 1 1 1\n"
        "ORIGIN 0 0 0\n"
        "POINT_DATA %u\n"
        "SCALARS volume_scalars double 1\n"
        "LOOKUP_TABLE default\n"
        ;

    VolumeASCIIExportData *expdata = (VolumeASCIIExportData*)user_data;
    GwyBrick *brick = expdata->brick;
    guint xres, yres, zres, i, j, k, n;
    const gchar *decimal_dot;
    guint decimal_dot_len;
    GString *str;
    const gdouble *d, *dd;
    gboolean must_fix_decimal_dot;
    gint precision;

    xres = gwy_brick_get_xres(brick);
    yres = gwy_brick_get_yres(brick);
    zres = gwy_brick_get_zres(brick);
    d = gwy_brick_get_data_const(brick);
    must_fix_decimal_dot = (expdata->decinfo->needs_decimal_dot
                            && expdata->args->decimal_dot);
    decimal_dot = expdata->decinfo->decimal_dot;
    decimal_dot_len = expdata->decinfo->decimal_dot_len;
    precision = expdata->args->precision;

    str = g_string_new(NULL);
    if (expdata->args->style == VOLUME_EXPORT_VTK) {
        n = xres*yres*zres;
        g_string_append_printf(str, vtk_header,
                               expdata->title, xres, yres, zres, n);
        for (i = 0; i < n; i++) {
            append_formatted_number(str, d[i], precision, must_fix_decimal_dot,
                                    decimal_dot, decimal_dot_len);
            g_string_append_c(str, '\n');
        }
    }
    else if (expdata->args->style == VOLUME_EXPORT_ZLINES) {
        n = xres*yres;
        for (i = 0; i < n; i++) {
            dd = d + i;
            for (j = 0; j < zres; j++) {
                append_formatted_number(str, *dd, precision,
                                        must_fix_decimal_dot,
                                        decimal_dot, decimal_dot_len);
                g_string_append_c(str, j == zres-1 ? '\n' : '\t');
                dd += n;
            }
        }
    }
    else if (expdata->args->style == VOLUME_EXPORT_LAYERS) {
        n = xres*yres;
        for (i = 0; i < zres; i++) {
            dd = d + i*n;
            for (j = 0; j < n; j++) {
                append_formatted_number(str, dd[j], precision,
                                        must_fix_decimal_dot,
                                        decimal_dot, decimal_dot_len);
                g_string_append_c(str, j == n-1 ? '\n' : '\t');
            }
        }
    }
    else if (expdata->args->style == VOLUME_EXPORT_MATRICES) {
        n = xres*yres;
        for (i = 0; i < zres; i++) {
            for (j = 0; j < yres; j++) {
                dd = d + i*n + j*xres;
                for (k = 0; k < xres; k++) {
                    append_formatted_number(str, dd[k], precision,
                                            must_fix_decimal_dot,
                                            decimal_dot, decimal_dot_len);
                    g_string_append_c(str, k == xres-1 ? '\n' : '\t');
                }
            }
            g_string_append_c(str, '\n');
        }
    }
    else {
        g_assert_not_reached();
    }

    *data_len = str->len;
    return g_string_free(str, FALSE);
}

static void
destroy_brick(gchar *data, G_GNUC_UNUSED gpointer user_data)
{
    g_free(data);
}

static void
fill_decimal_dot_info(DecimalDotInfo *info)
{
    struct lconv *locale_data;
    guint len;

    locale_data = localeconv();
    info->decimal_dot = g_strdup(locale_data->decimal_point);
    if (!info->decimal_dot || !(len = strlen(info->decimal_dot))) {
        g_warning("Cannot get decimal dot information from localeconv().");
        g_free(info->decimal_dot);
        info->decimal_dot = g_strdup(".");
        len = 1;
    }
    info->decimal_dot_len = len;
    info->needs_decimal_dot = !gwy_strequal(info->decimal_dot, ".");
}

static const gchar decimal_dot_key[] = "/module/volume_asciiexport/decimal-dot";
static const gchar precision_key[]   = "/module/volume_asciiexport/precision";
static const gchar style_key[]       = "/module/volume_asciiexport/style";

static void
volascexp_load_args(GwyContainer *settings,
                    VolumeASCIIExportArgs *args)
{
    *args = volascexp_defaults;

    gwy_container_gis_boolean_by_name(settings, decimal_dot_key,
                                      &args->decimal_dot);
    gwy_container_gis_int32_by_name(settings, precision_key, &args->precision);
    gwy_container_gis_enum_by_name(settings, style_key, &args->style);

    args->precision = MIN(args->precision, 16);
}

static void
volascexp_save_args(GwyContainer *settings,
                    VolumeASCIIExportArgs *args)
{
    gwy_container_set_boolean_by_name(settings, decimal_dot_key,
                                      args->decimal_dot);
    gwy_container_set_int32_by_name(settings, precision_key, args->precision);
    gwy_container_set_enum_by_name(settings, style_key, args->style);
}

/* vim: set cin et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */