public static void WriteResults(int ensembleIndex, out int nbMembers, CropEngine cropEngine) { CropInput input = cropEngine.CropInput; nbMembers = cropEngine.DTRain.Columns.Count - 1; // do not count time column // for Forecast and climate change 2 type of TS will be saved: WeightedTS and Envelope // the lines below will create a list 'selectedTypes' which has: // 1 element for historic: so results will only be saved as WeightedTS // 2 elements for Forecast and CC: so results will be saved as WeightedTS and Envelope CropInput.TSType[] selectedTypes;// = new TSTypeList<TSType>(); if (input.selectedMode == CropInput.Mode.Historic) { selectedTypes = new CropInput.TSType[2]; selectedTypes[0] = CropInput.TSType.WeightedTS; selectedTypes[1] = CropInput.TSType.Table; } else { selectedTypes = new CropInput.TSType[3]; selectedTypes[0] = CropInput.TSType.WeightedTS; selectedTypes[1] = CropInput.TSType.Envelope; selectedTypes[2] = CropInput.TSType.Table; } string csvSuffix = input.selectedArea.ToString() + "_" + input.basinName + "_" + input.idRow + ".csv"; // all csv files end with this string foreach (CropInput.TSType selectedType in selectedTypes) { // path to the output folder string outfolder = System.IO.Path.Combine(input.basinFolder, @"01 Analysis\AquaCrop\Results", input.userName, selectedType.ToString()); string selectedModeToString = input.selectedMode.ToString(); if (input.selectedMode == CropInput.Mode.ClimateChange) { selectedModeToString = "Climate change"; // workaround to handle the space in this type (web expects a space in the file name) } // Demand List <double> rdmValue_Demand; string outfn = Path.Combine(outfolder, selectedModeToString + "_CropWaterDemand_" + csvSuffix); dt2csv.ExportDT2csv(cropEngine.DTIrrDemand, outfn, -99, selectedType, "mm", "Cumulative", out rdmValue_Demand); System.Console.WriteLine("\tDemand csv saved at: " + outfn); // Biomass relative List <double> rdmValue_BiomassRel; outfn = Path.Combine(outfolder, selectedModeToString + "_BiomassRel_" + csvSuffix); dt2csv.ExportDT2csv(cropEngine.DTBiomassRel, outfn, -99, selectedType, "%", "LastValue", out rdmValue_BiomassRel); System.Console.WriteLine("\tBiomass rel csv saved at: " + outfn); // Rain List <double> rdmValue_Rain; outfn = Path.Combine(outfolder, selectedModeToString + "_Rain_" + csvSuffix); dt2csv.ExportDT2csv(cropEngine.DTRain, outfn, -99, selectedType, "mm", "Cumulative", out rdmValue_Rain); System.Console.WriteLine("\tRain csv saved at: " + outfn); // ET List <double> rdmValue_ET; outfn = Path.Combine(outfolder, selectedModeToString + "_ET_" + csvSuffix); dt2csv.ExportDT2csv(cropEngine.DTET, outfn, -99, selectedType, "mm", "Cumulative", out rdmValue_ET); System.Console.WriteLine("\tET csv saved at: " + outfn); // Wr List <double> rdmValue_Depletion; outfn = Path.Combine(outfolder, selectedModeToString + "_Depletion_" + csvSuffix); dt2csv.ExportDT2csv(cropEngine.DTDepletion, outfn, -99, selectedType, "mm", "None", out rdmValue_Depletion); System.Console.WriteLine("\tDepletion csv saved at: " + outfn); // Stage List <double> rdmValue_Stage; outfn = Path.Combine(outfolder, selectedModeToString + "_Stage_" + csvSuffix); dt2csv.ExportDT2csv(cropEngine.DTStage, outfn, -99, selectedType, "Stage", "None", out rdmValue_Stage); System.Console.WriteLine("\tStage csv saved at: " + outfn); // Root depth List <double> rdmValue_RootDepth; outfn = Path.Combine(outfolder, selectedModeToString + "_RootDepth_" + csvSuffix); dt2csv.ExportDT2csv(cropEngine.DTRootDepth, outfn, -99, selectedType, "m", "LastValue", out rdmValue_RootDepth); System.Console.WriteLine("\tRoot depth csv saved at: " + outfn); // Yield List <double> rdmValue_Yield; outfn = Path.Combine(outfolder, selectedModeToString + "_Yield_" + csvSuffix); dt2csv.ExportDT2csv(cropEngine.DTYield, outfn, -99, selectedType, "ton/ha", "LastValue", out rdmValue_Yield); System.Console.WriteLine("\tYield csv saved at: " + outfn); // Biomass List <double> rdmValue_DTBiomass; outfn = Path.Combine(outfolder, selectedModeToString + "_Biomass_" + csvSuffix); dt2csv.ExportDT2csv(cropEngine.DTBiomass, outfn, -99, selectedType, "ton/ha", "LastValue", out rdmValue_DTBiomass); System.Console.WriteLine("\tBiomass csv saved at: " + outfn); // Harvest index List <double> rdmValue_Hi; outfn = Path.Combine(outfolder, selectedModeToString + "_HarvestIndex_" + csvSuffix); dt2csv.ExportDT2csv(cropEngine.DTHi, outfn, -99, selectedType, "%", "LastValue", out rdmValue_Hi); System.Console.WriteLine("\tHarvest index csv saved at: " + outfn); // Wpet List <double> rdmValue_Wpet; outfn = Path.Combine(outfolder, selectedModeToString + "_Wpet_" + csvSuffix); dt2csv.ExportDT2csv(cropEngine.DTWpet, outfn, -99, selectedType, "kg/m^3", "LastValue", out rdmValue_Wpet); System.Console.WriteLine("\tRoot depth csv saved at: " + outfn); // RelCropTransp List <double> rdmValue_RelCropTransp; outfn = Path.Combine(outfolder, selectedModeToString + "_RelCropTransp_" + csvSuffix); dt2csv.ExportDT2csv(cropEngine.DTRelCropTransp, outfn, -99, selectedType, "%", "LastValue", out rdmValue_RelCropTransp); System.Console.WriteLine("\tRel Crop Transp csv saved at: " + outfn); if (selectedType == CropInput.TSType.WeightedTS) { List <string> rdmValue_all = new List <string>(); for (int j = 0; j < nbMembers; j++) { string row = rdmValue_Demand[j].ToString(CultureInfo.InvariantCulture) + ","; row += rdmValue_BiomassRel[j].ToString(CultureInfo.InvariantCulture) + ","; row += rdmValue_Rain[j].ToString(CultureInfo.InvariantCulture) + ","; row += rdmValue_ET[j].ToString(CultureInfo.InvariantCulture) + ","; row += rdmValue_Yield[j].ToString(CultureInfo.InvariantCulture) + ","; row += rdmValue_DTBiomass[j].ToString(CultureInfo.InvariantCulture) + ","; row += rdmValue_Hi[j].ToString(CultureInfo.InvariantCulture) + ","; row += rdmValue_Wpet[j].ToString(CultureInfo.InvariantCulture) + ","; row += rdmValue_RelCropTransp[j].ToString(CultureInfo.InvariantCulture); rdmValue_all.Add(row); } WriteRDMValues(ensembleIndex, cropEngine, rdmValue_all); } } }
public static void ExportDT2csv(DataTable dataDT, string outfn, int itemno, CropInput.TSType selectedType, string unit, string rdmStat, out List <double> rdmValues_tmp) { rdmValues_tmp = new List <double>(); // Make sure the directory exists System.IO.Directory.CreateDirectory(Path.GetDirectoryName(outfn)); string csv_fn = outfn.Replace(Path.GetExtension(outfn), ".csv"); StreamWriter swg = new StreamWriter(csv_fn, false, System.Text.Encoding.ASCII); //Initialise the csv file if (selectedType == CropInput.TSType.Envelope) { dataDT = Process_Envelope(dataDT, 0.25, 0.75); } if (selectedType == CropInput.TSType.Envelope || selectedType == CropInput.TSType.WeightedTS) { string csv_header = "Time"; int noitems = 1; //if itemno == -99 then all items are saved to the csv file if (itemno == -99) { noitems = dataDT.Columns.Count - 1; for (int i = 1; i <= noitems; i++) { csv_header = csv_header + "," + dataDT.Columns[i].Caption.ToString(); } } else { csv_header = csv_header + "," + dataDT.Columns[itemno].Caption.ToString(); } swg.WriteLine(csv_header); for (int k = 1; k <= noitems; k++) { rdmValues_tmp.Add(0.0); } // add a new line with unit if (unit.Length > 0) { csv_header = "unit==" + unit; } swg.WriteLine(csv_header); string csv_values = ""; if (dataDT.Rows.Count > 0) { for (int i = 0; i < dataDT.Rows.Count - 1; i++) { DateTime Dato; bool Isdate = DateTime.TryParse(dataDT.Rows[i][0].ToString(), out Dato); csv_values = Dato.ToString("yyyy-MM-dd HH:mm:ss"); bool ch_delvalue = false; for (int k = 1; k <= noitems; k++) { int item_count = k; //if (noitems == 1) // item_count = itemno; ch_delvalue = false; double Value; bool isNum = double.TryParse(dataDT.Rows[i][item_count].ToString(), out Value); if (isNum == false) { Value = -9999d; ch_delvalue = true; } if (ch_delvalue == true) { csv_values = csv_values + "," + "-9999"; } else { csv_values = csv_values + "," + Value.ToString("0.000", CultureInfo.InvariantCulture); } if (rdmStat == "Cumulative") { rdmValues_tmp[k - 1] = rdmValues_tmp[k - 1] + Value; } else if (rdmStat == "LastValue") { rdmValues_tmp[k - 1] = Value; } } swg.WriteLine(csv_values); } } swg.Close(); } else if (selectedType == CropInput.TSType.Table) { string csv_header = "description==Monthly " + dataDT.Columns[1].Caption.ToString() + " " + unit; swg.WriteLine(csv_header); csv_header = "Time,Jan.,Feb.,March,April,May,June,July,Aug.,Sept.,Oct.,Nov.,Dec.,Avg.,Min.,Max."; swg.WriteLine(csv_header); int noitems = dataDT.Columns.Count - 1; if (noitems > 1) { dataDT = Process_Envelope(dataDT, 0.25, 0.75); // then we use only the median } string csv_values = ""; double minMonth = Double.MaxValue; double maxMonth = Double.MinValue; double avgMonth = Double.NaN; double avg = Double.NaN; double acc = Double.NaN; int count = 0; int countMonth = 0; if (dataDT.Rows.Count > 0) { for (int i = 0; i < dataDT.Rows.Count - 1; i++) { DateTime Dato; bool Isdate = DateTime.TryParse(dataDT.Rows[i][0].ToString(), out Dato); if (i == 0) { csv_values = Dato.ToString("yyyy"); // add comma if the first year is incomplete //for (int iMonth = 11; iMonth > Dato.Month; iMonth--) // csv_values += ","; for (int iMonth = 0; iMonth < Dato.Month - 1; iMonth++) { csv_values += ","; } } bool ch_delvalue = false; int k = 1; ch_delvalue = false; double Value; bool isNum = double.TryParse(dataDT.Rows[i][k].ToString(), out Value); if (isNum == false) { Value = -9999d; ch_delvalue = true; } if (ch_delvalue == true) { csv_values = csv_values + "," + "-9999"; } else { if (double.IsNaN(avg)) { avg = Value; } else { avg = (avg * count + Value) / (count + 1); } if (double.IsNaN(acc)) { acc = Value; } else { acc += Value; } count++; } // check if next month of next row is different bool bNewMonth = false; bool bNewYear = false; DateTime nextDato = Dato; if (((dataDT.Rows.Count - 2) == i)) { bNewYear = true; bNewMonth = true; } else if (DateTime.TryParse(dataDT.Rows[i + 1][0].ToString(), out nextDato)) { if (nextDato.Year != Dato.Year) { bNewYear = true; bNewMonth = true; } else if (nextDato.Month != Dato.Month) { bNewMonth = true; } } if (bNewMonth) { if (Double.IsNaN(avg)) { csv_values += "," + "-9999"; } else { csv_values += "," + avg.ToString("0.000", CultureInfo.InvariantCulture); } // monthly stats minMonth = Math.Min(minMonth, avg); maxMonth = Math.Max(maxMonth, avg); if (double.IsNaN(avgMonth)) { avgMonth = avg; } else { avgMonth = (avgMonth * countMonth + avg) / (countMonth + 1); } // reset for next month avg = Double.NaN; countMonth++; } if (bNewYear) { // add comma if the last year is incomplete for (int iMonth = Dato.Month; iMonth < 12; iMonth++) { csv_values += ","; } csv_values += "," + avgMonth.ToString("0.000", CultureInfo.InvariantCulture) + "," + minMonth.ToString("0.000", CultureInfo.InvariantCulture) + "," + maxMonth.ToString("0.000", CultureInfo.InvariantCulture); minMonth = Double.MaxValue; maxMonth = Double.MinValue; avgMonth = Double.NaN; countMonth = 0; swg.WriteLine(csv_values); csv_values = nextDato.ToString("yyyy"); } } } swg.Close(); } }