完成调用丰的sif python算法 → 所有数据处理功能完成

This commit is contained in:
2022-01-14 11:56:12 +08:00
parent 8c78a0d715
commit a7cb3885db
16 changed files with 696 additions and 59 deletions

View File

@ -9,6 +9,164 @@ using System.Windows.Forms;
namespace mainProgram
{
public delegate void UpdateProgressBarInfoDelegate(int ipos, string vinfo);//申明代理用于进度条的更新UpdateProgressBarInfoDelegate 1
public class DateFolder : IComparable//接口
{
private int mYear;
private int mMonth;
private int mDay;
//int hout;
//int minute;
//int second;
public string mDateFolder;
public DateFolder(string dateFolder)
{
string[] tmp = dateFolder.Split('_');
mYear = int.Parse(tmp[0]);
mMonth = int.Parse(tmp[1]);
mDay = int.Parse(tmp[2]);
mDateFolder = dateFolder;
}
public string GetDateFolder()
{
return mDateFolder;
}
//重载运算符
public static bool operator >(DateFolder a, DateFolder b)
{
if (a.mYear > b.mYear)
{
return true;
}
else if (a.mYear < b.mYear)
{
return false;
}
else if (a.mYear == b.mYear)
{
if (a.mMonth > b.mMonth)
{
return true;
}
else if (a.mMonth < b.mMonth)
{
return false;
}
else if (a.mMonth == b.mMonth)
{
if (a.mDay > b.mDay)
{
return true;
}
else if (a.mDay < b.mDay)
{
return false;
}
else if (a.mDay == b.mDay)
{
}
}
}
return false;
}
public static bool operator <(DateFolder a, DateFolder b)
{
if (a.mYear < b.mYear)
{
return true;
}
else if (a.mYear > b.mYear)
{
return false;
}
else if (a.mYear == b.mYear)
{
if (a.mMonth < b.mMonth)
{
return true;
}
else if (a.mMonth > b.mMonth)
{
return false;
}
else if (a.mMonth == b.mMonth)
{
if (a.mDay < b.mDay)
{
return true;
}
else if (a.mDay > b.mDay)
{
return false;
}
else if (a.mDay == b.mDay)
{
}
}
}
return false;
}
//负数值:当前对象小于参数对象;
//正数值:当前对象大于参数对象;
//零:两个对象在比较时相等;
public int CompareTo(object obj)//IComparable接口
{
DateFolder df = (DateFolder)obj;
if (this.mYear < df.mYear)
{
return -1;
}
else if (this.mYear > df.mYear)
{
return 1;
}
else if (this.mYear == df.mYear)
{
if (this.mMonth < df.mMonth)
{
return -1;
}
else if (this.mMonth > df.mMonth)
{
return 1;
}
else if (this.mMonth == df.mMonth)
{
if (this.mDay < df.mDay)
{
return -1;
}
else if (this.mDay > df.mDay)
{
return 1;
}
else if (this.mDay == df.mDay)
{
}
}
}
return 0;
}
}
public class ProjectManager
{
public ProjectManager(string xx)
@ -20,7 +178,10 @@ namespace mainProgram
private string mMetadataPath;
private string mRawPath;
private string mRadPath;
private string mSifPath;
public string mSifPath;
public string mSifTmpPath;
private int mCsvFileCount;
public string ProjectPath
{
@ -34,7 +195,7 @@ namespace mainProgram
}
}
public void CreateProject(string dataPath)
public void CreateProject(string calFilePath, string dataPath)
{
if (mProjectPath.Length == 0)
{
@ -43,7 +204,11 @@ namespace mainProgram
DelectDir(mProjectPath);//删除工程路径下的所有内容
CreateProjectStructure();
CopyFileToFile(dataPath);//复制原始数据文件到工程结构对应文件夹
CopyCalFiles2Project(calFilePath);//复制定标文件到工程结构对应文件夹
CopyFiles2Project(dataPath);//复制原始数据文件到工程结构对应文件夹
////创建工程元数据
//string myXMLFilePath = mMetadataPath + "/metadata.xml";
@ -61,15 +226,17 @@ namespace mainProgram
}
public void OpenProject(string projectPath)
public void OpenProject()
{
mProjectPath = projectPath;
mMetadataPath = Path.Combine(mProjectPath, ".project");
mRawPath = Path.Combine(mProjectPath, "1raw");
mRadPath = Path.Combine(mProjectPath, "2rad");
mSifPath = Path.Combine(mProjectPath, "3sif");
BinaryReader br = new BinaryReader(new FileStream(Path.Combine(mMetadataPath, "CsvFileCount.dat"), FileMode.Open));
mCsvFileCount = br.ReadInt32();
br.Close();
//if (false == Directory.Exists(mMetadataPath))
//{
// Directory.CreateDirectory(mMetadataPath);
@ -275,16 +442,57 @@ namespace mainProgram
}
//文件复制实现https://www.cnblogs.com/sun-shadow/p/7553556.html
public void CopyFileToFile(string sourceFolderName)//CopyFileToFile
public void CopyFiles2Project(string sourceFolderName)//
{
if (Directory.Exists(mRawPath))
{
DelectDir(mRawPath);
}
CopySubFun(sourceFolderName, mRawPath, true);
mCsvFileCount = 0;
CopySubFun(sourceFolderName, mRawPath, true);//因为此函数会递归,所以把它摘出来
}
private void CopySubFun(string sourceFolderName, string destFolderName, bool overwrite)
public void CopyCalFiles2Project(string sourceFolderName)
{
if (!Directory.Exists(sourceFolderName))
{
return;
}
if (!Directory.Exists(mMetadataPath))
{
Directory.CreateDirectory(mMetadataPath);
}
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 = mMetadataPath;
string sourceFileName = Path.GetFileName(sourceFilePath);//获取文件名
File.Copy(sourceFilePath, Path.Combine(dest, sourceFileName), true);
}
else if (Directory.Exists(sourceFilePath))//是文件夹,拷贝文件夹;并递归
{
string lastDirectory = forlders[forlders.Length - 1];
string dest = Path.Combine(mMetadataPath, lastDirectory);
if (!Directory.Exists(dest))
{
Directory.CreateDirectory(dest);
}
CopySubFun(sourceFilePath, dest, true);
}
}
}
private void CopySubFun(string sourceFolderName, string destFolderName, bool overwrite)//因为此函数会递归,所以把它摘出来
{
if (!Directory.Exists(sourceFolderName))
{
@ -307,6 +515,9 @@ namespace mainProgram
string dest = destFolderName;
string sourceFileName = Path.GetFileName(sourceFilePath);//获取文件名
File.Copy(sourceFilePath, Path.Combine(dest, sourceFileName), overwrite);
//统计文件个数
mCsvFileCount++;
}
else if (Directory.Exists(sourceFilePath))//是文件夹,拷贝文件夹;并递归
{
@ -320,6 +531,11 @@ namespace mainProgram
CopySubFun(sourceFilePath, dest, overwrite);
}
}
//将文件个数写入到可执行文件所在目录 → 打开工程时需要读取
BinaryWriter bw = new BinaryWriter(new FileStream(Path.Combine(mMetadataPath, "CsvFileCount.dat"), FileMode.Create));
bw.Write(mCsvFileCount);
bw.Close();
}
private void DelectDir(string srcPath)
@ -388,13 +604,13 @@ namespace mainProgram
return txtFileCount;
}
public int getDatFileCount()
public int getCalFileCount()
{
string[] metaFilesPath = Directory.GetFileSystemEntries(mMetadataPath);
int datFileCount = 0;
for (int i = 0; i < metaFilesPath.Length; i++)//
{
if (metaFilesPath[i].Contains("dat"))
if (metaFilesPath[i].Contains("cal"))
{
datFileCount++;
}
@ -403,11 +619,14 @@ namespace mainProgram
return datFileCount;
}
public UpdateProgressBarInfoDelegate UpdateProgressBarInfo;//
//处理工程中的数据
public void Rad()
{
int counter = 0;
//读取标定文件:辐射定标文件 + 非线性校正文件
int datFileCount = getDatFileCount();
int datFileCount = getCalFileCount();
int txtFileCount = getTxtFileCount();
CalData[] calDatas = new CalData[datFileCount];
@ -422,7 +641,7 @@ namespace mainProgram
nonLinearDatas[d1] = ReadNonLinearFile(metaFilesPath[i]);
d1++;
}
else if (metaFilesPath[i].Contains("dat"))
else if (metaFilesPath[i].Contains("cal"))
{
calDatas[d2] = ReadCalFile(metaFilesPath[i]);
d2++;
@ -447,14 +666,12 @@ namespace mainProgram
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文件中的每条光谱
{
@ -469,9 +686,9 @@ namespace mainProgram
{
sp.NonLinearCorrection(nonLinearDatas[nonLinearData_index].nonLinearData, spectralData.spectral, spectralData.spectralDataLength);
}
if (nonLinearData_index >= 0)//福亮度转换
if (calData_index >= 0)//福亮度转换
{
sp.NonLinearCorrection(nonLinearDatas[nonLinearData_index].nonLinearData, spectralData.spectral, spectralData.spectralDataLength);
sp.RadCorrection(calDatas[calData_index].gain, calDatas[calData_index].exposureTime, spectralData.spectral, spectralData.exposureTime, spectralData.spectralDataLength);
}
spectralDataReaderWriter.UpdateSpectral(j, spectralData);
}
@ -479,8 +696,15 @@ namespace mainProgram
string destFileName = Path.Combine(dest, name);
spectralDataReaderWriter.SaveCSV(destFileName);
counter++;
float tmp1 = (float)counter / (float)mCsvFileCount;
float tmp2 = tmp1 * 100;
UpdateProgressBarInfo((int)tmp2, name + "\r\n");
}
}
//福亮度转换完成后,需要做得动作
}
/*
@ -505,7 +729,7 @@ namespace mainProgram
{
for (int i = 0; i < calData.GetLength(0); i++)
{
if (calData[i].SN.Contains(spectralData.SN))
if (calData[i].SN.Contains(spectralData.SN) && calData[i].position==spectralData.position)
{
return i;
}
@ -519,15 +743,24 @@ namespace mainProgram
CalData calFile;
string[] forlders = calFilePath.Split('\\');
calFile.SN = forlders[forlders.Length - 1];
string filename = forlders[forlders.Length - 1].Split('.')[0];
string[] xx = filename.Split('_');
calFile.SN = xx[0];
//string tmp = xx[1][xx[1].Length - 1];
int s = int.Parse(xx[1]);
calFile.position = s;
FileStream fs2 = new FileStream(calFilePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs2);
calFile.exposureTime = br.ReadInt32();
calFile.exposureTime = br.ReadUInt16();
calFile.temperature = br.ReadSingle();
calFile.pixelCount = br.ReadInt32();
calFile.temperature = br.ReadInt32();
calFile.waveLengthInNM = new float[calFile.pixelCount];
calFile.gain = new double[calFile.pixelCount];
calFile.offset = new double[calFile.pixelCount];
for (int ii = 0; ii < calFile.pixelCount; ii++)
{
@ -537,6 +770,10 @@ namespace mainProgram
{
calFile.gain[ii] = br.ReadDouble();
}
for (int ii = 0; ii < calFile.pixelCount; ii++)
{
calFile.offset[ii] = br.ReadDouble();
}
return calFile;
}
@ -545,7 +782,7 @@ namespace mainProgram
{
NonLinearData nonLinearData;
string[] forlders = nonLinearFilePath.Split('\\');
nonLinearData.SN = forlders[forlders.Length - 1];
nonLinearData.SN = forlders[forlders.Length - 1].Split('.')[0];
int lineCount = FindMaxRowCount(nonLinearFilePath);
@ -583,5 +820,123 @@ namespace mainProgram
return maxColunmCount;
}
DateFolder[] df;
public void FindTimespan()//因为此函数会递归,所以把它摘出来
{
if (!Directory.Exists(mRadPath))
{
return;
}
string[] dateFolders = Directory.GetFileSystemEntries(mRadPath);
df = new DateFolder[dateFolders.Length];
for (int i = 0; i < dateFolders.Length; i++)
{
string dateFolder = dateFolders[i];
if (Directory.Exists(dateFolder))//是文件夹
{
string[] splitPath = dateFolder.Split('\\');//
string lastDirectory = splitPath[splitPath.Length - 1];
df[i] = new DateFolder(lastDirectory);
}
}
Array.Sort(df);
}
public string GetMinDate()
{
return df[0].mDateFolder;
}
public string GetMaxDate()
{
return df[df.Length - 1].mDateFolder;
}
public void CopyWantedFiles(string minTime, string maxTime)
{
mSifTmpPath = Path.Combine(mProjectPath, "sifTmp");//mSifTmpPath
if (false == Directory.Exists(mSifTmpPath))
{
Directory.CreateDirectory(mSifTmpPath);
}
DelectDir(mSifTmpPath);//删除文件夹中的内容
//遍历,复制文件夹[minTimemaxTime]中的文件到mSifTmpPath
DateFolder minDateFolder = new DateFolder(minTime);
DateFolder maxDateFolder = new DateFolder(maxTime);
foreach (DateFolder currentFolder in df)
{
if (minDateFolder > currentFolder | currentFolder > maxDateFolder)
{
continue;
}
//复制文件
string sourcePath = Path.Combine(mRadPath, currentFolder.mDateFolder);
CopyWithModifyName(sourcePath, mSifTmpPath, true);
}
}
private void CopyWithModifyName(string sourceFolderPath, string destFolderPath, bool overwrite)//
{
if (!Directory.Exists(sourceFolderPath))
{
return;
}
if (!Directory.Exists(destFolderPath))
{
Directory.CreateDirectory(destFolderPath);
}
string[] sourceFilesPath = Directory.GetFileSystemEntries(sourceFolderPath);
for (int i = 0; i < sourceFilesPath.Length; i++)
{
string sourceFilePath = sourceFilesPath[i];
//构建文件名
string destFileName="";
string[] forlders = sourceFilePath.Split('\\');
string sourceFolderName = forlders[forlders.Length - 2];//获取文件所在目录的目录名
string sourceFileName = forlders[forlders.Length - 1];//获取文件名sourceFileName
//string sourceFileName = Path.GetFileName(sourceFilePath);
int firstIndex = sourceFileName.IndexOf("_");
int lastIndex = sourceFileName.LastIndexOf("_");
if (firstIndex != -1)
{
destFileName = sourceFileName.Substring(firstIndex);
}
if (File.Exists(sourceFilePath))//是文件,直接拷贝
{
File.Copy(sourceFilePath, Path.Combine(destFolderPath, sourceFolderName + destFileName), overwrite);
}
else if (Directory.Exists(sourceFilePath))//是文件夹,拷贝文件夹;并递归 → 减而治之
{
string lastDirectory = forlders[forlders.Length - 1];
string dest = Path.Combine(destFolderPath, lastDirectory);
if (!Directory.Exists(dest))
{
Directory.CreateDirectory(dest);
}
CopySubFun(sourceFilePath, dest, overwrite);
}
}
}
}
}