~ubuntu-branches/ubuntu/natty/pymca/natty

« back to all changes in this revision

Viewing changes to .pc/03_specfile-locale.patch/PyMca/specfile/src/specfile_py.c

  • Committer: Bazaar Package Importer
  • Author(s): Angel Abad
  • Date: 2010-11-18 22:28:16 UTC
  • mfrom: (2.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20101118222816-b8g5f3bmjp97cckq
Tags: 4.4.0-2ubuntu1
* Merge from debian unstable (LP: #677221). Remaining changes:
  - rm -f the data files from the pymca package instead of rm. fix FTBFS

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#/*##########################################################################
 
2
# Copyright (C) 2004-2008 European Synchrotron Radiation Facility
 
3
#
 
4
# This file is part of the PyMCA X-ray Fluorescence Toolkit developed at
 
5
# the ESRF by the Beamline Instrumentation Software Support (BLISS) group.
 
6
#
 
7
# This toolkit is free software; you can redistribute it and/or modify it 
 
8
# under the terms of the GNU General Public License as published by the Free
 
9
# Software Foundation; either version 2 of the License, or (at your option) 
 
10
# any later version.
 
11
#
 
12
# PyMCA is distributed in the hope that it will be useful, but WITHOUT ANY
 
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
14
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
15
# details.
 
16
#
 
17
# You should have received a copy of the GNU General Public License along with
 
18
# PyMCA; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
 
19
# Suite 330, Boston, MA 02111-1307, USA.
 
20
#
 
21
# PyMCA follows the dual licensing model of Trolltech's Qt and Riverbank's PyQt
 
22
# and cannot be used as a free plugin for a non-free program. 
 
23
#
 
24
# Please contact the ESRF industrial unit (industry@esrf.fr) if this license 
 
25
# is a problem for you.
 
26
#############################################################################*/
 
27
/*char RcsId[] = "$Header: /segfs/bliss/source/python/specfile/specfile-3.1/src/RCS/specfile_py.c,v 1.9 2005/05/25 13:30:16 sole Exp $"; */
 
28
/************************************************************************
 
29
 *
 
30
 *   File:          specfile_py.c
 
31
 *
 
32
 *   Project:       SpecFile library
 
33
 *
 
34
 *   Description:   python interface to Specfile library
 
35
 * 
 
36
 *   Author:        V.Rey
 
37
 *
 
38
 *   Date:          $Date: 2005/05/25 13:30:16 $
 
39
 *
 
40
 ************************************************************************/
 
41
/*
 
42
 *   Log: $Log: specfile_py.c,v $
 
43
 *   Log: Revision 1.9  2005/05/25 13:30:16  sole
 
44
 *   Log: Enormous memory leak corrected
 
45
 *   Log:
 
46
 *   Log: Revision 1.8  2005/05/18 10:47:30  sole
 
47
 *   Log: Problem with ifdef solved
 
48
 *   Log:
 
49
 *   Log: Revision 1.7  2004/05/21 12:33:02  sole
 
50
 *   Log: Working windows version
 
51
 *   Log:
 
52
 *   Log: Revision 1.6  2003/03/06 17:02:09  sole
 
53
 *   Log: Check for number of lines less than 0
 
54
 *   Log: (At most should be -1, but I let it as less than 0)
 
55
 *   Log:
 
56
 *   Log: Revision 1.5  2002/11/15 10:11:19  sole
 
57
 *   Log: Memory leak corrected in scandata_data
 
58
 *   Log:
 
59
 *   Log: Revision 1.4  2002/11/15 09:29:23  sole
 
60
 *   Log: PyArray_Return put back
 
61
 *   Log: Several memory leaks removed.
 
62
 *   Log:
 
63
 *   Log: Revision 1.3  2002/11/12 13:11:29  sole
 
64
 *   Log: <= -1 replaced by == -1
 
65
 *   Log:
 
66
 *
 
67
 *       New command for Scandata:  motorpos()
 
68
 *       interfaces with the library function 
 
69
 *       SfMotorPosByName()
 
70
 *
 
71
 *   Log: Revision 3.0  2000/12/20 14:17:19  rey
 
72
 *   Log: Python version available
 
73
 *   Log:
 
74
 *   Log: Revision 2.2  2000/12/20 12:12:08  rey
 
75
 *   Log: bug corrected with SfAllMotors
 
76
 *   Log:
 
77
 * Revision 2.1  2000/07/31  19:03:11  19:03:11  rey (Vicente Rey-Bakaikoa)
 
78
 * SfUpdate and bug corrected in ReadIndex
 
79
 * 
 
80
 * Revision 1.5  2000/02/16  13:58:27  13:58:27  rey (Vicente Rey-Bakaikoa)
 
81
 * Version before major changes for MCA support
 
82
 * 
 
83
 * Revision 1.1  99/10/19  15:28:32  15:28:32  rey (Vicente Rey-Bakaikoa)
 
84
 * Initial revision
 
85
 * 
 
86
 */
 
87
 
 
88
/************************************************************************
 
89
 
 
90
  Copyright 1999  by European Synchrotron Radiation Facility, Grenoble, 
 
91
                  France
 
92
 
 
93
                               ----------
 
94
 
 
95
                           All Rights Reserved
 
96
 
 
97
                               ----------
 
98
 
 
99
Permission to use, copy, modify, and distribute this software and its
 
100
documentation for any purpose and without fee is hereby granted,
 
101
provided that the above copyright notice appear in all copies and that
 
102
both that copyright notice and this permission notice appear in
 
103
supporting documentation, and that the names of European Synchrotron
 
104
Radiation Facility or ESRF or BLISS not be used in advertising or 
 
105
publicity pertaining to distribution of the software without specific, 
 
106
written prior permission.
 
107
 
 
108
EUROPEAN SYNCHROTRON RADIATION FACILITY DISCLAIMS ALL WARRANTIES WITH
 
109
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
 
110
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL EUROPEAN SYNCHROTRON
 
111
RADIATION FACILITY OR ESRF BE LIABLE FOR ANY SPECIAL, INDIRECT OR 
 
112
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 
 
113
DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 
114
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 
115
PERFORMANCE OF THIS SOFTWARE.
 
116
 
 
117
**************************************************************************/
 
118
#ifdef WIN32
 
119
#ifdef _DEBUG
 
120
#undef _DEBUG
 
121
#include <Python.h>
 
122
#define _DEBUG
 
123
#else
 
124
#include <Python.h>
 
125
#endif
 
126
#endif
 
127
 
 
128
#ifndef WIN32
 
129
#include <Python.h>
 
130
#endif
 
131
 
 
132
#include <numpy/arrayobject.h>
 
133
#include <SpecFile.h>
 
134
 
 
135
/*
 
136
 * Specfile exceptions
 
137
 */
 
138
static PyObject *ErrorObject;
 
139
 
 
140
     /* except specfile.error */
 
141
#define onError(message)  \
 
142
     {PyErr_SetString (ErrorObject,message); return NULL; }  
 
143
 
 
144
/*
 
145
 * Data types
 
146
 */
 
147
typedef struct {
 
148
   PyObject_HEAD
 
149
   SpecFile *sf;
 
150
   char     *name;
 
151
   short     length;
 
152
} specfileobject;
 
153
 
 
154
typedef struct {
 
155
   PyObject_HEAD
 
156
   specfileobject *file;
 
157
   long            index;
 
158
   long            cols;
 
159
} scandataobject;
 
160
 
 
161
staticforward PyTypeObject Specfiletype;
 
162
staticforward PyTypeObject Scandatatype;
 
163
 
 
164
#define is_specfileobject(v) ((v)->type == &Specfiletype)
 
165
#define is_scandataobject(v) ((v)->type == &Scandatatype)
 
166
 
 
167
 
 
168
/*
 
169
 * Function prototypes
 
170
 */
 
171
   /*
 
172
    * Utility function
 
173
    */
 
174
static char           * compList(long *nolist,long howmany);
 
175
 
 
176
   /*
 
177
    * Specfile methods
 
178
    */
 
179
static PyObject  * specfile_list      (PyObject *self,PyObject *args);
 
180
static PyObject  * specfile_allmotors (PyObject *self,PyObject *args);
 
181
static PyObject  * specfile_title     (PyObject *self,PyObject *args);
 
182
static PyObject  * specfile_user      (PyObject *self,PyObject *args);
 
183
static PyObject  * specfile_date      (PyObject *self,PyObject *args);
 
184
static PyObject  * specfile_epoch     (PyObject *self,PyObject *args);
 
185
static PyObject  * specfile_update    (PyObject *self,PyObject *args);
 
186
static PyObject  * specfile_scanno    (PyObject *self,PyObject *args);
 
187
static PyObject  * specfile_select    (PyObject *self,PyObject *args);
 
188
static PyObject  * specfile_show      (PyObject *self,PyObject *args);
 
189
 
 
190
static struct PyMethodDef  specfile_methods[] = 
 
191
{
 
192
   {"list",      specfile_list,      1},
 
193
   {"allmotors", specfile_allmotors, 1},
 
194
   {"title",     specfile_title,     1},
 
195
   {"user",      specfile_user,      1},
 
196
   {"date",      specfile_date,      1},
 
197
   {"epoch",     specfile_epoch,     1},
 
198
   {"update",    specfile_update,    1},
 
199
   {"scanno",    specfile_scanno,    1},
 
200
   {"select",    specfile_select,    1},
 
201
   {"show",      specfile_show,      1},
 
202
   { NULL, NULL}
 
203
};
 
204
 
 
205
   /*
 
206
    * Specfile python basic operations
 
207
    */
 
208
static PyObject * specfile_open   (char *filename);             /* create  */
 
209
static PyObject * specfile_close  (PyObject *self);             /* dealloc */
 
210
static int        specfile_noscans(PyObject *self);             /* length  */
 
211
static PyObject * specfile_scan   (PyObject *self,int index);   /* item    */
 
212
static int        specfile_print  (PyObject *self,FILE *fp, 
 
213
                                                   int flags);  /* print*/
 
214
static PyObject * specfile_getattr(PyObject *self,char *name);  /* getattr */
 
215
 
 
216
 
 
217
   /*
 
218
    * Scandata methods
 
219
    */
 
220
 
 
221
static PyObject   * scandata_data         (PyObject *self,PyObject *args);
 
222
static PyObject   * scandata_dataline     (PyObject *self,PyObject *args);
 
223
static PyObject   * scandata_datacol      (PyObject *self,PyObject *args);
 
224
static PyObject   * scandata_alllabels    (PyObject *self,PyObject *args);
 
225
static PyObject   * scandata_allmotorpos  (PyObject *self,PyObject *args);
 
226
static PyObject   * scandata_motorpos     (PyObject *self,PyObject *args);
 
227
static PyObject   * scandata_hkl          (PyObject *self,PyObject *args);
 
228
static PyObject   * scandata_number       (PyObject *self,PyObject *args);
 
229
static PyObject   * scandata_order        (PyObject *self,PyObject *args);
 
230
static PyObject   * scandata_command      (PyObject *self,PyObject *args);
 
231
static PyObject   * scandata_date         (PyObject *self,PyObject *args);
 
232
static PyObject   * scandata_cols         (PyObject *self,PyObject *args);
 
233
static PyObject   * scandata_lines        (PyObject *self,PyObject *args);
 
234
static PyObject   * scandata_header       (PyObject *self,PyObject *args);
 
235
static PyObject   * scandata_fileheader   (PyObject *self,PyObject *args);
 
236
static PyObject   * scandata_nbmca        (PyObject *self,PyObject *args);
 
237
static PyObject   * scandata_mca          (PyObject *self,PyObject *args);
 
238
static PyObject   * scandata_show         (PyObject *self,PyObject *args);
 
239
 
 
240
static struct PyMethodDef  scandata_methods[] = {
 
241
   {"data",        scandata_data,        1},
 
242
   {"dataline",    scandata_dataline,    1},
 
243
   {"datacol",     scandata_datacol,     1},
 
244
   {"alllabels",   scandata_alllabels,   1},
 
245
   {"allmotorpos", scandata_allmotorpos, 1},
 
246
   {"motorpos",    scandata_motorpos,    1},
 
247
   {"hkl",         scandata_hkl,         1},
 
248
   {"number",      scandata_number,      1},
 
249
   {"order",       scandata_order,       1},
 
250
   {"command",     scandata_command,     1},
 
251
   {"date",        scandata_date,        1},
 
252
   {"cols",        scandata_cols,        1},
 
253
   {"lines",       scandata_lines,       1},
 
254
   {"header",      scandata_header,      1},
 
255
   {"fileheader",  scandata_fileheader,  1},
 
256
   {"nbmca",       scandata_nbmca,       1},
 
257
   {"mca",         scandata_mca,         1},
 
258
   {"show",        scandata_show,        1},
 
259
   { NULL, NULL}
 
260
};
 
261
 
 
262
   /*
 
263
    * Scandata python basic operation
 
264
    */
 
265
static PyObject * scandata_new    (void);                         /* create */
 
266
static PyObject * scandata_free   (PyObject *self);               /* dealloc */
 
267
static int        scandata_size   (PyObject *self);               /* length */
 
268
static PyObject * scandata_col    (PyObject *self,int index);     /* item */
 
269
static PyObject * scandata_slice  (PyObject *self,int lidx,int hidx);/* slice */
 
270
static int        scandata_print  (PyObject *self,FILE *fp, 
 
271
                                                   int flags);    /* print*/
 
272
static PyObject * scandata_getattr(PyObject *self,char *name);    /* getattr */
 
273
 
 
274
/*
 
275
 * module init
 
276
 */
 
277
#ifndef WIN32
 
278
 void initspecfile(void);
 
279
#endif
 
280
   /*
 
281
    * Creators
 
282
    */
 
283
static PyObject *specfiletype_new(PyObject *self,PyObject *args);
 
284
static PyObject *scandatatype_new(PyObject *self,PyObject *args);
 
285
 
 
286
 
 
287
/*
 
288
 *  Specfile class
 
289
 * 
 
290
 *************************************************/
 
291
  /*
 
292
   * Instance methods
 
293
   */
 
294
static PyObject *
 
295
specfile_list(PyObject *self,PyObject *args)
 
296
{
 
297
    long      *scanlist; 
 
298
    long      no_scans;
 
299
    int       error = 0;
 
300
    char     *strlist;
 
301
    PyObject *pstr;
 
302
 
 
303
    specfileobject *v = (specfileobject *) self;
 
304
 
 
305
    if (!PyArg_ParseTuple(args, ""))   
 
306
        return NULL;
 
307
 
 
308
    no_scans = SfScanNo(v->sf);
 
309
    scanlist = SfList(v->sf,&error);
 
310
 
 
311
    if ( scanlist == NULL || no_scans == 0) {
 
312
        PyErr_SetString(PyExc_TypeError,"Cannot get scan list for file");
 
313
        return NULL;
 
314
    } else {
 
315
        strlist = (char *)compList(scanlist,no_scans);
 
316
        pstr = Py_BuildValue("s",strlist);
 
317
        free(scanlist);
 
318
        return pstr;
 
319
    } 
 
320
}
 
321
 
 
322
static PyObject *
 
323
specfile_allmotors(PyObject *self,PyObject *args)
 
324
{
 
325
    
 
326
    int error,i;
 
327
    char **motornames;
 
328
    long nb_motors;
 
329
    PyObject *t,*x;
 
330
 
 
331
    specfileobject *v = (specfileobject *) self;
 
332
 
 
333
    if (!PyArg_ParseTuple(args, ""))   
 
334
        return NULL;
 
335
 
 
336
    nb_motors = SfAllMotors(v->sf,1,&motornames,&error);
 
337
 
 
338
    if ( nb_motors == -1 ) 
 
339
           onError("cannot get motor names for specfile");
 
340
 
 
341
    t = PyList_New(nb_motors);
 
342
    for ( i = 0 ;i<nb_motors;i++) {
 
343
       x = PyString_FromString(motornames[i]);
 
344
       PyList_SetItem(t,i,x);
 
345
    }
 
346
 
 
347
    return t;
 
348
}
 
349
 
 
350
static PyObject *
 
351
specfile_title(PyObject *self,PyObject *args)
 
352
{
 
353
    int error;
 
354
    char *title;
 
355
    PyObject *pyo;
 
356
 
 
357
    specfileobject *v = (specfileobject *) self;
 
358
 
 
359
    if (!PyArg_ParseTuple(args, ""))   
 
360
        return NULL;
 
361
 
 
362
    title = SfTitle(v->sf,1,&error);
 
363
   
 
364
    if (title == NULL) 
 
365
        onError("cannot get title for specfile")
 
366
 
 
367
    pyo = Py_BuildValue("s",title);
 
368
    free(title);
 
369
    return pyo;
 
370
}
 
371
 
 
372
static PyObject *
 
373
specfile_user(PyObject *self,PyObject *args)
 
374
{
 
375
    int error;
 
376
    char *user;
 
377
 
 
378
    specfileobject *v = (specfileobject *) self;
 
379
 
 
380
    if (!PyArg_ParseTuple(args, ""))   
 
381
        return NULL;
 
382
 
 
383
    user = SfUser(v->sf,1,&error);
 
384
   
 
385
    if (user != NULL) {
 
386
        free(user);
 
387
        return Py_BuildValue("s",user);
 
388
    } else {
 
389
        onError("cannot get user for specfile");
 
390
    }
 
391
}
 
392
 
 
393
static PyObject *
 
394
specfile_date(PyObject *self,PyObject *args)
 
395
{
 
396
    int error;
 
397
    char *date;
 
398
    PyObject *pyo;
 
399
 
 
400
    specfileobject *v = (specfileobject *) self;
 
401
 
 
402
    if (!PyArg_ParseTuple(args, ""))   
 
403
        return NULL;
 
404
 
 
405
    date = SfFileDate(v->sf,1,&error);
 
406
   
 
407
    if (date == NULL) 
 
408
       onError("cannot get data for specfile")
 
409
 
 
410
    pyo = Py_BuildValue("s",date);
 
411
    free(date);
 
412
    return pyo;
 
413
}
 
414
 
 
415
static PyObject *
 
416
specfile_epoch(PyObject *self,PyObject *args)
 
417
{
 
418
    int error;
 
419
    long epoch;
 
420
 
 
421
    specfileobject *v = (specfileobject *) self;
 
422
 
 
423
    if (!PyArg_ParseTuple(args, ""))   
 
424
        return NULL;
 
425
 
 
426
    epoch = SfEpoch(v->sf,1,&error);
 
427
   
 
428
    if (epoch != -1) {
 
429
        return Py_BuildValue("l",epoch);
 
430
    } else {
 
431
        onError("cannot get epoch for specfile");
 
432
    }
 
433
}
 
434
 
 
435
static PyObject *
 
436
specfile_update(PyObject *self,PyObject *args)
 
437
{
 
438
    int error;
 
439
    short ret;
 
440
 
 
441
    specfileobject *v = (specfileobject *) self;
 
442
   
 
443
    if (!PyArg_ParseTuple(args, ""))   
 
444
        return NULL;
 
445
 
 
446
    ret = SfUpdate(v->sf,&error);
 
447
    if (ret == 1){
 
448
       v->length = SfScanNo(v->sf);
 
449
    }
 
450
    return(Py_BuildValue("i",ret));
 
451
}
 
452
 
 
453
static PyObject *
 
454
specfile_scanno(PyObject *self,PyObject *args)
 
455
{
 
456
    long scanno;
 
457
    specfileobject *v = (specfileobject *) self;
 
458
   
 
459
 
 
460
    if (!PyArg_ParseTuple(args, ""))   
 
461
        return NULL;
 
462
 
 
463
    scanno = v->length;
 
464
 
 
465
    return Py_BuildValue("l",scanno);
 
466
}
 
467
 
 
468
static PyObject *
 
469
specfile_select(PyObject *self,PyObject *args) 
 
470
{
 
471
 
 
472
    int  n,number,order,index; 
 
473
    char   *scanstr;
 
474
    int error;
 
475
 
 
476
    scandataobject *v;
 
477
    specfileobject *f = (specfileobject *)self;
 
478
    
 
479
   if (!PyArg_ParseTuple(args,"s",&scanstr)) {
 
480
       return NULL;
 
481
   } else {
 
482
       n = sscanf(scanstr,"%d.%d",&number,&order);
 
483
       if ( n < 1 || n > 2 ) onError("cannot decode scan number/order"); 
 
484
       if ( n == 1) order = 1;
 
485
   }
 
486
 
 
487
    index = SfIndex(f->sf,number,order);
 
488
 
 
489
    if (index == -1 ) 
 
490
          onError("scan not found");
 
491
 
 
492
    v = PyObject_NEW(scandataobject,&Scandatatype);
 
493
 
 
494
    if (v == NULL)
 
495
        return NULL;
 
496
 
 
497
    v->file  = f;
 
498
    v->index = index;
 
499
    v->cols  = SfNoColumns(f->sf,v->index,&error);
 
500
 
 
501
    Py_INCREF(self);
 
502
 
 
503
    return (PyObject *) v;
 
504
}
 
505
 
 
506
static PyObject *
 
507
specfile_show(PyObject *self,PyObject *args) 
 
508
{
 
509
 
 
510
    specfileobject *f = (specfileobject *)self;
 
511
 
 
512
    SfShow(f->sf);
 
513
 
 
514
    return (Py_BuildValue("l",0));
 
515
}
 
516
 
 
517
 
 
518
  /*
 
519
   * Basic specfiletype operations
 
520
   */
 
521
static PyObject *
 
522
specfile_open(char *filename) {      /* on x = specfile.Specfile(name) */
 
523
    specfileobject *self;
 
524
    SpecFile       *sf;
 
525
    int             error;
 
526
 
 
527
    self = PyObject_NEW(specfileobject,&Specfiletype);
 
528
 
 
529
    if (self == NULL)
 
530
        return NULL;
 
531
 
 
532
    if (( sf = SfOpen(filename,&error)) == NULL )
 
533
        onError("cannot open file");
 
534
 
 
535
    self->sf = sf;
 
536
    self->length = SfScanNo(sf);
 
537
    self->name = (char *)strdup(filename);
 
538
    strcpy(self->name,filename);
 
539
 
 
540
/*  Py_INCREF(self); */
 
541
    return (PyObject *)self;
 
542
}
 
543
 
 
544
static PyObject *
 
545
specfile_close(PyObject *self) {
 
546
 
 
547
    specfileobject *f = (specfileobject *) self;
 
548
 
 
549
    SfClose(f->sf);
 
550
    free(f->name);
 
551
 
 
552
    PyObject_DEL(f);
 
553
 
 
554
    return NULL;
 
555
}
 
556
 
 
557
 
 
558
  /*
 
559
   * Sequence type methods
 
560
   */
 
561
static int
 
562
specfile_noscans(PyObject *self) {
 
563
    int       no_scans;
 
564
 
 
565
    specfileobject *f = (specfileobject *)self;
 
566
    no_scans = f->length;
 
567
 
 
568
    return no_scans;
 
569
 
 
570
}
 
571
 
 
572
static PyObject *
 
573
specfile_scan(PyObject *self,int index) {
 
574
    int error;
 
575
 
 
576
    scandataobject *v;
 
577
    specfileobject *f = (specfileobject *)self;
 
578
 
 
579
    if ( index < 0 || index >= f->length) {
 
580
         PyErr_SetString(PyExc_IndexError,"scan out of bounds");
 
581
         return NULL;
 
582
    }
 
583
 
 
584
    v = PyObject_NEW(scandataobject,&Scandatatype);
 
585
 
 
586
    if (v == NULL)
 
587
        return NULL;
 
588
 
 
589
    v->file  = f;
 
590
    v->index = index+1;
 
591
    v->cols  = SfNoColumns(f->sf,v->index,&error);
 
592
 
 
593
    Py_INCREF(self);
 
594
 
 
595
    return (PyObject *) v;
 
596
}
 
597
 
 
598
 
 
599
static int
 
600
specfile_print(PyObject *self,FILE *fp,int flags) {
 
601
    int ok=0;
 
602
    specfileobject *f = (specfileobject *)self;
 
603
    fprintf(fp,"specfile('%s')", f->name);
 
604
    return ok;
 
605
}
 
606
 
 
607
static PyObject *
 
608
specfile_getattr(PyObject *self,char *name) {
 
609
   return Py_FindMethod(specfile_methods,self,name);
 
610
}
 
611
 
 
612
 
 
613
/*
 
614
 * Type descriptors
 
615
 */
 
616
static PySequenceMethods specfile_as_sequence = {
 
617
  specfile_noscans,    /*     length len(sf)     */
 
618
  0,              /*     concat sf1 + sf2   */
 
619
  0,              /*     repeat sf * n      */
 
620
  specfile_scan,       /*     item  sf[i], in    */
 
621
  0,              /*     slice sf[i:j]      */
 
622
  0,              /*     asset sf[i] = v    */
 
623
  0,              /* slice ass. sf[i:j] = v */
 
624
};
 
625
 
 
626
static PyTypeObject Specfiletype = {
 
627
    /* type header */ 
 
628
#ifdef WIN32
 
629
       PyObject_HEAD_INIT(NULL)
 
630
#else
 
631
       PyObject_HEAD_INIT(&PyType_Type)
 
632
#endif       
 
633
       0,
 
634
       "specfile",
 
635
       sizeof(specfileobject),
 
636
       0,
 
637
 
 
638
    /* standard methods */
 
639
       (destructor)   specfile_close,    /*  tp-dealloc   ref-count = 0  */
 
640
       (printfunc)    specfile_print,    /*  tp-print     print sf       */
 
641
       (getattrfunc)  specfile_getattr,  /*  tp-getattr   sf.attr        */
 
642
       (setattrfunc)  0,                 /*  tp-setattr   sf.attr = v    */
 
643
       (cmpfunc)      0,                 /*  tp-setattr   sf1 > sf2      */
 
644
       (reprfunc)     0,                 /*  tp-repr      'sf', print sf */
 
645
 
 
646
    /* type categories */
 
647
       0,                                /* tp as number   +,-,....      */
 
648
       &specfile_as_sequence,            /* tp as sequence +,[i],[i:j]...*/
 
649
       0,                                /* tp as mapping [key], len, ...*/
 
650
 
 
651
    /* more methods */
 
652
       (hashfunc)     0,                 /* tp_hash  dict(sf) */
 
653
       /* (binaryfunc)   0,               tp_call  sf()     */
 
654
       /* (reprfunc)     0,               tp_str   str(x)   */
 
655
};
 
656
 
 
657
/* end specfile class */
 
658
 
 
659
 
 
660
/* begin scandata */
 
661
static PyObject *
 
662
scandata_data(PyObject *self,PyObject *args) {
 
663
   
 
664
    int     error;
 
665
    int     ret;
 
666
    double  **data;
 
667
    long    *data_info;
 
668
    int i,j;
 
669
    npy_intp dimensions[2];
 
670
 
 
671
    SpecFile *sf;
 
672
    int     idx,didx;
 
673
 
 
674
    PyArrayObject *r_array;
 
675
 
 
676
    scandataobject *s = (scandataobject *) self;
 
677
 
 
678
    sf  = (s->file)->sf;
 
679
    idx = s->index;
 
680
 
 
681
    if (!PyArg_ParseTuple(args,"") )
 
682
           onError("wrong arguments for data");
 
683
    ret = SfData(sf,idx,&data,&data_info,&error);
 
684
    if ( ret == -1 )
 
685
           onError("cannot read data");
 
686
 
 
687
/*    printf("DATA: %d rows / %d columns\n", data_info[1], data_info[0]);*/
 
688
 
 
689
    dimensions[0] = data_info[1];
 
690
    dimensions[1] = data_info[0];
 
691
    r_array = (PyArrayObject *)PyArray_SimpleNew(2,dimensions,PyArray_DOUBLE);
 
692
 
 
693
   /*
 
694
    * Copy
 
695
    *   I should write a specfile function that copies all data in a 
 
696
    *   single pointer array
 
697
    */
 
698
    for (i=0;i<dimensions[0];i++) {
 
699
       for (j=0;j<dimensions[1];j++) {
 
700
          didx = j + i * dimensions[1];
 
701
          ((double *)r_array->data)[didx] = data[j][i];
 
702
       }
 
703
    }
 
704
    /* memcpy(array->data,data,PyArray_NBYTES(array)); */
 
705
 
 
706
    freeArrNZ((void ***)&data,data_info[ROW]);
 
707
    free(data_info);
 
708
    if (data != (double **) NULL) {
 
709
        free(data);
 
710
    }
 
711
/*    return (PyObject *)array; */
 
712
    return PyArray_Return(r_array);
 
713
}
 
714
 
 
715
static PyObject *
 
716
scandata_dataline(PyObject *self,PyObject *args) {
 
717
    int     error;
 
718
    int     lineno;
 
719
    npy_intp ret;
 
720
    double  *data;
 
721
 
 
722
    PyArrayObject *r_array;
 
723
 
 
724
    SpecFile *sf;
 
725
    int     idx;
 
726
 
 
727
    scandataobject *s = (scandataobject *) self;
 
728
 
 
729
    sf  = (s->file)->sf;
 
730
    idx = s->index;
 
731
 
 
732
    if (!PyArg_ParseTuple(args,"i",&lineno)) 
 
733
            onError("cannot decode arguments for line data"); 
 
734
    
 
735
    ret = SfDataLine(sf,idx,lineno,&data,&error);
 
736
     
 
737
    if (ret == -1 )
 
738
          onError("cannot get data for line");
 
739
 
 
740
    r_array  = (PyArrayObject *)PyArray_SimpleNew(1,&ret,PyArray_DOUBLE);
 
741
 
 
742
    memcpy(r_array->data,data,PyArray_NBYTES(r_array));
 
743
 
 
744
    return (PyObject *)r_array;
 
745
}
 
746
 
 
747
static PyObject *
 
748
scandata_datacol(PyObject *self,PyObject *args) {
 
749
    int     error;
 
750
    int     colno;
 
751
    npy_intp ret;
 
752
    char    *colname;
 
753
    double  *data;
 
754
 
 
755
    PyArrayObject *r_array;
 
756
 
 
757
    SpecFile *sf;
 
758
    int     idx;
 
759
    scandataobject *s = (scandataobject *) self;
 
760
 
 
761
    sf  = (s->file)->sf;
 
762
    idx = s->index;
 
763
 
 
764
    if (!PyArg_ParseTuple(args,"i",&colno)) {
 
765
      PyErr_Clear() ; 
 
766
      if (!PyArg_ParseTuple(args,"s",&colname)) {
 
767
    onError("cannot decode arguments for column data"); 
 
768
      } else {
 
769
    ret = SfDataColByName(sf,idx,colname,&data,&error);
 
770
      }
 
771
    } else {
 
772
      ret = SfDataCol(sf,idx,colno,&data,&error);
 
773
    }
 
774
    
 
775
    if (ret == -1 )
 
776
      onError("cannot get data for column");
 
777
    
 
778
    r_array      = (PyArrayObject *)PyArray_SimpleNew(1,&ret,PyArray_DOUBLE);
 
779
    
 
780
   if (data != (double *) NULL){
 
781
        memcpy(r_array->data,data,PyArray_NBYTES(r_array));
 
782
        free(data);
 
783
    }else{
 
784
        /* return an empty array? */
 
785
        printf("I should return an empty array ...\n");
 
786
                PyArray_FILLWBYTE(r_array, 0);
 
787
    }
 
788
 
 
789
    return PyArray_Return(r_array);
 
790
    /*
 
791
      it does not work for solaris and linux
 
792
      I should check the call to PyErr_Occurred()) in Numeric/Src/arrayobject.c
 
793
      PyArray_Return(array); 
 
794
     */
 
795
}
 
796
 
 
797
 
 
798
static PyObject * 
 
799
scandata_alllabels  (PyObject *self,PyObject *args)
 
800
{
 
801
    int error,i;
 
802
    char **labels;
 
803
    long nb_labels;
 
804
    PyObject *t,*x;
 
805
 
 
806
    scandataobject *v = (scandataobject *) self;
 
807
 
 
808
    if (!PyArg_ParseTuple(args, ""))   
 
809
        return NULL;
 
810
 
 
811
    nb_labels = SfAllLabels((v->file)->sf,v->index,&labels,&error);
 
812
 
 
813
    t = PyList_New(nb_labels);
 
814
    for ( i = 0 ;i<nb_labels;i++) {
 
815
       x = PyString_FromString(labels[i]);
 
816
       PyList_SetItem(t,i,x);
 
817
    }
 
818
 
 
819
    return t;
 
820
}
 
821
 
 
822
static PyObject   * 
 
823
scandata_allmotorpos  (PyObject *self,PyObject *args)
 
824
{
 
825
    int error,i;
 
826
    double *motorpos;
 
827
    long nb_motors;
 
828
    PyObject *t,*x;
 
829
 
 
830
    scandataobject *v = (scandataobject *) self;
 
831
 
 
832
    if (!PyArg_ParseTuple(args, ""))   
 
833
        return NULL;
 
834
 
 
835
    nb_motors = SfAllMotorPos((v->file)->sf,v->index,&motorpos,&error);
 
836
 
 
837
    t = PyList_New(nb_motors);
 
838
 
 
839
    for ( i = 0 ;i<nb_motors;i++) {
 
840
       x = PyFloat_FromDouble(motorpos[i]);
 
841
       PyList_SetItem(t,i,x);
 
842
    }
 
843
 
 
844
    return t;
 
845
}
 
846
 
 
847
static PyObject   * 
 
848
scandata_motorpos  (PyObject *self,PyObject *args)
 
849
{
 
850
    char   *motorname;
 
851
    int     error;
 
852
    double  motorpos;
 
853
 
 
854
    scandataobject *v = (scandataobject *) self;
 
855
 
 
856
    if (!PyArg_ParseTuple(args,"s",&motorname)) {
 
857
       return NULL;
 
858
    } 
 
859
 
 
860
    motorpos = SfMotorPosByName((v->file)->sf,v->index,motorname,&error);
 
861
 
 
862
    if (motorpos != HUGE_VAL) {
 
863
        return Py_BuildValue("f",motorpos);
 
864
    } else {
 
865
        onError("cannot get position for motor");
 
866
    }
 
867
   
 
868
}
 
869
 
 
870
static PyObject * 
 
871
scandata_hkl          (PyObject *self,PyObject *args)
 
872
{
 
873
    int idx,error;
 
874
    double *hkl;
 
875
    PyObject *pyo;
 
876
 
 
877
    SpecFile *sf;
 
878
 
 
879
    scandataobject *s = (scandataobject *) self;
 
880
 
 
881
    idx = s->index;
 
882
    if (idx == -1 ) {
 
883
        onError("empty scan data");
 
884
    }
 
885
    sf  = (s->file)->sf;
 
886
 
 
887
    hkl = SfHKL(sf,idx,&error);
 
888
 
 
889
    if (hkl == NULL) 
 
890
        onError("cannot get data for column");
 
891
 
 
892
    pyo = Py_BuildValue("ddd",hkl[0],hkl[1],hkl[2]);
 
893
    free(hkl);
 
894
    return pyo;
 
895
 
 
896
}
 
897
 
 
898
static PyObject * 
 
899
scandata_number       (PyObject *self,PyObject *args)
 
900
{
 
901
    int number,idx;
 
902
    SpecFile *sf;
 
903
 
 
904
    scandataobject *s = (scandataobject *) self;
 
905
 
 
906
    sf  = (s->file)->sf;
 
907
    idx = s->index;
 
908
 
 
909
    number = SfNumber(sf,idx);
 
910
 
 
911
    return Py_BuildValue("i",number);
 
912
 
 
913
}
 
914
 
 
915
static PyObject   * 
 
916
scandata_order        (PyObject *self,PyObject *args)
 
917
{
 
918
    int order,idx;
 
919
    SpecFile *sf;
 
920
 
 
921
    scandataobject *s = (scandataobject *) self;
 
922
 
 
923
    sf  = (s->file)->sf;
 
924
    idx = s->index;
 
925
 
 
926
    order = SfOrder(sf,idx);
 
927
 
 
928
    return Py_BuildValue("i",order);
 
929
 
 
930
}
 
931
 
 
932
static PyObject   * 
 
933
scandata_command      (PyObject *self,PyObject *args)
 
934
{
 
935
    int idx,error;
 
936
    char *command;
 
937
    PyObject *pyo;
 
938
 
 
939
    SpecFile *sf;
 
940
 
 
941
    scandataobject *s = (scandataobject *) self;
 
942
 
 
943
    idx = s->index;
 
944
    if (idx == -1 ) {
 
945
        onError("empty scan data");
 
946
    }
 
947
    sf  = (s->file)->sf;
 
948
 
 
949
    command = SfCommand(sf,idx,&error);
 
950
 
 
951
    if (command == NULL) 
 
952
       onError("cannot get command for scan")
 
953
 
 
954
    pyo = Py_BuildValue("s",command);
 
955
    free(command);
 
956
    return pyo;
 
957
 
 
958
}
 
959
 
 
960
static PyObject   * 
 
961
scandata_date      (PyObject *self,PyObject *args)
 
962
{
 
963
    int idx,error;
 
964
    char *date;
 
965
    PyObject *pyo;
 
966
 
 
967
    SpecFile *sf;
 
968
 
 
969
    scandataobject *s = (scandataobject *) self;
 
970
 
 
971
    idx = s->index;
 
972
    if (idx == -1 ) {
 
973
        onError("empty scan data");
 
974
    }
 
975
    sf  = (s->file)->sf;
 
976
 
 
977
    date = SfDate(sf,idx,&error);
 
978
 
 
979
    if (date == NULL) 
 
980
        onError("cannot get date for scan");
 
981
 
 
982
    pyo =  Py_BuildValue("s",date);
 
983
    free(date);
 
984
    return pyo;
 
985
 
 
986
}
 
987
 
 
988
static PyObject   * 
 
989
scandata_cols      (PyObject *self,PyObject *args)
 
990
{
 
991
    int cols,idx;
 
992
 
 
993
    scandataobject *s = (scandataobject *) self;
 
994
 
 
995
    idx = s->index;
 
996
    if (idx == -1 ) 
 
997
        onError("empty scan data");
 
998
   
 
999
    cols = s->cols; 
 
1000
 
 
1001
    if (cols == -1)
 
1002
        onError("cannot get cols for scan");
 
1003
 
 
1004
    return Py_BuildValue("l",cols);
 
1005
}
 
1006
 
 
1007
static PyObject   * 
 
1008
scandata_lines      (PyObject *self,PyObject *args)
 
1009
{
 
1010
    int lines,idx,error;
 
1011
 
 
1012
    SpecFile *sf;
 
1013
 
 
1014
    scandataobject *s = (scandataobject *) self;
 
1015
 
 
1016
    idx = s->index;
 
1017
    if (idx == -1 ) 
 
1018
        onError("empty scan data");
 
1019
    
 
1020
    sf  = (s->file)->sf;
 
1021
 
 
1022
    lines = SfNoDataLines(sf,idx,&error);
 
1023
 
 
1024
    /*if (lines == -1){*/
 
1025
    if (lines < 0){
 
1026
        onError("cannot get lines for scan");
 
1027
        lines=0;
 
1028
    }
 
1029
 
 
1030
    return Py_BuildValue("l",lines);
 
1031
}
 
1032
 
 
1033
static PyObject   * 
 
1034
scandata_fileheader     (PyObject *self,PyObject *args)
 
1035
{
 
1036
    int i,no_lines,idx,error;
 
1037
    char **lines;
 
1038
    char *searchstr;
 
1039
    PyObject *t,*x;
 
1040
 
 
1041
    SpecFile *sf;
 
1042
 
 
1043
    scandataobject *s = (scandataobject *) self;
 
1044
 
 
1045
    if (!PyArg_ParseTuple(args,"s",&searchstr)) 
 
1046
      return NULL;
 
1047
 
 
1048
    idx = s->index;
 
1049
    if (idx == -1 ) 
 
1050
        onError("empty scan data");
 
1051
    
 
1052
    sf  = (s->file)->sf;
 
1053
 
 
1054
    no_lines = SfFileHeader(sf,idx,searchstr,&lines,&error);
 
1055
 
 
1056
    if (no_lines == -1)
 
1057
        onError("cannot get lines for scan");
 
1058
 
 
1059
    t = PyList_New(no_lines);
 
1060
    for ( i = 0 ;i<no_lines;i++) {
 
1061
       x = PyString_FromString(lines[i]);
 
1062
       PyList_SetItem(t,i,x);
 
1063
    }
 
1064
 
 
1065
    return t;
 
1066
 
 
1067
    return Py_BuildValue("l",no_lines);
 
1068
}
 
1069
 
 
1070
static PyObject   * 
 
1071
scandata_header     (PyObject *self,PyObject *args)
 
1072
{
 
1073
    int i,no_lines,idx,error;
 
1074
    char **lines;
 
1075
    char *searchstr;
 
1076
    PyObject *t,*x;
 
1077
 
 
1078
    SpecFile *sf;
 
1079
 
 
1080
    scandataobject *s = (scandataobject *) self;
 
1081
 
 
1082
    if (!PyArg_ParseTuple(args,"s",&searchstr)) 
 
1083
      return NULL;
 
1084
 
 
1085
    idx = s->index;
 
1086
    if (idx == -1 ) 
 
1087
        onError("empty scan data");
 
1088
    
 
1089
    sf  = (s->file)->sf;
 
1090
 
 
1091
    no_lines = SfHeader(sf,idx,searchstr,&lines,&error);
 
1092
 
 
1093
    if (no_lines == -1)
 
1094
        onError("cannot get lines for scan");
 
1095
 
 
1096
    t = PyList_New(no_lines);
 
1097
    for ( i = 0 ;i<no_lines;i++) {
 
1098
       x = PyString_FromString(lines[i]);
 
1099
       PyList_SetItem(t,i,x);
 
1100
    }
 
1101
 
 
1102
    return t;
 
1103
 
 
1104
    return Py_BuildValue("l",no_lines);
 
1105
}
 
1106
 
 
1107
static PyObject   * 
 
1108
scandata_nbmca      (PyObject *self,PyObject *args)
 
1109
{
 
1110
    int nomca,idx,error;
 
1111
 
 
1112
    PyObject *pyo;
 
1113
    SpecFile *sf;
 
1114
 
 
1115
    scandataobject *s = (scandataobject *) self;
 
1116
 
 
1117
    idx = s->index;
 
1118
    if (idx == -1 ) {
 
1119
        onError("empty scan data");
 
1120
    }
 
1121
    sf  = (s->file)->sf;
 
1122
 
 
1123
    nomca = SfNoMca(sf,idx,&error);
 
1124
 
 
1125
    if (nomca == -1) 
 
1126
        onError("cannot get number of mca for scan");
 
1127
 
 
1128
    pyo =  Py_BuildValue("l",nomca);
 
1129
    return pyo;
 
1130
}
 
1131
 
 
1132
static PyObject   * 
 
1133
scandata_mca      (PyObject *self,PyObject *args)
 
1134
{
 
1135
    int    error;
 
1136
    npy_intp ret;
 
1137
    long   idx,mcano;
 
1138
 
 
1139
    double         *mcadata = NULL;
 
1140
    PyArrayObject  *r_array;
 
1141
 
 
1142
    SpecFile *sf;
 
1143
 
 
1144
    scandataobject *s = (scandataobject *) self;
 
1145
 
 
1146
    if (!PyArg_ParseTuple(args,"l",&mcano)) 
 
1147
            onError("cannot decode arguments for line data"); 
 
1148
    
 
1149
    idx = s->index;
 
1150
 
 
1151
    if (idx == -1 ) {
 
1152
        onError("empty scan data");
 
1153
    }
 
1154
 
 
1155
    sf  = (s->file)->sf;
 
1156
 
 
1157
    ret = SfGetMca(sf,idx,mcano,&mcadata,&error);
 
1158
 
 
1159
    if (ret == -1) 
 
1160
        onError("cannot get mca for scan");
 
1161
 
 
1162
    r_array = (PyArrayObject *)PyArray_SimpleNew(1,&ret,PyArray_DOUBLE);
 
1163
 
 
1164
 
 
1165
    if (mcadata != (double *) NULL){
 
1166
        memcpy(r_array->data,mcadata,PyArray_NBYTES(r_array));
 
1167
        free(mcadata);
 
1168
    }else{
 
1169
        printf("I should give back an empty array\n");
 
1170
    }
 
1171
 
 
1172
/*    return (PyObject *)array; */
 
1173
    
 
1174
    return PyArray_Return(r_array);
 
1175
    /*
 
1176
      it does not work for solaris and linux
 
1177
      I should check the call to PyErr_Occurred()) in Numeric/Src/arrayobject.c
 
1178
      PyArray_Return(array); 
 
1179
     */
 
1180
}
 
1181
 
 
1182
static PyObject   * 
 
1183
scandata_show      (PyObject *self,PyObject *args)
 
1184
{
 
1185
    int idx;
 
1186
 
 
1187
    SpecFile *sf;
 
1188
 
 
1189
    scandataobject *s = (scandataobject *) self;
 
1190
 
 
1191
    idx = s->index;
 
1192
 
 
1193
    if (idx == -1 ) 
 
1194
        onError("empty scan data");
 
1195
    
 
1196
    sf  = (s->file)->sf;
 
1197
 
 
1198
    SfShowScan(sf,idx);
 
1199
 
 
1200
    return Py_BuildValue("l",0);
 
1201
}
 
1202
 
 
1203
 
 
1204
   /*
 
1205
    * Scandata basic python operations
 
1206
    */
 
1207
static PyObject *
 
1208
scandata_new(void) {      /* on x = specfile.Scandata() */
 
1209
    scandataobject *self;
 
1210
 
 
1211
    self = PyObject_NEW(scandataobject,&Scandatatype);
 
1212
 
 
1213
    if (self == NULL)
 
1214
        return NULL;
 
1215
 
 
1216
    self->file  = NULL;
 
1217
    self->index = -1;
 
1218
    self->cols  = 0;
 
1219
 
 
1220
    return (PyObject *)self;
 
1221
}
 
1222
 
 
1223
static PyObject *
 
1224
scandata_free(PyObject *self) {
 
1225
    scandataobject *s =(scandataobject *)self; 
 
1226
    specfileobject *f = s->file; 
 
1227
    Py_DECREF((PyObject *)f);
 
1228
    PyObject_DEL(self);
 
1229
 
 
1230
    return NULL;
 
1231
}
 
1232
 
 
1233
static int
 
1234
scandata_size(PyObject *self) {
 
1235
 
 
1236
    scandataobject *s = (scandataobject *) self;
 
1237
  
 
1238
    return s->cols;
 
1239
}
 
1240
 
 
1241
static PyObject *
 
1242
scandata_col(PyObject *self,int index) {
 
1243
    int     error;
 
1244
    npy_intp ret;
 
1245
    double  *data;
 
1246
 
 
1247
    PyArrayObject *r_array;
 
1248
 
 
1249
    SpecFile *sf;
 
1250
    int      idx,col;
 
1251
 
 
1252
    scandataobject *s = (scandataobject *) self;
 
1253
    
 
1254
 
 
1255
    if ( index < 0 || index > (s->cols - 1) ) {
 
1256
         PyErr_SetString(PyExc_IndexError,"column out of bounds");
 
1257
         return NULL;
 
1258
    }
 
1259
 
 
1260
    sf  = (s->file)->sf;
 
1261
    idx = s->index;
 
1262
    col = index + 1;
 
1263
 
 
1264
    ret = SfDataCol(sf,idx,col,&data,&error);
 
1265
 
 
1266
    if (ret == -1 )
 
1267
          onError("cannot get data for column");
 
1268
 
 
1269
    r_array = (PyArrayObject *)PyArray_SimpleNew(1,&ret,PyArray_DOUBLE);
 
1270
 
 
1271
    if ( r_array == NULL ) 
 
1272
          onError("cannot get memory for array data");
 
1273
 
 
1274
    if (data != (double *) NULL){
 
1275
        memcpy(r_array->data,data,PyArray_NBYTES(r_array));
 
1276
        free(data);
 
1277
    }else{
 
1278
        /* return an empty array? */
 
1279
        printf("I should return an empty array ...\n");
 
1280
                PyArray_FILLWBYTE(r_array, 0);
 
1281
        }
 
1282
 
 
1283
/*    return (PyObject *)array; */
 
1284
/* put back the PyArray_Return call instead of the previous line */
 
1285
    return PyArray_Return(r_array);
 
1286
 
 
1287
    /*
 
1288
      it does not work for solaris and linux
 
1289
      I should check the call to PyErr_Occurred()) in Numeric/Src/arrayobject.c
 
1290
      PyArray_Return(array); 
 
1291
     */
 
1292
}
 
1293
 
 
1294
static PyObject *
 
1295
scandata_slice(PyObject *self,int ilow,int ihigh) {
 
1296
    return NULL;
 
1297
}
 
1298
 
 
1299
static int
 
1300
scandata_print(PyObject *self,FILE *fp,int flags) {
 
1301
    int ok=0;
 
1302
    SpecFile *sf;
 
1303
    int idx;
 
1304
 
 
1305
    scandataobject *s = (scandataobject *) self;
 
1306
 
 
1307
    idx = s->index;
 
1308
    if (idx == -1 ) {
 
1309
        fprintf(fp,"scandata('empty')");
 
1310
    } else {
 
1311
        sf  = (s->file)->sf;
 
1312
        fprintf(fp,"scandata('source: %s,scan: %d.%d')",
 
1313
                                    (s->file)->name,
 
1314
                                    (int) SfNumber(sf,idx), 
 
1315
                                    (int) SfOrder (sf,idx)); 
 
1316
    }
 
1317
    return ok;
 
1318
}
 
1319
 
 
1320
static PyObject *
 
1321
scandata_getattr(PyObject *self,char *name) {
 
1322
 
 
1323
   scandataobject *s = (scandataobject *)self;
 
1324
 
 
1325
   if (strcmp(name,"file") == 0) {
 
1326
        return (PyObject *) s->file;
 
1327
   }
 
1328
 
 
1329
   if (strcmp(name,"index") == 0) {
 
1330
        return Py_BuildValue("l",s->index);
 
1331
   }
 
1332
 
 
1333
   return Py_FindMethod(scandata_methods,self,name);
 
1334
}
 
1335
 
 
1336
 
 
1337
/*
 
1338
 * Type descriptors
 
1339
 */
 
1340
static PySequenceMethods scandata_as_sequence = {
 
1341
  scandata_size,       /*     length len(sf)     */
 
1342
  0,              /*     concat sf1 + sf2   */
 
1343
  0,              /*     repeat sf * n      */
 
1344
  scandata_col,        /*     item  sf[i], in    */
 
1345
  scandata_slice,      /*     slice sf[i:j]      */
 
1346
  0,              /*     asset sf[i] = v    */
 
1347
  0,              /* slice ass. sf[i:j] = v */
 
1348
};
 
1349
 
 
1350
static PyTypeObject Scandatatype = {
 
1351
    /* type header */   
 
1352
#ifdef WIN32
 
1353
       PyObject_HEAD_INIT(NULL)
 
1354
#else
 
1355
       PyObject_HEAD_INIT(&PyType_Type)
 
1356
#endif       
 
1357
       0,
 
1358
       "scandata",
 
1359
       sizeof(scandataobject),
 
1360
       0,
 
1361
 
 
1362
    /* standard methods */
 
1363
       (destructor)   scandata_free,     /*  tp-dealloc   ref-count = 0  */
 
1364
       (printfunc)    scandata_print,    /*  tp-print     print sf       */
 
1365
       (getattrfunc)  scandata_getattr,  /*  tp-getattr   sf.attr        */
 
1366
       (setattrfunc)  0,                 /*  tp-setattr   sf.attr = v    */
 
1367
       (cmpfunc)      0,                 /*  tp-setattr   sf1 > sf2      */
 
1368
       (reprfunc)     0,                 /*  tp-repr      'sf', print sf */
 
1369
 
 
1370
    /* type categories */
 
1371
       0,                                /* tp as number   +,-,....      */
 
1372
       &scandata_as_sequence,            /* tp as sequence +,[i],[i:j]...*/
 
1373
       0,                                /* tp as mapping [key], len, ...*/
 
1374
 
 
1375
    /* more methods */
 
1376
       (hashfunc)     0,                 /* tp_hash  dict(sf) */
 
1377
      /* (binaryfunc)   0,                 tp_call  sf()     */
 
1378
      /* (reprfunc)     0,                 tp_str   str(x)   */
 
1379
};
 
1380
 
 
1381
/* end scandata */
 
1382
 
 
1383
 
 
1384
/*
 
1385
 * Class creators
 
1386
 */
 
1387
static struct PyMethodDef specfiletype_methods[] = {
 
1388
   {"Specfile",  specfiletype_new,  1},
 
1389
   {"Scandata",  scandatatype_new,  1},
 
1390
   { NULL, NULL}
 
1391
};
 
1392
 
 
1393
 
 
1394
static PyObject *
 
1395
specfiletype_new(self,args)
 
1396
  PyObject *self;
 
1397
  PyObject *args;
 
1398
{
 
1399
   PyObject *ret;
 
1400
   char *filename;
 
1401
 
 
1402
   if (!PyArg_ParseTuple(args,"s",&filename)) 
 
1403
      return NULL;
 
1404
   ret = (PyObject *)specfile_open(filename);
 
1405
 
 
1406
   return ret;
 
1407
 
 
1408
}
 
1409
 
 
1410
static PyObject *
 
1411
scandatatype_new(self,args)
 
1412
  PyObject *self;
 
1413
  PyObject *args;
 
1414
{
 
1415
   PyObject *ret;
 
1416
 
 
1417
   if (!PyArg_ParseTuple(args,"")) 
 
1418
      return NULL;
 
1419
   ret = (PyObject *)scandata_new();
 
1420
   return ret;
 
1421
 
 
1422
}
 
1423
 
 
1424
/*
 
1425
 * Module init
 
1426
 */
 
1427
DL_EXPORT(void)
 
1428
initspecfile(void) {
 
1429
    PyObject *m,*d;
 
1430
 
 
1431
    Specfiletype.ob_type = &PyType_Type;
 
1432
    Scandatatype.ob_type = &PyType_Type;
 
1433
    m = Py_InitModule("specfile",specfiletype_methods);
 
1434
    
 
1435
/*    printf("Loading test specfile module\n");*/
 
1436
 
 
1437
    import_array();
 
1438
 
 
1439
    d = PyModule_GetDict(m);
 
1440
    ErrorObject = Py_BuildValue("s","specfile.error");
 
1441
    PyDict_SetItemString(d,"error",ErrorObject);
 
1442
 
 
1443
    if (PyErr_Occurred())
 
1444
        Py_FatalError("can't initialize module specfile");
 
1445
}
 
1446
 
 
1447
 
 
1448
 
 
1449
/*
 
1450
 * Utility functions
 
1451
 */
 
1452
static char *
 
1453
compList(long *nolist,long howmany)
 
1454
{
 
1455
     long this,colon;
 
1456
     char buf[30];
 
1457
     static char str[5000];
 
1458
     char *retstr;
 
1459
 
 
1460
     if (howmany < 1) { return((char *)NULL);}
 
1461
 
 
1462
     sprintf(buf,"%d",(int) nolist[0]);
 
1463
 
 
1464
     *str = '\0';
 
1465
 
 
1466
     strcat(str,buf); 
 
1467
 
 
1468
     colon=0;
 
1469
     for(this=1;this<howmany;this++) {
 
1470
         if ((nolist[this] - nolist[this-1]) == 1) {
 
1471
            colon = 1;
 
1472
         } else {
 
1473
            if (colon) {
 
1474
               sprintf(buf,":%d,%d",(int) nolist[this-1],(int) nolist[this]);
 
1475
               colon=0;
 
1476
            } else {
 
1477
               sprintf(buf,",%d",(int) nolist[this]);
 
1478
            }
 
1479
            strcat(str,buf); 
 
1480
         }
 
1481
     }
 
1482
 
 
1483
     if (howmany != 1 ) {
 
1484
        if (colon) {
 
1485
           sprintf(buf,":%d",(int) nolist[howmany-1]);
 
1486
           strcat(str,buf); 
 
1487
        }
 
1488
     }
 
1489
     retstr = (char *)strdup(str);
 
1490
     return(retstr);
 
1491
}