// End ReadDataPointFile public static void ArrayInitializer(ref SALSADataPointProperties[] DataArray, int sizemax, int sizereadin) { if (DataArray != null) { if (DataArray.Length < sizereadin) { Exception e = SALSAUtility.SALSAError(" Data Array too small for file Length " + DataArray.Length.ToString() + " Needs " + sizereadin.ToString()); throw (e); } return; } int size = sizereadin; if (size == 0) { size = sizemax; } DataArray = new SALSADataPointProperties[size]; return; }
// End ReadDataPointProperties public static void AppendDataPointProperties(SALSADataPointProperties DataPointProps, ref string InputLine) { //Calling Routine must supply any needed delimiter before this is appended InputLine += "PointProperties"; if (DataPointProps.valuesset) { // x y z are set InputLine += "\tvaluesset:true\tsource:" + DataPointProps.source + "\tx:" + DataPointProps.x.ToString("E4"); InputLine += "\ty:" + DataPointProps.y.ToString("E4"); InputLine += "\tz:" + DataPointProps.z.ToString("E4"); } if (DataPointProps.errorsset) { // x y z errors are set InputLine += "\terrorsset:true\txerr:" + DataPointProps.xerr.ToString("E4"); InputLine += "\tyerr:" + DataPointProps.yerr.ToString("E4"); InputLine += "\tzerr:" + DataPointProps.zerr.ToString("E4"); } if (DataPointProps.family1 >= 0) { InputLine += "\tfamily1:" + DataPointProps.family1.ToString(); if (DataPointProps.familylabel1.Length > 0) { InputLine += "\tfamilylabel1:" + DataPointProps.familylabel1; } } if (DataPointProps.family2 >= 0) { InputLine += "\tfamily2:" + DataPointProps.family2.ToString(); if (DataPointProps.familylabel2.Length > 0) { InputLine += "\tfamilylabel2:" + DataPointProps.familylabel2; } } if (DataPointProps.cluster >= 0) { InputLine += "\tcluster:" + DataPointProps.cluster.ToString(); if (DataPointProps.clusterlabel.Length > 0) { InputLine += "\tclusterlabel:" + DataPointProps.clusterlabel; } } if (DataPointProps.group >= 0) { InputLine += "\tgroup:" + DataPointProps.group.ToString(); if (DataPointProps.grouplabel.Length > 0) { InputLine += "\tgrouplabel:" + DataPointProps.grouplabel; } } if (DataPointProps.pointlabel.Length > 0) { InputLine += "\tpointlabel:" + DataPointProps.pointlabel; } InputLine += "\tFixedorVaried:" + DataPointProps.FixedorVaried.ToString(); InputLine += "\tPointType:" + DataPointProps.PointType.ToString(); if (DataPointProps.LocalPointNumber >= 0) { InputLine += "\tLocalPointNumber:" + DataPointProps.LocalPointNumber.ToString(); } if (DataPointProps.OriginalPointNumber >= 0) { InputLine += "\tOriginalPointNumber:" + DataPointProps.OriginalPointNumber.ToString(); } }
// End WriteFileProperties public static void ReadDataPointProperties(SALSADataPointProperties DataPointProps, string InputLine) { InputLine = InputLine.Trim(new[] { ' ', '\t' }); string[] split = InputLine.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); DataPointProps.valuesset = false; DataPointProps.errorsset = false; for (int itembackwards = 0; itembackwards < split.Length; itembackwards++) { int item = split.Length - itembackwards - 1; if (split[item].Equals("PointProperties")) { continue; } string[] colonsplit = split[item].Split(new[] { ':' }, 2); if (colonsplit[0].Equals("PointProperties")) { continue; } if (colonsplit.Length < 2) { if (item == 0) { Console.WriteLine("No deliminator in item " + split[item] + " in Line " + InputLine); } else { split[item - 1] += " " + split[item]; continue; } } colonsplit[0] = colonsplit[0].Trim(); colonsplit[1] = colonsplit[1].Trim(); if (colonsplit[0].Equals("x")) { DataPointProps.x = Convert.ToDouble(colonsplit[1]); DataPointProps.valuesset = true; } if (colonsplit[0].Equals("y")) { DataPointProps.y = Convert.ToDouble(colonsplit[1]); } if (colonsplit[0].Equals("z")) { DataPointProps.z = Convert.ToDouble(colonsplit[1]); } if (colonsplit[0].Equals("xerr")) { DataPointProps.xerr = Convert.ToDouble(colonsplit[1]); DataPointProps.errorsset = true; } if (colonsplit[0].Equals("source")) { DataPointProps.source = colonsplit[1]; } if (colonsplit[0].Equals("yerr")) { DataPointProps.yerr = Convert.ToDouble(colonsplit[1]); } if (colonsplit[0].Equals("zerr")) { DataPointProps.zerr = Convert.ToDouble(colonsplit[1]); } if (colonsplit[0].Equals("family1")) { DataPointProps.family1 = Convert.ToInt32(colonsplit[1]); } if (colonsplit[0].Equals("familylabel1")) { DataPointProps.familylabel1 = colonsplit[1]; } if (colonsplit[0].Equals("family2")) { DataPointProps.family2 = Convert.ToInt32(colonsplit[1]); } if (colonsplit[0].Equals("familylabel2")) { DataPointProps.familylabel2 = colonsplit[1]; } if (colonsplit[0].Equals("cluster")) { DataPointProps.cluster = Convert.ToInt32(colonsplit[1]); } if (colonsplit[0].Equals("clusterlabel")) { DataPointProps.clusterlabel = colonsplit[1]; } if (colonsplit[0].Equals("group")) { DataPointProps.group = Convert.ToInt32(colonsplit[1]); } if (colonsplit[0].Equals("grouplabel")) { DataPointProps.grouplabel = colonsplit[1]; } if (colonsplit[0].Equals("pointlabel")) { DataPointProps.pointlabel = colonsplit[1]; } if (colonsplit[0].Equals("FixedorVaried")) { DataPointProps.FixedorVaried = Convert.ToInt32(colonsplit[1]); } if (colonsplit[0].Equals("PointType")) { DataPointProps.PointType = Convert.ToInt32(colonsplit[1]); } if (colonsplit[0].Equals("LocalPointNumber")) { DataPointProps.LocalPointNumber = Convert.ToInt32(colonsplit[1]); } if (colonsplit[0].Equals("OriginalPointNumber")) { DataPointProps.OriginalPointNumber = Convert.ToInt32(colonsplit[1]); } } return; }
// End ArrayInitializer // Write label-cluster results into a file public static void WriteDataPointFile(string CoreOutputFileName, bool write2Das3D, string OutputStyles, SALSAFileProperties FileProperties, SALSADataPointProperties[] DataPoints, int NumberofDataPoints) { var DothisOutputStyle = new bool[5]; for (int StyleIndex = 0; StyleIndex < 5; StyleIndex++) { DothisOutputStyle[StyleIndex] = false; if (OutputStyles.Contains("all")) { DothisOutputStyle[StyleIndex] = true; } } if (OutputStyles.Contains("colon")) { DothisOutputStyle[0] = true; } if (OutputStyles.Contains("family1")) { DothisOutputStyle[1] = true; } if (OutputStyles.Contains("family2")) { DothisOutputStyle[2] = true; } if (OutputStyles.Contains("cluster")) { DothisOutputStyle[3] = true; } if (OutputStyles.Contains("group")) { DothisOutputStyle[4] = true; } bool setgroup = false; bool OutputValues = false; SALSADataPointProperties FirstOne = DataPoints[0]; if (FirstOne.family1 < 0) { DothisOutputStyle[1] = false; } if (FirstOne.family2 < 0) { DothisOutputStyle[2] = false; } if (FirstOne.cluster < 0) { DothisOutputStyle[3] = false; } if (FirstOne.group < 0) { DothisOutputStyle[4] = false; } if ((FirstOne.family1 < 0) && (FirstOne.family2 < 0) && (FirstOne.cluster < 0) && (FirstOne.group < 0)) { DothisOutputStyle[4] = true; setgroup = true; } if (FirstOne.valuesset) { OutputValues = true; } int LocalVectorDimension = FileProperties.LocalVectorDimension; int LocalPointIncrement = FileProperties.LocalPointStartIndex; int numberoffiles = 0; for (int StyleIndex = 0; StyleIndex < 5; StyleIndex++) { if (DothisOutputStyle[StyleIndex]) { ++numberoffiles; } } if (numberoffiles == 0) { SALSAUtility.SALSAError("No files output for core name " + CoreOutputFileName); return; } if (numberoffiles > 1) { if (OutputStyles.Contains("SameFileName")) { Exception e = SALSAUtility.SALSAError("Attempt to generate multiple outputs to same file " + CoreOutputFileName); throw (e); } } for (int StyleIndex = 0; StyleIndex < 5; StyleIndex++) { if (!DothisOutputStyle[StyleIndex]) { continue; } string OutputFileName = ""; if (!OutputStyles.Contains("SameFileName")) { if (StyleIndex == 0) { OutputFileName = CoreOutputFileName.Replace(".txt", "Colon.txt"); } if (StyleIndex == 1) { OutputFileName = CoreOutputFileName.Replace(".txt", "Family1.txt"); } if (StyleIndex == 2) { OutputFileName = CoreOutputFileName.Replace(".txt", "Family2.txt"); } if (StyleIndex == 3) { OutputFileName = CoreOutputFileName.Replace(".txt", "Cluster.txt"); } if (StyleIndex == 4) { OutputFileName = CoreOutputFileName.Replace(".txt", "Group.txt"); } } else { OutputFileName = CoreOutputFileName; } try { StreamWriter sw = null; if (!string.IsNullOrEmpty(OutputFileName)) { sw = new StreamWriter(OutputFileName, false, Encoding.UTF8); } if (sw != null) { if (StyleIndex == 0) { WriteFileProperties(FileProperties, sw); // Write Header of a Colon File } for (int GlobalDataPoint = 0; GlobalDataPoint < NumberofDataPoints; GlobalDataPoint++) { SALSADataPointProperties ThesePointProperties = DataPoints[GlobalDataPoint]; if ((LocalVectorDimension == 2) && write2Das3D && OutputValues) { ThesePointProperties.z = 0.0; ThesePointProperties.zerr = 0.0; } if (StyleIndex == 0) { string OutputLine = ""; AppendDataPointProperties(ThesePointProperties, ref OutputLine); sw.WriteLine(OutputLine); continue; } int IntegerIndex = 0; if (StyleIndex == 1) { IntegerIndex = ThesePointProperties.family1; } if (StyleIndex == 2) { IntegerIndex = ThesePointProperties.family2; } if (StyleIndex == 3) { IntegerIndex = ThesePointProperties.cluster; } if (StyleIndex == 4) { IntegerIndex = ThesePointProperties.group; if (setgroup) { IntegerIndex = 1; } } string Coordinates = ""; if (OutputValues) { Coordinates = ThesePointProperties.x.ToString("E4") + "\t" + ThesePointProperties.y.ToString("E4") + "\t"; if ((LocalVectorDimension == 3) || write2Das3D) { Coordinates += ThesePointProperties.z.ToString("E4") + "\t"; } } sw.WriteLine( String.Format((GlobalDataPoint + LocalPointIncrement).ToString() + "\t" + Coordinates + IntegerIndex.ToString())); } sw.Flush(); sw.Close(); } } catch { Exception e = SALSAUtility.SALSAError(" Failed to Write Properties File " + CoreOutputFileName); throw (e); } } // End Loop over File Types return; }
public static bool ReadDataPointFile(string MDSClusterFileName, ref int FileType, SALSAFileProperties FileProperties, ref SALSADataPointProperties[] DataPoints, ref int NumberofPoints) { // FileType = 0 simple integers and positions: PointNumber x y z LABEL or Group Number (LABEL if file name contains label) // FileType = 1 Just integers -- Point Number and Group Number: PointNumber Group Number // File Type = 2 Integer and Label: Point LABEL (LABEL if file name contains label) // File Type = 3 Pure colon style -- with positions // File Type = 4 Pure Colon Style -- no positions // File Type = 5 Pure Colon Style -- Labels // File Type = 6 Hybrid Style -- with positions // File Type = 7 Hybrid Style -- no positions // File Type = 8 Hybrid Style -- Labels bool positionsset = false; bool colonsinput = false; bool NonColonsInput = false; bool labelfile = false; string LowerFileName = MDSClusterFileName.ToLower(); if (LowerFileName.Contains("label")) { labelfile = true; } NumberofPoints = 0; try { // Check if file exists if (!File.Exists(MDSClusterFileName)) { Exception e = SALSAUtility.SALSAError("File " + MDSClusterFileName + " does not exists"); throw (e); } // Create a new stream to read from a file using (StreamReader sr = File.OpenText(MDSClusterFileName)) { // Read contents of a file String inputLineStr; int newlabelnumber = -1; int LocalStart = FileProperties.LocalPointStartIndex; int OriginalStart = FileProperties.OriginalPointStartIndex; while ((inputLineStr = sr.ReadLine()) != null) { if (inputLineStr.Length < 2) { continue; //replace empty line } inputLineStr = inputLineStr.Trim(new[] { ' ', '\t' }); try { // Parse each record string inputLineStr = inputLineStr.Replace("\t\t", "\t"); if (inputLineStr.Contains("FileProperties")) { // Not a Data Point string EndofInput = inputLineStr.Replace("FileProperties", ""); ReadFileProperties(FileProperties, EndofInput); colonsinput = true; LocalStart = FileProperties.LocalPointStartIndex; OriginalStart = FileProperties.OriginalPointStartIndex; } else { // Must be a Data Point ArrayInitializer(ref DataPoints, SALSAUtility.NumberOriginalPoints, FileProperties.NumberPointsinFile); DataPoints[NumberofPoints] = new SALSADataPointProperties(); bool incrementcount = false; int PointDataStarts = inputLineStr.IndexOf("PointProperties"); if (PointDataStarts >= 0) { // Some Colon Information string EndofInput = inputLineStr.Substring(PointDataStarts); EndofInput = EndofInput.Replace("PointProperties", ""); ReadDataPointProperties(DataPoints[NumberofPoints], EndofInput); colonsinput = true; if (DataPoints[NumberofPoints].valuesset) { positionsset = true; } incrementcount = true; DataPoints[NumberofPoints].LocalPointNumber = NumberofPoints + FileProperties.LocalPointStartIndex; } // End Processing Colon Point Information if (PointDataStarts < 0) { // traditional bare line PointDataStarts = inputLineStr.Length; } if (PointDataStarts > 0) { // Process number information string StartofString = inputLineStr.Substring(0, PointDataStarts); StartofString = StartofString.Trim(new[] { ' ', '\t' }); if (StartofString.Length > 0) { // You come here only for traditional bare line of x,y,z coordinates. string[] split = StartofString.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); if ((split.Length != 5) && (split.Length != 2) && (split.Length != 4)) { Exception e = SALSAUtility.SALSAError(" Bad Line " + split.Length.ToString() + " " + NumberofPoints.ToString() + " " + inputLineStr); throw (e); } newlabelnumber = Convert.ToInt32(split[0]); if ((NumberofPoints + LocalStart) != newlabelnumber) { Exception e = SALSAUtility.SALSAError("Unexpected Label Number " + newlabelnumber + " Expected " + NumberofPoints.ToString() + " + " + LocalStart.ToString()); throw (e); } if (DataPoints[NumberofPoints].LocalPointNumber >= 0) { if ((DataPoints[NumberofPoints].LocalPointNumber - LocalStart) != NumberofPoints) { Exception e = SALSAUtility.SALSAError("Unexpected Local Number " + DataPoints[NumberofPoints].LocalPointNumber + " Expected " + NumberofPoints.ToString() + " + " + LocalStart.ToString()); throw (e); } } DataPoints[NumberofPoints].LocalPointNumber = NumberofPoints; if (DataPoints[NumberofPoints].OriginalPointNumber >= 0) { if ((DataPoints[NumberofPoints].OriginalPointNumber - OriginalStart) < 0) { Exception e = SALSAUtility.SALSAError("Unexpected Original Number " + DataPoints[NumberofPoints]. OriginalPointNumber + " Local Point " + NumberofPoints.ToString() + " Original Increment " + OriginalStart.ToString()); throw (e); } DataPoints[NumberofPoints].OriginalPointNumber -= OriginalStart; } else { DataPoints[NumberofPoints].OriginalPointNumber = newlabelnumber; } if (labelfile) { DataPoints[NumberofPoints].pointlabel = split[split.Length - 1]; } else { DataPoints[NumberofPoints].group = Convert.ToInt32(split[split.Length - 1]); } if (split.Length >= 4) { DataPoints[NumberofPoints].valuesset = true; DataPoints[NumberofPoints].x = Convert.ToDouble(split[1]); DataPoints[NumberofPoints].y = Convert.ToDouble(split[2]); positionsset = true; } if (split.Length == 5) { DataPoints[NumberofPoints].z = Convert.ToDouble(split[3]); } incrementcount = true; NonColonsInput = true; } // End Processing non colon Point information } if (incrementcount) { ++NumberofPoints; } } } catch (Exception e) { SALSAUtility.SALSAError("Failed to load data array " + inputLineStr + " " + " " + NumberofPoints.ToString() + " " + newlabelnumber.ToString() + " " + e); throw (e); } } FileType = 1; if (positionsset) { FileType = 0; } if (labelfile) { FileType = 2; } if (colonsinput) { if (NonColonsInput) { FileType += 6; } else { FileType += 3; } } if (FileProperties.NumberOriginalPoints == 0) { FileProperties.NumberOriginalPoints = NumberofPoints; } if (FileProperties.NumberPointsinFile == 0) { FileProperties.NumberPointsinFile = NumberofPoints; } if (FileProperties.NumberPointsinFile != NumberofPoints) { Exception e = SALSAUtility.SALSAError("Unexpected Number of Points in File " + NumberofPoints.ToString() + " Read but Expected " + FileProperties.NumberPointsinFile.ToString()); throw (e); } sr.Close(); } } catch (Exception e) { SALSAUtility.SALSAError("Failed to read data from " + MDSClusterFileName + " " + e); throw (e); } return(true); }