Hi guys, I wanted to get a quick sanity check before I go too far. I would describe the following as a test although it will be part of my eventual recipe. I was wondering if you would be so kind as to give me a sanity check before I go too far. Seems to be working as expected at least from a python POV.
from os import listdir
import fnmatch
import pandas as pd
import numpy as np
import click
def retun_CSV(directory):
files = listdir(directory)
csv = []
for file in files:
if fnmatch.fnmatch(file, '*.csv'):
csv.append(file)
return csv
def read_directions(files, _results_dir, referenceSpeed):
i=0
labels = []
for file in files:
table = pd.read_csv(_results_dir + '\\' + file)
#columns = table.columns
if i ==0:
points = table[['Points:0', 'Points:1', 'Points:2']]
u = table['Velocities_n:0'].to_numpy()
v = table['Velocities_n:1'].to_numpy()
U = np.sqrt(u**2+v**2).reshape(-1,1)/referenceSpeed
direction = (270+np.degrees(np.arctan2(v,u))).reshape(-1,1)
label = float(file.replace('deg0.csv', '').replace('Results_', ''))
labels.append(label)
if i == 0:
Speed = U
Direction = direction
else:
Speed = np.concatenate((Speed, U), axis=1)
Direction = np.concatenate((Direction, direction), axis=1)
i+=1
point_labels = ['X', 'Y', 'Z']
points.columns = point_labels
Direction = pd.DataFrame(data=Direction, columns=labels).reset_index(drop=True)
Speed = pd.DataFrame(data=Speed, columns=labels).reset_index(drop=True)
return Speed, Direction, points
def toFile(df, path, fileType):
try:
if fileType == 'CSV':
df.to_csv(path)
elif fileType == 'feather':
df.columns = df.columns.astype(str)
df.to_feather(path)
return True
except:
return False
@click.command()
@click.option('--filetype', default='feather', help='file format, feather is recomended')
@click.option('--refspeed', default=10, help='the speed at reference height, usually 10m/s at 10m')
@click.option('--path', default='16 Direction Results', help='the location at which the directional CFD data is located in csv flies from the vtk export')
def formatCFD(filetype, refspeed, path):
#_results_dir = r'16 Direction Results'
_results_dir = path
csv = retun_CSV(_results_dir)
local_speed, local_direction, points = read_directions(csv, _results_dir, refspeed)
#local_speed.to_feather('localSpeedFactor.feather')
toFile(local_speed, 'results\localSpeedFactor.feather', filetype)
toFile(local_direction, 'results\localDirection.feather', filetype)
toFile(points, 'results\points.feather', filetype)
if __name__ == '__main__':
formatCFD()
And this is my entry.py:
from dataclasses import dataclass
from pollination_dsl.function import Function, command, Inputs, Outputs
@dataclass
class formatCFD(Function):
"""
A function to take CFD data from individual csv exports from vtk to a single
file for local speed up factor and direction. Where the number of columns
will be number of directions, and number of rows will be the number of points
or sensors. This will also create points file with the XYZ coordinates of the
sensors
"""
path = Inputs.file(
description='A directory containing the csv data',
path='16 Direction Results'
)
fileType = Inputs.str(
description='The file formate, CSV or feather. feather is recomended as it writes faster, and is very compact',
default = 'feather',
spec={'type': 'string'}
)
refSpeed = Inputs.float(
description='A value with units m/s, usually 10m/s, at the reference height of 10m',
default=10.0,
spec={'type': 'float',}
)
@command
def formatCFD(self):
return 'formatCFD.py --fileType --refSpeed --path > results\localSpeedFactor.feather results\localDirection.feather'
speedFactor = Outputs.file(
description='Output files for wind speedup.',
path='results\localSpeedFactor.feather'
)
direction = Outputs.file(
description='Output files for wind speedup.',
path='results\localDirection.feather'
)
Thanks, Darren