Skip to content

Commit

Permalink
IO: Update of 2D fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmanuel Branlard committed Jul 13, 2024
1 parent d14f11d commit 7d39f05
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 24 deletions.
1 change: 1 addition & 0 deletions pydatview/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ def detectFormat(filename, **kwargs):
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
70 changes: 67 additions & 3 deletions pydatview/io/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)

2 changes: 1 addition & 1 deletion pydatview/io/fast_output_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def writeDataFrame(self, df, filename, binary=True):

def __repr__(self):
s='<{} object> with attributes:\n'.format(type(self).__name__)
s+=' - filename: {}\n'.format(filename)
s+=' - filename: {}\n'.format(self.filename)
s+=' - data ({})\n'.format(type(self.data))
s+=' - description: {}\n'.format(self.description)
s+='and keys: {}\n'.format(self.keys())
Expand Down
39 changes: 29 additions & 10 deletions pydatview/io/gnuplot_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ class GNUPlotFile(File):
Main methods
------------
- read, write, toDataFrame, keys
- read, write, toDataFrame, to2DFields, keys
Examples
--------
f = GNUPlotFile('file.xxx')
f = GNUPlotFile('file.dat')
print(f.keys())
print(f.toDataFrame().columns)
Expand Down Expand Up @@ -175,7 +175,7 @@ def is_meshgrid(self):
@property
def x_values(self):
if len(self['data'])==1:
x= self['data'][:,0]
x = self['data'][0][:,0]
else:
x = check_first_column_same(self['data'])
if x is None:
Expand All @@ -186,7 +186,9 @@ def x_values(self):
@property
def y_values(self):
if len(self['data'])==1:
y= self['data'][:,1]
if self['data'][0].shape[1]<2:
return None
y= self['data'][0][:,1]
else:
y = check_second_column_unique(self['data'])
if y is None:
Expand Down Expand Up @@ -236,13 +238,24 @@ def to2DFields(self, **kwargs):
if self['column_names'] is None:
colNames = default_colnames(self['data'][0].shape[1])

if not self.is_meshgrid:
return None


if len(kwargs.keys())>0:
print('[WARN] GNUPlotFile: to2DFields: ignored keys: ',kwargs.keys())

if not self.is_meshgrid:
if len(self['data'])>1:
# Probably hard to do, or have to provide different sets
return None
# --- Single table..., likely dubious results
M = self['data'][0]
if M.shape[1]<2:
return None
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

# --- Mesh grid
ds = xr.Dataset()
ds = xr.Dataset(coords={colNames[0]: x, colNames[1]: y})
data = np.array(self['data'])
Expand Down Expand Up @@ -323,7 +336,11 @@ def check_second_column_unique(arrays):
if not arrays:
return None
unique_values = []
if len(arrays[0].shape)!=2:
return None
for array in arrays:
if array.shape[1]<2:
return None
second_column = array[:, 1]
unique_value = np.unique(second_column)
if len(unique_value) != 1:
Expand All @@ -341,12 +358,14 @@ def find_string_with_n_splits(strings, n):
def default_colnames(n):
colNames=['C{}'.format(i) for i in range(n)]
colNames[0] = 'x'
colNames[1] = 'y'
if len(colNames)>1:
colNames[1] = 'y'
return colNames

if __name__ == '__main__':
from welib.essentials import *
plt = GNUPlotFile('diff.000000.dat')
#plt = GNUPlotFile('diff.000000.dat')
plt = GNUPlotFile('C:/Work/Courses/440/project_solution/data/CFD_x.dat')
print(plt)
df =plt.toDataFrame()
print(df)
Expand Down
2 changes: 1 addition & 1 deletion pydatview/io/rosco_performance_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ROSCOPerformanceFile(File):
Main methods
------------
- read, write, toDataFrame, keys
- read, write, toDataFrame, to2DFields, keys
Examples
--------
Expand Down
30 changes: 21 additions & 9 deletions pydatview/io/turbsim_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,26 +726,38 @@ def to2DFields(self, nTOut=10, nYOut=3, nZOut=3, **kwargs):
ds['y'].attrs['unit'] = 'm'
ds['z'].attrs['unit'] = 'm'

ds['(y,z)_u_avg_[m/s]']= (['y','z'], np.squeeze(np.mean(self['u'][0,:,:,:], axis=0)))
ds['(y,z)_v_avg_[m/s]']= (['y','z'], np.squeeze(np.mean(self['u'][1,:,:,:], axis=0)))
ds['(y,z)_w_avg_[m/s]']= (['y','z'], np.squeeze(np.mean(self['u'][2,:,:,:], axis=0)))

ds['(t,y)_u_avg_[m/s]']= (['t','y'], np.squeeze(np.mean(self['u'][0,:,:,:], axis=2)))
ds['(t,y)_v_avg_[m/s]']= (['t','y'], np.squeeze(np.mean(self['u'][1,:,:,:], axis=2)))
ds['(t,y)_w_avg_[m/s]']= (['t','y'], np.squeeze(np.mean(self['u'][2,:,:,:], axis=2)))

ds['(t,z)_u_avg_[m/s]']= (['t','z'], np.squeeze(np.mean(self['u'][0,:,:,:], axis=1)))
ds['(t,z)_v_avg_[m/s]']= (['t','z'], np.squeeze(np.mean(self['u'][1,:,:,:], axis=1)))
ds['(t,z)_w_avg_[m/s]']= (['t','z'], np.squeeze(np.mean(self['u'][2,:,:,:], axis=1)))

for it in IT:
ds['u_t={:.2f}_[m/s]'.format(self.t[it])] = (['y','z'], np.squeeze(self['u'][0,it,:,:]))
ds['(y,z)_u_t={:.2f}_[m/s]'.format(self.t[it])] = (['y','z'], np.squeeze(self['u'][0,it,:,:]))
for it in IT:
ds['v_t={:.2f}_[m/s]'.format(self.t[it])] = (['y','z'], np.squeeze(self['u'][1,it,:,:]))
ds['(y,z)_v_t={:.2f}_[m/s]'.format(self.t[it])] = (['y','z'], np.squeeze(self['u'][1,it,:,:]))
for it in IT:
ds['w_t={:.2f}_[m/s]'.format(self.t[it])] = (['y','z'], np.squeeze(self['u'][2,it,:,:]))
ds['(y,z)_w_t={:.2f}_[m/s]'.format(self.t[it])] = (['y','z'], np.squeeze(self['u'][2,it,:,:]))
# --- TZ planes
for iy in IY:
ds['u_y={:.2f}_[m/s]'.format(self['y'][iy])] = (['t','z'], np.squeeze(self['u'][0,:,iy,:]))
ds['(t,z)_u_y={:.2f}_[m/s]'.format(self['y'][iy])] = (['t','z'], np.squeeze(self['u'][0,:,iy,:]))
for iy in IY:
ds['v_y={:.2f}_[m/s]'.format(self['y'][iy])] = (['t','z'], np.squeeze(self['u'][1,:,iy,:]))
ds['(t,z)_v_y={:.2f}_[m/s]'.format(self['y'][iy])] = (['t','z'], np.squeeze(self['u'][1,:,iy,:]))
for iy in IY:
ds['w_y={:.2f}_[m/s]'.format(self['y'][iy])] = (['t','z'], np.squeeze(self['u'][2,:,iy,:]))
ds['(t,z)_w_y={:.2f}_[m/s]'.format(self['y'][iy])] = (['t','z'], np.squeeze(self['u'][2,:,iy,:]))
# --- TY planes
for iz in IZ:
ds['u_z={:.2f}_[m/s]'.format(self['z'][iz])] = (['t','y'], np.squeeze(self['u'][0,:,:,iz]))
ds['(t,y)_u_z={:.2f}_[m/s]'.format(self['z'][iz])] = (['t','y'], np.squeeze(self['u'][0,:,:,iz]))
for iz in IZ:
ds['v_z={:.2f}_[m/s]'.format(self['z'][iz])] = (['t','y'], np.squeeze(self['u'][1,:,:,iz]))
ds['(t,y)_v_z={:.2f}_[m/s]'.format(self['z'][iz])] = (['t','y'], np.squeeze(self['u'][1,:,:,iz]))
for iz in IZ:
ds['w_z={:.2f}_[m/s]'.format(self['z'][iz])] = (['t','y'], np.squeeze(self['u'][2,:,:,iz]))
ds['(t,y)_w_z={:.2f}_[m/s]'.format(self['z'][iz])] = (['t','y'], np.squeeze(self['u'][2,:,:,iz]))
return ds

def toDataset(self):
Expand Down

0 comments on commit 7d39f05

Please sign in to comment.