File: win_stm.c

package info (click to toggle)
gwyddion 2.62-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 51,952 kB
  • sloc: ansic: 398,486; python: 7,877; sh: 5,492; makefile: 4,723; xml: 3,883; cpp: 1,969; pascal: 418; perl: 154; ruby: 130
file content (233 lines) | stat: -rw-r--r-- 7,420 bytes parent folder | download
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
/*
 *  $Id: win_stm.c 23822 2021-06-10 06:58:57Z yeti-dn $
 *  Copyright (C) 2014 Jeffrey J. Schwartz.
 *  E-mail: schwartz@physics.ucla.edu
 *
 *  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.
 */

/*
 * This module serves to open WinSTM data files in Gywddion.
 * Currently, multiple data channels are supported with limited
 * meta data import.  No file export is supported; it is assumed
 * that no changes will be saved, or if so then another file
 * format will be used.
 *
 * TODO: Import full meta data list.
 */

/**
 * [FILE-MAGIC-USERGUIDE]
 * WinSTM data
 * .stm
 * Read
 **/

/**
 * [FILE-MAGIC-FREEDESKTOP]
 * <mime-type type="application/x-winstm-spm">
 *   <comment>WinSTM data file</comment>
 *   <magic priority="80">
 *     <match type="string" offset="0" value="WinSTM"/>
 *   </magic>
 * </mime-type>
 **/

#include <string.h>
#include <app/gwyapp.h>
#include <app/gwymoduleutils-file.h>
#include <libgwyddion/gwymacros.h>
#include <libgwyddion/gwymath.h>
#include <libgwymodule/gwymodule-file.h>
#include <libprocess/stats.h>
#include "err.h"
#include "get.h"

#define EXTENSION ".stm"

#define MAGIC "WinSTM"
#define MAGIC_SIZE (sizeof(MAGIC) - 1)

enum {
    HEADER_SIZE = 1368,
    DATA_SIZE = 768
};

typedef struct {
    gint32 NChan;
    gint32 NSpec;
    gint32 Xres;
    gint32 Yres;
    gdouble Gain;
    gdouble RangeX;
    gdouble RangeY;
    gdouble Bias;
    gdouble Current;
} WinSTM_File;

static gboolean      module_register(void);
static gint          winSTM_detect  (const GwyFileDetectInfo *fileinfo,
                                     gboolean only_name);
static GwyContainer* winSTM_load    (const gchar *filename,
                                     GwyRunType mode,
                                     GError **error);

static GwyModuleInfo module_info = {
    GWY_MODULE_ABI_VERSION,
    &module_register,
    N_("Imports WinSTM (.stm) files."),
    "Jeffrey J. Schwartz <schwartz@physics.ucla.edu>",
    "0.6",
    "Jeffrey J. Schwartz",
    "April 2014",
};

GWY_MODULE_QUERY2(module_info, win_stm)

static gboolean
module_register(void)
{
    gwy_file_func_register("win_stm",
                           N_("WinSTM files (.stm)"),
                           (GwyFileDetectFunc)&winSTM_detect,
                           (GwyFileLoadFunc)&winSTM_load,
                           NULL,
                           NULL);
    return TRUE;
}

static gint
winSTM_detect(const GwyFileDetectInfo *fileinfo,
              gboolean only_name)
{
    if (only_name)
        return g_str_has_suffix(fileinfo->name_lowercase, EXTENSION) ? 20 : 0;
    if (fileinfo->buffer_len > MAGIC_SIZE
        && memcmp(fileinfo->head, MAGIC, MAGIC_SIZE) == 0)
        return 100;
    return 0;
}

static GwyContainer*
winSTM_load(const gchar *filename,
            G_GNUC_UNUSED GwyRunType mode,
            GError **error)
{
    WinSTM_File stm;
    GwyContainer *container = gwy_container_new();
    GwyDataField *dfield;
    guchar *buffer;
    const guchar *p;
    GError *err = NULL;
    gsize size;
    gdouble *data;
    guint filemin;
    gint k, v;

    if (!gwy_file_get_contents(filename, &buffer, &size, &err)) {
        err_GET_FILE_CONTENTS(error, &err);
        g_clear_error(&err);
        return NULL;
    }
    filemin = HEADER_SIZE;
    if (size <= filemin) {
        err_TOO_SHORT(error);
        gwy_file_abandon_contents(buffer, size, NULL);
        return NULL;
    }
    if (memcmp(buffer, MAGIC, MAGIC_SIZE) != 0) {
        err_FILE_TYPE(error, "WinSTM");
        gwy_file_abandon_contents(buffer, size, NULL);
        return NULL;
    }
    p = buffer + MAGIC_SIZE;
    p += 926;
    stm.NChan = gwy_get_gint32_le(&p);
    stm.NSpec = gwy_get_gint16_le(&p);
    p += 86;
    stm.RangeX = gwy_get_gdouble_le(&p) * (1E-10);
    stm.RangeY = gwy_get_gdouble_le(&p) * (1E-10);
    p += 328;
    filemin = HEADER_SIZE + DATA_SIZE;
    if (size < filemin) {
        err_TOO_SHORT(error);
        gwy_file_abandon_contents(buffer, size, NULL);
        return NULL;
    }
    for (v = 0; v < stm.NChan; v++) {
        gchar key[40];
        gchar UnitWord[64];
        gchar Title[100];
        gchar *pch;
        gchar *s;
        GwyContainer *meta;
        p += 2;
        get_CHARARRAY0(Title, &p);
        p += 522;
        stm.Gain = gwy_get_gdouble_le(&p);
        get_CHARARRAY0(UnitWord, &p);
        pch = strstr(UnitWord, "ngstroms");
        if (pch != NULL)
            stm.Gain *= 1E-10;
        if (strcmp(UnitWord, "pAmps") == 0)
            stm.Gain *= 1E-12;
        stm.Bias = gwy_get_gdouble_le(&p);
        stm.Current = gwy_get_gdouble_le(&p) * (1E3);
        stm.Xres = gwy_get_gint32_le(&p);
        stm.Yres = gwy_get_gint32_le(&p);
        p += 48;
        dfield = gwy_data_field_new(stm.Xres, stm.Yres,
                        stm.RangeX, stm.RangeY, FALSE);
        data = gwy_data_field_get_data(dfield);
        filemin = HEADER_SIZE + DATA_SIZE + ((v+1) * 4 * stm.Xres * stm.Yres);
        if (size < filemin) {
            err_TOO_SHORT(error);
            gwy_file_abandon_contents(buffer, size, NULL);
            return NULL;
        }

        for (k = 0; k < stm.Xres * stm.Yres; k++)
            data[k] = gwy_get_gint32_le(&p) * stm.Gain;
        gwy_si_unit_set_from_string(gwy_data_field_get_si_unit_xy(dfield), "m");
        if (pch != NULL)
            gwy_si_unit_set_from_string(gwy_data_field_get_si_unit_z(dfield), "m");
        else {
            if (strcmp(UnitWord, "pAmps") == 0)
                gwy_si_unit_set_from_string(gwy_data_field_get_si_unit_z(dfield), "A");
            else
                gwy_si_unit_set_from_string(gwy_data_field_get_si_unit_z(dfield), "V");
        }
        g_snprintf(key, sizeof(key), "/%i/data", v);
        gwy_container_set_object_by_name(container, key, dfield);
        g_snprintf(key, sizeof(key), "/%i/data/title", v);
        s = g_convert(Title, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
        gwy_container_set_string_by_name(container, key, (guchar *)s);
        meta = gwy_container_new();
        gwy_container_set_string_by_name(meta, "Bias",
                    (const guchar *)g_strdup_printf("%.3f V", stm.Bias));
        gwy_container_set_string_by_name(meta, "Current Set Point",
                    (const guchar *)g_strdup_printf("%.3f pA", stm.Current));
        g_snprintf(key, sizeof(key), "/%i/meta", v);
        gwy_container_set_object_by_name(container, key, meta);
        g_object_unref(meta);
        g_object_unref(dfield);
    }
    gwy_file_channel_import_log_add(container, 0, NULL, filename);
    gwy_file_abandon_contents(buffer, size, NULL);
    return container;
}

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