Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="proposedChanges"></param>
        internal protected override void OnPrecommit(Variable.Changes proposedChanges)
        {
            NetCDF.nc_enddef(NcId);
            base.OnPrecommit(proposedChanges);
            if (((NetCDFDataSet)DataSet).Initializing)
            {
                return;
            }
            NetCDF.nc_redef(NcId);

            NetCDFDataSet    dataSet = (NetCDFDataSet)this.DataSet;
            AttributeTypeMap atm     = new AttributeTypeMap(dataSet.NcId, varid);

            /* Updating attributes in the netCDF file */
            foreach (var item in proposedChanges.MetadataChanges)
            {
                dataSet.WriteNetCdfAttribute(varid, item.Key, item.Value, atm);
            }

            // NetCDF doesn't supports storing of arrays of kind A[x:0, y:10]
            // so we keep this actual shape in the reserved attribute.
            if (Rank > 1)
            {
                int min, max;
                GetMinMax(proposedChanges.Shape, out min, out max);
                if (min == 0 && max > 0)
                {
                    dataSet.WriteNetCdfAttribute(varid, AttributeTypeMap.AttributeVarActualShape,
                                                 proposedChanges.Shape, atm);
                    isPresentedAttrActualShape = true;
                }
                else if (isPresentedAttrActualShape)
                {
                    NetCDF.nc_del_att(NcId, varid, AttributeTypeMap.AttributeVarActualShape);
                    isPresentedAttrActualShape = false;
                }
            }

            atm.Store();

            /* Saving attached coordinate systems as "coordinates" attributes.
             * Common netCDF model supports for only one such attribute,
             * so all others CS except first are not compatible with that model.
             * Their attribute names are "coordinates2","coordinates3" etc.
             * Names of coordinate systems are provided with corresponed
             * attributes "coordinatesName","coordinates2Name",...
             */
            if (proposedChanges.CoordinateSystems != null)
            {
                int n = proposedChanges.InitialSchema.CoordinateSystems.Length;

                foreach (var cs in proposedChanges.CoordinateSystems)
                {
                    if (Array.Exists(proposedChanges.InitialSchema.CoordinateSystems, css => css == cs.Name))
                    {
                        continue;
                    }

                    // This CS has been added.
                    string name = "coordinates";
                    if (n > 0)
                    {
                        name += n.ToString();
                    }
                    n++;

                    StringBuilder sb    = new StringBuilder();
                    bool          first = true;
                    foreach (Variable axis in cs.Axes)
                    {
                        if (!first)
                        {
                            sb.Append(' ');
                        }
                        else
                        {
                            first = false;
                        }
                        sb.Append(axis.Name);
                    }
                    dataSet.WriteNetCdfAttribute(varid, name, sb.ToString(), null);
                    dataSet.WriteNetCdfAttribute(varid, name + "Name", cs.Name, null);
                }
            }
        }