File: volume_mfmrecalc.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 (514 lines) | stat: -rw-r--r-- 19,884 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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
 *  $Id: volume_mfmrecalc.c 21471 2018-09-26 19:47:48Z klapetek $
 *  Copyright (C) 2017-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 <string.h>
#include <gtk/gtk.h>
#include <libgwyddion/gwymacros.h>
#include <libgwyddion/gwymath.h>
#include <libprocess/brick.h>
#include <libprocess/mfm.h>
#include <libgwydgets/gwycombobox.h>
#include <libgwydgets/gwystock.h>
#include <libgwydgets/gwydgetutils.h>
#include <libgwydgets/gwyradiobuttons.h>
#include <libgwymodule/gwymodule-volume.h>
#include <app/gwyapp.h>
#include <app/gwymoduleutils.h>
#include "../process/mfmops.h" //mfmops should go to libprocess

#define VMFM_RECALC_RUN_MODES (GWY_RUN_IMMEDIATE | GWY_RUN_INTERACTIVE)

typedef enum  {
    SIGNAL_PHASE_DEG = 0,
    SIGNAL_PHASE_RAD = 1,
    SIGNAL_FREQUENCY = 2,
    SIGNAL_AMPLITUDE_V = 3,
    SIGNAL_AMPLITUDE_M = 4
} MfmRecalcSignal;

enum {
    RESPONSE_RESET   = 1,
};

typedef struct {
    MfmRecalcSignal signal;
    gdouble spring_constant;
    gdouble quality;
    gdouble base_frequency;
    gdouble base_amplitude;
    gboolean new_channel;
    GwyMFMGradientType result;
} VMfmRecalcArgs;

typedef struct {
    VMfmRecalcArgs *args;
    GSList *signal;
    GtkObject *spring_constant;
    GtkObject *quality;
    GtkWidget *quality_widget;
    GtkObject *base_frequency;
    GtkWidget *base_frequency_widget;
    GtkObject *base_amplitude;
    GtkWidget *base_amplitude_widget;
    GtkWidget *new_channel;
    GtkWidget *result;
} VMfmRecalcControls;

static gboolean module_register                (void);
static void     volume_mfmrecalc               (GwyContainer *data,
                                                GwyRunType run);
static gboolean vmfm_recalc_dialog             (VMfmRecalcArgs *args,
                                                MfmRecalcSignal guess);
static void     update_sensitivity             (VMfmRecalcControls *controls);
static void     vmfm_recalc_sanitize_args      (VMfmRecalcArgs *args);
static void     vmfm_recalc_load_args          (GwyContainer *container,
                                                VMfmRecalcArgs *args);
static void     vmfm_recalc_save_args          (GwyContainer *container,
                                                VMfmRecalcArgs *args);
static void     signal_changed                 (GtkToggleButton *toggle,
                                                VMfmRecalcControls *controls);
static void     new_channel_changed            (GtkToggleButton *check,
                                                VMfmRecalcControls *controls);
static void     vmfm_recalc_dialog_update      (VMfmRecalcControls *controls);
static void     mfm_recalc_dialog_update_values(VMfmRecalcControls *controls,
                                                VMfmRecalcArgs *args);

static const VMfmRecalcArgs vmfm_recalc_defaults = {
    SIGNAL_PHASE_DEG, 40, 1000, 150, 0.2, TRUE, GWY_MFM_GRADIENT_MFM,
};

static GwyModuleInfo module_info = {
    GWY_MODULE_ABI_VERSION,
    &module_register,
    N_("Converts the MFM data to force gradient."),
    "Petr Klapetek <klapetek@gwyddion.net>, Robb Puttock <robb.puttock@npl.co.uk>",
    "1.1",
    "David Nečas (Yeti) & Petr Klapetek",
    "2018",
};

GWY_MODULE_QUERY2(module_info, volume_mfmrecalc)

static gboolean
module_register(void)
{
    gwy_volume_func_register("volume_mfmrecalc",
                             (GwyVolumeFunc)&volume_mfmrecalc,
                             N_("/_Magnetic Data to Force Gradient..."),
                             NULL,
                             VMFM_RECALC_RUN_MODES,
                             GWY_MENU_FLAG_VOLUME,
                             N_("Recalculate to force gradient"));

    return TRUE;
}

static void
issue_warning(GtkWindow *window)
{
    GtkWidget *dialog;

    dialog = gtk_message_dialog_new(window,
                                    GTK_DIALOG_DESTROY_WITH_PARENT,
                                    GTK_MESSAGE_ERROR,
                                    GTK_BUTTONS_OK,
                                    _("Data value units must be "
                                      "deg, rad, m, Hz or V "
                                      "for the recalculation"));
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
}

static void
volume_mfmrecalc(GwyContainer *data, GwyRunType run)
{
    VMfmRecalcArgs args;
    GwyBrick *brick = NULL, *out;
    GwyDataField *preview;
    gint oldid, newid, xres, yres;
    gboolean ok;
    GwySIUnit *zunit;
    MfmRecalcSignal guess;
    GQuark bquark, pquark;

    g_return_if_fail(run & VMFM_RECALC_RUN_MODES);
    vmfm_recalc_load_args(gwy_app_settings_get(), &args);
    gwy_app_data_browser_get_current(GWY_APP_BRICK, &brick,
                                     GWY_APP_BRICK_ID, &oldid,
                                     GWY_APP_BRICK_KEY, &bquark,
                                     0);
    g_return_if_fail(GWY_IS_BRICK(brick));
    pquark = gwy_app_get_brick_preview_key_for_id(oldid);

    //guess the units
    zunit = gwy_brick_get_si_unit_w(brick);
    if (gwy_si_unit_equal_string(zunit, "deg"))
        guess = SIGNAL_PHASE_DEG;
    else if (gwy_si_unit_equal_string(zunit, "rad"))
        guess = SIGNAL_PHASE_RAD;
    else if (gwy_si_unit_equal_string(zunit, "Hz"))
        guess = SIGNAL_FREQUENCY;
    else if(gwy_si_unit_equal_string(zunit, "V"))
        guess = SIGNAL_AMPLITUDE_V;
    else if (gwy_si_unit_equal_string(zunit, "m"))
        guess = SIGNAL_AMPLITUDE_M;
    else {
        issue_warning(gwy_app_find_window_for_channel(data, oldid));
        return;
    }

    args.signal = guess;

    if (run == GWY_RUN_INTERACTIVE) {
        ok = vmfm_recalc_dialog(&args, guess);
        vmfm_recalc_save_args(gwy_app_settings_get(), &args);
        if (!ok)
            return;
    }

    if (args.new_channel)
        out = gwy_brick_duplicate(brick);
    else {
        GQuark quarks[2];

        quarks[0] = bquark;
        quarks[1] = pquark;
        gwy_app_undo_qcheckpointv(data, 2, quarks);
        out = brick;
    }

    if (args.signal == SIGNAL_PHASE_DEG) {
        gwy_brick_mfm_phase_to_force_gradient(out,
                                              args.spring_constant,
                                              args.quality,
                                              args.result);
        gwy_brick_multiply(out, M_PI/180);
    }
    else if (args.signal == SIGNAL_PHASE_RAD) {
        gwy_brick_mfm_phase_to_force_gradient(out,
                                              args.spring_constant,
                                              args.quality,
                                              args.result);
    }
    else if (args.signal == SIGNAL_FREQUENCY) {
        gwy_brick_mfm_frequency_shift_to_force_gradient(out,
                                                        args.spring_constant,
                                                        args.base_frequency,
                                                        args.result);
    }
    else if (args.signal == SIGNAL_AMPLITUDE_M) {
        gwy_brick_mfm_amplitude_shift_to_force_gradient(out,
                                                        args.spring_constant,
                                                        args.quality,
                                                        args.base_amplitude*1e-9,
                                                        args.result);
    }

    xres = gwy_brick_get_xres(out);
    yres = gwy_brick_get_yres(out);
    preview = gwy_data_field_new(xres, yres, xres, yres, FALSE);
    gwy_brick_mean_xy_plane(out, preview);

    if (args.new_channel) {
        newid = gwy_app_data_browser_add_brick(out, preview, data, TRUE);
        gwy_app_set_brick_title(data, oldid, _("Recalculated Data"));
        gwy_app_volume_log_add_volume(data, oldid, newid);
        gwy_app_sync_volume_items(data, data, oldid, newid,
                                  GWY_DATA_ITEM_GRADIENT,
                                  0);
        g_object_unref(out);
    }
    else {
        gwy_brick_data_changed(out);
        gwy_app_volume_log_add_volume(data, oldid, oldid);
        gwy_container_set_object(data, pquark, preview);
    }
    g_object_unref(preview);
}

static gboolean
vmfm_recalc_dialog(VMfmRecalcArgs *args, MfmRecalcSignal guess)
{
    GtkWidget *dialog, *table;
    VMfmRecalcControls controls;
    gint response, row;

    static const GwyEnum signals[] = {
        { N_("Phase (radians)"),   SIGNAL_PHASE_RAD, },
        { N_("Phase (degrees)"),   SIGNAL_PHASE_DEG, },
        { N_("Frequency shift"),   SIGNAL_FREQUENCY, },
        { N_("Amplitude (V)"),     SIGNAL_AMPLITUDE_V, },
        { N_("Amplitude (m)"),     SIGNAL_AMPLITUDE_M, },
    };

    static const GwyEnum results[] = {
        { N_("Force gradient"),                GWY_MFM_GRADIENT_FORCE, },
        { N_("MFM force gradient"),            GWY_MFM_GRADIENT_MFM, },
        { N_("Pixel area MFM force gradient"), GWY_MFM_GRADIENT_MFM_AREA, },
    };


    controls.args = args;

    dialog = gtk_dialog_new_with_buttons(_("MFM Recalculate Data"),
                                         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);

    table = gtk_table_new(10, 3, FALSE);
    gtk_container_set_border_width(GTK_CONTAINER(table), 4);
    gtk_table_set_col_spacings(GTK_TABLE(table), 6);
    gtk_table_set_row_spacings(GTK_TABLE(table), 2);
    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), table, TRUE, TRUE, 4);
    row = 0;

    controls.signal
        = gwy_radio_buttons_create(signals, G_N_ELEMENTS(signals),
                                   G_CALLBACK(signal_changed),
                                   &controls, args->signal);
    row = gwy_radio_buttons_attach_to_table(controls.signal,
                                            GTK_TABLE(table), 2, row);
    row++;

    gtk_table_set_row_spacing(GTK_TABLE(table), row-1, 8);
    controls.spring_constant = gtk_adjustment_new(args->spring_constant,
                                                  0.01, 1000.0, 0.01, 1, 0);
    gwy_table_attach_adjbar(table, row, _("_Spring constant:"), "N/m",
                            controls.spring_constant, GWY_HSCALE_LOG);
    row++;

    controls.quality = gtk_adjustment_new(args->quality,
                                          0.01, 10000.0, 0.01, 1, 0);
    gwy_table_attach_adjbar(table, row, _("_Quality factor:"), NULL,
                            controls.quality, GWY_HSCALE_LOG);
    row++;

    controls.base_frequency = gtk_adjustment_new(args->base_frequency,
                                                 1, 1000000.0, 1, 10, 0);
    gwy_table_attach_adjbar(table, row, _("_Base frequency:"), "Hz",
                            controls.base_frequency, GWY_HSCALE_LOG);
    row++;

    controls.base_amplitude = gtk_adjustment_new(args->base_amplitude,
                                                 0.01, 1000.0, 0.01, 1, 0);
    gwy_table_attach_adjbar(table, row, _("_Base amplitude:"), "V, nm",
                            controls.base_amplitude, GWY_HSCALE_LOG);
    row++;

    controls.result
        = gwy_enum_combo_box_new(results, G_N_ELEMENTS(results),
                                 G_CALLBACK(gwy_enum_combo_box_update_int),
                                 &args->result, args->result, TRUE);
    gwy_table_attach_adjbar(table, row, _("Result _type:"), NULL,
                            GTK_OBJECT(controls.result),
                            GWY_HSCALE_WIDGET_NO_EXPAND);

    row++;

    gtk_table_set_row_spacing(GTK_TABLE(table), row-1, 8);
    controls.new_channel
             = gtk_check_button_new_with_mnemonic(_("Create new volume data"));
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(controls.new_channel),
                                 args->new_channel);
    gtk_table_attach(GTK_TABLE(table), controls.new_channel,
                     0, 2, row, row+1, GTK_FILL, 0, 0, 0);
    g_signal_connect(controls.new_channel, "toggled",
                     G_CALLBACK(new_channel_changed), &controls);
    row++;

    update_sensitivity(&controls);

    gtk_widget_show_all(dialog);
    do {
        response = gtk_dialog_run(GTK_DIALOG(dialog));
        switch (response) {
            case GTK_RESPONSE_CANCEL:
            case GTK_RESPONSE_DELETE_EVENT:
            gtk_widget_destroy(dialog);
            case GTK_RESPONSE_NONE:
            return FALSE;
            break;

            case GTK_RESPONSE_OK:
            mfm_recalc_dialog_update_values(&controls, args);
            break;

            case RESPONSE_RESET:
            *args = vmfm_recalc_defaults;
            args->signal = guess;
            vmfm_recalc_dialog_update(&controls);
            break;

            default:
            g_assert_not_reached();
            break;
        }
    } while (response != GTK_RESPONSE_OK);

    gtk_widget_destroy(dialog);

    return TRUE;
}

static void
update_sensitivity(VMfmRecalcControls *controls)
{
    VMfmRecalcArgs *args = controls->args;
    GtkWidget *label;
    gboolean is_phase, is_freq, is_amplitude;

    is_phase = (args->signal == SIGNAL_PHASE_DEG
                || args->signal == SIGNAL_PHASE_RAD);
    is_freq = (args->signal == SIGNAL_FREQUENCY);
    is_amplitude = (args->signal == SIGNAL_AMPLITUDE_V
                    || args->signal == SIGNAL_AMPLITUDE_M);

    gwy_table_hscale_set_sensitive(controls->base_frequency, is_freq);
    gwy_table_hscale_set_sensitive(controls->quality, is_phase || is_amplitude);
    gwy_table_hscale_set_sensitive(controls->base_amplitude, is_amplitude);

    /* Reset all to insensitive and then make the enabled buttons sensitive.
     * When there are more choices for one quantity type, this may become more
     * complex. */
    gwy_radio_buttons_set_sensitive(controls->signal, FALSE);
    gtk_widget_set_sensitive(gwy_radio_buttons_find(controls->signal,
                                                    args->signal), TRUE);

    label = gwy_table_hscale_get_units(controls->base_amplitude);
    /* This is correct, the signal is in [m], but the user enters base
     * amplitude in [nm]. */
    if (args->signal == SIGNAL_AMPLITUDE_M)
        gtk_label_set_text(GTK_LABEL(label), "nm");
    else
        gtk_label_set_text(GTK_LABEL(label), "V");
}

static void
vmfm_recalc_dialog_update(VMfmRecalcControls *controls)
{
    VMfmRecalcArgs *args = controls->args;

    gtk_adjustment_set_value(GTK_ADJUSTMENT(controls->spring_constant),
                             args->spring_constant);
    gtk_adjustment_set_value(GTK_ADJUSTMENT(controls->quality),
                             args->quality);
    gtk_adjustment_set_value(GTK_ADJUSTMENT(controls->base_frequency),
                             args->base_frequency);
    gtk_adjustment_set_value(GTK_ADJUSTMENT(controls->base_amplitude),
                             args->base_amplitude);
}

static void
mfm_recalc_dialog_update_values(VMfmRecalcControls *controls,
                                VMfmRecalcArgs *args)
{
    args->spring_constant
        = gtk_adjustment_get_value(GTK_ADJUSTMENT(controls->spring_constant));
    args->quality
        = gtk_adjustment_get_value(GTK_ADJUSTMENT(controls->quality));
    args->base_frequency
        = gtk_adjustment_get_value(GTK_ADJUSTMENT(controls->base_frequency));
    args->base_amplitude
        = gtk_adjustment_get_value(GTK_ADJUSTMENT(controls->base_amplitude));
}

static void
signal_changed(GtkToggleButton *toggle, VMfmRecalcControls *controls)
{
    if (toggle && !gtk_toggle_button_get_active(toggle))
        return;

    controls->args->signal = gwy_radio_buttons_get_current(controls->signal);
    update_sensitivity(controls);
}

static void
new_channel_changed(GtkToggleButton *check,
                    VMfmRecalcControls *controls)
{
    VMfmRecalcArgs *args = controls->args;
    args->new_channel = gtk_toggle_button_get_active(check);
}

static const gchar base_amplitude_key[]  = "/module/volume_mfmrecalc/base_amplitude";
static const gchar base_frequency_key[]  = "/module/volume_mfmrecalc/base_frequency";
static const gchar new_channel_key[]     = "/module/volume_mfmrecalc/new_channel";
static const gchar quality_key[]         = "/module/volume_mfmrecalc/quality";
static const gchar signal_key[]          = "/module/volume_mfmrecalc/signal";
static const gchar spring_constant_key[] = "/module/volume_mfmrecalc/spring_constant";
static const gchar result_key[]          = "/module/volume_mfmrecalc/result";


static void
vmfm_recalc_sanitize_args(VMfmRecalcArgs *args)
{
    args->signal = CLAMP(args->signal, 0, SIGNAL_AMPLITUDE_M);
    args->result = CLAMP(args->result, 0, GWY_MFM_GRADIENT_MFM_AREA);
    args->spring_constant = CLAMP(args->spring_constant, 0.01, 1000);
    args->quality = CLAMP(args->quality, 0.01, 10000);
    args->base_frequency = CLAMP(args->base_frequency, 1, 1000000);
    args->base_amplitude = CLAMP(args->base_amplitude, 0.1, 100);
    args->new_channel = !!args->new_channel;
}

static void
vmfm_recalc_load_args(GwyContainer *container,
              VMfmRecalcArgs *args)
{
    *args = vmfm_recalc_defaults;

    gwy_container_gis_enum_by_name(container, signal_key, &args->signal);
    gwy_container_gis_enum_by_name(container, result_key, &args->result);
    gwy_container_gis_double_by_name(container, spring_constant_key,
                                     &args->spring_constant);
    gwy_container_gis_double_by_name(container, quality_key, &args->quality);
    gwy_container_gis_double_by_name(container, base_frequency_key,
                                     &args->base_frequency);
    gwy_container_gis_double_by_name(container, base_amplitude_key,
                                     &args->base_amplitude);
    gwy_container_gis_boolean_by_name(container, new_channel_key,
                                      &args->new_channel);

    vmfm_recalc_sanitize_args(args);
}

static void
vmfm_recalc_save_args(GwyContainer *container,
              VMfmRecalcArgs *args)
{
    gwy_container_set_enum_by_name(container, signal_key, args->signal);
    gwy_container_set_enum_by_name(container, result_key, args->result);
    gwy_container_set_double_by_name(container, spring_constant_key,
                                     args->spring_constant);
    gwy_container_set_double_by_name(container, quality_key, args->quality);
    gwy_container_set_double_by_name(container, base_frequency_key,
                                     args->base_frequency);
    gwy_container_set_double_by_name(container, base_amplitude_key,
                                     args->base_amplitude);
    gwy_container_set_boolean_by_name(container, new_channel_key,
                                      args->new_channel);
}

/* 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 : */