/// <summary> /// Join data files /// </summary> /// <param name="inFiles">input nc files</param> /// <param name="outFile">joined output nc file</param> /// <param name="timeDimStr">time dimension name string</param> public static void JoinDataFiles_Time_bak1(List <string> inFiles, string outFile, string timeDimStr) { //Check number of selected files int fNum = inFiles.Count; if (fNum < 2) { MessageBox.Show("There should be at least two files!", "Error"); return; } //Open the first file int i, j; string aFile = inFiles[0]; NetCDFDataInfo aDataInfo = new NetCDFDataInfo(); aDataInfo.ReadDataInfo(aFile); //Change time dimension as unlimit for (i = 0; i < aDataInfo.Dimensions.Count; i++) { Dimension aDimS = aDataInfo.Dimensions[i]; if (aDimS.DimName == timeDimStr) { //aDimS.dimLen = -1; //aDataInfo.dimList[i] = aDimS; aDataInfo.unlimdimid = aDimS.DimId; break; } } for (i = 0; i < aDataInfo.nvars; i++) { if (Global.MIMath.IsNumeric(aDataInfo.Variables[i].Name.Substring(0, 1))) { aDataInfo.Variables[i].Name = 'V' + aDataInfo.Variables[i].Name; } } int tDimid = 0; NetCDF4.nc_inq_dimid(aDataInfo.ncid, timeDimStr, out tDimid); object[] sttu = aDataInfo.GetStartTimeInfo(); DateTime sTime = (DateTime)sttu[0]; TimeUnit aTU = (TimeUnit)sttu[1]; //Create output nc file and write the data of the first file aDataInfo.CreateNCFile(outFile); //Write the data of first data file NetCDFDataInfo bDataInfo = new NetCDFDataInfo(); bDataInfo.ReadDataInfo(inFiles[0]); for (i = 0; i < bDataInfo.nvars; i++) { Variable aVarS = aDataInfo.Variables[i]; object[] varData = new object[1]; bDataInfo.GetVarData(aVarS, ref varData); int[] start = new int[aVarS.DimNumber]; int[] count = new int[aVarS.DimNumber]; for (int v = 0; v < aVarS.DimNumber; v++) { start[v] = 0; count[v] = bDataInfo.Dimensions[aVarS.Dimensions[v].DimId].DimLength; } aDataInfo.WriteVara(aVarS, start, count, varData); } //Join data //Add data for (i = 1; i < fNum; i++) { aFile = inFiles[i]; bDataInfo = new NetCDFDataInfo(); bDataInfo.ReadDataInfo(aFile); int tDimNum = 1; NetCDF4.nc_inq_dimlen(aDataInfo.ncid, tDimid, out tDimNum); for (j = 0; j < bDataInfo.nvars; j++) { Variable aVarS = bDataInfo.Variables[j]; int vIdx = aDataInfo.VariableNames.IndexOf(aVarS.Name); if (vIdx < 0) { continue; } if (aVarS.DimNumber > 1) { if (Array.IndexOf(aVarS.DimIds, tDimid) < 0) { continue; } object[] varData = new object[1]; bDataInfo.GetVarData(aVarS, ref varData); int[] start = new int[aVarS.DimNumber]; int[] count = new int[aVarS.DimNumber]; for (int v = 0; v < aVarS.DimNumber; v++) { start[v] = 0; count[v] = bDataInfo.Dimensions[aVarS.Dimensions[v].DimId].DimLength; } start[Array.IndexOf(aVarS.DimIds, tDimid)] = tDimNum; bDataInfo.WriteVaraData(aDataInfo.ncid, aDataInfo.Variables[vIdx].VarId, aVarS.NCType, start, count, varData); } if (aVarS.Name == timeDimStr) { object[] varData = new object[1]; bDataInfo.GetVarData(aVarS, ref varData); sttu = bDataInfo.GetStartTimeInfo(); DateTime nTime = (DateTime)sttu[0]; TimeUnit nTU = (TimeUnit)sttu[1]; if (DateTime.Compare(sTime, nTime) != 0) { double shift = 0.0; switch (aTU) { case TimeUnit.Day: shift = nTime.Subtract(sTime).TotalDays; break; case TimeUnit.Hour: shift = nTime.Subtract(sTime).TotalHours; break; case TimeUnit.Minute: shift = nTime.Subtract(sTime).TotalMinutes; break; case TimeUnit.Second: shift = nTime.Subtract(sTime).TotalSeconds; break; } for (int d = 0; d < varData.Length; d++) { varData[d] = (double)varData[d] + shift; } } int[] start = new int[aVarS.DimNumber]; int[] count = new int[aVarS.DimNumber]; for (int v = 0; v < aVarS.DimNumber; v++) { start[v] = 0; count[v] = bDataInfo.Dimensions[aVarS.Dimensions[v].DimId].DimLength; } start[Array.IndexOf(aVarS.DimIds, tDimid)] = tDimNum; bDataInfo.WriteVaraData(aDataInfo.ncid, aDataInfo.Variables[vIdx].VarId, aVarS.NCType, start, count, varData); } } } //Close data file aDataInfo.CloseNCFile(); }
/// <summary> /// Join data files /// </summary> /// <param name="inFiles">input nc files</param> /// <param name="outFile">joined output nc file</param> /// <param name="timeDimStr">time dimension name string</param> public static void JoinDataFiles_Time(List <string> inFiles, string outFile, string timeDimStr) { string timeVarStr = timeDimStr; //Check number of selected files int fNum = inFiles.Count; if (fNum < 2) { MessageBox.Show("There should be at least two files!", "Error"); return; } //Open the first file int i, j; string aFile = inFiles[0]; NetCDFDataInfo aDataInfo = new NetCDFDataInfo(); aDataInfo.ReadDataInfo(aFile); switch (aDataInfo.Convention) { case Conventions.WRFOUT: timeDimStr = "Time"; timeVarStr = "Times"; break; } //Change time dimension as unlimit for (i = 0; i < aDataInfo.Dimensions.Count; i++) { Dimension aDimS = aDataInfo.Dimensions[i]; if (aDimS.DimName == timeDimStr) { //aDimS.dimLen = -1; //aDataInfo.dimList[i] = aDimS; aDataInfo.unlimdimid = aDimS.DimId; break; } } for (i = 0; i < aDataInfo.nvars; i++) { if (Global.MIMath.IsNumeric(aDataInfo.Variables[i].Name.Substring(0, 1))) { aDataInfo.Variables[i].Name = 'V' + aDataInfo.Variables[i].Name; } } int tDimid = 0; NetCDF4.nc_inq_dimid(aDataInfo.ncid, timeDimStr, out tDimid); //object[] sttu = aDataInfo.GetStartTimeInfo(); //DateTime sTime = (DateTime)sttu[0]; //TimeUnit aTU = (TimeUnit)sttu[1]; //Create output nc file and write the data of the first file aDataInfo.CreateNCFile(outFile); //Write the data of first data file NetCDFDataInfo bDataInfo = new NetCDFDataInfo(); bDataInfo.ReadDataInfo(inFiles[0]); for (i = 0; i < bDataInfo.nvars; i++) { Variable aVarS = aDataInfo.Variables[i]; int dimNum = aVarS.DimNumber; int[] start = new int[dimNum]; int[] count = new int[dimNum]; if (dimNum == 4) { start[2] = 0; count[2] = aVarS.Dimensions[2].DimLength; start[3] = 0; count[3] = aVarS.Dimensions[3].DimLength; for (int d1 = 0; d1 < aVarS.Dimensions[0].DimLength; d1++) { start[0] = d1; count[0] = 1; for (int d2 = 0; d2 < aVarS.Dimensions[1].DimLength; d2++) { start[1] = d2; count[1] = 1; object[] varaData = new object[1]; bDataInfo.GetVaraData(aVarS, start, count, ref varaData); aDataInfo.WriteVara(aVarS, start, count, varaData); } } } else if (dimNum == 3) { start[1] = 0; count[1] = aVarS.Dimensions[1].DimLength; start[2] = 0; count[2] = aVarS.Dimensions[2].DimLength; for (int d1 = 0; d1 < aVarS.Dimensions[0].DimLength; d1++) { start[0] = d1; count[0] = 1; object[] varaData = new object[1]; bDataInfo.GetVaraData(aVarS, start, count, ref varaData); aDataInfo.WriteVara(aVarS, start, count, varaData); } } else { for (int v = 0; v < aVarS.DimNumber; v++) { start[v] = 0; count[v] = bDataInfo.Dimensions[aVarS.Dimensions[v].DimId].DimLength; object[] varData = new object[1]; bDataInfo.GetVarData(aVarS, ref varData); aDataInfo.WriteVara(aVarS, start, count, varData); } } } //Join data //Add data for (i = 1; i < fNum; i++) { aFile = inFiles[i]; bDataInfo = new NetCDFDataInfo(); bDataInfo.ReadDataInfo(aFile); int tDimNum = 1; NetCDF4.nc_inq_dimlen(aDataInfo.ncid, tDimid, out tDimNum); for (j = 0; j < bDataInfo.nvars; j++) { Variable aVarS = bDataInfo.Variables[j]; int vIdx = aDataInfo.VariableNames.IndexOf(aVarS.Name); if (vIdx < 0) { continue; } if (Array.IndexOf(aVarS.DimIds, tDimid) < 0) { continue; } int dimNum = aVarS.DimNumber; int[] start = new int[dimNum]; int[] count = new int[dimNum]; if (dimNum == 4) { start[2] = 0; count[2] = aVarS.Dimensions[2].DimLength; start[3] = 0; count[3] = aVarS.Dimensions[3].DimLength; for (int d1 = 0; d1 < aVarS.Dimensions[0].DimLength; d1++) { start[0] = d1; count[0] = 1; for (int d2 = 0; d2 < aVarS.Dimensions[1].DimLength; d2++) { start[0] = d1; start[1] = d2; count[1] = 1; object[] varaData = new object[1]; bDataInfo.GetVaraData(aVarS, start, count, ref varaData); start[Array.IndexOf(aVarS.DimIds, tDimid)] += tDimNum; bDataInfo.WriteVaraData(aDataInfo.ncid, aDataInfo.Variables[vIdx].VarId, aVarS.NCType, start, count, varaData); } } } else if (dimNum == 3) { start[1] = 0; count[1] = aVarS.Dimensions[1].DimLength; start[2] = 0; count[2] = aVarS.Dimensions[2].DimLength; for (int d1 = 0; d1 < aVarS.Dimensions[0].DimLength; d1++) { start[0] = d1; count[0] = 1; object[] varaData = new object[1]; bDataInfo.GetVaraData(aVarS, start, count, ref varaData); start[Array.IndexOf(aVarS.DimIds, tDimid)] += tDimNum; bDataInfo.WriteVaraData(aDataInfo.ncid, aDataInfo.Variables[vIdx].VarId, aVarS.NCType, start, count, varaData); } } else { for (int v = 0; v < aVarS.DimNumber; v++) { start[v] = 0; count[v] = bDataInfo.Dimensions[aVarS.Dimensions[v].DimId].DimLength; object[] varData = new object[1]; bDataInfo.GetVarData(aVarS, ref varData); start[Array.IndexOf(aVarS.DimIds, tDimid)] = tDimNum; bDataInfo.WriteVaraData(aDataInfo.ncid, aDataInfo.Variables[vIdx].VarId, aVarS.NCType, start, count, varData); } } } } //Close data file aDataInfo.CloseNCFile(); }
private void B_Convert_Click(object sender, EventArgs e) { //Check number of selected files int fNum = _M4Files.Length; if (fNum == 0) { return; } SaveFileDialog aDlg = new SaveFileDialog(); aDlg.Filter = "NetCDF File (*.nc)|*.nc"; if (aDlg.ShowDialog() == DialogResult.OK) { string outFile = aDlg.FileName; //Show progressbar toolStripProgressBar1.Visible = true; toolStripProgressBar1.Value = 0; this.Cursor = Cursors.WaitCursor; //File loop int i; //NetCDFData CNetCDFData = new NetCDFData(); NetCDFDataInfo outDataInfo = new NetCDFDataInfo(); List <string> varList = new List <string>(); MICAPS4DataInfo inDataInfo = new MICAPS4DataInfo(); inDataInfo.ReadDataInfo(_M4Files[0]); //Set data info outDataInfo.FileName = outFile; outDataInfo.IsGlobal = false; outDataInfo.isLatLon = true; outDataInfo.MissingValue = inDataInfo.MissingValue; outDataInfo.unlimdimid = 2; //Add dimensions: lon, lat, time Dimension lonDim = outDataInfo.AddDimension("lon", inDataInfo.XNum); Dimension latDim = outDataInfo.AddDimension("lat", inDataInfo.YNum); Dimension timeDim = outDataInfo.AddDimension("time", -1); //Add variables outDataInfo.AddVariable("lon", NetCDF4.NcType.NC_DOUBLE, new Dimension[] { lonDim }); outDataInfo.AddVariableAttribute("lon", "units", "degrees_east"); outDataInfo.AddVariableAttribute("lon", "long_name", "longitude"); outDataInfo.AddVariable("lat", NetCDF4.NcType.NC_DOUBLE, new Dimension[] { latDim }); outDataInfo.AddVariableAttribute("lat", "units", "degrees_north"); outDataInfo.AddVariableAttribute("lat", "long_name", "latitude"); outDataInfo.AddVariable("time", NetCDF4.NcType.NC_DOUBLE, new Dimension[] { timeDim }); outDataInfo.AddVariableAttribute("time", "units", "days since 1-1-1 00:00:00"); outDataInfo.AddVariableAttribute("time", "long_name", "time"); //outDataInfo.AddVariableAttribute("time", "delta_t", "0000-00-01 00:00:00"); outDataInfo.AddVariable("dust", NetCDF4.NcType.NC_DOUBLE, new Dimension[] { timeDim, latDim, lonDim }); outDataInfo.AddVariableAttribute("dust", "units", "ug/m-3"); outDataInfo.AddVariableAttribute("dust", "long_name", "dust concentration"); outDataInfo.AddVariableAttribute("dust", "missing_value", 9999); //Add global attributes outDataInfo.AddGlobalAttribute("title", "Asian dust storm forecast"); outDataInfo.AddGlobalAttribute("model", "CUACE/Dust"); outDataInfo.AddGlobalAttribute("institute", "Chinese Academy of Meteological Sciences"); //Creat NetCDF file outDataInfo.CreateNCFile(outFile); DateTime sTime = DateTime.Parse("0001-1-1 00:00:00"); for (i = 0; i < fNum; i++) { string aFile = _M4Files[i]; if (i > 0) { inDataInfo = new MICAPS4DataInfo(); inDataInfo.ReadDataInfo(aFile); } else { //Write lon,lat data outDataInfo.WriteVar("lon", inDataInfo.X); outDataInfo.WriteVar("lat", inDataInfo.Y); } //Write time data object[] tData = new object[1]; tData[0] = inDataInfo.DateTime.Subtract(sTime).TotalDays; int[] start = new int[1]; int[] count = new int[1]; start[0] = i; count[0] = 1; outDataInfo.WriteVara("time", start, count, tData); //Write dust data object[] dustData = new object[inDataInfo.XNum * inDataInfo.YNum]; for (int m = 0; m < inDataInfo.YNum; m++) { for (int n = 0; n < inDataInfo.XNum; n++) { dustData[m * inDataInfo.XNum + n] = inDataInfo.GridData[m, n]; } } start = new int[3]; start[0] = i; start[1] = 0; start[2] = 0; count = new int[3]; count[0] = 1; count[1] = latDim.DimLength; count[2] = lonDim.DimLength; outDataInfo.WriteVara("dust", start, count, dustData); //Set progressbar value toolStripProgressBar1.Value = (int)((double)(i + 1) / (double)fNum * 100); Application.DoEvents(); } outDataInfo.CloseNCFile(); //Hide progressbar toolStripProgressBar1.Visible = false; toolStripProgressBar1.Value = 0; this.Cursor = Cursors.Default; } }