Skip to content

Commit 0cb2a54

Browse files
authored
Merge pull request #631 from achaikou/expose_datasources_to_python_bindings
Expose datasources to python bindings
2 parents 5287707 + 321998a commit 0cb2a54

7 files changed

Lines changed: 383 additions & 65 deletions

File tree

lib/include/segyio/segy.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ typedef struct {
139139

140140
segy_file* segy_open( const char* path, const char* mode );
141141
int segy_mmap( segy_datasource* );
142+
segy_datasource* segy_memopen( unsigned char* addr, size_t size );
143+
142144
int segy_flush( segy_datasource* );
143145
int segy_close( segy_datasource* );
144146

@@ -774,6 +776,8 @@ typedef enum {
774776
SEGY_READONLY,
775777
SEGY_NOTFOUND,
776778
SEGY_MEMORY_ERROR,
779+
SEGY_DS_FLUSH_ERROR,
780+
SEGY_DS_CLOSE_ERROR,
777781
// values are duplicated until enum is properly cleaned
778782
SEGY_DS_READ_ERROR = SEGY_FREAD_ERROR,
779783
SEGY_DS_WRITE_ERROR = SEGY_FWRITE_ERROR,

lib/src/segy.c

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,11 @@ static int mmapflush( segy_datasource* self ) {
565565
return SEGY_OK;
566566
}
567567

568+
static int memflush( segy_datasource* self ) {
569+
(void)self; // mark parameter as unused
570+
return SEGY_OK;
571+
}
572+
568573
static int mmapclose( segy_datasource* self ) {
569574
#ifdef HAVE_MMAP
570575
int err = mmapflush( self );
@@ -578,6 +583,12 @@ static int mmapclose( segy_datasource* self ) {
578583
return SEGY_OK;
579584
}
580585

586+
static int memclose( segy_datasource* self ) {
587+
memfile* mp = (memfile*)self->stream;
588+
free( mp );
589+
return SEGY_OK;
590+
}
591+
581592
static int formatsize( int format ) {
582593
switch( format ) {
583594
case SEGY_IBM_FLOAT_4_BYTE: return 4;
@@ -676,6 +687,47 @@ segy_file* segy_open( const char* path, const char* mode ) {
676687
return ds;
677688
}
678689

690+
segy_datasource* segy_memopen( unsigned char* addr, size_t size ) {
691+
if( !addr || !size ) return NULL;
692+
693+
/* as we don't support operations that change file size on update, no memory
694+
* resize would be needed
695+
*/
696+
memfile* mp = malloc( sizeof( memfile ) );
697+
if( !mp ) {
698+
return NULL;
699+
}
700+
mp->addr = addr;
701+
mp->cur = addr;
702+
mp->size = size;
703+
704+
segy_datasource* ds = malloc( sizeof( segy_datasource ) );
705+
if( !ds ) {
706+
free( mp );
707+
return NULL;
708+
}
709+
ds->stream = mp;
710+
711+
ds->read = memread;
712+
ds->write = memwrite;
713+
ds->seek = memseek;
714+
ds->tell = memtell;
715+
ds->size = memsize;
716+
ds->flush = memflush;
717+
ds->close = memclose;
718+
719+
ds->writable = true;
720+
721+
// on init assume a size of 4-bytes-per-element and big-endian
722+
ds->elemsize = 4;
723+
ds->lsb = false;
724+
725+
ds->minimize_requests_number = false;
726+
ds->memory_speedup = true;
727+
728+
return ds;
729+
}
730+
679731
int segy_mmap( segy_datasource* ds ) {
680732
#ifndef HAVE_MMAP
681733
return SEGY_MMAP_INVALID;
@@ -735,7 +787,7 @@ int segy_flush( segy_datasource* ds ) {
735787
if( !ds->writable ) return SEGY_OK;
736788

737789
int flusherr = ds->flush( ds );
738-
if( flusherr != 0 ) return SEGY_DS_ERROR;
790+
if( flusherr != 0 ) return SEGY_DS_FLUSH_ERROR;
739791

740792
return SEGY_OK;
741793
}
@@ -759,7 +811,7 @@ int segy_close( segy_datasource* ds ) {
759811

760812
err = ds->close(ds);
761813
free( ds );
762-
if( err != 0 ) return SEGY_DS_ERROR;
814+
if( err != 0 ) return SEGY_DS_CLOSE_ERROR;
763815
return SEGY_OK;
764816
}
765817

python/segyio/create.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,9 @@ def create(filename, spec):
193193
if endian is None:
194194
endian = 'big'
195195

196-
197-
fd = _segyio.segyfd(str(filename), 'w+', c_endianness(endian))
196+
fd = _segyio.segyfd(
197+
filename=str(filename), mode='w+', endianness=c_endianness(endian)
198+
)
198199
fd.segymake(
199200
samples = len(samples),
200201
tracecount = tracecount,

python/segyio/open.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ def open(filename, mode="r", iline = 189,
149149
raise ValueError(', '.join((problem, solution)))
150150

151151
from . import _segyio
152-
fd = _segyio.segyfd(str(filename), mode, c_endianness(endian))
152+
fd = _segyio.segyfd(
153+
filename=str(filename), mode=mode, endianness=c_endianness(endian)
154+
)
153155
fd.segyopen()
154156
metrics = fd.metrics()
155157

0 commit comments

Comments
 (0)