def _determine_fields_from_ions(ds, ion_list, fields, ftype, sampling_type):
    """
    Figure out what fields need to be added based on the ions present
    and the particle type

    For sph (determined by sampling_type):
    Identify if the number_density fields for the desired ions already
    exist on the dataset, and if so, just add them to the list
    of fields to be added to the ray.
    If not, add these ion fields to the dataset as particle fields,
    which prompts them being smoothed to the grid, and add the resulting
    smoothed fields (e.g. ("gas", "x_number_density")) to the list of fields
    to be added to the ray.  Include the 'temperature' field for
    calculating the width of voigt profiles in the absorption spectrum.

    For grid:
    Check if the number_density fields for these ions exist, and if so, add
    them to field list. If not, leave them off, as they'll be generated
    on the fly by SpectrumGenerator as long as we include the 'density',
    'temperature', and appropriate 'metallicity' fields.
    """
    fields_to_add_to_ds = []
    for ion in ion_list:
        atom = ion[0].capitalize()
        ion_state = ion[1]
        nuclei_field = '%s_nuclei_mass_density' % atom
        metallicity_field = '%s_metallicity' % atom
        field = '%s_p%d_number_density' % (atom, ion_state - 1)
        if ion_state == 1:
            alias_field = '%s_number_density' % atom
        else:
            alias_field = field
        if (ftype, field) not in ds.derived_field_list:
            if (ftype, alias_field) not in ds.derived_field_list:
                if sampling_type == 'particle':
                    fields_to_add_to_ds.append((atom, ion_state))
                    fields.append(('gas', field))
                else:
                    fields.append(('gas', 'density'))
                    if ('gas', metallicity_field) in ds.derived_field_list:
                        fields.append(('gas', metallicity_field))
                    elif ('gas', nuclei_field) in ds.derived_field_list:
                        fields.append(('gas', nuclei_field))
                    elif atom != 'H':
                        fields.append(('gas', 'metallicity'))
                    else:
                        pass
            else:
                fields.append(('gas', alias_field))
        else:
            fields.append(('gas', field))
    return fields, fields_to_add_to_ds
