docs: def set_resource_type(resource, type_id, types={}, **kwargs):
      """
          Set this resource to be a certain type.
          Type objects (a dictionary keyed on type_id) may be
          passed in to save on loading.
          This function does not call save. It must be done afterwards.
          New resource attributes are added to the resource if the template
          requires them. Resource attributes on the resource but not used by
          the template are not removed.
          @returns list of new resource attributes
          ,new resource type object
      """
  
      ref_key = resource.ref_key
  
      existing_attr_ids = []
      for res_attr in resource.attributes:
          existing_attr_ids.append(res_attr.attr_id)
  
      if type_id in types:
          type_i = types[type_id]
      else:
          type_i = db.DBSession.query(TemplateType).filter(TemplateType.id==type_id).options(joinedload_all('typeattrs')).one()
  
      type_attrs = dict()
      for typeattr in type_i.typeattrs:
          type_attrs[typeattr.attr_id]={
                              'is_var':typeattr.attr_is_var,
                              'default_dataset_id': typeattr.default_dataset.id if typeattr.default_dataset else None}
  
      # check if attributes exist
      missing_attr_ids = set(type_attrs.keys()) - set(existing_attr_ids)
  
      # add attributes if necessary
      new_res_attrs = []
  
      #This is a dict as the length of the list may not match the new_res_attrs
      #Keyed on attr_id, as resource_attr_id doesn't exist yet, and there should only
      #be one attr_id per template.
      new_res_scenarios = {}
      for attr_id in missing_attr_ids:
          ra_dict = dict(
              ref_key = ref_key,
              attr_id = attr_id,
              attr_is_var = type_attrs[attr_id]['is_var'],
              node_id    = resource.id   if ref_key == 'NODE' else None,
              link_id    = resource.id   if ref_key == 'LINK' else None,
              group_id   = resource.id   if ref_key == 'GROUP' else None,
              network_id = resource.id   if ref_key == 'NETWORK' else None,
  
          )
          new_res_attrs.append(ra_dict)
  
  
  
          if type_attrs[attr_id]['default_dataset_id'] is not None:
              if hasattr(resource, 'network'):
                  for s in resource.network.scenarios:
  
                      if new_res_scenarios.get(attr_id) is None:
                          new_res_scenarios[attr_id] = {}
  
                      new_res_scenarios[attr_id][s.id] =  dict(
                          dataset_id = type_attrs[attr_id]['default_dataset_id'],
                          scenario_id = s.id,
                          #Not stored in the DB, but needed to connect the RA ID later.
                          attr_id = attr_id,
                          ref_key = ref_key,
                          node_id    = ra_dict['node_id'],
                          link_id    = ra_dict['link_id'],
                          group_id   = ra_dict['group_id'],
                          network_id = ra_dict['network_id'],
                      )
  
  
      resource_type = None
      for rt in resource.types:
          if rt.type_id == type_i.id:
              break
          else:
              errors = check_type_compatibility(rt.type_id, type_i.id)
              if len(errors) > 0:
                  raise HydraError("Cannot apply type %s to resource as it "
                                   "conflicts with type %s. Errors are: %s"
                                   %(type_i.name, resource.get_name(),
                                     rt.templatetype.name, ','.join(errors)))
      else:
          # add type to tResourceType if it doesn't exist already
          resource_type = dict(
              node_id    = resource.id   if ref_key == 'NODE' else None,
              link_id    = resource.id   if ref_key == 'LINK' else None,
              group_id   = resource.id   if ref_key == 'GROUP' else None,
              network_id = resource.id   if ref_key == 'NETWORK' else None,
              ref_key    = ref_key,
              type_id    = type_id,
          )
  
      return new_res_attrs, resource_type, new_res_scenarios