def set_source_id_from_evid(db, collection='arrival',
                            use_immortal_cursor=False,
                            update_all=False):
    """
    Sets source_id in arrivals by matching evid attributes in the
    source collection.   The concept here is that source_id is the
    unique key in MsPASS but evid is a foreign key defined when
    importing data from a CSS3.0 database.   The evid key can thus
    not be trusted so if we override it with a source_id we can
    guarantee it is unique.  This function will only work if the evid
    attribute is set in documents in the source collection.  Currently
    this can be done with a function called load_css30_sources.

    :param db:  MongoDB handle or MsPASS Database object managing these data.
    :param collection:  collection to search default is arrival, but any
      collection that has evid defined could be scanned.  The algorithm
      simply tries to match evid and updates documents it finds with
      the object id of the source record it matches.
    :param use_immortal_cursor:  If true the cursor in the update is made
      "immortal" meaning it won't time out.  This may be necessary if the
      arrival collection is large.
    :param update_all:  if true the function will force cross referencing and 
      setting every document in arrival.  The default (False) updates only 
      documents where the source_id is not yet set.  The default behavior, 
      for example, would be the norm when new arrival data is added to a 
      database and need that indexing. 
    :return: tuple of results with mixed content.  0 and 1 are integers
      defining number of documents processed and the number altered
      respectively.   2 is a dict keyed by evid with a value equal to the
      count of the number of documents found and altered with that evid.
      Component 3 is the dict of the complement to 2; evid keyed dict
      with the count of the number of documents encountered but which
      for which evid did not match any document in source.

    """
    dbarr = db[collection]
    dbsrc = db['source']
    if update_all:
        query = {}
    else:
        query = {'source_id': None}
    if use_immortal_cursor:
        alldocs = dbarr.find(query, no_cursor_timeout=True)
    else:
        alldocs = dbarr.find(query)
    number_arrivals = 0
    number_set = 0
    evid_set = dict()
    not_set = dict()
    for doc in alldocs:
        if 'evid' in doc:
            evid = doc['evid']
            query = {'evid': evid}
            n = dbsrc.count_documents(query)
            if n == 0:
                if evid in not_set:
                    nset = not_set[evid]
                    nset += 1
                    not_set[evid] = nset
                else:
                    not_set[evid] = 1
            else:
                srcrec = dbsrc.find_one(query)
                source_id = srcrec['source_id']
                arroid = doc['_id']
                dbarr.update_one(
                    {'_id': arroid},
                    {'$set': {'source_id': source_id}}
                )
                number_set += 1
                if evid in evid_set:
                    nset = evid_set[evid]
                    nset += 1
                    evid_set[evid] = nset
                else:
                    evid_set[evid] = 1
        number_arrivals += 1
    return [number_arrivals, number_set, evid_set, not_set]