0% found this document useful (0 votes)
72 views

Last TTTTT Tted It

This document contains C# code for a Windows Forms application that analyzes turbine power curve data. It defines classes and methods for loading turbine performance data from files, filtering the data, and formatting it into data tables. On form load, it populates a standard turbine power curve data table for comparison purposes. The application allows users to select a turbine, date range, and optional power curtailment filter to generate analyzed power curve data tables from raw turbine performance text files.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Last TTTTT Tted It

This document contains C# code for a Windows Forms application that analyzes turbine power curve data. It defines classes and methods for loading turbine performance data from files, filtering the data, and formatting it into data tables. On form load, it populates a standard turbine power curve data table for comparison purposes. The application allows users to select a turbine, date range, and optional power curtailment filter to generate analyzed power curve data tables from raw turbine performance text files.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

using System;

using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace Test2
{

public partial class Form1 : Form


{
private string fullpath;
public static class MyGlobals
{
public static DateTime Date = DateTime.Now; // can change because not
const
public static String ErrorLog = "The Following Dated Mean Files Are
Missing: ";
public static DataTable StandardTable = new DataTable();

private DataTable formulateTable(string turbine,DateTime startDate,


DateTime endDate, DateTime meanFileDate)
{

string date = meanFileDate.ToString("yyMMdd");


string location = (string)TurbineSel.SelectedItem;
string dir = "D:\\ptoject 1\\New folder - Copy\\%location
%".Replace("%location%", location);
string file = String.Concat(turbine, "_m", date, ".txt");
string fullpath = Path.Combine(dir, file);

DataTable fullTable = new DataTable();

DateTime tempDate= new DateTime();

String filePath="";
fullTable.Clear();

//fullTable = createTable(turbine, startDate);

int diff = DateTime.Compare(startDate, endDate);


if (diff > 0)
{
tempDate=endDate;
endDate=startDate;
startDate=tempDate;

//startDate=startDate.AddDays(1);

while (diff<=0)
{
filePath = fullpath;

if (File.Exists(@filePath))
{
fullTable.Merge(createTable(turbine, startDate));
startDate = startDate.AddDays(1);
diff = DateTime.Compare(startDate, endDate);
}
else
{
MyGlobals.ErrorLog = String.Concat(MyGlobals.ErrorLog,
startDate.ToString("yyyy-MM-dd")," ,");
startDate = startDate.AddDays(1);
diff = DateTime.Compare(startDate, endDate);
continue;

}
}

return fullTable;
}

private DataTable createTable(string turbine, DateTime meanFileDate)


{
string date = meanFileDate.ToString("yyMMdd");
string delimiter = ";";
int records = 0;

/// <summary>
/// Converts a given delimited file into a dataset.
/// Assumes that the first line
/// of the text file contains the column names.
/// </summary>
/// <param name="File">The name of the file to open</param>
/// <param name="TableName">The name of the
/// Table to be made within the DataSet returned</param>
/// <param name="delimiter">The string to delimit by</param>
/// <returns></returns>

//The DataSet to Return


DataSet result = new DataSet();
DataTable resultTable = new DataTable();

string TableName = "Turbine_Power_Curve";

//Open the file in a stream reader.


StreamReader s = new StreamReader(fullpath);

//Split the first line into the columns


string[] columns = s.ReadLine().Split(delimiter.ToCharArray());
s.ReadLine();
s.ReadLine();
columns = s.ReadLine().Split(delimiter.ToCharArray());

//Add the new DataTable to the RecordSet


result.Tables.Add(TableName);

//Cycle the colums, adding those that don't exist yet


//and sequencing the one that do.
foreach (string col in columns)
{
bool added = false;
string next = "";
int i = 0;
while (!added)
{
//Build the column name and remove any unwanted characters.
string columnname = col + next;
columnname = columnname.Replace("#", "");
columnname = columnname.Replace("'", "");
columnname = columnname.Replace("&", "");

//See if the column already exists


if (!result.Tables[TableName].Columns.Contains(columnname))
{
//if it doesn't then we add it here and mark it as added
result.Tables[TableName].Columns.Add(columnname);
added = true;
}
else
{
//if it did exist then we increment the sequencer and try
again.
i++;
next = "_" + i.ToString();
}
}
}

//Read the rest of the data in the file.


string AllData = s.ReadToEnd();
//Split off each row at the Carriage Return/Line Feed
//Default line ending in most windows exports.
//You may have to edit this to match your particular file.
//This will work for Excel, Access, etc. default exports.

string[] rows = AllData.Split("\r\n".ToCharArray());

records = rows.Length;

short j = 0;
//Now add each row to the DataSet
foreach (string r in rows)
{
if (r != "")
{
//Split the row at the delimiter.
string[] items = r.Split(delimiter.ToCharArray());

//Add the item


result.Tables[TableName].Rows.Add(items);
j++;
}

System.Data.DataTable dt = (System.Data.DataTable)result.Tables[0];
DataTable filteredTable = dt.DefaultView.ToTable(false,
"power_limit_min", "active_power_avg", "wind_speed_avg");

//DataView dv = filteredTable.DefaultView;
//dv.RowFilter = "wind_speed_avg > 3";
//filteredTable=dv.ToTable();

DataTable dtCloned = filteredTable.Clone();


dtCloned.Columns[0].DataType = typeof(float);
dtCloned.Columns[1].DataType = typeof(float);
dtCloned.Columns[2].DataType = typeof(float);
foreach (DataRow row in filteredTable.Rows)
{
dtCloned.ImportRow(row);
}
DataView dv = dtCloned.DefaultView;
String query = "wind_speed_avg > 3 AND active_power_avg >10";
if (PowerCurtailment.Checked)
{
query = String.Concat(query, " AND power_limit_min>1000");
}

dv.RowFilter = query;
// AND power_limit_min>1000
filteredTable = dv.ToTable();

DataTable finalTable = filteredTable.DefaultView.ToTable(false,


"active_power_avg", "wind_speed_avg");
finalTable.Columns["active_power_avg"].ColumnName = "ActualPower /kW";
finalTable.Columns["wind_speed_avg"].ColumnName = "Average Wind
Speed /ms-1";
return finalTable;
}

public Form1()
{
InitializeComponent();

MyGlobals.StandardTable.Clear();
MyGlobals.StandardTable.Columns.Add("WindSpeed", typeof(float));

MyGlobals.StandardTable.Columns.Add("Standard Curve", typeof(float));

MyGlobals.StandardTable.Rows.Add(new object[] { 3, 23.6 });


MyGlobals.StandardTable.Rows.Add(new object[] { 3.5, 46.9 });
MyGlobals.StandardTable.Rows.Add(new object[] { 4, 77.9 });
MyGlobals.StandardTable.Rows.Add(new object[] { 4.5, 115.1 });
MyGlobals.StandardTable.Rows.Add(new object[] { 5, 160.6 });
MyGlobals.StandardTable.Rows.Add(new object[] { 5.5, 216.4 });
MyGlobals.StandardTable.Rows.Add(new object[] { 6, 284.6 });
MyGlobals.StandardTable.Rows.Add(new object[] { 6.5, 363.5 });
MyGlobals.StandardTable.Rows.Add(new object[] { 7, 455.3 });
MyGlobals.StandardTable.Rows.Add(new object[] { 7.5, 562.5 });
MyGlobals.StandardTable.Rows.Add(new object[] { 8, 684.5 });
MyGlobals.StandardTable.Rows.Add(new object[] { 8.5, 804.2 });
MyGlobals.StandardTable.Rows.Add(new object[] { 9, 945 });
MyGlobals.StandardTable.Rows.Add(new object[] { 9.5, 1088.40 });
MyGlobals.StandardTable.Rows.Add(new object[] { 10, 1221.80 });
MyGlobals.StandardTable.Rows.Add(new object[] { 10.5, 1333.70 });
MyGlobals.StandardTable.Rows.Add(new object[] { 11, 1416.00 });
MyGlobals.StandardTable.Rows.Add(new object[] { 11.5, 1465.80 });
MyGlobals.StandardTable.Rows.Add(new object[] { 12, 1485.00 });
MyGlobals.StandardTable.Rows.Add(new object[] { 12.5, 1492.30 });
MyGlobals.StandardTable.Rows.Add(new object[] { 13, 1500 });
MyGlobals.StandardTable.Rows.Add(new object[] { 13.5, 1500 });
MyGlobals.StandardTable.Rows.Add(new object[] { 14, 1500 });
MyGlobals.StandardTable.Rows.Add(new object[] { 14.5, 1500 });
MyGlobals.StandardTable.Rows.Add(new object[] { 15, 1500 });
MyGlobals.StandardTable.Rows.Add(new object[] { 15.5, 1500 });
MyGlobals.StandardTable.Rows.Add(new object[] { 16, 1500 });
MyGlobals.StandardTable.Rows.Add(new object[] { 16.5, 1500 });
MyGlobals.StandardTable.Rows.Add(new object[] { 17, 1500 });
MyGlobals.StandardTable.Rows.Add(new object[] { 17.5, 1500 });
MyGlobals.StandardTable.Rows.Add(new object[] { 18, 1500 });
MyGlobals.StandardTable.Rows.Add(new object[] { 18.5, 1500 });
MyGlobals.StandardTable.Rows.Add(new object[] { 19, 1500 });
MyGlobals.StandardTable.Rows.Add(new object[] { 19.5, 1500 });
MyGlobals.StandardTable.Rows.Add(new object[] { 20, 1500 });
MyGlobals.StandardTable.Rows.Add(new object[] { 20.5, 1500 });
MyGlobals.StandardTable.Rows.Add(new object[] { 21, 1500 });
MyGlobals.StandardTable.Rows.Add(new object[] { 21.5, 1500 });
MyGlobals.StandardTable.Rows.Add(new object[] { 22, 1500 });

chart1.Series.Clear();

var enumerableTable2 = (MyGlobals.StandardTable as


System.ComponentModel.IListSource).GetList();
chart1.DataBindTable(enumerableTable2, "WindSpeed");
chart1.Series[0].ChartType =
System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
chart1.ChartAreas[0].AxisX.LabelStyle.Format = "{0.00}";
chart1.ChartAreas[0].AxisX.Title = "Wind Speed /ms-1";
chart1.ChartAreas[0].AxisY.Title = "Active Power /kW";

private void button1_Click(object sender, EventArgs e)


{
MyGlobals.Date = StartDate.Value;
try
{

DataTable dt = new DataTable();


dt.Clear();
dt = formulateTable( TurbineSel.Text, StartDate.Value,
EndDate.Value); // (.txt file,Datatable name,delimiter)

DataView dv = dt.DefaultView;
dv.Sort = "Average Wind Speed /ms-1";
//test

resultTable.DataSource = dt;
chart1.Series.Clear();
var enumerableTable = (dt as
System.ComponentModel.IListSource).GetList();
chart1.DataBindTable(enumerableTable, "Average Wind Speed /ms-1");
chart1.Series[0].ChartType =
System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;

var enumerableTable2 = (MyGlobals.StandardTable as


System.ComponentModel.IListSource).GetList();
chart1.DataBindTable(enumerableTable2, "WindSpeed");
chart1.Series[1].ChartType =
System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;

if (MyGlobals.ErrorLog.Length > 45)


{
MessageBox.Show(String.Concat(MyGlobals.ErrorLog),
"Mean Files Missing",
MessageBoxButtons.OK,
//MessageBoxIcon.Error
//MessageBoxIcon.Warning // for Warning
//MessageBoxIcon.Error // for Error
MessageBoxIcon.Information // for Information
//MessageBoxIcon.Question // for Question
);
}

catch (FileNotFoundException ex)


{
// Write error.
MessageBox.Show(String.Concat("Mean file missing for the selected
date", ex),
"File Missing",
MessageBoxButtons.OK,
MessageBoxIcon.Error
//MessageBoxIcon.Warning // for Warning
//MessageBoxIcon.Error // for Error
//MessageBoxIcon.Information // for Information
//MessageBoxIcon.Question // for Question
);

}
catch (IndexOutOfRangeException)
{
// Write error.
MessageBox.Show(String.Concat("No Mean files found for the selected
date range"),
"File Missing",
MessageBoxButtons.OK,
MessageBoxIcon.Error
//MessageBoxIcon.Warning // for Warning
//MessageBoxIcon.Error // for Error
//MessageBoxIcon.Information // for Information
//MessageBoxIcon.Question // for Question
);

private void Form1_Load(object sender, EventArgs e)


{
TurbineSel.SelectedIndex = 0;
}

private void resultTable_CellContentClick(object sender,


DataGridViewCellEventArgs e)
{

private void chart1_Click(object sender, EventArgs e)


{

private void StartDate_ValueChanged_1(object sender, EventArgs e)


{
EndDate.Value = StartDate.Value;
}

}
}

You might also like