Esempio n. 1
0
        /// <summary>
        /// Add attribute
        /// </summary>
        /// <param name="attName">attribute name</param>
        /// <param name="attValue">attribute value</param>
        public void AddAttribute(string attName, string attValue)
        {
            AttStruct aAtt = new AttStruct();

            aAtt.NCType   = NetCDF4.NcType.NC_CHAR;
            aAtt.attName  = attName;
            aAtt.attValue = attValue;
            aAtt.attLen   = attValue.Length;

            _attributes.Add(aAtt);
            _attNumber = _attributes.Count;
        }
Esempio n. 2
0
        /// <summary>
        /// Add attribute
        /// </summary>
        /// <param name="attName">attribute name</param>
        /// <param name="attValue">attribute value</param>
        public void AddAttribute(string attName, double attValue)
        {
            AttStruct aAtt = new AttStruct();

            aAtt.NCType   = NetCDF4.NcType.NC_DOUBLE;
            aAtt.attName  = attName;
            aAtt.attValue = attValue;
            aAtt.attLen   = 1;

            _attributes.Add(aAtt);
            _attNumber = _attributes.Count;
        }
Esempio n. 3
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);
        }