Ejemplo n.º 1
0
        internal static NetCdfVariable <DataType> Read(NetCDFDataSet dataSet, int varid)
        {
            string name;
            NcType ncType;

            int[] dimIds;
            int   natts, res, ndims;

            // Number of dimensions
            res = NetCDF.nc_inq_varndims(dataSet.NcId, varid, out ndims);
            NetCDFDataSet.HandleResult(res);

            dimIds = new int[ndims];

            // About variable
            res = NetCDF.nc_inq_var(dataSet.NcId, varid, out name, out ncType, out ndims, dimIds, out natts);
            NetCDFDataSet.HandleResult(res);

            // Dimensions names
            string[] dims = new string[ndims];
            for (int i = 0; i < ndims; i++)
            {
                string dimName;
                res = NetCDF.nc_inq_dimname(dataSet.NcId, dimIds[i], out dimName);
                NetCDFDataSet.HandleResult(res);

                dims[i] = dimName.ToString();
            }

#if DEBUG
            StringBuilder msg = new StringBuilder();
            for (int i = 0; i < dims.Length; i++)
            {
                msg.Append(dims[i]);
                if (i < dims.Length - 1)
                {
                    msg.Append(',');
                }
            }
            Debug.WriteLineIf(NetCDFDataSet.TraceNetCDFDataSet.TraceInfo, String.Format("Variable {0}: {1} dimensions ({2}), {3} attributes.",
                                                                                        name, ndims, msg.ToString(), natts));
#endif

            var var = new NetCdfVariable <DataType>(dataSet, varid, name.ToString(), dims);
            if (var.Rank > 0 && var.changes != null)
            {
                // Providing correct "proposed" shape taken from the input file
                int[] shape = var.ReadShape();
                if (shape == null)
                {
                    shape = new int[dims.Length];
                }
                Rectangle   ar    = new Rectangle(new int[dims.Length], shape);
                DataChanges oldch = var.changes;
                DataChanges newch = new DataChanges(oldch.ChangeSet, oldch.InitialSchema, oldch.MetadataChanges,
                                                    oldch.CoordinateSystems,
                                                    shape, ar, oldch.Data);
                var.changes = newch;
            }
            return(var);
        }