def query_attr_where(params, table_ref=True):
    """
      Construct where conditions when building neighbors query
        Create portion of WHERE clauses for weeding out NULL-valued geometries
        Input: dict of params:
            {'subquery': ...,
             'numerator': 'data1',
             'denominator': 'data2',
             '': ...}
        Output:
          'idx_replace."data1" IS NOT NULL AND idx_replace."data2" IS NOT NULL'
        Input:
        {'subquery': ...,
         'time_cols': ['time1', 'time2', 'time3'],
         'etc': ...}
        Output: 'idx_replace."time1" IS NOT NULL AND idx_replace."time2" IS NOT
          NULL AND idx_replace."time3" IS NOT NULL'
    """
    attr_string = []
    template = "\"%s\" IS NOT NULL"
    if table_ref:
        template = "idx_replace." + template

    if ('time_cols' in params) or ('ind_vars' in params):
        # markov or gwr where clauses
        attrs = (params['time_cols'] if 'time_cols' in params
                 else params['ind_vars'])
        # add values to template
        for attr in attrs:
            attr_string.append(template % attr)
    else:
        # moran where clauses

        # get keys
        attrs = [k for k in params
                 if k not in ('id_col', 'geom_col', 'subquery',
                              'num_ngbrs', 'subquery')]

        # add values to template
        for attr in attrs:
            attr_string.append(template % params[attr])

        if 'denominator' in attrs:
            attr_string.append(
              "idx_replace.\"%s\" <> 0" % params['denominator'])

    out = " AND ".join(attr_string)

    return out