588 lines
21 KiB
C#
588 lines
21 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Xml;
|
||
using System.IO;
|
||
using System.Windows.Forms;
|
||
|
||
namespace mainProgram
|
||
{
|
||
public class ProjectManager
|
||
{
|
||
public ProjectManager(string xx)
|
||
{
|
||
mProjectPath = xx;
|
||
}
|
||
|
||
private string mProjectPath;
|
||
private string mMetadataPath;
|
||
private string mRawPath;
|
||
private string mRadPath;
|
||
private string mSifPath;
|
||
|
||
public string ProjectPath
|
||
{
|
||
get
|
||
{
|
||
return mProjectPath;
|
||
}
|
||
set
|
||
{
|
||
mProjectPath = value;
|
||
}
|
||
}
|
||
|
||
public void CreateProject(string dataPath)
|
||
{
|
||
if (mProjectPath.Length == 0)
|
||
{
|
||
return;
|
||
}
|
||
|
||
DelectDir(mProjectPath);//删除工程路径下的所有内容
|
||
CreateProjectStructure();
|
||
CopyFileToFile(dataPath);//复制原始数据文件到工程结构对应文件夹
|
||
|
||
////创建工程元数据
|
||
//string myXMLFilePath = mMetadataPath + "/metadata.xml";
|
||
////生成xml文件
|
||
//GenerateXMLFile(myXMLFilePath);
|
||
////遍历xml文件的信息
|
||
//GetXMLInformation(myXMLFilePath);
|
||
////修改xml文件的信息
|
||
//ModifyXmlInformation(myXMLFilePath);
|
||
////向xml文件添加节点信息
|
||
//AddXmlInformation(myXMLFilePath);
|
||
////删除指定节点信息
|
||
//DeleteXmlInformation(myXMLFilePath);
|
||
|
||
|
||
}
|
||
|
||
public void OpenProject(string projectPath)
|
||
{
|
||
mProjectPath = projectPath;
|
||
|
||
mMetadataPath = Path.Combine(mProjectPath, ".project");
|
||
mRawPath = Path.Combine(mProjectPath, "1raw");
|
||
mRadPath = Path.Combine(mProjectPath, "2rad");
|
||
mSifPath = Path.Combine(mProjectPath, "3sif");
|
||
|
||
//if (false == Directory.Exists(mMetadataPath))
|
||
//{
|
||
// Directory.CreateDirectory(mMetadataPath);
|
||
|
||
// MessageBox.Show("工程打开成功!", "提示");
|
||
//}
|
||
//if (false == Directory.Exists(mRawPath))
|
||
//{
|
||
// Directory.CreateDirectory(mRawPath);
|
||
//}
|
||
//if (false == Directory.Exists(mRadPath))
|
||
//{
|
||
// Directory.CreateDirectory(mRadPath);
|
||
//}
|
||
//if (false == Directory.Exists(mSifPath))
|
||
//{
|
||
// Directory.CreateDirectory(mSifPath);
|
||
//}
|
||
}
|
||
|
||
public void SaveProject()
|
||
{
|
||
|
||
}
|
||
|
||
public void AddData()
|
||
{
|
||
|
||
}
|
||
|
||
public void DeleteData()
|
||
{
|
||
|
||
}
|
||
|
||
//生成工程元数据
|
||
public void GenerateXMLFile(string xmlFilePath)
|
||
{
|
||
try
|
||
{
|
||
//初始化一个xml实例
|
||
XmlDocument myXmlDoc = new XmlDocument();
|
||
//创建xml的根节点
|
||
XmlElement rootElement = myXmlDoc.CreateElement("Computers");
|
||
//将根节点加入到xml文件中(AppendChild)
|
||
myXmlDoc.AppendChild(rootElement);
|
||
|
||
//初始化第一层的第一个子节点
|
||
XmlElement firstLevelElement1 = myXmlDoc.CreateElement("Computer");
|
||
//填充第一层的第一个子节点的属性值(SetAttribute)
|
||
firstLevelElement1.SetAttribute("ID", "11111111");
|
||
firstLevelElement1.SetAttribute("Description", "Made in China");
|
||
//将第一层的第一个子节点加入到根节点下
|
||
rootElement.AppendChild(firstLevelElement1);
|
||
//初始化第二层的第一个子节点
|
||
XmlElement secondLevelElement11 = myXmlDoc.CreateElement("name");
|
||
//填充第二层的第一个子节点的值(InnerText)
|
||
secondLevelElement11.InnerText = "Lenovo";
|
||
firstLevelElement1.AppendChild(secondLevelElement11);
|
||
XmlElement secondLevelElement12 = myXmlDoc.CreateElement("price");
|
||
secondLevelElement12.InnerText = "5000";
|
||
firstLevelElement1.AppendChild(secondLevelElement12);
|
||
|
||
|
||
XmlElement firstLevelElement2 = myXmlDoc.CreateElement("Computer");
|
||
firstLevelElement2.SetAttribute("ID", "2222222");
|
||
firstLevelElement2.SetAttribute("Description", "Made in USA");
|
||
rootElement.AppendChild(firstLevelElement2);
|
||
XmlElement secondLevelElement21 = myXmlDoc.CreateElement("name");
|
||
secondLevelElement21.InnerText = "IBM";
|
||
firstLevelElement2.AppendChild(secondLevelElement21);
|
||
XmlElement secondLevelElement22 = myXmlDoc.CreateElement("price");
|
||
secondLevelElement22.InnerText = "10000";
|
||
firstLevelElement2.AppendChild(secondLevelElement22);
|
||
|
||
//将xml文件保存到指定的路径下
|
||
myXmlDoc.Save(xmlFilePath);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine(ex.ToString());
|
||
}
|
||
}
|
||
|
||
public void GetXMLInformation(string xmlFilePath)
|
||
{
|
||
try
|
||
{
|
||
XmlDocument myXmlDoc = new XmlDocument();//初始化一个xml实例
|
||
myXmlDoc.Load(xmlFilePath);//加载xml文件(参数为xml文件的路径)
|
||
|
||
XmlNode rootNode = myXmlDoc.SelectSingleNode("Computers");//获得第一个姓名匹配的节点(SelectSingleNode):此xml文件的根节点
|
||
|
||
string innerXmlInfo = rootNode.InnerXml.ToString();//分别获得该节点的InnerXml和OuterXml信息
|
||
string outerXmlInfo = rootNode.OuterXml.ToString();
|
||
|
||
XmlNodeList firstLevelNodeList = rootNode.ChildNodes;//获得该节点的子节点(即:该节点的第一层子节点)
|
||
foreach (XmlNode node in firstLevelNodeList)
|
||
{
|
||
XmlAttributeCollection attributeCol = node.Attributes;//获得该节点的属性集合
|
||
foreach (XmlAttribute attri in attributeCol)
|
||
{
|
||
//获取属性名称与属性值
|
||
string name = attri.Name;
|
||
string value = attri.Value;
|
||
Console.WriteLine("{0} = {1}", name, value);
|
||
}
|
||
|
||
//判断此节点是否还有子节点
|
||
if (node.HasChildNodes)
|
||
{
|
||
|
||
XmlNode secondLevelNode1 = node.FirstChild;//获取该节点的第一个子节点
|
||
string name = secondLevelNode1.Name;//获取该节点的名字
|
||
string innerText = secondLevelNode1.InnerText;//获取该节点的值(即:InnerText)
|
||
Console.WriteLine("{0} = {1}", name, innerText);
|
||
|
||
XmlNode secondLevelNode2 = node.ChildNodes[1];//获取该节点的第二个子节点(用数组下标获取)
|
||
name = secondLevelNode2.Name;
|
||
innerText = secondLevelNode2.InnerText;
|
||
Console.WriteLine("{0} = {1}", name, innerText);
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine(ex.ToString());
|
||
}
|
||
}
|
||
|
||
public void ModifyXmlInformation(string xmlFilePath)
|
||
{
|
||
try
|
||
{
|
||
XmlDocument myXmlDoc = new XmlDocument();
|
||
myXmlDoc.Load(xmlFilePath);
|
||
|
||
XmlNode rootNode = myXmlDoc.FirstChild;
|
||
XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
|
||
foreach (XmlNode node in firstLevelNodeList)
|
||
{
|
||
//修改此节点的属性值
|
||
if (node.Attributes["Description"].Value.Equals("Made in USA"))
|
||
{
|
||
node.Attributes["Description"].Value = "Made in HongKong";
|
||
}
|
||
}
|
||
//要想使对xml文件所做的修改生效,必须执行以下Save方法
|
||
myXmlDoc.Save(xmlFilePath);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine(ex.ToString());
|
||
}
|
||
|
||
}
|
||
|
||
public void AddXmlInformation(string xmlFilePath)
|
||
{
|
||
try
|
||
{
|
||
XmlDocument myXmlDoc = new XmlDocument();
|
||
myXmlDoc.Load(xmlFilePath);
|
||
//添加一个带有属性的节点信息
|
||
foreach (XmlNode node in myXmlDoc.FirstChild.ChildNodes)
|
||
{
|
||
XmlElement newElement = myXmlDoc.CreateElement("color");
|
||
newElement.InnerText = "black";
|
||
newElement.SetAttribute("IsMixed", "Yes");
|
||
node.AppendChild(newElement);
|
||
}
|
||
//保存更改
|
||
myXmlDoc.Save(xmlFilePath);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine(ex.ToString());
|
||
}
|
||
}
|
||
|
||
public void DeleteXmlInformation(string xmlFilePath)
|
||
{
|
||
try
|
||
{
|
||
XmlDocument myXmlDoc = new XmlDocument();
|
||
myXmlDoc.Load(xmlFilePath);
|
||
foreach (XmlNode node in myXmlDoc.FirstChild.ChildNodes)
|
||
{
|
||
//记录该节点下的最后一个子节点(简称:最后子节点)
|
||
XmlNode lastNode = node.LastChild;
|
||
//删除最后子节点下的左右子节点
|
||
lastNode.RemoveAll();
|
||
//删除最后子节点
|
||
node.RemoveChild(lastNode);
|
||
}
|
||
//保存对xml文件所做的修改
|
||
myXmlDoc.Save(xmlFilePath);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine(ex.ToString());
|
||
}
|
||
}
|
||
|
||
//文件复制实现:https://www.cnblogs.com/sun-shadow/p/7553556.html
|
||
public void CopyFileToFile(string sourceFolderName)//CopyFileToFile
|
||
{
|
||
if (Directory.Exists(mRawPath))
|
||
{
|
||
DelectDir(mRawPath);
|
||
}
|
||
CopySubFun(sourceFolderName, mRawPath, true);
|
||
}
|
||
|
||
private void CopySubFun(string sourceFolderName, string destFolderName, bool overwrite)
|
||
{
|
||
if (!Directory.Exists(sourceFolderName))
|
||
{
|
||
return;
|
||
}
|
||
if (!Directory.Exists(destFolderName))
|
||
{
|
||
Directory.CreateDirectory(destFolderName);
|
||
}
|
||
|
||
string[] sourceFilesPath = Directory.GetFileSystemEntries(sourceFolderName);
|
||
|
||
for (int i = 0; i < sourceFilesPath.Length; i++)
|
||
{
|
||
string sourceFilePath = sourceFilesPath[i];
|
||
string[] forlders = sourceFilePath.Split('\\');
|
||
|
||
if (File.Exists(sourceFilePath))//是文件,直接拷贝
|
||
{
|
||
string dest = destFolderName;
|
||
string sourceFileName = Path.GetFileName(sourceFilePath);//获取文件名
|
||
File.Copy(sourceFilePath, Path.Combine(dest, sourceFileName), overwrite);
|
||
}
|
||
else if (Directory.Exists(sourceFilePath))//是文件夹,拷贝文件夹;并递归
|
||
{
|
||
string lastDirectory = forlders[forlders.Length - 1];
|
||
string dest = Path.Combine(destFolderName, lastDirectory);
|
||
|
||
if (!Directory.Exists(dest))
|
||
{
|
||
Directory.CreateDirectory(dest);
|
||
}
|
||
CopySubFun(sourceFilePath, dest, overwrite);
|
||
}
|
||
}
|
||
}
|
||
|
||
private void DelectDir(string srcPath)
|
||
{
|
||
DirectoryInfo dir = new DirectoryInfo(srcPath);
|
||
FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录
|
||
foreach (FileSystemInfo i in fileinfo)
|
||
{
|
||
if (i is DirectoryInfo) //判断是否文件夹
|
||
{
|
||
DirectoryInfo subdir = new DirectoryInfo(i.FullName);
|
||
subdir.Delete(true); //删除子目录和文件
|
||
}
|
||
else
|
||
{
|
||
File.Delete(i.FullName); //删除指定文件
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
|
||
//工程结构
|
||
public void CreateProjectStructure()
|
||
{
|
||
//创建工程文件夹结构
|
||
//mMetadataPath = mProjectPath + "\\.project\\";
|
||
//mRawPath = mProjectPath + "\\1raw\\";
|
||
//mRadPath = mProjectPath + "\\2rad\\";
|
||
//mSifPath = mProjectPath + "\\3sif\\";
|
||
|
||
mMetadataPath = Path.Combine(mProjectPath, ".project");
|
||
mRawPath = Path.Combine(mProjectPath, "1raw");
|
||
mRadPath = Path.Combine(mProjectPath, "2rad");
|
||
mSifPath = Path.Combine(mProjectPath, "3sif");
|
||
|
||
if (false == Directory.Exists(mMetadataPath))
|
||
{
|
||
Directory.CreateDirectory(mMetadataPath);
|
||
}
|
||
if (false == Directory.Exists(mRawPath))
|
||
{
|
||
Directory.CreateDirectory(mRawPath);
|
||
}
|
||
if (false == Directory.Exists(mRadPath))
|
||
{
|
||
Directory.CreateDirectory(mRadPath);
|
||
}
|
||
if (false == Directory.Exists(mSifPath))
|
||
{
|
||
Directory.CreateDirectory(mSifPath);
|
||
}
|
||
}
|
||
|
||
public int getTxtFileCount()
|
||
{
|
||
string[] metaFilesPath = Directory.GetFileSystemEntries(mMetadataPath);
|
||
int txtFileCount = 0;
|
||
for (int i = 0; i < metaFilesPath.Length; i++)//
|
||
{
|
||
if (metaFilesPath[i].Contains("txt"))
|
||
{
|
||
txtFileCount++;
|
||
}
|
||
}
|
||
return txtFileCount;
|
||
}
|
||
|
||
public int getDatFileCount()
|
||
{
|
||
string[] metaFilesPath = Directory.GetFileSystemEntries(mMetadataPath);
|
||
int datFileCount = 0;
|
||
for (int i = 0; i < metaFilesPath.Length; i++)//
|
||
{
|
||
if (metaFilesPath[i].Contains("dat"))
|
||
{
|
||
datFileCount++;
|
||
}
|
||
}
|
||
|
||
return datFileCount;
|
||
}
|
||
|
||
//处理工程中的数据
|
||
public void Rad()
|
||
{
|
||
//读取标定文件:辐射定标文件 + 非线性校正文件
|
||
int datFileCount = getDatFileCount();
|
||
int txtFileCount = getTxtFileCount();
|
||
|
||
CalData[] calDatas = new CalData[datFileCount];
|
||
NonLinearData[] nonLinearDatas = new NonLinearData[txtFileCount];
|
||
|
||
int d1 = 0, d2 = 0;
|
||
string[] metaFilesPath = Directory.GetFileSystemEntries(mMetadataPath);
|
||
for (int i = 0; i < datFileCount + txtFileCount; i++)
|
||
{
|
||
if (metaFilesPath[i].Contains("txt"))
|
||
{
|
||
nonLinearDatas[d1] = ReadNonLinearFile(metaFilesPath[i]);
|
||
d1++;
|
||
}
|
||
else if (metaFilesPath[i].Contains("dat"))
|
||
{
|
||
calDatas[d2] = ReadCalFile(metaFilesPath[i]);
|
||
d2++;
|
||
}
|
||
}
|
||
|
||
//对文件夹中的文件遍历处理:非线性校正 + 辐射定标
|
||
Console.WriteLine("工程目录为" + mProjectPath);
|
||
DelectDir(mRadPath);
|
||
string[] sourceFilesPath = Directory.GetFileSystemEntries(mRawPath);
|
||
|
||
for (int i = 0; i < sourceFilesPath.Length; i++)//mRawPath下的日期文件夹
|
||
{
|
||
string sourceFilePath = sourceFilesPath[i];
|
||
string[] forlders = sourceFilePath.Split('\\');
|
||
|
||
string lastDirectory = forlders[forlders.Length - 1];
|
||
string dest = Path.Combine(mRadPath, lastDirectory);
|
||
|
||
if (!Directory.Exists(dest))
|
||
{
|
||
Directory.CreateDirectory(dest);
|
||
}
|
||
|
||
//福亮度转换
|
||
string[] rawFiles = Directory.GetFileSystemEntries(Path.Combine(mRawPath, lastDirectory));
|
||
|
||
foreach (string rawFileName in rawFiles)//日期文件夹下的文件
|
||
{
|
||
string[] nameTmp = rawFileName.Split('\\');
|
||
|
||
string name = nameTmp[nameTmp.Length - 1];
|
||
SpectralDataReaderWriter spectralDataReaderWriter = new SpectralDataReaderWriter(rawFileName);
|
||
for (int j = 1; j <= spectralDataReaderWriter.TotalSpectralCount; j++)//处理 csv文件中的每条光谱
|
||
{
|
||
SpectralData spectralData = spectralDataReaderWriter.GetSpectral(j);
|
||
|
||
int nonLinearData_index = GetnonLinearDataIndex(spectralData, nonLinearDatas);
|
||
int calData_index = GetCalDataIndex(spectralData, calDatas);
|
||
|
||
SpectralProcessor sp = new SpectralProcessor();
|
||
|
||
if (nonLinearData_index >= 0)//非线性校正
|
||
{
|
||
sp.NonLinearCorrection(nonLinearDatas[nonLinearData_index].nonLinearData, spectralData.spectral, spectralData.spectralDataLength);
|
||
}
|
||
if (nonLinearData_index >= 0)//福亮度转换
|
||
{
|
||
sp.NonLinearCorrection(nonLinearDatas[nonLinearData_index].nonLinearData, spectralData.spectral, spectralData.spectralDataLength);
|
||
}
|
||
spectralDataReaderWriter.UpdateSpectral(j, spectralData);
|
||
}
|
||
|
||
string destFileName = Path.Combine(dest, name);
|
||
spectralDataReaderWriter.SaveCSV(destFileName);
|
||
|
||
}
|
||
}
|
||
}
|
||
|
||
/*
|
||
返回值:-1代表没有匹配项
|
||
*/
|
||
public int GetnonLinearDataIndex(SpectralData spectralData, NonLinearData[] nonLinearData)
|
||
{
|
||
for (int i = 0; i < nonLinearData.GetLength(0); i++)
|
||
{
|
||
if (nonLinearData[i].SN.Contains(spectralData.SN))
|
||
{
|
||
return i;
|
||
}
|
||
}
|
||
return -1;
|
||
}
|
||
|
||
/*
|
||
返回值:-1代表没有匹配项
|
||
*/
|
||
public int GetCalDataIndex(SpectralData spectralData, CalData[] calData)
|
||
{
|
||
for (int i = 0; i < calData.GetLength(0); i++)
|
||
{
|
||
if (calData[i].SN.Contains(spectralData.SN))
|
||
{
|
||
return i;
|
||
}
|
||
}
|
||
return -1;
|
||
}
|
||
|
||
|
||
public CalData ReadCalFile(string calFilePath)
|
||
{
|
||
CalData calFile;
|
||
|
||
string[] forlders = calFilePath.Split('\\');
|
||
calFile.SN = forlders[forlders.Length - 1];
|
||
|
||
FileStream fs2 = new FileStream(calFilePath, FileMode.Open, FileAccess.Read);
|
||
BinaryReader br = new BinaryReader(fs2);
|
||
calFile.exposureTime = br.ReadInt32();
|
||
calFile.pixelCount = br.ReadInt32();
|
||
calFile.temperature = br.ReadInt32();
|
||
calFile.waveLengthInNM = new float[calFile.pixelCount];
|
||
calFile.gain = new double[calFile.pixelCount];
|
||
|
||
for (int ii = 0; ii < calFile.pixelCount; ii++)
|
||
{
|
||
calFile.waveLengthInNM[ii] = br.ReadSingle();
|
||
}
|
||
for (int ii = 0; ii < calFile.pixelCount; ii++)
|
||
{
|
||
calFile.gain[ii] = br.ReadDouble();
|
||
}
|
||
|
||
return calFile;
|
||
}
|
||
|
||
public NonLinearData ReadNonLinearFile(string nonLinearFilePath)
|
||
{
|
||
NonLinearData nonLinearData;
|
||
string[] forlders = nonLinearFilePath.Split('\\');
|
||
nonLinearData.SN = forlders[forlders.Length - 1];
|
||
|
||
|
||
int lineCount = FindMaxRowCount(nonLinearFilePath);
|
||
nonLinearData.nonLinearData = new double[lineCount];
|
||
|
||
FileStream fs = new FileStream(nonLinearFilePath, FileMode.Open, FileAccess.Read);
|
||
StreamReader sr = new StreamReader(fs);
|
||
string strLine = "";//记录每次读取的一行记录
|
||
|
||
int i = 0;
|
||
while ((strLine = sr.ReadLine()) != null)//逐行读取CSV中的数据
|
||
{
|
||
nonLinearData.nonLinearData[i] = double.Parse(strLine);
|
||
i++;
|
||
}
|
||
|
||
return nonLinearData;
|
||
}
|
||
|
||
public int FindMaxRowCount(string filePath)
|
||
{
|
||
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
|
||
StreamReader sr = new StreamReader(fs);
|
||
|
||
string strLine = "";
|
||
int maxColunmCount = 0;
|
||
|
||
while ((strLine = sr.ReadLine()) != null)
|
||
{
|
||
maxColunmCount++;
|
||
}
|
||
|
||
sr.Close();
|
||
fs.Close();
|
||
|
||
return maxColunmCount;
|
||
}
|
||
}
|
||
}
|