Example #1
0
        /// <summary>
        /// Convert specified amount of this resource to another value using ResourceType supplied converter
        /// </summary>
        /// <param name="converterName">Name of converter to use</param>
        /// <param name="amount">Amount to convert</param>
        /// <returns>Value to report</returns>
        public object ConvertTo(string converterName, double amount)
        {
            // get converted value
            if (converterName.StartsWith("$"))
            {
                // calculate price as special case using pricing structure if present.
                ResourcePricing price;
                switch (converterName)
                {
                case "$+":
                    price = Price(PurchaseOrSalePricingStyleType.Purchase);
                    break;

                case "$-":
                    price = Price(PurchaseOrSalePricingStyleType.Sale);
                    break;

                default:
                    price = Price(PurchaseOrSalePricingStyleType.Both);
                    break;
                }

                if (price.PricePerPacket > 0)
                {
                    double packets = amount / price.PacketSize;
                    // this does not include whole packet restriction as needs to report full value
                    return(packets * price.PricePerPacket);
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                ResourceUnitsConverter converter = Apsim.Children(this, typeof(ResourceUnitsConverter)).Where(a => string.Compare(a.Name, converterName, true) == 0).FirstOrDefault() as ResourceUnitsConverter;
                if (converter != null)
                {
                    double result = amount;
                    // convert to edible proportion for all HumanFoodStore converters
                    // this assumes these are all nutritional. Price will be handled above.
                    if (this.GetType() == typeof(HumanFoodStoreType))
                    {
                        result *= (this as HumanFoodStoreType).EdibleProportion;
                    }
                    return(result * converter.Factor);
                }
                else
                {
                    string warning = "Unable to find the required unit converter [r=" + converterName + "] in resource [r=" + this.Name + "]";
                    Warnings.Add(warning);
                    Summary.WriteWarning(this, warning);
                    return(null);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Convert the current amount of this resource to another value using ResourceType supplied converter
        /// </summary>
        /// <param name="converterName">Name of converter to use</param>
        /// <returns>Value to report</returns>
        public double ConversionFactor(string converterName)
        {
            ResourceUnitsConverter converter = Apsim.Children(this, typeof(ResourceUnitsConverter)).Where(a => a.Name.ToLower() == converterName.ToLower()).FirstOrDefault() as ResourceUnitsConverter;

            if (converter is null)
            {
                return(0);
            }
            else
            {
                return(converter.Factor);
            }
        }
 /// <summary>
 /// Convert specified amount of this resource to another value using ResourceType supplied converter
 /// </summary>
 /// <param name="converterName">Name of converter to use</param>
 /// <param name="amount">Amount to convert</param>
 /// <returns>Value to report</returns>
 public object ConvertTo(string converterName, double amount)
 {
     // get converted value
     if (converterName == "$")
     {
         // calculate price as special case using pricing structure if present.
         ResourcePricing price = Price;
         if (price.PricePerPacket > 0)
         {
             double packets = amount / price.PacketSize;
             // this does not include whole packet restriction as needs to report full value
             //if(price.UseWholePackets)
             //{
             //    packets = Math.Floor(packets);
             //}
             return(packets * price.PricePerPacket);
         }
         else
         {
             return(null);
         }
     }
     else
     {
         ResourceUnitsConverter converter = Apsim.Children(this, typeof(ResourceUnitsConverter)).Where(a => a.Name.ToLower() == converterName.ToLower()).FirstOrDefault() as ResourceUnitsConverter;
         if (converter != null)
         {
             return(amount * converter.Factor);
         }
         else
         {
             string warning = "Unable to find the required unit converter [r=" + converterName + "] in resource [r=" + this.Name + "]";
             Warnings.Add(warning);
             Summary.WriteWarning(this, warning);
             return(null);
         }
     }
 }