File: nanoscope-ii.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 (308 lines) | stat: -rw-r--r-- 8,800 bytes parent folder | download | duplicates (2)
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
/*
 *  $Id: nanoscope-ii.c 22642 2019-11-03 11:46:07Z yeti-dn $
 *  Copyright (C) 2004 David Necas (Yeti), Petr Klapetek.
 *  E-mail: yeti@gwyddion.net, klapetek@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.
 */

/**
 * [FILE-MAGIC-FREEDESKTOP]
 * <mime-type type="application/x-nanoscope-ii-spm">
 *   <comment>Nanoscope II SPM data</comment>
 *   <magic priority="80">
 *     <match type="string" offset="0" value="Data_File_Type 7\r\n"/>
 *   </magic>
 * </mime-type>
 **/

/**
 * [FILE-MAGIC-FILEMAGIC]
 * # Old Nanoscope II
 * 0 string Data_File_Type\ 7\x0d\x0a Nanoscope II SPM data
 **/

/**
 * [FILE-MAGIC-USERGUIDE]
 * Veeco Nanoscope II
 * .001, .002, etc.
 * Read
 **/

#include "config.h"
#include <string.h>
#include <stdlib.h>
#include <libgwyddion/gwymacros.h>
#include <libgwyddion/gwyutils.h>
#include <libgwyddion/gwymath.h>
#include <libprocess/stats.h>
#include <libgwymodule/gwymodule-file.h>
#include <app/gwymoduleutils-file.h>

#include "err.h"

#define MAGIC "Data_File_Type 7\r\n"
#define MAGIC_SIZE (sizeof(MAGIC)-1)

#define Nanometer (1e-9)

enum { HEADER_SIZE = 2048 };

typedef struct {
    GHashTable *hash;
    GwyDataField *data_field;
} NanoscopeData;

static gboolean        module_register       (void);
static gint            nanoscope_detect      (const GwyFileDetectInfo *fileinfo,
                                              gboolean only_name);
static GwyContainer*   nanoscope_load        (const gchar *filename,
                                              GwyRunType mode,
                                              GError **error);
static GwyDataField*   hash_to_data_field    (GHashTable *hash,
                                              const guchar *buffer,
                                              gsize size,
                                              GError **error);
static GHashTable*     read_hash             (gchar *buffer,
                                              GError **error);
static GwyContainer*   nanoscope_get_metadata(GHashTable *hash);

static GwyModuleInfo module_info = {
    GWY_MODULE_ABI_VERSION,
    &module_register,
    N_("Imports Digital Instruments Nanoscope II data files."),
    "Yeti <yeti@gwyddion.net>",
    "0.3",
    "David Nečas (Yeti) & Petr Klapetek",
    "2007",
};

GWY_MODULE_QUERY2(module_info, nanoscope_ii)

static gboolean
module_register(void)
{
    gwy_file_func_register("nanoscope-ii",
                           N_("Nanoscope II files"),
                           (GwyFileDetectFunc)&nanoscope_detect,
                           (GwyFileLoadFunc)&nanoscope_load,
                           NULL,
                           NULL);

    return TRUE;
}

static gint
nanoscope_detect(const GwyFileDetectInfo *fileinfo,
                 gboolean only_name)
{
    gint score = 0;

    if (only_name)
        return 0;

    if (fileinfo->buffer_len > MAGIC_SIZE
        && (memcmp(fileinfo->head, MAGIC, MAGIC_SIZE) == 0)
        && fileinfo->file_size > HEADER_SIZE)
        score = 100;

    return score;
}

static GwyContainer*
nanoscope_load(const gchar *filename,
               G_GNUC_UNUSED GwyRunType mode,
               GError **error)
{
    GwyContainer *meta, *container = NULL;
    GwyDataField *dfield;
    GError *err = NULL;
    guchar *buffer = NULL;
    gchar *header;
    gsize size = 0;
    GHashTable *hash;

    if (!gwy_file_get_contents(filename, &buffer, &size, &err)) {
        err_GET_FILE_CONTENTS(error, &err);
        return NULL;
    }

    if (size <= HEADER_SIZE || memcmp(buffer, MAGIC, MAGIC_SIZE) != 0) {
        err_FILE_TYPE(error, "Nanoscope II");
        gwy_file_abandon_contents(buffer, size, NULL);
        return NULL;
    }

    header = g_memdup(buffer, HEADER_SIZE + 1);
    header[HEADER_SIZE] = '\0';
    hash = read_hash(header + MAGIC_SIZE, error);
    /* Must keep header allocated, hash items are direct pointers to it */
    if (!hash) {
        g_free(header);
        gwy_file_abandon_contents(buffer, size, NULL);
        return NULL;
    }

    dfield = hash_to_data_field(hash, buffer + HEADER_SIZE, size - HEADER_SIZE,
                                &err);
    meta = nanoscope_get_metadata(hash);
    gwy_file_abandon_contents(buffer, size, NULL);
    g_hash_table_destroy(hash);
    g_free(header);
    if (!dfield)
        return NULL;

    container = gwy_container_new();
    gwy_container_set_object_by_name(container, "/0/data", dfield);
    g_object_unref(dfield);

    if (meta) {
        gwy_container_set_object_by_name(container, "/0/meta", meta);
        g_object_unref(meta);
    }

    gwy_app_channel_title_fall_back(container, 0);
    gwy_file_channel_import_log_add(container, 0, NULL, filename);

    return container;
}

static GwyDataField*
hash_to_data_field(GHashTable *hash,
                   const guchar *buffer,
                   gsize size,
                   GError **error)
{
    GwyDataField *dfield;
    GwySIUnit *unitz, *unitxy;
    gchar *val;
    gint xres, yres, i, j;
    gdouble xreal, yreal, q;
    gdouble *data;
    const gint16 *d16;

    if (!require_keys(hash, error, "num_samp", "scan_sz", "z_scale", NULL))
        return NULL;

    val = g_hash_table_lookup(hash, "num_samp");
    xres = yres = atoi(val);
    if (err_DIMENSION(error, xres))
        return NULL;

    if (err_SIZE_MISMATCH(error, xres*yres*sizeof(gint16), size, FALSE))
        return NULL;

    val = g_hash_table_lookup(hash, "scan_sz");
    xreal = yreal = Nanometer*g_ascii_strtod(val, NULL);
    if (xreal <= 0) {
        err_INVALID(error, "scan_sz");
        return NULL;
    }

    val = g_hash_table_lookup(hash, "z_scale");
    q = Nanometer/16384.0*g_ascii_strtod(val, NULL);
    if (q <= 0.0) {
        err_INVALID(error, "scan_sz");
        return NULL;
    }
    dfield = gwy_data_field_new(xres, yres, xreal, yreal, FALSE);
    data = gwy_data_field_get_data(dfield);
    d16 = (const gint16*)buffer;

    for (i = 0; i < yres; i++) {
        for (j = 0; j < xres; j++)
            data[(yres-1 - i)*xres + j] = q*GINT16_FROM_LE(d16[i*xres + j]);
    }

    unitz = gwy_si_unit_new("m");
    gwy_data_field_set_si_unit_z(dfield, unitz);
    g_object_unref(unitz);

    unitxy = gwy_si_unit_new("m");
    gwy_data_field_set_si_unit_xy(dfield, unitxy);
    g_object_unref(unitxy);

    return dfield;
}

static GHashTable*
read_hash(gchar *buffer,
          GError **error)
{
    GHashTable *hash;
    gchar *line, *value;

    hash = g_hash_table_new(g_str_hash, g_str_equal);
    while ((line = gwy_str_next_line(&buffer)) && line[0] != '\x1a') {
        g_strstrip(line);
        if (!line[0])
            continue;

        gwy_debug("<%s>", line);
        value = strchr(line, '=');
        if (!value) {
            g_set_error(error, GWY_MODULE_FILE_ERROR,
                        GWY_MODULE_FILE_ERROR_DATA,
                        _("Malformed header line (missing =)."));
            g_hash_table_destroy(hash);

            return NULL;
        }
        *value = '\0';
        g_strstrip(line);

        value++;
        g_strstrip(value);
        if (*value)
            g_hash_table_insert(hash, line, value);
    }

    return hash;
}

static GwyContainer*
nanoscope_get_metadata(GHashTable *hash)
{
    static const struct {
        gchar *key;
        gchar *name;
        gchar *unit;
    }
    metadata[] = {
        { "bias_volt[0]", "Bias", " mV" },
        { "scan_rate", "Scan rate", " Hz" },
        { "time", "Date", NULL },
    };

    GwyContainer *meta;
    const gchar *val;
    guint i;

    meta = gwy_container_new();
    for (i = 0; i < G_N_ELEMENTS(metadata); i++) {
        if ((val = g_hash_table_lookup(hash, metadata[i].key)))
            gwy_container_set_string_by_name(meta, metadata[i].name,
                                             g_strconcat(val, metadata[i].unit,
                                                         NULL));
    }
    if (gwy_container_get_n_items(meta))
        return meta;

    g_object_unref(meta);
    return NULL;
}

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