2022-03-30 10:53:11 +02:00
import logging
2022-03-15 10:11:21 +01:00
import subprocess
import tempfile
2022-03-02 14:25:53 +01:00
import numpy as np
2022-03-30 10:53:11 +02:00
log = logging . getLogger ( " read_swash " )
2022-03-04 10:21:02 +01:00
class ReadSwash :
def __init__ ( self ) :
self . _n_x = None
self . _n_t = None
self . _t = None
self . _x = None
@classmethod
def read_nohead ( cls , path ) :
2022-03-31 16:05:06 +02:00
with tempfile . TemporaryFile ( mode = " w+t " ) as tmpfile :
log . info ( f " Replacing \\ s with \\ n in ' { path } ' " )
subprocess . run ( ( " sed " , r " s/ \ s \ +/ \ n/g;/^$/d " , path ) , stdout = tmpfile , text = True )
log . info ( f " Loading ' { path } ' " )
tmpfile . seek ( 0 )
a = np . asarray ( tmpfile . readlines ( ) , dtype = float )
log . debug ( f " path= { a } " )
return a
2022-03-04 10:21:02 +01:00
def read_time ( self , path ) :
self . _t = np . unique ( self . read_nohead ( path ) )
self . _n_t = self . _t . size
2022-03-15 10:11:21 +01:00
return self . t
2022-03-04 10:21:02 +01:00
def read_x ( self , path ) :
self . _x = np . unique ( self . read_nohead ( path ) )
self . _n_x = self . _x . size
2022-03-15 10:11:21 +01:00
return self . x
2022-03-04 10:21:02 +01:00
2022-03-04 10:43:45 +01:00
def read_scalar ( self , path , const = False ) :
if const :
2022-03-15 10:43:29 +01:00
return self . read_nohead ( path ) . reshape ( ( self . _n_t , self . _n_x ) ) [ 0 , : ]
return self . read_nohead ( path ) . reshape ( ( self . _n_t , self . _n_x ) )
2022-03-04 10:21:02 +01:00
2022-03-15 11:37:59 +01:00
def read_const ( self , path ) :
return self . read_scalar ( path , const = True )
2022-03-04 10:21:02 +01:00
def read_vector ( self , path ) :
2022-03-15 10:43:29 +01:00
return self . read_nohead ( path ) . reshape ( ( self . _n_t , 2 , self . _n_x ) )
2022-03-04 10:21:02 +01:00
def read_scalar_lay ( self , path ) :
2022-03-15 10:43:29 +01:00
return self . read_nohead ( path ) . reshape ( ( self . _n_t , - 1 , self . _n_x ) )
2022-03-04 10:21:02 +01:00
def read_vector_lay ( self , path ) :
2022-03-15 13:34:15 +01:00
return self . read_nohead ( path ) . reshape ( ( self . _n_t , - 1 , 2 , self . _n_x ) )
2022-03-04 10:21:02 +01:00
@property
def t ( self ) :
return self . _t
@property
def x ( self ) :
return self . _x