def set_netcode_from_site(db, collection='arrival', time_key=None,
                          use_immortal_cursor=False, stations_to_ignore=None):
    """
    This function scans a MongoDB collection that is assumed to contain
    a "sta" for station code to be cross referenced with metadata stored in
    the site collection.   (The default collection is arrival, but this could
    be used for any table for which sta needs to be regularized to a seed
    net:sta pair.)  The entire objective of the function is to add missing
    seed net codes.   The algorithm used here is the most basic possible and
    looks only for a match of sta and and option time matched with
    a site's operation interval defined by a starttime to endtime time interval.
    I returns two lists of problem children that have to be handled
    separately:  (1) a set container of station names that have no matching
    value in the current site collection, and (2) a set container with
    a tuple of [net, sta, startime, endtime] values of net:sta combinations
    that are ambiguous.  They are defined as ambiguous if a common sta
    code appears in two or more networks.   Both cases need to be handled
    by subsequent processing. The first, requires scrounging for the
    station metadata.  A good foundation for that is obspy's get_stations
    function.  The ambiguous station code problem requires rules and special
    handling.   That will be deal with in tools planned for the near future
    but which do not exist at the time this function was finalized.
    The key idea in both cases is to use the output of this function to
    guide additional processing with two different workflows aimed at
    building a clean database to initiate processing.

    :param db: is a MongoDB database handle.  It can be as basic as
      the return of client('dbname') but it can also be an instance
      of the mspass Database class.  There is no default.
    :param collection:  MongoDB collection to be updated.  Processing
      keys on the data with key="sta" and (optional) value of time_key arg.
      (Default is arrival)
    :param time_key: is a document key in collection containing a time
      used to match any site's operational starttime to endtime time window.
      Default is None which turns off that selection.  Ambiguous keys may
      be reduce in large datasets by using a time key.
    :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 stations_to_ignore: is expected to be a set container listing
      any station codes to be ignored in processing.   This can be used to
      reduce processing overhead or handle sites where net is null and
      not needed at all.   Default is None which turns this option off.
    :return:  Summary of results in the form of a 4 element tuple.
    :rtype:  tuple with the following contents:
        0 - number of documents processed
        1 - number of documents updated in this run
        2 - set container of tuples with content (net,sta,starttime,endtime)
            of all documents matching the reference sta code but having
            different net codes or time spans.   These data are stored in a
            set container to easily sort out the unique combinations.
        3 - set container of station codes that found in collection that
            had no matching entry in the site collection.
    """

    dbh = db[collection]
    dbsite = db['site']
    ambiguous_sta = set()
    not_found_set = set()
    # This is kind of an ugly way to handle null ignore list but is functional
    if stations_to_ignore == None:
        stations_to_ignore = set()
    query = {}
    updaterec = {}
    nprocessed = 0
    nupdates = 0
    if use_immortal_cursor:
        dbcursor = dbh.find({}, no_cursor_timeout=True)
    else:
        dbcursor = dbh.find({})
    for doc in dbcursor:
        nprocessed += 1
        id = doc['_id']
        if not ('sta' in doc):
            print('set_netcode_from_site (WARNING):  document with id=', id,
                  ' has no sta attribute -skipped')
            continue
        sta = doc['sta']
        if sta in stations_to_ignore:
            continue
        if 'net' in doc:
            # silently skip records for which net is already defined for efficiency
            continue
        query.clear()
        query['sta'] = {'$eq': sta}
        if time_key != None:
            if time_key in doc:
                time = doc[time_key]
            # site has starttime and endtime defined so no need to test for
            # their presence.
                query['starttime'] = {"$lt": time}
                query['endtime'] = {"$gt": time}
            else:
                # for now just log this as an error
                print("Time key=", time_key,
                      " not found in document for sta=", sta)
        found = dbsite.find(query)
        nfound = found.count()
        if nfound == 1:
            x = found.next()
            updaterec.clear()
            net = x['net']
            updaterec['net'] = net
            dbh.update_one(
                {'_id': id},
                {'$set': updaterec}
            )
            nupdates += 1
        elif nfound > 1:
            # this dependence on set uniqueness approach may be
            # a bit inefficient for large collections.  Perhaps should
            # test before add
            for x in found:
                net = x['net']
                st = x['starttime']
                et = x['endtime']
                val = tuple([net, sta, st, et])
                ambiguous_sta.add(val)
        else:
            not_found_set.add(sta)
    return [nprocessed, nupdates, ambiguous_sta, not_found_set]