Ejemplo n.º 1
0
        /// <summary>
        /// Join data files
        /// </summary>
        /// <param name="inFiles">input nc files</param>
        /// <param name="outFile">joined output nc file</param>
        public static void JoinDataFiles_Variable(List <string> inFiles, string outFile)
        {
            //Check number of selected files
            int fNum = inFiles.Count;

            if (fNum < 2)
            {
                MessageBox.Show("There should be at least two files!", "Error");
                return;
            }

            //Check top two files to decide joining time or variables
            string aFile = inFiles[0];

            NetCDFDataInfo aDataInfo = new NetCDFDataInfo();

            aDataInfo.ReadDataInfo(aFile);

            //Join data
            int i, j, res = 0;

            //Copy first file to output file
            File.Copy(aDataInfo.FileName, outFile, true);

            //Open output file
            int ncid = 0;

            res = NetCDF4.nc_open(outFile, (int)NetCDF4.CreateMode.NC_WRITE, out ncid);
            if (res != 0)
            {
                goto ERROR;
            }

            //Add data
            for (i = 1; i < fNum; i++)
            {
                aFile = inFiles[i];
                NetCDFDataInfo bDataInfo = new NetCDFDataInfo();
                bDataInfo.ReadDataInfo(aFile);
                //if (GetDataJoinType(aDataInfo, bDataInfo, "time") != 2)
                //    continue;

                for (j = 0; j < bDataInfo.nvars; j++)
                {
                    Variable aVarS = (Variable)bDataInfo.Variables[j].Clone();
                    if (aVarS.DimNumber > 1 && (!aDataInfo.VariableNames.Contains(aVarS.Name)))
                    {
                        aDataInfo.AddNewVariable(aVarS, ncid);

                        object[] varData = new object[1];
                        if (aVarS.DimNumber > 1)
                        {
                            int[] start = new int[aVarS.DimNumber];
                            int[] count = new int[aVarS.DimNumber];
                            for (int v = 1; v < aVarS.DimNumber; v++)
                            {
                                start[v] = 0;
                                count[v] = aVarS.Dimensions[v].DimLength;
                            }
                            for (int d = 0; d < aVarS.Dimensions[0].DimLength; d++)
                            {
                                start[0] = d;
                                count[0] = 1;

                                bDataInfo.GetVaraData(bDataInfo.Variables[j], start, count, ref varData);
                                aDataInfo.WriteVaraData(ncid, aVarS.VarId, aVarS.NCType, start, count, varData);
                            }
                        }
                        else
                        {
                            bDataInfo.GetVarData(bDataInfo.Variables[j], ref varData);
                            aDataInfo.WriteVarData(ncid, aVarS.VarId, aVarS.NCType, varData);
                        }
                    }
                }
            }

            //Close data file
            res = NetCDF4.nc_close(ncid);
            if (res != 0)
            {
                goto ERROR;
            }

            return;

ERROR:
            MessageBox.Show("Error: " + NetCDF4.nc_strerror(res), "Error");

            return;
        }
Ejemplo n.º 2
0
        /// <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_bak(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;
            }

            //Check top two files to decide joining time or variables
            string aFile = inFiles[0];

            NetCDFDataInfo aDataInfo = new NetCDFDataInfo();

            aDataInfo.ReadDataInfo(aFile);

            //Join data
            int i, j, res = 0;

            //Copy first file to output file
            File.Copy(aDataInfo.FileName, outFile, true);

            //Open output file
            int ncid = 0;

            res = NetCDF4.nc_open(outFile, (int)NetCDF4.CreateMode.NC_WRITE, out ncid);
            if (res != 0)
            {
                goto ERROR;
            }
            int tDimid = 0;

            NetCDF4.nc_inq_dimid(ncid, timeDimStr, out tDimid);

            //Add data
            for (i = 1; i < fNum; i++)
            {
                aFile = inFiles[i];
                NetCDFDataInfo bDataInfo = new NetCDFDataInfo();
                bDataInfo.ReadDataInfo(aFile);
                if (GetDataJoinType(aDataInfo, bDataInfo, timeDimStr) != 1)
                {
                    continue;
                }

                int tDimNum = 1;
                NetCDF4.nc_inq_dimlen(ncid, tDimid, out tDimNum);
                for (j = 0; j < bDataInfo.nvars; j++)
                {
                    Variable aVarS = bDataInfo.Variables[j];
                    if (aVarS.DimNumber > 1 || aVarS.Name == timeDimStr)
                    {
                        if (Array.IndexOf(aVarS.DimIds, tDimid) < 0)
                        {
                            continue;
                        }

                        object[] varData = new object[1];
                        bDataInfo.GetVarData(bDataInfo.Variables[j], 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.DimIds[v]].DimLength;
                        }
                        start[Array.IndexOf(aVarS.DimIds, tDimid)] = tDimNum;
                        bDataInfo.WriteVaraData(ncid, aVarS.VarId, aVarS.NCType, start, count, varData);
                    }
                }
            }

            //Close data file
            res = NetCDF4.nc_close(ncid);
            if (res != 0)
            {
                goto ERROR;
            }

            return;

ERROR:
            MessageBox.Show("Error: " + NetCDF4.nc_strerror(res), "Error");

            return;
        }