-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsdf_python.c
More file actions
1963 lines (1637 loc) · 55.6 KB
/
sdf_python.c
File metadata and controls
1963 lines (1637 loc) · 55.6 KB
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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright Notice and License Terms for
* SDF (Self-Describing Format) Python reader
*
* Copyright (c) 2011-2016, SDF Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the SDF Development Team nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <float.h>
#include <Python.h>
#include <numpy/arrayobject.h>
#include <structmember.h>
#include <stdlib.h>
#include "sdf.h"
#include "sdf_extension.h"
#include "sdf_helper.h"
#include "stack_allocator.h"
#include "commit_info.h"
#ifdef _MSC_VER
# include <windows.h>
# include <fileapi.h>
# include <limits.h>
# define PATH_MAX MAX_PATH
#endif
/* Backwards compatibility */
#if PY_MAJOR_VERSION < 3
#define PyInt_FromLong PyLong_FromLong
#define PyASCII_FromString PyString_FromString
#else
PyObject *PyASCII_FromString(char *str)
{
PyObject *ob = PyUnicode_FromString(str);
if (ob == NULL) {
PyErr_Clear();
ob = PyUnicode_FromString("");
}
return ob;
}
#endif
#ifndef NPY_ARRAY_F_CONTIGUOUS
#define NPY_ARRAY_F_CONTIGUOUS NPY_F_CONTIGUOUS
#endif
#ifndef PyVarObject_HEAD_INIT
#define PyVarObject_HEAD_INIT(type, size) \
PyObject_HEAD_INIT(type) size,
#endif
#ifndef PyArray_SetBaseObject
#define PyArray_SetBaseObject(array, base) \
PyArray_BASE(array) = base
#endif
#if PY_MAJOR_VERSION >= 3
#define MOD_ERROR_VAL NULL
#define MOD_SUCCESS_VAL(val) val
#define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
#define MOD_DEF(ob, name, doc, methods) \
static struct PyModuleDef moduledef = { \
PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \
ob = PyModule_Create(&moduledef);
#else
#define MOD_ERROR_VAL
#define MOD_SUCCESS_VAL(val)
#define MOD_INIT(name) void init##name(void)
#define MOD_DEF(ob, name, doc, methods) \
ob = Py_InitModule3(name, methods, doc);
#endif
#define IJ(i,j) ((i) + (j) * (block->adims[0] + 1))
int sdf_free_block_data(sdf_file_t *h, sdf_block_t *b);
static const int typemap[] = {
0,
NPY_INT32,
NPY_INT64,
NPY_FLOAT,
NPY_DOUBLE,
#ifdef NPY_FLOAT128
NPY_FLOAT128,
#else
0,
#endif
NPY_STRING,
NPY_BOOL,
NPY_VOID,
};
typedef struct {
PyObject_HEAD
sdf_file_t *h;
PyObject *blocklist;
} SDFObject;
typedef struct {
PyObject_HEAD
SDFObject *sdf;
sdf_block_t *b;
void **mem;
int memlen;
} ArrayObject;
typedef struct Block_struct Block;
struct Block_struct {
PyObject_HEAD
PyObject *id;
PyObject *name;
PyObject *data_length;
PyObject *datatype;
PyObject *dims;
PyObject *data;
PyObject *labels;
PyObject *units;
PyObject *extents;
PyObject *geometry;
PyObject *species_id;
PyObject *mult;
PyObject *stagger;
PyObject *dict;
PyObject *material_names;
PyObject *material_ids;
PyObject *grid_id;
PyObject *blocklist;
Block *grid;
Block *grid_mid;
Block *parent;
SDFObject *sdf;
sdf_block_t *b;
int dim, ndims;
int sdfref;
npy_intp adims[4];
};
typedef struct {
PyObject_HEAD
PyObject *dict;
} BlockList;
static PyTypeObject ArrayType = {
PyVarObject_HEAD_INIT(NULL, 0)
"Array", /* tp_name */
sizeof(ArrayObject), /* tp_basicsize */
0, /* tp_itemsize */
};
static PyTypeObject BlockType = {
PyVarObject_HEAD_INIT(NULL, 0)
"sdf.Block", /* tp_name */
sizeof(Block), /* tp_basicsize */
0, /* tp_itemsize */
};
static PyTypeObject BlockListType = {
PyVarObject_HEAD_INIT(NULL, 0)
"sdf.BlockList", /* tp_name */
sizeof(BlockList), /* tp_basicsize */
0, /* tp_itemsize */
};
static PyTypeObject BlockBase;
static PyTypeObject BlockMeshType;
static PyTypeObject BlockPlainMeshType;
static PyTypeObject BlockPointMeshType;
static PyTypeObject BlockLagrangianMeshType;
static PyTypeObject BlockPlainVariableType;
static PyTypeObject BlockPointVariableType;
static PyTypeObject BlockArrayType;
static PyTypeObject BlockConstantType;
static PyTypeObject BlockStationType;
static PyTypeObject BlockStitchedType;
static PyTypeObject BlockStitchedPathType;
static PyTypeObject BlockStitchedMaterialType;
static PyTypeObject BlockNameValueType;
static PyTypeObject SDFType = {
PyVarObject_HEAD_INIT(NULL, 0)
"sdf.SDF", /* tp_name */
sizeof(SDFObject), /* tp_basicsize */
0, /* tp_itemsize */
};
static inline char *PyBytes_As_C(PyObject *ob)
{
#if PY_MAJOR_VERSION < 3
return strdup(PyString_AsString(ob));
#else
PyObject *ascii = PyUnicode_AsASCIIString(ob);
char *str = strdup(PyBytes_AsString(ascii));
Py_DECREF(ascii);
return str;
#endif
}
/*
* Array type methods
******************************************************************************/
static PyObject *
Array_new(PyTypeObject *type, SDFObject *sdf, sdf_block_t *b)
{
ArrayObject *self;
self = (ArrayObject*)type->tp_alloc(type, 0);
if (self) {
self->sdf = sdf;
self->b = b;
self->mem = NULL;
self->memlen = 0;
Py_INCREF(self->sdf);
}
return (PyObject*)self;
}
static void
Array_dealloc(PyObject *self)
{
ArrayObject *ob = (ArrayObject*)self;
Py_ssize_t n;
if (!ob) return;
if (ob->memlen) {
for (n = 0; n < ob->memlen; n++)
free(ob->mem[n]);
free(ob->mem);
ob->memlen = 0;
} else
sdf_free_block_data(ob->sdf->h, ob->b);
Py_XDECREF(ob->sdf);
self->ob_type->tp_free(self);
}
/*
* BlockList type methods
******************************************************************************/
static PyMemberDef BlockList_members[] = {
{"__dict__", T_OBJECT, offsetof(BlockList, dict), READONLY},
{0}
};
static void
BlockList_dealloc(PyObject *self)
{
BlockList *ob = (BlockList*)self;
Py_XDECREF(ob->dict);
self->ob_type->tp_free(self);
}
/*
* Block type methods
******************************************************************************/
static PyMemberDef Block_members[] = {
{"id", T_OBJECT_EX, offsetof(Block, id), 0, "Block id"},
{"name", T_OBJECT_EX, offsetof(Block, name), 0, "Block name"},
{"data_length", T_OBJECT_EX, offsetof(Block, data_length), 0, "Data size"},
{"datatype", T_OBJECT_EX, offsetof(Block, datatype), 0, "Data type"},
{"dims", T_OBJECT_EX, offsetof(Block, dims), 0, "Data dimensions"},
{"data", T_OBJECT_EX, offsetof(Block, data), 0, "Block data contents"},
{"blocklist", T_OBJECT_EX, offsetof(Block, blocklist), 0, "Blocklist"},
{"__dict__", T_OBJECT_EX, offsetof(Block, dict), READONLY},
{NULL} /* Sentinel */
};
static PyMemberDef BlockMesh_members[] = {
{"labels", T_OBJECT_EX, offsetof(Block, labels), 0, "Axis labels"},
{"units", T_OBJECT_EX, offsetof(Block, units), 0, "Axis units"},
{"extents", T_OBJECT_EX, offsetof(Block, extents), 0, "Axis extents"},
{"geometry", T_OBJECT_EX, offsetof(Block, geometry), 0, "Domain geometry"},
{"mult", T_OBJECT_EX, offsetof(Block, mult), 0, "Multiplication factors"},
{NULL} /* Sentinel */
};
static PyMemberDef BlockMeshVariable_members[] = {
{"grid", T_OBJECT_EX, offsetof(Block, grid), 0, "Associated mesh"},
{"grid_mid", T_OBJECT_EX, offsetof(Block, grid_mid), 0,
"Associated median mesh"},
{"grid_id", T_OBJECT_EX, offsetof(Block, grid_id), 0, "Associated mesh id"},
{"stagger", T_OBJECT_EX, offsetof(Block, stagger), 0, "Grid stagger"},
{"units", T_OBJECT_EX, offsetof(Block, units), 0, "Units of variable"},
{"mult", T_OBJECT_EX, offsetof(Block, mult), 0, "Multiplication factor"},
{NULL} /* Sentinel */
};
static PyMemberDef BlockPointVariable_members[] = {
{"grid", T_OBJECT_EX, offsetof(Block, grid), 0, "Associated mesh"},
{"grid_mid", T_OBJECT_EX, offsetof(Block, grid_mid), 0,
"Associated median mesh"},
{"grid_id", T_OBJECT_EX, offsetof(Block, grid_id), 0, "Associated mesh id"},
{"units", T_OBJECT_EX, offsetof(Block, units), 0, "Units of variable"},
{"mult", T_OBJECT_EX, offsetof(Block, mult), 0, "Multiplication factor"},
{"species_id", T_OBJECT_EX, offsetof(Block, species_id), 0, "Species ID"},
{NULL} /* Sentinel */
};
static PyMemberDef BlockPlainMesh_members[] = {
{NULL} /* Sentinel */
};
static PyMemberDef BlockPointMesh_members[] = {
{"species_id", T_OBJECT_EX, offsetof(Block, species_id), 0, "Species ID"},
{NULL} /* Sentinel */
};
static PyMemberDef BlockStitchedMaterial_members[] = {
{"material_names", T_OBJECT_EX, offsetof(Block, material_names), 0,
"Material names"},
{"material_ids", T_OBJECT_EX, offsetof(Block, material_ids), 0,
"Material IDs"},
{"grid", T_OBJECT_EX, offsetof(Block, grid), 0, "Associated mesh"},
{"grid_mid", T_OBJECT_EX, offsetof(Block, grid_mid), 0,
"Associated median mesh"},
{"grid_id", T_OBJECT_EX, offsetof(Block, grid_id), 0, "Associated mesh id"},
{"stagger", T_OBJECT_EX, offsetof(Block, stagger), 0, "Grid stagger"},
{NULL} /* Sentinel */
};
static PyObject *Block_getdata(Block *block, void *closure);
static int Block_setdata(Block *block, PyObject *value, void *closure);
static PyGetSetDef Block_getset[] = {
{"data", (getter)Block_getdata, (setter)Block_setdata,
"Block data contents", NULL},
{NULL} /* Sentinel */
};
static PyObject *
Block_alloc(SDFObject *sdf, sdf_block_t *b)
{
Block *ob;
PyTypeObject *type;
Py_ssize_t i;
if (!b->datatype_out && b->blocktype != SDF_BLOCKTYPE_STITCHED)
return NULL;
switch(b->blocktype) {
case SDF_BLOCKTYPE_PLAIN_MESH:
type = &BlockPlainMeshType;
break;
case SDF_BLOCKTYPE_POINT_MESH:
type = &BlockPointMeshType;
break;
case SDF_BLOCKTYPE_LAGRANGIAN_MESH:
type = &BlockLagrangianMeshType;
break;
case SDF_BLOCKTYPE_PLAIN_VARIABLE:
case SDF_BLOCKTYPE_PLAIN_DERIVED:
type = &BlockPlainVariableType;
break;
case SDF_BLOCKTYPE_POINT_VARIABLE:
case SDF_BLOCKTYPE_POINT_DERIVED:
type = &BlockPointVariableType;
break;
case SDF_BLOCKTYPE_ARRAY:
type = &BlockArrayType;
break;
case SDF_BLOCKTYPE_CONSTANT:
type = &BlockConstantType;
break;
case SDF_BLOCKTYPE_STATION:
type = &BlockStationType;
break;
case SDF_BLOCKTYPE_STITCHED:
if (b->stagger == 10 || b->stagger == 12)
type = &BlockStitchedPathType;
else
type = &BlockStitchedType;
break;
case SDF_BLOCKTYPE_STITCHED_MATERIAL:
case SDF_BLOCKTYPE_CONTIGUOUS_MATERIAL:
type = &BlockStitchedMaterialType;
break;
case SDF_BLOCKTYPE_NAMEVALUE:
type = &BlockNameValueType;
break;
default:
type = &BlockType;
}
ob = (Block *)type->tp_alloc(type, 0);
ob->sdf = sdf;
ob->b = b;
ob->dim = 0;
ob->ndims = b->ndims;
ob->sdfref = 0;
if (b->id) {
ob->id = PyASCII_FromString(b->id);
if (!ob->id) goto error;
}
if (b->name) {
ob->name = PyASCII_FromString(b->name);
if (!ob->name) goto error;
}
if (b->mesh_id) {
ob->grid_id = PyASCII_FromString(b->mesh_id);
if (!ob->grid_id) goto error;
}
ob->blocklist = sdf->blocklist;
if (!ob->blocklist) goto error;
ob->data_length = PyLong_FromLongLong(b->data_length);
if (!ob->data_length) goto error;
if (b->datatype_out < 0 || b->datatype_out > sdf_datatype_len) goto error;
ob->datatype = PyArray_TypeObjectFromType(typemap[b->datatype_out]);
if (!ob->datatype) goto error;
if (b->ndims) {
if (b->blocktype == SDF_BLOCKTYPE_NAMEVALUE)
ob->dims = PyTuple_New(1);
else if (b->blocktype == SDF_BLOCKTYPE_STITCHED) {
ob->dims = PyTuple_New(1);
ob->data = PyTuple_New(b->ndims);
} else
ob->dims = PyTuple_New(b->ndims);
if (!ob->dims) goto error;
}
if (b->extents) {
ob->extents = PyTuple_New(2 * b->ndims);
if (!ob->extents) goto error;
for (i=0; i < 2 * b->ndims; i++) {
PyTuple_SetItem(ob->extents, i, PyFloat_FromDouble(b->extents[i]));
}
}
switch(b->blocktype) {
case SDF_BLOCKTYPE_PLAIN_MESH:
case SDF_BLOCKTYPE_POINT_MESH:
case SDF_BLOCKTYPE_LAGRANGIAN_MESH:
ob->geometry = PyLong_FromLong(b->geometry);
if (!ob->geometry) goto error;
case SDF_BLOCKTYPE_PLAIN_VARIABLE:
case SDF_BLOCKTYPE_PLAIN_DERIVED:
case SDF_BLOCKTYPE_POINT_VARIABLE:
case SDF_BLOCKTYPE_POINT_DERIVED:
case SDF_BLOCKTYPE_ARRAY:
ob->sdfref = 1;
Py_INCREF(ob->sdf);
for (i=0; i < b->ndims; i++) {
ob->adims[i] = b->dims[i];
PyTuple_SetItem(ob->dims, i, PyLong_FromLong(b->dims[i]));
}
break;
case SDF_BLOCKTYPE_STITCHED:
case SDF_BLOCKTYPE_NAMEVALUE:
PyTuple_SetItem(ob->dims, 0, PyLong_FromLong(b->ndims));
break;
case SDF_BLOCKTYPE_CONSTANT:
PyTuple_SetItem(ob->dims, 0, PyLong_FromLong(1));
break;
}
switch(b->blocktype) {
case SDF_BLOCKTYPE_POINT_MESH:
case SDF_BLOCKTYPE_POINT_VARIABLE:
case SDF_BLOCKTYPE_POINT_DERIVED:
if (b->material_id) {
ob->species_id = PyASCII_FromString(b->material_id);
if (!ob->species_id) goto error;
}
break;
}
switch(b->blocktype) {
case SDF_BLOCKTYPE_PLAIN_VARIABLE:
case SDF_BLOCKTYPE_PLAIN_DERIVED:
case SDF_BLOCKTYPE_POINT_VARIABLE:
case SDF_BLOCKTYPE_POINT_DERIVED:
if (b->mult) {
ob->mult = PyFloat_FromDouble(b->mult);
if (!ob->mult) goto error;
}
break;
case SDF_BLOCKTYPE_PLAIN_MESH:
case SDF_BLOCKTYPE_POINT_MESH:
if (b->dim_mults) {
ob->mult = PyTuple_New(b->ndims);
if (!ob->mult) goto error;
for (i=0; i < b->ndims; i++) {
PyTuple_SetItem(ob->mult, i,
PyFloat_FromDouble(b->dim_mults[i]));
}
}
break;
}
switch(b->blocktype) {
case SDF_BLOCKTYPE_PLAIN_VARIABLE:
case SDF_BLOCKTYPE_PLAIN_DERIVED:
case SDF_BLOCKTYPE_STITCHED_MATERIAL:
case SDF_BLOCKTYPE_CONTIGUOUS_MATERIAL:
ob->stagger = PyLong_FromLong(b->stagger);
if (!ob->stagger) goto error;
break;
}
return (PyObject *)ob;
error:
Py_DECREF(ob);
return NULL;
}
static void
Block_dealloc(PyObject *self)
{
Block *ob = (Block*)self;
if (!ob) return;
Py_XDECREF(ob->id);
Py_XDECREF(ob->name);
Py_XDECREF(ob->data_length);
Py_XDECREF(ob->datatype);
Py_XDECREF(ob->dims);
Py_XDECREF(ob->extents);
Py_XDECREF(ob->geometry);
Py_XDECREF(ob->species_id);
Py_XDECREF(ob->mult);
Py_XDECREF(ob->stagger);
Py_XDECREF(ob->labels);
Py_XDECREF(ob->units);
Py_XDECREF(ob->dict);
Py_XDECREF(ob->data);
Py_XDECREF(ob->parent);
Py_XDECREF(ob->material_names);
Py_XDECREF(ob->material_ids);
Py_XDECREF(ob->grid_id);
if (ob->sdfref > 0) {
ob->sdfref--;
Py_XDECREF(ob->sdf);
}
/* Object should not free itself unless tp_alloc has been defined */
self->ob_type->tp_free(self);
}
/* FIXME:
* The functions Block_getdata, Block_setdata can give the following
* weird behaviour:
*
* >>> print block.data
* array([0., 1., ..., 100.]
* >>> del block.data
* >>> print block.data
* array([0., 1., ..., 100.]
*
* where we would expect an error, because block.data shouldn't
* exist anymore.
*
* Possible solutions:
* - add a flag 'data_deleted', that is checked by Block_getdata
* before it tries to reload
* - replace 'block.data' with the method 'block.get_data()'
* that returns a numpy array that can just be deleted, without
* any need for Block_setdata.
* Obviously this solution breaks backward compatibility.
*
* AMW - 2015-07-16
*/
static PyObject *Block_getdata(Block *block, void *closure)
{
void *data;
SDFObject *sdf = block->sdf;
sdf_block_t *b = block->b;
PyObject *ob;
ArrayObject *array = NULL;
Py_ssize_t ndims, n;
volatile Py_ssize_t i, j;
npy_intp *dims;
npy_intp adims[1];
void *mem;
int float1d = 0, double1d = 0, float2d = 0, double2d = 0;
/* Already populated numpy array. Just return it. */
if (block->data) {
Py_INCREF(block->data);
return block->data;
}
if (block->parent) {
ob = Block_getdata(block->parent, closure);
Py_DECREF(ob);
}
if (!sdf || !sdf->h || !b)
return PyErr_Format(PyExc_Exception, "Unknown SDF file\n");
sdf->h->current_block = b;
sdf_helper_read_data(sdf->h, b);
if (b->grids && b->grids[0])
data = b->grids[0];
else
data = b->data;
if (!data)
return PyErr_Format(PyExc_Exception, "Unable to read SDF block\n");
dims = block->adims;
ndims = b->ndims;
if (b->grids && b->grids[0]) {
block->data = PyTuple_New(b->ndims);
if (!block->data) goto free_mem;
array = (ArrayObject*)Array_new(&ArrayType, sdf, b);
if (!array) goto free_mem;
if (block->parent) {
array->memlen = b->ndims;
array->mem = malloc(array->memlen * sizeof(*array->mem));
}
if (b->blocktype == SDF_BLOCKTYPE_PLAIN_MESH
|| b->blocktype == SDF_BLOCKTYPE_POINT_MESH) {
ndims = 1;
dims = adims;
if (block->parent) {
if (b->datatype_out == SDF_DATATYPE_REAL4)
float1d = 1;
else
double1d = 1;
}
} else {
if (block->parent) {
if (b->datatype_out == SDF_DATATYPE_REAL4)
float2d = 1;
else
double2d = 1;
}
}
for (n = 0; n < b->ndims; n++) {
data = b->grids[n];
mem = NULL;
if (float1d) {
float v1, v2, *ptr_in, *ptr_out;
Py_ssize_t ntot = block->adims[n];
ptr_in = data;
ptr_out = data = mem = malloc(ntot * sizeof(*ptr_out));
if (!mem) goto free_mem;
for (i = 0; i < ntot; i++) {
v1 = *ptr_in;
v2 = *(ptr_in+1);
*ptr_out++ = 0.5 * (v1 + v2);
ptr_in++;
}
} else if (double1d) {
double v1, v2, *ptr_in, *ptr_out;
Py_ssize_t ntot = block->adims[n];
ptr_in = data;
ptr_out = data = mem = malloc(ntot * sizeof(*ptr_out));
if (!mem) goto free_mem;
for (i = 0; i < ntot; i++) {
v1 = *ptr_in;
v2 = *(ptr_in+1);
*ptr_out++ = 0.5 * (v1 + v2);
ptr_in++;
}
} else if (float2d) {
float *ptr_in, *ptr_out;
Py_ssize_t ntot = block->adims[0] * block->adims[1];
ptr_in = data;
ptr_out = data = mem = malloc(ntot * sizeof(*ptr_out));
if (!mem) goto free_mem;
if (block->adims[0] == 1) {
for (j = 0; j < block->adims[1]; j++) {
*ptr_out++ = 0.5 * (ptr_in[IJ(0,j)]
+ ptr_in[IJ(0,j+1)]);
}
} else if (block->adims[1] == 1) {
for (i = 0; i < block->adims[0]; i++) {
*ptr_out++ = 0.5 * (ptr_in[IJ(i,0)]
+ ptr_in[IJ(i+1,0)]);
}
} else {
for (j = 0; j < block->adims[1]; j++) {
for (i = 0; i < block->adims[0]; i++) {
*ptr_out++ = 0.25 * (ptr_in[IJ(i,j)] + ptr_in[IJ(i+1,j)]
+ ptr_in[IJ(i,j+1)] + ptr_in[IJ(i+1,j+1)]);
}}
}
} else if (double2d) {
double *ptr_in, *ptr_out;
Py_ssize_t ntot = block->adims[0] * block->adims[1];
ptr_in = data;
ptr_out = data = mem = malloc(ntot * sizeof(*ptr_out));
if (!mem) goto free_mem;
if (block->adims[0] == 1) {
for (j = 0; j < block->adims[1]; j++) {
*ptr_out++ = 0.5 * (ptr_in[IJ(0,j)]
+ ptr_in[IJ(0,j+1)]);
}
} else if (block->adims[1] == 1) {
for (i = 0; i < block->adims[0]; i++) {
*ptr_out++ = 0.5 * (ptr_in[IJ(i,0)]
+ ptr_in[IJ(i+1,0)]);
}
} else {
for (j = 0; j < block->adims[1]; j++) {
for (i = 0; i < block->adims[0]; i++) {
*ptr_out++ = 0.25 * (ptr_in[IJ(i,j)] + ptr_in[IJ(i+1,j)]
+ ptr_in[IJ(i,j+1)] + ptr_in[IJ(i+1,j+1)]);
}}
}
}
if (mem)
array->mem[n] = mem;
if (b->blocktype == SDF_BLOCKTYPE_PLAIN_MESH
|| b->blocktype == SDF_BLOCKTYPE_POINT_MESH)
dims[0] = block->adims[n];
ob = PyArray_NewFromDescr(&PyArray_Type,
PyArray_DescrFromType(typemap[b->datatype_out]), ndims,
dims, NULL, data, NPY_ARRAY_F_CONTIGUOUS, NULL);
if (!ob) goto free_mem;
PyTuple_SetItem(block->data, n, ob);
PyArray_SetBaseObject((PyArrayObject*)ob, (PyObject*)array);
Py_INCREF(array);
}
Py_DECREF(array);
} else {
block->data = PyArray_NewFromDescr(&PyArray_Type,
PyArray_DescrFromType(typemap[b->datatype_out]), ndims,
dims, NULL, data, NPY_ARRAY_F_CONTIGUOUS, NULL);
if (!block->data) goto free_mem;
array = (ArrayObject*)Array_new(&ArrayType, sdf, b);
if (!array) goto free_mem;
PyArray_SetBaseObject((PyArrayObject*)block->data, (PyObject*)array);
}
Py_INCREF(block->data);
return block->data;
free_mem:
if (block->data) Py_DECREF(block->data);
if (array) Py_DECREF(array);
sdf_free_block_data(sdf->h, b);
return PyErr_Format(PyExc_Exception, "Error whilst reading SDF block\n");
}
static int
Block_setdata(Block *block, PyObject *value, void *closure)
{
Py_XDECREF(block->data);
if ( value != NULL )
Py_INCREF(value);
block->data = value;
return 0;
}
/*
* SDF type methods
******************************************************************************/
static void
SDF_dealloc(PyObject* self)
{
sdf_file_t *h = ((SDFObject*)self)->h;
if (h) {
sdf_stack_destroy(h);
sdf_close(h);
}
self->ob_type->tp_free(self);
}
static void
setup_mesh(SDFObject *sdf, PyObject *dict, sdf_block_t *b, PyObject *dict_id)
{
char *block_name = NULL;
char *mesh_id = NULL;
PyObject *ob = NULL;
Block *parent, *block = NULL;
Py_ssize_t n, len_name, len_id;
if (!sdf->h || !b) return;
len_name = strlen(b->name);
block_name = malloc(len_name + 5);
if (!block_name) goto free_mem;
memcpy(block_name, b->name, len_name);
block_name[len_name] = '\0';
block = (Block*)Block_alloc(sdf, b);
if (!block) goto free_mem;
block->labels = PyTuple_New(b->ndims);
if (!block->labels) goto free_mem;
block->units = PyTuple_New(b->ndims);
if (!block->units) goto free_mem;
for (n = 0; n < b->ndims; n++) {
ob = PyASCII_FromString(b->dim_labels[n]);
if (!ob) goto free_mem;
PyTuple_SetItem(block->labels, n, ob);
ob = PyASCII_FromString(b->dim_units[n]);
if (!ob) goto free_mem;
PyTuple_SetItem(block->units, n, ob);
}
PyDict_SetItemString(dict_id, b->id, (PyObject*)block);
PyDict_SetItemString(dict, block_name, (PyObject*)block);
Py_DECREF(block);
if (b->blocktype == SDF_BLOCKTYPE_POINT_MESH) {
free(block_name);
return;
}
/* Add median mesh block */
parent = block;
memcpy(block_name+len_name, "_mid", 5);
block = (Block*)Block_alloc(sdf, b);
if (!block) goto free_mem;
/* This block needs a reference to the parent and also needs the parent to
exist as long as we do. */
block->parent = parent;
Py_INCREF(block->parent);
block->labels = PyTuple_New(b->ndims);
if (!block->labels) goto free_mem;
block->units = PyTuple_New(b->ndims);
if (!block->units) goto free_mem;
for (n = 0; n < b->ndims; n++) {
if (block->adims[n] > 1)
block->adims[n]--;
PyTuple_SetItem(block->dims, n, PyLong_FromLong(block->adims[n]));
ob = PyASCII_FromString(b->dim_labels[n]);
if (!ob) goto free_mem;
PyTuple_SetItem(block->labels, n, ob);
ob = PyASCII_FromString(b->dim_units[n]);
if (!ob) goto free_mem;
PyTuple_SetItem(block->units, n, ob);
}
len_id = strlen(b->id);
mesh_id = malloc(len_id + 5);
if (!mesh_id) goto free_mem;
memcpy(mesh_id, b->id, len_id);
memcpy(mesh_id+len_id, "_mid", 5);
Py_DECREF(block->id);
block->id = PyASCII_FromString(mesh_id);
if (!block->id) goto free_mem;
Py_DECREF(block->name);
block->name = PyASCII_FromString(block_name);
if (!block->name) goto free_mem;
PyDict_SetItemString(dict_id, mesh_id, (PyObject*)block);
PyDict_SetItemString(dict, block_name, (PyObject*)block);
Py_DECREF(block);
free(mesh_id);
free(block_name);
return;
free_mem:
if (block_name) free(block_name);
if (mesh_id) free(mesh_id);
if (block) Py_DECREF(block);
if (ob) Py_DECREF(ob);
}
static void extract_station_time_histories(sdf_file_t *h, PyObject *stations,
PyObject *variables, double t0, double t1, PyObject *dict)
{
Py_ssize_t nvars, i, nstat;
PyObject *sub;
char **var_names, *timehis, *v, *key;
long *stat, ii;
int *size, *offset, nrows, row_size;
sdf_block_t *b;
npy_intp dims[1];
if ( !stations ) {
nstat = 1;
stat = (long *)malloc(sizeof(long));
stat[0] = 0;
} else {
/* Force 'stat' to be valid input for sdf_read_station_timehis */
nstat = PyList_Size(stations);
stat = (long *)calloc(nstat, sizeof(long));
nstat = 0;
for ( ii=0; ii<h->current_block->nstations; ii++ ) {
sub = PyLong_FromLong(ii+1);
if ( PySequence_Contains(stations, sub) ) {
stat[nstat] = ii;
nstat++;
}
Py_DECREF(sub);
}
}
if ( !nstat ) {
free(stat);
return;
}
if ( !variables ) {
free(stat);
return;
}
nvars = PyList_Size(variables);
if ( !nvars ) {
free(stat);
return;
}
var_names = (char **)malloc(nvars*sizeof(char *));
for ( i=0; i<nvars; i++ ) {