OptionalModelObject ReverseTranslator::translateCoilCoolingDXSingleSpeed( const WorkspaceObject & workspaceObject )
{
OptionalModelObject result,temp;
  OptionalSchedule schedule;

  //get the Schedule
  OptionalWorkspaceObject owo = workspaceObject.getTarget(Coil_Cooling_DX_SingleSpeedFields::AvailabilityScheduleName);
  if(!owo)
  {
    LOG(Error, "Error importing object: "
             << workspaceObject.briefDescription()
             << " Can't find Schedule.");
    return result;
  }
  temp = translateAndMapWorkspaceObject(*owo);
  if(temp)
  {
    schedule=temp->optionalCast<Schedule>();
  }

  if( !schedule  )
  {
    LOG(Error,"Error importing object: "
             << workspaceObject.briefDescription()
             <<"Failed to convert iddObject (schedule) into ModelObject. Maybe it does not exist in model yet");
    return result;
  }

  //collect the curves
  owo = workspaceObject.getTarget(Coil_Cooling_DX_SingleSpeedFields::TotalCoolingCapacityFunctionofTemperatureCurveName);
  if(!owo)
  {
    LOG(Error, "Error importing object: "
             << workspaceObject.briefDescription()
             << " Can't find TotalCoolingCapacityFunctionOfTemperatureCurve.");
    return result;
  }
  if( owo->numSources() > 1 )
  {
    owo = owo->workspace().addObject(owo.get());
  }

  temp= translateAndMapWorkspaceObject( *owo );

  if(!temp)
  {
    LOG(Error, "Error importing object: "
             << workspaceObject.briefDescription()
             << " Can't convert workspace curve into a model curve. ");
    return result;
  }
  boost::optional<Curve> tccfot = temp->optionalCast<Curve>();
  if( ! tccfot )
  {
    LOG(Error, "Error importing object: "
             << workspaceObject.briefDescription()
             << " curve is wrong type. ");
    return result;
  }

  owo = workspaceObject.getTarget(Coil_Cooling_DX_SingleSpeedFields::EnergyInputRatioFunctionofTemperatureCurveName);
  if(!owo)
  {
    LOG(Error, "Error importing object: "
             << workspaceObject.briefDescription()
             << " Can't find EnergyInputRatioFunctionofTemperatureCurveName.");
    return result;
  }
  if( owo->numSources() > 1 )
  {
    owo = owo->workspace().addObject(owo.get());
  }
  temp = translateAndMapWorkspaceObject( *owo );
  if(!temp)
  {
    LOG(Error, "Error importing object: "
             << workspaceObject.briefDescription()
             << " Can't convert workspace curve into a model curve. ");
    return result;
  }
  boost::optional<Curve> eirfot = temp->optionalCast<Curve>();
  if( ! eirfot )
  {
    LOG(Error, "Error importing object: "
             << workspaceObject.briefDescription()
             << " curve is wrong type. ");
    return result;
  }


  owo = workspaceObject.getTarget(Coil_Cooling_DX_SingleSpeedFields::TotalCoolingCapacityFunctionofFlowFractionCurveName);
  if(!owo)
  {
    LOG(Error, "Error importing object: "
             << workspaceObject.briefDescription()
             << " Can't find TotalCoolingCapacityFunctionofFlowFractionCurveName.");
    return result;
  }
  if( owo->numSources() > 1 )
  {
    owo = owo->workspace().addObject(owo.get());
  }
  temp = translateAndMapWorkspaceObject( *owo );
  if(!temp)
  {
    LOG(Error, "Error importing object: "
             << workspaceObject.briefDescription()
             << " Can't convert workspace curve into a model curve. ");
    return result;
  }
  boost::optional<Curve> tccfoff = temp->optionalCast<Curve>();
  if( ! tccfoff )
  {
    LOG(Error, "Error importing object: "
             << workspaceObject.briefDescription()
             << " curve is wrong type. ");
    return result;
  }

  owo = workspaceObject.getTarget(Coil_Cooling_DX_SingleSpeedFields::EnergyInputRatioFunctionofFlowFractionCurveName);
  if(!owo)
  {
    LOG(Error, "Error importing object: "
             << workspaceObject.briefDescription()
             << " Can't find EnergyInputRatioFunctionofFlowFractionCurveName.");
    return result;
  }
  if( owo->numSources() > 1 )
  {
    owo = owo->workspace().addObject(owo.get());
  }
  temp = translateAndMapWorkspaceObject( *owo );
  if(!temp)
  {
    LOG(Error, "Error importing object: "
             << workspaceObject.briefDescription()
             << " Can't convert workspace curve into a model curve. ");
    return result;
  }
  boost::optional<Curve> eirfoff = temp->optionalCast<Curve>();
  if( ! eirfoff )
  {
    LOG(Error, "Error importing object: "
             << workspaceObject.briefDescription()
             << " curve is wrong type. ");
    return result;
  }

  owo = workspaceObject.getTarget(Coil_Cooling_DX_SingleSpeedFields::PartLoadFractionCorrelationCurveName);
  if(!owo)
  {
    LOG(Error, "Error importing object: "
             << workspaceObject.briefDescription()
             << " Can't find PartLoadFractionCorrelationCurveName.");
    return result;
  }
  if( owo->numSources() > 1 )
  {
    owo = owo->workspace().addObject(owo.get());
  }
  temp = translateAndMapWorkspaceObject( *owo );
  if(!temp)
  {
    LOG(Error, "Error importing object: "
             << workspaceObject.briefDescription()
             << " Can't convert workspace curve into a model curve. ");
    return result;
  }
  boost::optional<Curve> plfcc = temp->optionalCast<Curve>();
  if( ! plfcc )
  {
    LOG(Error, "Error importing object: "
             << workspaceObject.briefDescription()
             << " curve is wrong type. ");
    return result;
  }


  try {
    CoilCoolingDXSingleSpeed coil(m_model,
                                  *schedule,
                                  *tccfot,
                                  *tccfoff,
                                  *eirfot,
                                  *eirfoff,
                                  *plfcc);

    OptionalString optS = workspaceObject.name();
    if( optS )
    {
      coil.setName( *optS );
    }


    OptionalDouble d = workspaceObject.getDouble(Coil_Cooling_DX_SingleSpeedFields::GrossRatedTotalCoolingCapacity);
    if(d)
    {
      coil.setRatedTotalCoolingCapacity(*d);
    }

    d=workspaceObject.getDouble(Coil_Cooling_DX_SingleSpeedFields::GrossRatedSensibleHeatRatio);
    if(d)
    {
      coil.setRatedSensibleHeatRatio(*d);
    }

    d=workspaceObject.getDouble(Coil_Cooling_DX_SingleSpeedFields::GrossRatedCoolingCOP);
    if(d)
    {
      coil.setRatedCOP(*d);
    }

    d=workspaceObject.getDouble(Coil_Cooling_DX_SingleSpeedFields::RatedAirFlowRate);
    if(d)
    {
      coil.setRatedAirFlowRate(*d);
    }

    d=workspaceObject.getDouble(Coil_Cooling_DX_SingleSpeedFields::RatedEvaporatorFanPowerPerVolumeFlowRate);
    if(d)
    {
      coil.setRatedEvaporatorFanPowerPerVolumeFlowRate(*d);
    }

    d=workspaceObject.getDouble(Coil_Cooling_DX_SingleSpeedFields::NominalTimeforCondensateRemovaltoBegin);
    if(d)
    {
      coil.setNominalTimeForCondensateRemovalToBegin(*d);
    }

    d=workspaceObject.getDouble(Coil_Cooling_DX_SingleSpeedFields::RatioofInitialMoistureEvaporationRateandSteadyStateLatentCapacity);
    if(d)
    {
      coil.setRatioOfInitialMoistureEvaporationRateAndSteadyStateLatentCapacity(*d);
    }

    d=workspaceObject.getDouble(Coil_Cooling_DX_SingleSpeedFields::MaximumCyclingRate);
    if(d)
    {
      coil.setMaximumCyclingRate(*d);
    }

    d=workspaceObject.getDouble(Coil_Cooling_DX_SingleSpeedFields::LatentCapacityTimeConstant);
    if(d)
    {
      coil.setLatentCapacityTimeConstant(*d);
    }

    optS=workspaceObject.getString(Coil_Cooling_DX_SingleSpeedFields::CondenserAirInletNodeName);
    if(optS)
    {
      coil.setCondenserAirInletNodeName(*optS);
    }

    optS = workspaceObject.getString(Coil_Cooling_DX_SingleSpeedFields::CondenserType);
    if(optS)
    {
      coil.setCondenserType(*optS);
    }

    d=workspaceObject.getDouble(Coil_Cooling_DX_SingleSpeedFields::EvaporativeCondenserEffectiveness);
    if(d)
    {
      coil.setEvaporativeCondenserEffectiveness(*d);
    }

    d=workspaceObject.getDouble(Coil_Cooling_DX_SingleSpeedFields::EvaporativeCondenserAirFlowRate);
    if(d)
    {
      coil.setEvaporativeCondenserAirFlowRate(*d);
    }

    d=workspaceObject.getDouble(Coil_Cooling_DX_SingleSpeedFields::EvaporativeCondenserPumpRatedPowerConsumption);
    if(d)
    {
      coil.setEvaporativeCondenserPumpRatedPowerConsumption(*d);
    }

    d=workspaceObject.getDouble(Coil_Cooling_DX_SingleSpeedFields::CrankcaseHeaterCapacity);
    if(d)
    {
      coil.setCrankcaseHeaterCapacity(*d);
    }

    d=workspaceObject.getDouble(Coil_Cooling_DX_SingleSpeedFields::MaximumOutdoorDryBulbTemperatureforCrankcaseHeaterOperation);
    if(d)
    {
      coil.setMaximumOutdoorDryBulbTemperatureForCrankcaseHeaterOperation(*d);
    }

    result=coil;

  }
  catch (std::exception& e) {
    LOG(Error,"Could not reverse translate " << workspaceObject.briefDescription()
        << ", because " << e.what() << ".");
  }
  return result;
}
OptionalModelObject ReverseTranslator::translateSizingZone( const WorkspaceObject & workspaceObject )
{
    boost::optional<WorkspaceObject> target = workspaceObject.getTarget(Sizing_ZoneFields::ZoneorZoneListName);

    std::vector<ThermalZone> thermalZones;

    if( target ) {

        // just one thermal zone
        boost::optional<ModelObject> mo;
        if (target->iddObject().type() == IddObjectType::Zone) {
            mo = translateAndMapWorkspaceObject(target.get());
            if( mo ) {
                if( boost::optional<Space> space = mo->optionalCast<Space>() ) {
                    boost::optional<ThermalZone> thermalZone = space->thermalZone();
                    if (thermalZone) {
                        thermalZones.push_back(*thermalZone);
                    }
                }
            }
        } else if (target->iddObject().type() == IddObjectType::ZoneList) {

            // get all thermal zones in zone list
            for (const IdfExtensibleGroup& idfGroup : target->extensibleGroups()) {
                WorkspaceExtensibleGroup workspaceGroup = idfGroup.cast<WorkspaceExtensibleGroup>();
                OptionalWorkspaceObject owo = workspaceGroup.getTarget(0);
                if (owo) {
                    mo = translateAndMapWorkspaceObject(owo.get());
                    if( mo ) {
                        if( boost::optional<Space> space = mo->optionalCast<Space>() ) {
                            boost::optional<ThermalZone> thermalZone = space->thermalZone();
                            if (thermalZone) {
                                thermalZones.push_back(*thermalZone);
                            }
                        }
                    }
                }
            }
        }
    }

    if(thermalZones.empty())
    {
        LOG(Error, "Error importing object: "
            << workspaceObject.briefDescription()
            << " Can't find associated ThermalZone(s).");

        return boost::none;
    }

    boost::optional<ModelObject> result;
    for (ThermalZone thermalZone : thermalZones) {

        // sizing zone is constructed in thermal zone ctor
        openstudio::model::SizingZone sizingZone = thermalZone.sizingZone();

        // return first sizing zone
        if (!result) {
            result = sizingZone;
        }

        boost::optional<std::string> s;
        boost::optional<double> value;

        // ZoneCoolingDesignSupplyAirTemperature

        value = workspaceObject.getDouble(Sizing_ZoneFields::ZoneCoolingDesignSupplyAirTemperature);
        if( value )
        {
            sizingZone.setZoneCoolingDesignSupplyAirTemperature(value.get());
        }

        // ZoneHeatingDesignSupplyAirTemperature

        value = workspaceObject.getDouble(Sizing_ZoneFields::ZoneHeatingDesignSupplyAirTemperature);
        if( value )
        {
            sizingZone.setZoneHeatingDesignSupplyAirTemperature(value.get());
        }

        // ZoneCoolingDesignSupplyAirHumidityRatio

        value = workspaceObject.getDouble(Sizing_ZoneFields::ZoneHeatingDesignSupplyAirHumidityRatio);
        if( value )
        {
            sizingZone.setZoneHeatingDesignSupplyAirHumidityRatio(value.get());
        }

        // ZoneHeatingDesignSupplyAirHumidityRatio

        value = workspaceObject.getDouble(Sizing_ZoneFields::ZoneHeatingDesignSupplyAirHumidityRatio);
        if( value )
        {
            sizingZone.setZoneHeatingDesignSupplyAirHumidityRatio(value.get());
        }

        // DesignSpecificationOutdoorAirObjectName

        target = workspaceObject.getTarget(Sizing_ZoneFields::DesignSpecificationOutdoorAirObjectName);
        if (target) {
            OptionalModelObject mo = translateDesignSpecificationOutdoorAir(*target);
            if (mo) {
                if (mo->optionalCast<DesignSpecificationOutdoorAir>()) {
                    std::vector<Space> spaces = thermalZone.spaces();
                    OS_ASSERT(spaces.size() == 1);
                    spaces[0].setDesignSpecificationOutdoorAir(mo->cast<DesignSpecificationOutdoorAir>());
                }
            }
        }

        // ZoneHeatingSizingFactor

        value = workspaceObject.getDouble(Sizing_ZoneFields::ZoneHeatingSizingFactor);
        if( value )
        {
            sizingZone.setZoneHeatingSizingFactor(value.get());
        }

        // ZoneCoolingSizingFactor

        value = workspaceObject.getDouble(Sizing_ZoneFields::ZoneCoolingSizingFactor);
        if( value )
        {
            sizingZone.setZoneCoolingSizingFactor(value.get());
        }

        // CoolingDesignAirFlowMethod

        s = workspaceObject.getString(Sizing_ZoneFields::CoolingDesignAirFlowMethod);
        if( s )
        {
            sizingZone.setCoolingDesignAirFlowMethod(s.get());
        }

        // CoolingDesignAirFlowRate

        value = workspaceObject.getDouble(Sizing_ZoneFields::CoolingDesignAirFlowRate);
        if( value )
        {
            sizingZone.setCoolingDesignAirFlowRate(value.get());
        }

        // CoolingMinimumAirFlowperZoneFloorArea

        value = workspaceObject.getDouble(Sizing_ZoneFields::CoolingMinimumAirFlowperZoneFloorArea);
        if( value )
        {
            sizingZone.setCoolingMinimumAirFlowperZoneFloorArea(value.get());
        }

        // CoolingMinimumAirFlow

        value = workspaceObject.getDouble(Sizing_ZoneFields::CoolingMinimumAirFlow);
        if( value )
        {
            sizingZone.setCoolingMinimumAirFlow(value.get());
        }

        // CoolingMinimumAirFlowFraction

        value = workspaceObject.getDouble(Sizing_ZoneFields::CoolingMinimumAirFlowFraction);
        if( value )
        {
            sizingZone.setCoolingMinimumAirFlowFraction(value.get());
        }

        // HeatingDesignAirFlowMethod

        s = workspaceObject.getString(Sizing_ZoneFields::HeatingDesignAirFlowMethod);
        if( s )
        {
            sizingZone.setHeatingDesignAirFlowMethod(s.get());
        }

        // HeatingDesignAirFlowRate

        value = workspaceObject.getDouble(Sizing_ZoneFields::HeatingDesignAirFlowRate);
        if( value )
        {
            sizingZone.setHeatingDesignAirFlowRate(value.get());
        }

        // HeatingMaximumAirFlowperZoneFloorArea

        value = workspaceObject.getDouble(Sizing_ZoneFields::HeatingMaximumAirFlowperZoneFloorArea);
        if( value )
        {
            sizingZone.setHeatingMaximumAirFlowperZoneFloorArea(value.get());
        }

        // HeatingMaximumAirFlow

        value = workspaceObject.getDouble(Sizing_ZoneFields::HeatingMaximumAirFlow);
        if( value )
        {
            sizingZone.setHeatingMaximumAirFlow(value.get());
        }

        // HeatingMaximumAirFlowFraction

        value = workspaceObject.getDouble(Sizing_ZoneFields::HeatingMaximumAirFlowFraction);
        if( value )
        {
            sizingZone.setHeatingMaximumAirFlowFraction(value.get());
        }

        //DesignSpecification_ZoneAirDistribution

        boost::optional<WorkspaceObject> _designSpecification
            = workspaceObject.getTarget(Sizing_ZoneFields::DesignSpecificationZoneAirDistributionObjectName);

        if( _designSpecification )
        {
            // ZoneAirDistributionEffectivenessinCoolingMode

            value = _designSpecification->getDouble(
                        DesignSpecification_ZoneAirDistributionFields::ZoneAirDistributionEffectivenessinCoolingMode);
            if( value )
            {
                sizingZone.setDesignZoneAirDistributionEffectivenessinCoolingMode(value.get());
            }

            // ZoneAirDistributionEffectivenessinHeatingMode

            value = _designSpecification->getDouble(
                        DesignSpecification_ZoneAirDistributionFields::ZoneAirDistributionEffectivenessinHeatingMode);
            if( value )
            {
                sizingZone.setDesignZoneAirDistributionEffectivenessinHeatingMode(value.get());
            }
        }

    }

    return result;
}