コード例 #1
0
        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;
            }
        }
コード例 #2
0
        /// <summary>
        /// Add time dimension
        /// </summary>
        /// <param name="inFile">input nc file</param>
        /// <param name="outFile">output nc file</param>
        /// <param name="aTime">time</param>
        /// <param name="timeUnit">time unit (days, minutes, seconds)</param>
        public static void AddTimeDimension(string inFile, string outFile, DateTime aTime, string timeUnit)
        {
            //Set data info
            NetCDFDataInfo aDataInfo = new NetCDFDataInfo();

            aDataInfo.ReadDataInfo(inFile);
            NetCDFDataInfo bDataInfo = (NetCDFDataInfo)aDataInfo.Clone();

            //Check variables if time included
            List <string> varList = new List <string>();
            int           j;

            for (j = 0; j < aDataInfo.Variables.Count; j++)
            {
                varList.Add(aDataInfo.Variables[j].Name.ToLower());
            }
            if (varList.Contains("time"))
            {
                return;
            }

            //set start time of the data
            DateTime sTime  = new DateTime();
            double   tValue = 0;

            switch (timeUnit.ToLower())
            {
            case "minutes":
                sTime  = aTime.AddYears(-1);
                tValue = (aTime - sTime).TotalMinutes;
                break;

            case "seconds":
                sTime  = aTime.AddYears(-1);
                tValue = (aTime - sTime).TotalSeconds;
                break;

            default:
                sTime  = DateTime.Parse("1800-1-1 00:00:00");
                tValue = (aTime - sTime).TotalDays;
                break;
            }

            //Set data info, Add time dimension and variable
            Dimension tDim = new Dimension();

            tDim.DimName   = "time";
            tDim.DimLength = 1;
            tDim.DimId     = bDataInfo.ndims;
            bDataInfo.AddDimension(tDim);

            Variable         tVar    = new Variable();
            List <AttStruct> attList = new List <AttStruct>();
            AttStruct        aAtts   = new AttStruct();

            aAtts.attName  = "units";
            aAtts.NCType   = NetCDF4.NcType.NC_CHAR;
            aAtts.attValue = timeUnit.ToLower() + " since " + sTime.ToString("yyyy-M-d HH:mm:ss");
            aAtts.attLen   = ((string)aAtts.attValue).Length;
            attList.Add(aAtts);
            aAtts          = new AttStruct();
            aAtts.attName  = "long_name";
            aAtts.NCType   = NetCDF4.NcType.NC_CHAR;
            aAtts.attValue = "Time";
            aAtts.attLen   = ((string)aAtts.attValue).Length;
            attList.Add(aAtts);
            //aAtts = new AttStruct();
            //aAtts.attName = "delta_t";
            //aAtts.ncType = NetCDF4.NcType.NC_CHAR;
            //aAtts.attValue = "0000-00-01 00:00:00";
            //aAtts.attLen = ((string)aAtts.attValue).Length;
            //attList.Add(aAtts);
            tVar.Attributes = attList;
            tVar.Dimensions.Add(tDim);
            tVar.AttNumber = attList.Count;
            tVar.NCType    = NetCDF4.NcType.NC_DOUBLE;
            //tVar.nDims = 1;
            tVar.VarId = bDataInfo.nvars;
            tVar.Name  = "time";
            //tVar.isDataVar = false;
            bDataInfo.AddVariable(tVar);
            for (j = 0; j < bDataInfo.Variables.Count; j++)
            {
                Variable aVarS = bDataInfo.Variables[j];
                if (aVarS.DimNumber > 1)
                {
                    aVarS.Dimensions.Insert(0, tDim);
                    //for (int d = 0; d < aVarS.DimNumber; d++)
                    //    aVarS.Dimensions.Add(aVarS.Dimensions[d]);
                    //aVarS.nDims += 1;
                    bDataInfo.Variables[j] = aVarS;
                }
            }

            //Get and set data array
            object[] dataArray = new object[0];
            int      cLen      = 0;

            for (j = 0; j < aDataInfo.nvars; j++)
            {
                object[] varData = new object[1];
                if (aDataInfo.GetVarData(aDataInfo.Variables[j], ref varData))
                {
                    Array.Resize(ref dataArray, cLen + varData.Length);
                    Array.Copy(varData, 0, dataArray, cLen, varData.Length);
                    cLen = dataArray.Length;
                }
            }
            Array.Resize(ref dataArray, cLen + 1);
            dataArray[cLen] = tValue;

            //Write NetCDF data file
            bDataInfo.WriteNetCDFData(outFile, dataArray);
        }
コード例 #3
0
        private void B_AddTimeDim_Click(object sender, EventArgs e)
        {
            //Check number of selected files
            int fNum = LB_SelectedFiles.Items.Count;

            if (fNum == 0)
            {
                MessageBox.Show("No selected files!", "Error");
                return;
            }

            //Show progressbar
            toolStripProgressBar1.Visible = true;
            toolStripProgressBar1.Value   = 0;
            this.Cursor = Cursors.WaitCursor;

            //File loop
            int i, j;
            //NetCDFData CNetCDFData = new NetCDFData();
            NetCDFDataInfo aDataInfo = new NetCDFDataInfo();
            List <string>  varList   = new List <string>();

            for (i = 0; i < fNum; i++)
            {
                string aFile = Path.Combine(TB_DataFolder.Text, LB_SelectedFiles.Items[i].ToString());
                aDataInfo = new NetCDFDataInfo();
                aDataInfo.ReadDataInfo(aFile);
                //CNetCDFData.ReadNetCDFDataInfo(aFile, ref aDataInfo);
                string         outFile   = Path.Combine(TB_DataFolder.Text, "T_" + LB_SelectedFiles.Items[i].ToString());
                NetCDFDataInfo bDataInfo = (NetCDFDataInfo)aDataInfo.Clone();
                //bDataInfo.CreatNCFile(outFile);

                //Check variables if time included
                varList.Clear();
                for (j = 0; j < aDataInfo.Variables.Count; j++)
                {
                    varList.Add(aDataInfo.Variables[j].Name.ToLower());
                }
                if (varList.Contains("time"))
                {
                    continue;
                }

                //Read time of the data
                string timeStr = LB_SelectedFiles.Items[i].ToString();
                timeStr = timeStr.Split('.')[1].TrimStart('A');
                int      days  = int.Parse(timeStr.Substring(4));
                DateTime aTime = DateTime.Parse(timeStr.Substring(0, 4) + "-01-01");
                aTime = aTime.AddDays(days - 1);
                DateTime sTime = DateTime.Parse("0001-1-1 00:00:00");

                //Set data info, Add time dimension and variable
                Dimension tDim = new Dimension();
                tDim.DimName   = "time";
                tDim.DimLength = 1;
                tDim.DimId     = bDataInfo.ndims;
                bDataInfo.AddDimension(tDim);

                Variable         tVar    = new Variable();
                List <AttStruct> attList = new List <AttStruct>();
                AttStruct        aAtts   = new AttStruct();
                aAtts.attName  = "units";
                aAtts.NCType   = NetCDF4.NcType.NC_CHAR;
                aAtts.attValue = "days since 1-1-1 00:00:00";
                aAtts.attLen   = ((string)aAtts.attValue).Length;
                attList.Add(aAtts);
                aAtts          = new AttStruct();
                aAtts.attName  = "long_name";
                aAtts.NCType   = NetCDF4.NcType.NC_CHAR;
                aAtts.attValue = "Time";
                aAtts.attLen   = ((string)aAtts.attValue).Length;
                attList.Add(aAtts);
                aAtts          = new AttStruct();
                aAtts.attName  = "delta_t";
                aAtts.NCType   = NetCDF4.NcType.NC_CHAR;
                aAtts.attValue = "0000-00-01 00:00:00";
                aAtts.attLen   = ((string)aAtts.attValue).Length;
                attList.Add(aAtts);
                tVar.Attributes = attList;
                tVar.Dimensions.Add(tDim);
                tVar.AttNumber = attList.Count;
                tVar.NCType    = NetCDF4.NcType.NC_DOUBLE;
                //tVar.nDims = 1;
                tVar.VarId = bDataInfo.nvars;
                tVar.Name  = "time";
                //tVar.isDataVar = false;
                bDataInfo.AddVariable(tVar);
                for (j = 0; j < bDataInfo.Variables.Count; j++)
                {
                    Variable aVarS = bDataInfo.Variables[j];
                    if (aVarS.DimNumber > 1)
                    {
                        aVarS.Dimensions.Add(tDim);
                        for (int d = 0; d < aVarS.DimNumber; d++)
                        {
                            aVarS.Dimensions.Add(aVarS.Dimensions[d]);
                        }

                        //aVarS.nDims += 1;
                        bDataInfo.Variables[j] = aVarS;
                    }
                }

                //Get and set data array
                object[] dataArray = new object[0];
                int      cLen      = 0;
                for (j = 0; j < aDataInfo.nvars; j++)
                {
                    object[] varData = new object[1];
                    if (aDataInfo.GetVarData(aDataInfo.Variables[j], ref varData))
                    {
                        Array.Resize(ref dataArray, cLen + varData.Length);
                        Array.Copy(varData, 0, dataArray, cLen, varData.Length);
                        cLen = dataArray.Length;
                    }
                }
                Array.Resize(ref dataArray, cLen + 1);
                dataArray[cLen] = (aTime - sTime).Days;

                //Write NetCDF data file
                bDataInfo.WriteNetCDFData(outFile, dataArray);
                //CNetCDFData.WriteNetCDFData(outFile, bDataInfo, dataArray);

                //Set progressbar value
                toolStripProgressBar1.Value = (int)((double)(i + 1) / (double)fNum * 100);
                Application.DoEvents();
            }

            //Hide progressbar
            toolStripProgressBar1.Visible = false;
            toolStripProgressBar1.Value   = 0;
            this.Cursor = Cursors.Default;
        }