File: sicmfile.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 (707 lines) | stat: -rw-r--r-- 29,578 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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/*
 *  $Id: sicmfile.c 19948 2017-06-22 14:05:41Z yeti-dn $
 *  IonScope SICM file format importer
 *  Copyright (C) 2008-2010 Matthew Caldwell.
 *  E-mail: m.caldwell@ucl.ac.uk
 *
 *  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-sicm-spm">
 *   <comment>IonScope SICM data</comment>
 *   <magic priority="50">
 *     <match type="string" offset="0" value="\x32\x00"/>
 *   </magic>
 * </mime-type>
 **/

/**
 * [FILE-MAGIC-USERGUIDE]
 * IonScope SICM
 * .img
 * Read
 **/

/*--------------------------------------------------------------------------
  Dependencies
----------------------------------------------------------------------------*/

#include "config.h"
#include <libgwyddion/gwymath.h>
#include <libprocess/stats.h>
#include <app/gwymoduleutils-file.h>

#include "err.h"

/*--------------------------------------------------------------------------
  Constants
----------------------------------------------------------------------------*/

#define EXTENSION ".img"

enum
{
    SICM_VERSION = 50,
    HEADER_SIZE = 830
};

/*--------------------------------------------------------------------------
  Types
----------------------------------------------------------------------------*/

/* Structure to hold the file header data */
typedef struct _SICMImage
{
                                        /* offset in file */

    gint16  version; /* 50 */           /* 0 */
    gint16  xdim;                       /* 2 */
    gint16  ydim;                       /* 4 */

    /* STM Param Record */
    gdouble fsdHVA;                     /* 6 */
    gdouble fsdDAC;                     /* 12 */
    gdouble fsdADC;                     /* 18 */
    gdouble haGain;                     /* 24 */
    gdouble piezoCalX;                  /* 30 */
    gdouble piezoCalY;                  /* 36 */
    gdouble piezoCalZ;                  /* 42 */
    gdouble gainZ;                      /* 48 */
    gint16  maxADC;                     /* 54 */

    /* Scan Para Record */
    gdouble scanSize; /* 10^-8 m */     /* 56 */
    gint16  ctrlOS;                     /* 62 */
    gint16  imagOS;                     /* 64 */
    gint16  ctrlPts;                    /* 66 */
    gint16  xDimension; /* == xdim */   /* 68 */
    gint16  yDimension; /* == ydim */   /* 70 */

    /* Loop Record */
    gdouble loopGain;                   /* 72 */
    gdouble setPoint;   /* pA */        /* 78 */
    gdouble tipVoltage; /* mV (?) */    /* 84 */
    gdouble tipXPos;    /* 10^-8 m */   /* 90 */
    gdouble tipYPos;    /* 10^-8 m */   /* 96 */

    /* Plane Param Record */
    gdouble A;                          /* 102 */
    gdouble B;                          /* 108 */
    gdouble D;                          /* 114 */
    gint16  fitX;                       /* 120 */
    gint16  fitY;                       /* 122 */
    gint16  min;                        /* 124 */
    gint16  max;                        /* 126 */
    gdouble scale;                      /* 128 */

    /* Scan Setup Record */
    gdouble     scanAngle;              /* 134 */
    gdouble     xSlope;                 /* 140 */
    gdouble     ySlope;                 /* 146 */
    gboolean    fitting;                /* 152 */
    gboolean    polarity;               /* 153 */
    gboolean    scan1D;                 /* 154 */
    gboolean    startCenter;            /* 155 */

    /* Time Record */
    guchar  date[79];                   /* 156 */
    guchar  time[79];                   /* 235 */

    gint16  scanMode;                   /* 314 */
    guint16 version2; /* == version */  /* 316 */
    gdouble range;                      /* 318 */
    guchar  space2[7];                  /* 324 */
    guchar  comment[81];                /* 331 */
    guchar  title[81];                  /* 412 */

    /* CITS Record */
    gint16  NCITS;                      /* 493 */
    gint16  settle;                     /* 495 */
    gdouble vArray[8];                  /* 497 */
    gdouble offArray[8];                /* 545 */

    /* Break Record */
    gint16  noPts;                      /* 593 */
    gint16  settle2;                    /* 595 */
    gdouble vStart;                     /* 597 */
    gdouble vEnd;                       /* 603 */
    gdouble threshold;                  /* 609 */

    gint16  loopMode;                   /* 615 */

    /* Hopping Mode Record */
    guint16 hopAmp;                     /* 617 */
    guint16 riseRate;                   /* 619 */
    guint16 riseToFallTime;             /* 621 */
    guint16 fallRate;                   /* 623 */
    gint16  dcSetPoint;                 /* 625 */
    guchar  prescanSqrSize;             /* 627 */
    guint16 prescanHopAmp;              /* 628 */
    guint16 minHopAmp;                  /* 630 */
    guchar  absHopMode;                 /* 632 */
    guchar  fastPrescanMode;            /* 633 */
    guchar  resLevels[8];               /* 634 */
    guint16 resThresholds[8];           /* 642 */
    guchar  numResLevels;               /* 658 */

    guchar  space[7];                   /* 659 */

    /* Info Strings */
    guchar  modeStr[41];                /* 666 */
    guchar  loopStr[41];                /* 707 */
    guchar  sizeStr[41];                /* 748 */
    guchar  posStr[41];                 /* 789 */

    /* Start of heightfield data */     /* 830 */
    /* xdim x ydim array of gint16 */
}
SICMImage;

/*--------------------------------------------------------------------------
  Prototypes
----------------------------------------------------------------------------*/

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

/*--------------------------------------------------------------------------
  Implementation
----------------------------------------------------------------------------*/

/* module details */
static GwyModuleInfo module_info =
{
    GWY_MODULE_ABI_VERSION,
    &module_register,
    N_("Imports IonScope SICM data files."),
    "Matthew Caldwell <m.caldwell@ucl.ac.uk>",
    "1.3",
    "Matthew Caldwell",
    "2007-2010",
};
GWY_MODULE_QUERY2(module_info, sicmfile)

/*--------------------------------------------------------------------------*/

/* announce ourselves to gwyddion */
static gboolean module_register ( void )
{
    gwy_file_func_register("sicm",
                           N_("IonScope SICM files (.img)"),
                           (GwyFileDetectFunc) &sicm_detect,
                           (GwyFileLoadFunc) &sicm_load,
                           NULL,
                           NULL );

    return TRUE;
}

/*--------------------------------------------------------------------------*/

/* check a candidate file for likely SICMness */
static gint sicm_detect ( const GwyFileDetectInfo *fileinfo,
                          gboolean only_name )
{
    /* name isn't much of a test since every second SPM format -- and various
       non-SPM ones too -- use a .IMG extension, but we have nothing else to go on */
    if (only_name)
    {
        return g_str_has_suffix(fileinfo->name_lowercase, EXTENSION) ? 20 : 0;
    }
    else
    {
        /* otherwise, we can perform a slightly more discriminating test: */
        const guchar* p = fileinfo->head;

        if ( fileinfo->buffer_len > 6                   /* big enough to test */
             && gwy_get_gint16_le(&p) == SICM_VERSION   /* version has to serve as magic number */
             && (fileinfo->file_size == HEADER_SIZE     /* file size is consistent */
                                        + 2
                                        * gwy_get_gint16_le(&p)
                                        * gwy_get_gint16_le(&p) ) )
            return 100;
    }

    return 0;
}

/*--------------------------------------------------------------------------*/

/* load file data. */
static GwyContainer* sicm_load ( const gchar *filename,
                                 G_GNUC_UNUSED GwyRunType mode,
                                 GError **error )
{
    SICMImage sicm;
    GwyContainer *container = NULL, *meta = NULL;
    GwySIUnit *unit;
    GwyDataField *dfield;
    guchar *buffer;
    const guchar *p, *unit_name;
    GError *err = NULL;
    gsize size, expected_size;
    const gint16 *rawdata;
    gdouble *data;
    gdouble scaling;
    gint i,j;

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

    if (size <= HEADER_SIZE)
    {
        err_TOO_SHORT(error);
        gwy_file_abandon_contents(buffer, size, NULL);
        return NULL;
    }

    p = buffer;

    sicm.version = gwy_get_gint16_le(&p);

    /* version and size tests as in sicm_detect above */
    if ( sicm.version != SICM_VERSION )
    {
        err_FILE_TYPE(error, "SICM");
        gwy_file_abandon_contents(buffer, size, NULL);
        return NULL;
    }

    sicm.xdim = gwy_get_gint16_le(&p);
    sicm.ydim = gwy_get_gint16_le(&p);

    expected_size = 2 * sicm.xdim * sicm.ydim + HEADER_SIZE;
    if (size != expected_size)
    {
        err_SIZE_MISMATCH(error, expected_size, size, TRUE);
        gwy_file_abandon_contents(buffer, size, NULL);
        return NULL;
    }

    /* load in the header data */
    sicm.fsdHVA = gwy_get_pascal_real_le(&p);
    sicm.fsdDAC = gwy_get_pascal_real_le(&p);
    sicm.fsdADC = gwy_get_pascal_real_le(&p);
    sicm.haGain = gwy_get_pascal_real_le(&p);
    sicm.piezoCalX = gwy_get_pascal_real_le(&p);
    sicm.piezoCalY = gwy_get_pascal_real_le(&p);
    sicm.piezoCalZ = gwy_get_pascal_real_le(&p);
    sicm.gainZ = gwy_get_pascal_real_le(&p);
    sicm.maxADC = gwy_get_gint16_le(&p);

    sicm.scanSize = gwy_get_pascal_real_le(&p);
    sicm.ctrlOS = gwy_get_gint16_le(&p);
    sicm.imagOS = gwy_get_gint16_le(&p);
    sicm.ctrlPts = gwy_get_gint16_le(&p);
    sicm.xDimension = gwy_get_gint16_le(&p);
    sicm.yDimension = gwy_get_gint16_le(&p);

    sicm.loopGain = gwy_get_pascal_real_le(&p);
    sicm.setPoint = gwy_get_pascal_real_le(&p);
    sicm.tipVoltage = gwy_get_pascal_real_le(&p);
    sicm.tipXPos = gwy_get_pascal_real_le(&p);
    sicm.tipYPos = gwy_get_pascal_real_le(&p);

    sicm.A = gwy_get_pascal_real_le(&p);
    sicm.B = gwy_get_pascal_real_le(&p);
    sicm.D = gwy_get_pascal_real_le(&p);
    sicm.fitX = gwy_get_gint16_le(&p);
    sicm.fitY = gwy_get_gint16_le(&p);
    sicm.min = gwy_get_gint16_le(&p);
    sicm.max = gwy_get_gint16_le(&p);
    sicm.scale = gwy_get_pascal_real_le(&p);

    sicm.scanAngle = gwy_get_pascal_real_le(&p);
    sicm.xSlope = gwy_get_pascal_real_le(&p);
    sicm.ySlope = gwy_get_pascal_real_le(&p);

    sicm.fitting = gwy_get_gboolean8(&p);
    sicm.polarity = gwy_get_gboolean8(&p);
    sicm.scan1D = gwy_get_gboolean8(&p);
    sicm.startCenter = gwy_get_gboolean8(&p);

    memcpy(sicm.date, p+1, 78);
    sicm.date[78] = 0;
    p += 79;

    memcpy(sicm.time, p+1, 78);
    sicm.time[78] = 0;
    p += 79;

    sicm.scanMode = gwy_get_gint16_le(&p);
    sicm.version2 = gwy_get_guint16_le(&p);
    sicm.range = gwy_get_pascal_real_le(&p);

    memcpy(sicm.space2, p+1, 6);
    sicm.space2[6] = 0;
    p += 7;
    memcpy(sicm.comment, p+1, 80);
    sicm.comment[80] = 0;
    p += 81;
    memcpy(sicm.title, p+1, 80);
    sicm.title[80] = 0;
    p += 81;

    sicm.NCITS = gwy_get_gint16_le(&p);
    sicm.settle = gwy_get_gint16_le(&p);
    for ( i = 0; i < 8; ++i )
        sicm.vArray[i] = gwy_get_pascal_real_le(&p);
    for ( i = 0; i < 8; ++i )
        sicm.offArray[i] = gwy_get_pascal_real_le(&p);

    sicm.noPts = gwy_get_gint16_le(&p);
    sicm.settle2 = gwy_get_gint16_le(&p);
    sicm.vStart = gwy_get_pascal_real_le(&p);
    sicm.vEnd = gwy_get_pascal_real_le(&p);
    sicm.threshold = gwy_get_pascal_real_le(&p);

    sicm.loopMode = gwy_get_gint16_le(&p);

    sicm.hopAmp = gwy_get_guint16_le(&p);
    sicm.riseRate = gwy_get_guint16_le(&p);
    sicm.riseToFallTime = gwy_get_guint16_le(&p);
    sicm.fallRate = gwy_get_guint16_le(&p);
    sicm.dcSetPoint = gwy_get_gint16_le(&p);
    sicm.prescanSqrSize = *p++;
    sicm.prescanHopAmp = gwy_get_guint16_le(&p);
    sicm.minHopAmp = gwy_get_guint16_le(&p);
    sicm.absHopMode = *p++;
    sicm.fastPrescanMode = *p++;
    for ( i = 0; i < 8; ++i )
        sicm.resLevels[i] = *p++;
    for ( i = 0; i < 8; ++i )
        sicm.resThresholds[i] = gwy_get_guint16_le(&p);
    sicm.numResLevels = *p++;

    memcpy(sicm.space, p+1, 6);
    sicm.space[6] = 0;
    p += 7;

    memcpy(sicm.modeStr, p+1, 40);
    sicm.modeStr[40] = 0;
    p += 41;
    memcpy(sicm.loopStr, p+1, 40);
    sicm.loopStr[40] = 0;
    p += 41;
    memcpy(sicm.sizeStr, p+1, 40);
    sicm.sizeStr[40] = 0;
    p += 41;
    memcpy(sicm.posStr, p+1, 40);
    sicm.posStr[40] = 0;
    p += 41;

    dfield = gwy_data_field_new( sicm.xdim,
                                 sicm.ydim,
                                 sicm.scanSize * 1e-8,
                                 sicm.scanSize * 1e-8,
                                 FALSE );
    data = gwy_data_field_get_data ( dfield );
    rawdata = (const gint16*) p;

    /* scale factor depends on the channel data type, which we get from the mode string */
    switch ( sicm.modeStr[0] )
    {
        case 'C':
            /* current: we don't have any sensible way of scaling
               this, because the current is measured by an external patch
               amplifier and delivered as a voltage using a mV/pA conversion rate
               that is not recorded in the file. all we can realistically do is
               present that voltage, so fall through to case below */

        case 'A':
            /* ADC channels: scale to voltage range */
            scaling = sicm.fsdADC / 32767;
            unit_name = "V";
            break;

        default:
            /* topography: scale to piezo range, which we don't explicitly
               know but can determine from sensitivity and voltage range
               with a 1e-6 factor to convert from microns to metres */
            scaling = sicm.piezoCalZ * sicm.fsdHVA * sicm.fsdDAC * 1e-6 / 32767.0;
            unit_name = "m";
    }


    /* image data is stored bottom-up, so reverse order of rows */
    for (i = 0; i < sicm.ydim; ++i)
    {
        int r1 = i * sicm.xdim;
        int r2 = (sicm.ydim - i - 1) * sicm.xdim;

        for ( j = 0; j < sicm.xdim; ++j )
        {
            data[r1 + j] = scaling * GINT16_FROM_LE(rawdata[r2 + j]);
        }
    }

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

    unit = gwy_si_unit_new(unit_name);
    gwy_data_field_set_si_unit_z(dfield, unit);
    g_object_unref(unit);

    /* build the data container */
    container = gwy_container_new();
    gwy_container_set_object_by_name(container, "/0/data", dfield);
    gwy_container_set_string_by_name(container, "/0/data/title",
                                     g_convert(sicm.modeStr, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL));
    g_object_unref(dfield);

    /* add pretty much all metadata from the header
       a bunch of this doesn't seem to be used meaningfully; we prefix
       probably useless or placeholder fields with a tilde so they sort
       to the bottom of the browser -- these should probably be omitted
       entirely when we're sure they're not needed */
    meta = gwy_container_new();

    gwy_container_set_string_by_name(meta,
                                     "Title",
                                     g_convert(sicm.title, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL));
    gwy_container_set_string_by_name(meta,
                                     "Comment",
                                     g_convert(sicm.comment, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL));
    gwy_container_set_string_by_name(meta,
                                     "Time Begun",
                                     g_convert(sicm.date, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL));
    gwy_container_set_string_by_name(meta,
                                     "Time Completed",
                                     g_convert(sicm.time, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL));
    gwy_container_set_string_by_name(meta,
                                     "Channel Mode",
                                     g_convert(sicm.modeStr, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL));
    gwy_container_set_string_by_name(meta,
                                     "Loop Info",
                                     g_convert(sicm.loopStr, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL));
    gwy_container_set_string_by_name(meta,
                                     "Scan Size",
                                     g_convert(sicm.sizeStr, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL));
    gwy_container_set_string_by_name(meta,
                                     "Probe Position",
                                     g_convert(sicm.posStr, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL));

    gwy_container_set_string_by_name(meta,
                                     "Head Voltage Amplifier FSD",
                                     g_strdup_printf("%g V", sicm.fsdHVA));
    gwy_container_set_string_by_name(meta,
                                     "DA Converter FSD",
                                     g_strdup_printf("%g V", sicm.fsdDAC));
    gwy_container_set_string_by_name(meta,
                                     "AD Converter FSD",
                                     g_strdup_printf("%g V", sicm.fsdADC));
    gwy_container_set_string_by_name(meta,
                                     "Head Amplifier Gain",
                                     g_strdup_printf("%g", sicm.haGain));
    gwy_container_set_string_by_name(meta,
                                     "Piezo X Sensitivity",
                                     g_strdup_printf("%g", sicm.piezoCalX));
    gwy_container_set_string_by_name(meta,
                                     "Piezo Y Sensitivity",
                                     g_strdup_printf("%g", sicm.piezoCalY));
    gwy_container_set_string_by_name(meta,
                                     "Piezo Z Sensitivity",
                                     g_strdup_printf("%g", sicm.piezoCalZ));
    gwy_container_set_string_by_name(meta,
                                     "Z Gain",
                                     g_strdup_printf("%g", sicm.gainZ));
    gwy_container_set_string_by_name(meta,
                                     "Size",
                                     g_strdup_printf("%g x10^-8 m", sicm.scanSize));
    gwy_container_set_string_by_name(meta,
                                     "Loop Gain",
                                     g_strdup_printf("%g", sicm.loopGain));
    gwy_container_set_string_by_name(meta,
                                     "Set Point",
                                     g_strdup_printf("%g", sicm.setPoint));
    gwy_container_set_string_by_name(meta,
                                     "~ Tip Voltage",
                                     g_strdup_printf("%g mV", sicm.tipVoltage));
    gwy_container_set_string_by_name(meta,
                                     "Tip Position X",
                                     g_strdup_printf("%g x10^-8 m", sicm.tipXPos));
    gwy_container_set_string_by_name(meta,
                                     "Tip Position Y",
                                     g_strdup_printf("%g x10^-8 m", sicm.tipYPos));
    gwy_container_set_string_by_name(meta,
                                     "~ Plane Param A",
                                     g_strdup_printf("%g", sicm.A));
    gwy_container_set_string_by_name(meta,
                                     "~ Plane Param B",
                                     g_strdup_printf("%g", sicm.B));
    gwy_container_set_string_by_name(meta,
                                     "~ Plane Param D",
                                     g_strdup_printf("%g", sicm.D));
    gwy_container_set_string_by_name(meta,
                                     "~ Plane Param Scale",
                                     g_strdup_printf("%g", sicm.scale));
    gwy_container_set_string_by_name(meta,
                                     "Scan Angle",
                                     g_strdup_printf("%g", sicm.scanAngle));
    gwy_container_set_string_by_name(meta,
                                     "~ Slope X",
                                     g_strdup_printf("%g", sicm.xSlope));
    gwy_container_set_string_by_name(meta,
                                     "~ Slope Y",
                                     g_strdup_printf("%g", sicm.ySlope));
    gwy_container_set_string_by_name(meta,
                                     "~ Range",
                                     g_strdup_printf("%g", sicm.range));
    gwy_container_set_string_by_name(meta,
                                     "~ vArray",
                                     g_strdup_printf("[%g, %g, %g, %g, %g, %g, %g, %g]",
                                     sicm.vArray[0], sicm.vArray[1], sicm.vArray[2], sicm.vArray[3],
                                     sicm.vArray[4], sicm.vArray[4], sicm.vArray[6], sicm.vArray[7]));
    gwy_container_set_string_by_name(meta,
                                     "~ offArray",
                                     g_strdup_printf("[%g, %g, %g, %g, %g, %g, %g, %g]",
                                     sicm.offArray[0], sicm.offArray[1], sicm.offArray[2], sicm.offArray[3],
                                     sicm.offArray[4], sicm.offArray[4], sicm.offArray[6], sicm.offArray[7]));
    gwy_container_set_string_by_name(meta,
                                     "~ V Start",
                                     g_strdup_printf("%g", sicm.vStart));
    gwy_container_set_string_by_name(meta,
                                     "~ V End",
                                     g_strdup_printf("%g", sicm.vEnd));
    gwy_container_set_string_by_name(meta,
                                     "~ Threshold",
                                     g_strdup_printf("%g", sicm.threshold));

    gwy_container_set_string_by_name(meta,
                                     "ADC Max",
                                     g_strdup_printf("%d", sicm.maxADC));
    gwy_container_set_string_by_name(meta,
                                     "Oversampling (Control)",
                                     g_strdup_printf("%d", sicm.ctrlOS));
    gwy_container_set_string_by_name(meta,
                                     "Oversampling (Image)",
                                     g_strdup_printf("%d", sicm.imagOS));
    gwy_container_set_string_by_name(meta,
                                     "Control Points",
                                     g_strdup_printf("%d", sicm.ctrlPts));
    gwy_container_set_string_by_name(meta,
                                     "~ Fit X",
                                     g_strdup_printf("%d", sicm.fitX));
    gwy_container_set_string_by_name(meta,
                                     "~ Fit Y",
                                     g_strdup_printf("%d", sicm.fitY));
    gwy_container_set_string_by_name(meta,
                                     "~ Min",
                                     g_strdup_printf("%d", sicm.min));
    gwy_container_set_string_by_name(meta,
                                     "~ Max",
                                     g_strdup_printf("%d", sicm.max));
    gwy_container_set_string_by_name(meta,
                                     "~ Scan Mode",
                                     g_strdup_printf("%d", sicm.scanMode));
    gwy_container_set_string_by_name(meta,
                                     "~ NCITS",
                                     g_strdup_printf("%d", sicm.NCITS));
    gwy_container_set_string_by_name(meta,
                                     "~ CITS Settle",
                                     g_strdup_printf("%d", sicm.settle));
    gwy_container_set_string_by_name(meta,
                                     "Break Points",
                                     g_strdup_printf("%d", sicm.noPts));
    gwy_container_set_string_by_name(meta,
                                     "~ Break Settle",
                                     g_strdup_printf("%d", sicm.settle2));
    gwy_container_set_string_by_name(meta,
                                     "Loop Mode",
                                     g_strdup_printf("%d", sicm.loopMode));

    gwy_container_set_string_by_name(meta,
                                     "~ Fitting",
                                     g_strdup(sicm.fitting ? "yes" : "no"));
    gwy_container_set_string_by_name(meta,
                                     "Polarity",
                                     g_strdup(sicm.polarity ? "positive" : "negative"));
    gwy_container_set_string_by_name(meta,
                                     "~ 1D Scan",
                                     g_strdup(sicm.scan1D ? "yes" : "no"));
    gwy_container_set_string_by_name(meta,
                                     "Start Center",
                                     g_strdup(sicm.startCenter ? "yes" : "no"));

    /* Format has been updated to add fields that are only valid in hopping mode.
       We prefix these with an asterisk so they sort together.
       In earlier versions this part of the header was left empty,
       so for older files all these fields will be zero even when
       hopping was used. */
    gwy_container_set_string_by_name(meta,
                                     "* Hop Amplitude",
                                     g_strdup_printf("%d nm", sicm.hopAmp));
    gwy_container_set_string_by_name(meta,
                                     "* Rise Rate",
                                     g_strdup_printf("%d nm/ms", sicm.riseRate));
    gwy_container_set_string_by_name(meta,
                                     "* Rise-to-Fall Time",
                                     g_strdup_printf("%d ms", sicm.riseToFallTime));
    gwy_container_set_string_by_name(meta,
                                     "* Fall Rate",
                                     g_strdup_printf("%d nm/ms", sicm.fallRate));
    gwy_container_set_string_by_name(meta,
                                     "* DC Break Set Point",
                                     g_strdup_printf("%d permille", sicm.dcSetPoint));
    gwy_container_set_string_by_name(meta,
                                     "* Prescan Square Size",
                                     g_strdup_printf("%d pixels", sicm.prescanSqrSize));
    gwy_container_set_string_by_name(meta,
                                     "* Prescan Hop Amplitude",
                                     g_strdup_printf("%d nm", sicm.prescanHopAmp));
    gwy_container_set_string_by_name(meta,
                                     "* Minimum Hop Amplitude",
                                     g_strdup_printf("%d nm", sicm.minHopAmp));
    gwy_container_set_string_by_name(meta,
                                     "* Absolute Hopping Mode",
                                     g_strdup(sicm.absHopMode ? "yes" : "no"));
    gwy_container_set_string_by_name(meta,
                                     "* Fast Prescan Mode",
                                     g_strdup(sicm.fastPrescanMode ? "yes" : "no"));
    gwy_container_set_string_by_name(meta,
                                     "* Resolution Levels",
                                     g_strdup_printf("[%d, %d, %d, %d, %d, %d, %d, %d] (pixels)",
                                     sicm.resLevels[0], sicm.resLevels[1], sicm.resLevels[2], sicm.resLevels[3],
                                     sicm.resLevels[4], sicm.resLevels[4], sicm.resLevels[6], sicm.resLevels[7]));
    gwy_container_set_string_by_name(meta,
                                     "* Resolution Thresholds",
                                     g_strdup_printf("[%d, %d, %d, %d, %d, %d, %d, %d] (nm)",
                                     sicm.resThresholds[0], sicm.resThresholds[1], sicm.resThresholds[2], sicm.resThresholds[3],
                                     sicm.resThresholds[4], sicm.resThresholds[4], sicm.resThresholds[6], sicm.resThresholds[7]));
    gwy_container_set_string_by_name(meta,
                                     "* Resolution Levels Used",
                                     g_strdup_printf("%d", sicm.numResLevels));

    gwy_container_set_object_by_name(container, "/0/meta", meta);
    g_object_unref(meta);

    gwy_file_channel_import_log_add(container, 0, NULL, filename);

    gwy_file_abandon_contents(buffer, size, NULL);

    return container;
}