Skip to content

Commit

Permalink
Misc updates from welib
Browse files Browse the repository at this point in the history
  • Loading branch information
ebranlard committed Sep 27, 2024
1 parent 5df30f6 commit 44f15f3
Show file tree
Hide file tree
Showing 12 changed files with 1,057 additions and 489 deletions.
5 changes: 4 additions & 1 deletion weio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def fileFormats(userpath=None, ignoreErrors=False, verbose=False):
from .hawcstab2_pwr_file import HAWCStab2PwrFile
from .hawcstab2_ind_file import HAWCStab2IndFile
from .hawcstab2_cmb_file import HAWCStab2CmbFile
from .gnuplot_file import GNUPlotFile
from .mannbox_file import MannBoxFile
from .flex_blade_file import FLEXBladeFile
from .flex_profile_file import FLEXProfileFile
Expand Down Expand Up @@ -104,6 +105,7 @@ def addFormat(priority, fmt):
addFormat(60, FileFormat(NetCDFFile))
addFormat(60, FileFormat(VTKFile))
addFormat(60, FileFormat(TDMSFile))
addFormat(60, FileFormat(GNUPlotFile))
addFormat(60, FileFormat(ParquetFile))
addFormat(60, FileFormat(PickleFile))
addFormat(70, FileFormat(CactusFile))
Expand Down Expand Up @@ -236,13 +238,14 @@ def detectFormat(filename, **kwargs):
extMatch = True
else:
# Try patterns if present
extPatterns = [ef.replace('.','\.').replace('$','\$').replace('*','[.]*') for ef in myformat.extensions if '*' in ef]
extPatterns = [ef.replace('.',r'\.').replace('$',r'\$').replace('*','[.]*') for ef in myformat.extensions if '*' in ef]
if len(extPatterns)>0:
extPatMatch = [re.match(pat, ext) is not None for pat in extPatterns]
extMatch = any(extPatMatch)
else:
extMatch = False
if extMatch: # we have a match on the extension
#print('Trying format: ',myformat)
valid, F = isRightFormat(myformat, filename, **kwargs)
if valid:
#print('File detected as :',myformat)
Expand Down
14 changes: 9 additions & 5 deletions weio/bladed_out_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,11 @@ def read_bladed_sensor_file(sensorfile):
try:
# Combine the strings into one string
combined_string = ''.join(sensorLines)

# Search everything betwee AXITICK and AXISLAB with a regex pattern
t_line = re.search(r'(?<=AXITICK).+?(?=AXISLAB)', combined_string, flags=re.DOTALL)
t_line=t_line.group(0)
# Search for a regex pattern that spans across multiple strings
line = re.search(r'(?<=AXITICK).+?(?=(AXISLAB|NVARS))', combined_string, flags=re.DOTALL)
line=line.group(0)
# Replace consecutive whitespace characters with a single space
t_line = re.sub(r'\s+', ' ', t_line)
t_line = re.sub(r'\s+', ' ', line)
except:
pass

Expand Down Expand Up @@ -107,6 +106,7 @@ def read_bladed_sensor_file(sensorfile):
except:
pass
def repUnits(s):
s = s.replace('[[','[').replace(']]',']')
s = s.replace('TT','s^2').replace('T','s').replace('A','rad')
s = s.replace('P','W').replace('L','m').replace('F','N').replace('M','kg')
return s
Expand Down Expand Up @@ -184,6 +184,10 @@ def read_bladed_output(sensorFilename, readTimeFilesOnly=False):
data = np.fromfile(fid_2, sensorInfo['Precision'])

try:
if nMajor==0:
nMajor=int(np.floor(len(data)/nSections/nSensors))
data=data[0:nMajor*nSections*nSensors]
sensorInfo['nMajor']=nMajor
if sensorInfo['NDIMENS'] == 3:
data = np.reshape(data,(nMajor, nSections, nSensors), order='C')

Expand Down
70 changes: 67 additions & 3 deletions weio/csv_file.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import os

from .file import File, WrongFormatError
import pandas as pd

try:
from .file import File, WrongFormatError
except:
File=dict
WrongFormatError = type('WrongFormatError', (Exception,),{})

class CSVFile(File):
"""
Read/write a CSV file.
Expand Down Expand Up @@ -51,7 +55,23 @@ def __init__(self, filename=None, sep=None, colNames=None, commentChar=None, com
raise Exception('Provide either `commentChar` or `commentLines` for CSV file types')
if (len(self.colNames)>0) and (self.colNamesLine is not None):
raise Exception('Provide either `colNames` or `colNamesLine` for CSV file types')
super(CSVFile, self).__init__(filename=filename,**kwargs)
if filename:
self.read(filename, **kwargs)
else:
self.filename = None

def read(self, filename=None, **kwargs):
if filename:
self.filename = filename
if not self.filename:
raise Exception('No filename provided')
if not os.path.isfile(self.filename):
raise OSError(2,'File not found:',self.filename)
if os.stat(self.filename).st_size == 0:
raise EmptyFileError('File is empty:',self.filename)
# Calling children function
self._read(**kwargs)


def _read(self):
COMMENT_CHAR=['#','!',';']
Expand Down Expand Up @@ -283,3 +303,47 @@ def __repr__(self):
def _toDataFrame(self):
return self.data

def to2DFields(self, **kwargs):
import xarray as xr
if len(kwargs.keys())>0:
print('[WARN] CSVFile: to2DFields: ignored keys: ',kwargs.keys())
if len(self.data)==0:
return None
M = self.data.values
if self.data.columns[0].lower()=='index':
M = M[:,1:]
s1 = 'rows'
s2 = 'columns'
ds = xr.Dataset(coords={s1: range(M.shape[0]), s2: range(M.shape[1])})
ds['data'] = ([s1, s2], M)
return ds

# --------------------------------------------------------------------------------
# --- Properties NOTE: copy pasted from file.py to make this file standalone..
# --------------------------------------------------------------------------------
@property
def size(self):
return os.path.getsize(self.filename)
@property
def encoding(self):
import codecs
import chardet
""" Detects encoding"""
try:
byts = min(32, self.size)
except TypeError:
return None
with open(self.filename, 'rb') as f:
raw = f.read(byts)
if raw.startswith(codecs.BOM_UTF8):
return 'utf-8-sig'
else:
result = chardet.detect(raw)
return result['encoding']

if __name__ == '__main__':
f = CSVFile('C:/Work/Courses/440/project_solution/data/CFD_u.dat')
print(f)
ds = f.to2DFields()
print(ds)

Loading

0 comments on commit 44f15f3

Please sign in to comment.