1
#/*##########################################################################
2
# Copyright (C) 2004-2008 European Synchrotron Radiation Facility
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.
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)
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
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.
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.
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
/************************************************************************
32
* Project: SpecFile library
34
* Description: python interface to Specfile library
38
* Date: $Date: 2005/05/25 13:30:16 $
40
************************************************************************/
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
46
* Log: Revision 1.8 2005/05/18 10:47:30 sole
47
* Log: Problem with ifdef solved
49
* Log: Revision 1.7 2004/05/21 12:33:02 sole
50
* Log: Working windows version
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)
56
* Log: Revision 1.5 2002/11/15 10:11:19 sole
57
* Log: Memory leak corrected in scandata_data
59
* Log: Revision 1.4 2002/11/15 09:29:23 sole
60
* Log: PyArray_Return put back
61
* Log: Several memory leaks removed.
63
* Log: Revision 1.3 2002/11/12 13:11:29 sole
64
* Log: <= -1 replaced by == -1
67
* New command for Scandata: motorpos()
68
* interfaces with the library function
71
* Log: Revision 3.0 2000/12/20 14:17:19 rey
72
* Log: Python version available
74
* Log: Revision 2.2 2000/12/20 12:12:08 rey
75
* Log: bug corrected with SfAllMotors
77
* Revision 2.1 2000/07/31 19:03:11 19:03:11 rey (Vicente Rey-Bakaikoa)
78
* SfUpdate and bug corrected in ReadIndex
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
83
* Revision 1.1 99/10/19 15:28:32 15:28:32 rey (Vicente Rey-Bakaikoa)
88
/************************************************************************
90
Copyright 1999 by European Synchrotron Radiation Facility, Grenoble,
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.
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.
117
**************************************************************************/
132
#include <numpy/arrayobject.h>
133
#include <SpecFile.h>
136
* Specfile exceptions
138
static PyObject *ErrorObject;
140
/* except specfile.error */
141
#define onError(message) \
142
{PyErr_SetString (ErrorObject,message); return NULL; }
156
specfileobject *file;
161
staticforward PyTypeObject Specfiletype;
162
staticforward PyTypeObject Scandatatype;
164
#define is_specfileobject(v) ((v)->type == &Specfiletype)
165
#define is_scandataobject(v) ((v)->type == &Scandatatype)
169
* Function prototypes
174
static char * compList(long *nolist,long howmany);
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);
190
static struct PyMethodDef specfile_methods[] =
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},
206
* Specfile python basic operations
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 */
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);
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},
263
* Scandata python basic operation
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 */
278
void initspecfile(void);
283
static PyObject *specfiletype_new(PyObject *self,PyObject *args);
284
static PyObject *scandatatype_new(PyObject *self,PyObject *args);
290
*************************************************/
295
specfile_list(PyObject *self,PyObject *args)
303
specfileobject *v = (specfileobject *) self;
305
if (!PyArg_ParseTuple(args, ""))
308
no_scans = SfScanNo(v->sf);
309
scanlist = SfList(v->sf,&error);
311
if ( scanlist == NULL || no_scans == 0) {
312
PyErr_SetString(PyExc_TypeError,"Cannot get scan list for file");
315
strlist = (char *)compList(scanlist,no_scans);
316
pstr = Py_BuildValue("s",strlist);
323
specfile_allmotors(PyObject *self,PyObject *args)
331
specfileobject *v = (specfileobject *) self;
333
if (!PyArg_ParseTuple(args, ""))
336
nb_motors = SfAllMotors(v->sf,1,&motornames,&error);
338
if ( nb_motors == -1 )
339
onError("cannot get motor names for specfile");
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);
351
specfile_title(PyObject *self,PyObject *args)
357
specfileobject *v = (specfileobject *) self;
359
if (!PyArg_ParseTuple(args, ""))
362
title = SfTitle(v->sf,1,&error);
365
onError("cannot get title for specfile")
367
pyo = Py_BuildValue("s",title);
373
specfile_user(PyObject *self,PyObject *args)
378
specfileobject *v = (specfileobject *) self;
380
if (!PyArg_ParseTuple(args, ""))
383
user = SfUser(v->sf,1,&error);
387
return Py_BuildValue("s",user);
389
onError("cannot get user for specfile");
394
specfile_date(PyObject *self,PyObject *args)
400
specfileobject *v = (specfileobject *) self;
402
if (!PyArg_ParseTuple(args, ""))
405
date = SfFileDate(v->sf,1,&error);
408
onError("cannot get data for specfile")
410
pyo = Py_BuildValue("s",date);
416
specfile_epoch(PyObject *self,PyObject *args)
421
specfileobject *v = (specfileobject *) self;
423
if (!PyArg_ParseTuple(args, ""))
426
epoch = SfEpoch(v->sf,1,&error);
429
return Py_BuildValue("l",epoch);
431
onError("cannot get epoch for specfile");
436
specfile_update(PyObject *self,PyObject *args)
441
specfileobject *v = (specfileobject *) self;
443
if (!PyArg_ParseTuple(args, ""))
446
ret = SfUpdate(v->sf,&error);
448
v->length = SfScanNo(v->sf);
450
return(Py_BuildValue("i",ret));
454
specfile_scanno(PyObject *self,PyObject *args)
457
specfileobject *v = (specfileobject *) self;
460
if (!PyArg_ParseTuple(args, ""))
465
return Py_BuildValue("l",scanno);
469
specfile_select(PyObject *self,PyObject *args)
472
int n,number,order,index;
477
specfileobject *f = (specfileobject *)self;
479
if (!PyArg_ParseTuple(args,"s",&scanstr)) {
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;
487
index = SfIndex(f->sf,number,order);
490
onError("scan not found");
492
v = PyObject_NEW(scandataobject,&Scandatatype);
499
v->cols = SfNoColumns(f->sf,v->index,&error);
503
return (PyObject *) v;
507
specfile_show(PyObject *self,PyObject *args)
510
specfileobject *f = (specfileobject *)self;
514
return (Py_BuildValue("l",0));
519
* Basic specfiletype operations
522
specfile_open(char *filename) { /* on x = specfile.Specfile(name) */
523
specfileobject *self;
527
self = PyObject_NEW(specfileobject,&Specfiletype);
532
if (( sf = SfOpen(filename,&error)) == NULL )
533
onError("cannot open file");
536
self->length = SfScanNo(sf);
537
self->name = (char *)strdup(filename);
538
strcpy(self->name,filename);
540
/* Py_INCREF(self); */
541
return (PyObject *)self;
545
specfile_close(PyObject *self) {
547
specfileobject *f = (specfileobject *) self;
559
* Sequence type methods
562
specfile_noscans(PyObject *self) {
565
specfileobject *f = (specfileobject *)self;
566
no_scans = f->length;
573
specfile_scan(PyObject *self,int index) {
577
specfileobject *f = (specfileobject *)self;
579
if ( index < 0 || index >= f->length) {
580
PyErr_SetString(PyExc_IndexError,"scan out of bounds");
584
v = PyObject_NEW(scandataobject,&Scandatatype);
591
v->cols = SfNoColumns(f->sf,v->index,&error);
595
return (PyObject *) v;
600
specfile_print(PyObject *self,FILE *fp,int flags) {
602
specfileobject *f = (specfileobject *)self;
603
fprintf(fp,"specfile('%s')", f->name);
608
specfile_getattr(PyObject *self,char *name) {
609
return Py_FindMethod(specfile_methods,self,name);
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 */
626
static PyTypeObject Specfiletype = {
629
PyObject_HEAD_INIT(NULL)
631
PyObject_HEAD_INIT(&PyType_Type)
635
sizeof(specfileobject),
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 */
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, ...*/
652
(hashfunc) 0, /* tp_hash dict(sf) */
653
/* (binaryfunc) 0, tp_call sf() */
654
/* (reprfunc) 0, tp_str str(x) */
657
/* end specfile class */
662
scandata_data(PyObject *self,PyObject *args) {
669
npy_intp dimensions[2];
674
PyArrayObject *r_array;
676
scandataobject *s = (scandataobject *) self;
681
if (!PyArg_ParseTuple(args,"") )
682
onError("wrong arguments for data");
683
ret = SfData(sf,idx,&data,&data_info,&error);
685
onError("cannot read data");
687
/* printf("DATA: %d rows / %d columns\n", data_info[1], data_info[0]);*/
689
dimensions[0] = data_info[1];
690
dimensions[1] = data_info[0];
691
r_array = (PyArrayObject *)PyArray_SimpleNew(2,dimensions,PyArray_DOUBLE);
695
* I should write a specfile function that copies all data in a
696
* single pointer array
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];
704
/* memcpy(array->data,data,PyArray_NBYTES(array)); */
706
freeArrNZ((void ***)&data,data_info[ROW]);
708
if (data != (double **) NULL) {
711
/* return (PyObject *)array; */
712
return PyArray_Return(r_array);
716
scandata_dataline(PyObject *self,PyObject *args) {
722
PyArrayObject *r_array;
727
scandataobject *s = (scandataobject *) self;
732
if (!PyArg_ParseTuple(args,"i",&lineno))
733
onError("cannot decode arguments for line data");
735
ret = SfDataLine(sf,idx,lineno,&data,&error);
738
onError("cannot get data for line");
740
r_array = (PyArrayObject *)PyArray_SimpleNew(1,&ret,PyArray_DOUBLE);
742
memcpy(r_array->data,data,PyArray_NBYTES(r_array));
744
return (PyObject *)r_array;
748
scandata_datacol(PyObject *self,PyObject *args) {
755
PyArrayObject *r_array;
759
scandataobject *s = (scandataobject *) self;
764
if (!PyArg_ParseTuple(args,"i",&colno)) {
766
if (!PyArg_ParseTuple(args,"s",&colname)) {
767
onError("cannot decode arguments for column data");
769
ret = SfDataColByName(sf,idx,colname,&data,&error);
772
ret = SfDataCol(sf,idx,colno,&data,&error);
776
onError("cannot get data for column");
778
r_array = (PyArrayObject *)PyArray_SimpleNew(1,&ret,PyArray_DOUBLE);
780
if (data != (double *) NULL){
781
memcpy(r_array->data,data,PyArray_NBYTES(r_array));
784
/* return an empty array? */
785
printf("I should return an empty array ...\n");
786
PyArray_FILLWBYTE(r_array, 0);
789
return PyArray_Return(r_array);
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);
799
scandata_alllabels (PyObject *self,PyObject *args)
806
scandataobject *v = (scandataobject *) self;
808
if (!PyArg_ParseTuple(args, ""))
811
nb_labels = SfAllLabels((v->file)->sf,v->index,&labels,&error);
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);
823
scandata_allmotorpos (PyObject *self,PyObject *args)
830
scandataobject *v = (scandataobject *) self;
832
if (!PyArg_ParseTuple(args, ""))
835
nb_motors = SfAllMotorPos((v->file)->sf,v->index,&motorpos,&error);
837
t = PyList_New(nb_motors);
839
for ( i = 0 ;i<nb_motors;i++) {
840
x = PyFloat_FromDouble(motorpos[i]);
841
PyList_SetItem(t,i,x);
848
scandata_motorpos (PyObject *self,PyObject *args)
854
scandataobject *v = (scandataobject *) self;
856
if (!PyArg_ParseTuple(args,"s",&motorname)) {
860
motorpos = SfMotorPosByName((v->file)->sf,v->index,motorname,&error);
862
if (motorpos != HUGE_VAL) {
863
return Py_BuildValue("f",motorpos);
865
onError("cannot get position for motor");
871
scandata_hkl (PyObject *self,PyObject *args)
879
scandataobject *s = (scandataobject *) self;
883
onError("empty scan data");
887
hkl = SfHKL(sf,idx,&error);
890
onError("cannot get data for column");
892
pyo = Py_BuildValue("ddd",hkl[0],hkl[1],hkl[2]);
899
scandata_number (PyObject *self,PyObject *args)
904
scandataobject *s = (scandataobject *) self;
909
number = SfNumber(sf,idx);
911
return Py_BuildValue("i",number);
916
scandata_order (PyObject *self,PyObject *args)
921
scandataobject *s = (scandataobject *) self;
926
order = SfOrder(sf,idx);
928
return Py_BuildValue("i",order);
933
scandata_command (PyObject *self,PyObject *args)
941
scandataobject *s = (scandataobject *) self;
945
onError("empty scan data");
949
command = SfCommand(sf,idx,&error);
952
onError("cannot get command for scan")
954
pyo = Py_BuildValue("s",command);
961
scandata_date (PyObject *self,PyObject *args)
969
scandataobject *s = (scandataobject *) self;
973
onError("empty scan data");
977
date = SfDate(sf,idx,&error);
980
onError("cannot get date for scan");
982
pyo = Py_BuildValue("s",date);
989
scandata_cols (PyObject *self,PyObject *args)
993
scandataobject *s = (scandataobject *) self;
997
onError("empty scan data");
1002
onError("cannot get cols for scan");
1004
return Py_BuildValue("l",cols);
1008
scandata_lines (PyObject *self,PyObject *args)
1010
int lines,idx,error;
1014
scandataobject *s = (scandataobject *) self;
1018
onError("empty scan data");
1022
lines = SfNoDataLines(sf,idx,&error);
1024
/*if (lines == -1){*/
1026
onError("cannot get lines for scan");
1030
return Py_BuildValue("l",lines);
1034
scandata_fileheader (PyObject *self,PyObject *args)
1036
int i,no_lines,idx,error;
1043
scandataobject *s = (scandataobject *) self;
1045
if (!PyArg_ParseTuple(args,"s",&searchstr))
1050
onError("empty scan data");
1054
no_lines = SfFileHeader(sf,idx,searchstr,&lines,&error);
1057
onError("cannot get lines for scan");
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);
1067
return Py_BuildValue("l",no_lines);
1071
scandata_header (PyObject *self,PyObject *args)
1073
int i,no_lines,idx,error;
1080
scandataobject *s = (scandataobject *) self;
1082
if (!PyArg_ParseTuple(args,"s",&searchstr))
1087
onError("empty scan data");
1091
no_lines = SfHeader(sf,idx,searchstr,&lines,&error);
1094
onError("cannot get lines for scan");
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);
1104
return Py_BuildValue("l",no_lines);
1108
scandata_nbmca (PyObject *self,PyObject *args)
1110
int nomca,idx,error;
1115
scandataobject *s = (scandataobject *) self;
1119
onError("empty scan data");
1123
nomca = SfNoMca(sf,idx,&error);
1126
onError("cannot get number of mca for scan");
1128
pyo = Py_BuildValue("l",nomca);
1133
scandata_mca (PyObject *self,PyObject *args)
1139
double *mcadata = NULL;
1140
PyArrayObject *r_array;
1144
scandataobject *s = (scandataobject *) self;
1146
if (!PyArg_ParseTuple(args,"l",&mcano))
1147
onError("cannot decode arguments for line data");
1152
onError("empty scan data");
1157
ret = SfGetMca(sf,idx,mcano,&mcadata,&error);
1160
onError("cannot get mca for scan");
1162
r_array = (PyArrayObject *)PyArray_SimpleNew(1,&ret,PyArray_DOUBLE);
1165
if (mcadata != (double *) NULL){
1166
memcpy(r_array->data,mcadata,PyArray_NBYTES(r_array));
1169
printf("I should give back an empty array\n");
1172
/* return (PyObject *)array; */
1174
return PyArray_Return(r_array);
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);
1183
scandata_show (PyObject *self,PyObject *args)
1189
scandataobject *s = (scandataobject *) self;
1194
onError("empty scan data");
1200
return Py_BuildValue("l",0);
1205
* Scandata basic python operations
1208
scandata_new(void) { /* on x = specfile.Scandata() */
1209
scandataobject *self;
1211
self = PyObject_NEW(scandataobject,&Scandatatype);
1220
return (PyObject *)self;
1224
scandata_free(PyObject *self) {
1225
scandataobject *s =(scandataobject *)self;
1226
specfileobject *f = s->file;
1227
Py_DECREF((PyObject *)f);
1234
scandata_size(PyObject *self) {
1236
scandataobject *s = (scandataobject *) self;
1242
scandata_col(PyObject *self,int index) {
1247
PyArrayObject *r_array;
1252
scandataobject *s = (scandataobject *) self;
1255
if ( index < 0 || index > (s->cols - 1) ) {
1256
PyErr_SetString(PyExc_IndexError,"column out of bounds");
1264
ret = SfDataCol(sf,idx,col,&data,&error);
1267
onError("cannot get data for column");
1269
r_array = (PyArrayObject *)PyArray_SimpleNew(1,&ret,PyArray_DOUBLE);
1271
if ( r_array == NULL )
1272
onError("cannot get memory for array data");
1274
if (data != (double *) NULL){
1275
memcpy(r_array->data,data,PyArray_NBYTES(r_array));
1278
/* return an empty array? */
1279
printf("I should return an empty array ...\n");
1280
PyArray_FILLWBYTE(r_array, 0);
1283
/* return (PyObject *)array; */
1284
/* put back the PyArray_Return call instead of the previous line */
1285
return PyArray_Return(r_array);
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);
1295
scandata_slice(PyObject *self,int ilow,int ihigh) {
1300
scandata_print(PyObject *self,FILE *fp,int flags) {
1305
scandataobject *s = (scandataobject *) self;
1309
fprintf(fp,"scandata('empty')");
1312
fprintf(fp,"scandata('source: %s,scan: %d.%d')",
1314
(int) SfNumber(sf,idx),
1315
(int) SfOrder (sf,idx));
1321
scandata_getattr(PyObject *self,char *name) {
1323
scandataobject *s = (scandataobject *)self;
1325
if (strcmp(name,"file") == 0) {
1326
return (PyObject *) s->file;
1329
if (strcmp(name,"index") == 0) {
1330
return Py_BuildValue("l",s->index);
1333
return Py_FindMethod(scandata_methods,self,name);
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 */
1350
static PyTypeObject Scandatatype = {
1353
PyObject_HEAD_INIT(NULL)
1355
PyObject_HEAD_INIT(&PyType_Type)
1359
sizeof(scandataobject),
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 */
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, ...*/
1376
(hashfunc) 0, /* tp_hash dict(sf) */
1377
/* (binaryfunc) 0, tp_call sf() */
1378
/* (reprfunc) 0, tp_str str(x) */
1387
static struct PyMethodDef specfiletype_methods[] = {
1388
{"Specfile", specfiletype_new, 1},
1389
{"Scandata", scandatatype_new, 1},
1395
specfiletype_new(self,args)
1402
if (!PyArg_ParseTuple(args,"s",&filename))
1404
ret = (PyObject *)specfile_open(filename);
1411
scandatatype_new(self,args)
1417
if (!PyArg_ParseTuple(args,""))
1419
ret = (PyObject *)scandata_new();
1428
initspecfile(void) {
1431
Specfiletype.ob_type = &PyType_Type;
1432
Scandatatype.ob_type = &PyType_Type;
1433
m = Py_InitModule("specfile",specfiletype_methods);
1435
/* printf("Loading test specfile module\n");*/
1439
d = PyModule_GetDict(m);
1440
ErrorObject = Py_BuildValue("s","specfile.error");
1441
PyDict_SetItemString(d,"error",ErrorObject);
1443
if (PyErr_Occurred())
1444
Py_FatalError("can't initialize module specfile");
1453
compList(long *nolist,long howmany)
1457
static char str[5000];
1460
if (howmany < 1) { return((char *)NULL);}
1462
sprintf(buf,"%d",(int) nolist[0]);
1469
for(this=1;this<howmany;this++) {
1470
if ((nolist[this] - nolist[this-1]) == 1) {
1474
sprintf(buf,":%d,%d",(int) nolist[this-1],(int) nolist[this]);
1477
sprintf(buf,",%d",(int) nolist[this]);
1483
if (howmany != 1 ) {
1485
sprintf(buf,":%d",(int) nolist[howmany-1]);
1489
retstr = (char *)strdup(str);