def query_galcoords(gal_lon,<EOL>gal_lat,<EOL>filtersystem='<STR_LIT>',<EOL>field_deg2=<NUM_LIT:1.0>,<EOL>usebinaries=True,<EOL>extinction_sigma=<NUM_LIT:0.1>,<EOL>magnitude_limit=<NUM_LIT>,<EOL>maglim_filtercol=<NUM_LIT:4>,<EOL>trilegal_version=<NUM_LIT>,<EOL>extraparams=None,<EOL>forcefetch=False,<EOL>cachedir='<STR_LIT>',<EOL>verbose=True,<EOL>timeout=<NUM_LIT>,<EOL>refresh=<NUM_LIT>,<EOL>maxtimeout=<NUM_LIT>):
<EOL>inputparams = copy.deepcopy(TRILEGAL_INPUT_PARAMS)<EOL>inputparams['<STR_LIT>'] = '<STR_LIT:1>' if usebinaries else '<STR_LIT:0>'<EOL>inputparams['<STR_LIT>'] = '<STR_LIT>' % extinction_sigma<EOL>inputparams['<STR_LIT>'] = '<STR_LIT>' % field_deg2<EOL>inputparams['<STR_LIT>'] = str(maglim_filtercol)<EOL>inputparams['<STR_LIT>'] = '<STR_LIT>' % magnitude_limit<EOL>inputparams['<STR_LIT>'] = str(trilegal_version)<EOL>inputparams['<STR_LIT>'] = '<STR_LIT>' % gal_lon<EOL>inputparams['<STR_LIT>'] = '<STR_LIT>' % gal_lat<EOL>if field_deg2 > <NUM_LIT>:<EOL><INDENT>LOGERROR("<STR_LIT>")<EOL>return None<EOL><DEDENT>extinction_info = dust.extinction_query(gal_lon,<EOL>gal_lat,<EOL>coordtype='<STR_LIT>',<EOL>forcefetch=forcefetch,<EOL>verbose=verbose,<EOL>timeout=timeout)<EOL>try:<EOL><INDENT>Av_infinity = extinction_info['<STR_LIT>']['<STR_LIT>']['<STR_LIT>']<EOL>inputparams['<STR_LIT>'] = '<STR_LIT>' % Av_infinity<EOL><DEDENT>except Exception as e:<EOL><INDENT>LOGEXCEPTION(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % (gal_lon, gal_lat,<EOL>inputparams['<STR_LIT>'])<EOL>)<EOL><DEDENT>if filtersystem in TRILEGAL_FILTER_SYSTEMS:<EOL><INDENT>inputparams['<STR_LIT>'] = (<EOL>TRILEGAL_FILTER_SYSTEMS[filtersystem]['<STR_LIT>']<EOL>)<EOL><DEDENT>else:<EOL><INDENT>LOGERROR('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % filtersystem)<EOL>return None<EOL><DEDENT>trilegal_params = copy.deepcopy(TRILEGAL_DEFAULT_PARAMS)<EOL>trilegal_params.update(inputparams)<EOL>if extraparams and isinstance(extraparams, dict):<EOL><INDENT>trilegal_params.update(extraparams)<EOL><DEDENT>if '<STR_LIT>' in cachedir:<EOL><INDENT>cachedir = os.path.expanduser(cachedir)<EOL><DEDENT>if not os.path.exists(cachedir):<EOL><INDENT>os.makedirs(cachedir)<EOL><DEDENT>cachekey = repr(inputparams)<EOL>cachekey = hashlib.sha256(cachekey.encode()).hexdigest()<EOL>cachefname = os.path.join(cachedir, '<STR_LIT>' % cachekey)<EOL>provenance = '<STR_LIT>'<EOL>lockfile = os.path.join(cachedir, '<STR_LIT>' % cachekey)<EOL>if forcefetch or (not os.path.exists(cachefname)):<EOL><INDENT>if os.path.exists(lockfile):<EOL><INDENT>with open(lockfile,'<STR_LIT:r>') as infd:<EOL><INDENT>lock_contents = infd.read()<EOL><DEDENT>lock_contents = lock_contents.replace('<STR_LIT:\n>','<STR_LIT>')<EOL>LOGERROR('<STR_LIT>'<EOL>'<STR_LIT>' %<EOL>lock_contents)<EOL>return None<EOL><DEDENT>else:<EOL><INDENT>with open(lockfile,'<STR_LIT:w>') as outfd:<EOL><INDENT>outfd.write(datetime.utcnow().isoformat())<EOL><DEDENT><DEDENT>provenance = '<STR_LIT>'<EOL>try:<EOL><INDENT>if verbose:<EOL><INDENT>LOGINFO('<STR_LIT>'<EOL>% repr(inputparams))<EOL><DEDENT>posturl = TRILEGAL_POSTURL.format(formversion=trilegal_version)<EOL>req = requests.post(posturl,<EOL>data=trilegal_params,<EOL>timeout=timeout)<EOL>resp = req.text<EOL>resultfile = TRILEGAL_REGEX.search(resp)<EOL>if resultfile:<EOL><INDENT>resultfile = resultfile[<NUM_LIT:0>]<EOL>waitdone = False<EOL>timeelapsed = <NUM_LIT:0.0><EOL>resultfileurl = '<STR_LIT>' % (<EOL>TRILEGAL_BASEURL,<EOL>resultfile.replace('<STR_LIT>','<STR_LIT>')<EOL>)<EOL>if verbose:<EOL><INDENT>LOGINFO(<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT>while not waitdone:<EOL><INDENT>if timeelapsed > maxtimeout:<EOL><INDENT>LOGERROR('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT:%s>' % repr(inputparams))<EOL>if os.path.exists(lockfile):<EOL><INDENT>os.remove(lockfile)<EOL><DEDENT>return None<EOL><DEDENT>time.sleep(refresh)<EOL>timeelapsed = timeelapsed + refresh<EOL>try:<EOL><INDENT>resreq = requests.get(resultfileurl)<EOL>resreq.raise_for_status()<EOL>if verbose:<EOL><INDENT>LOGINFO('<STR_LIT>')<EOL><DEDENT>with gzip.open(cachefname,'<STR_LIT:wb>') as outfd:<EOL><INDENT>for chunk in resreq.iter_content(chunk_size=<NUM_LIT>):<EOL><INDENT>outfd.write(chunk)<EOL><DEDENT><DEDENT>tablefname = cachefname<EOL>waitdone = True<EOL>if verbose:<EOL><INDENT>LOGINFO('<STR_LIT>')<EOL><DEDENT><DEDENT>except Exception as e:<EOL><INDENT>if verbose:<EOL><INDENT>LOGINFO('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>% (timeelapsed, resultfileurl))<EOL><DEDENT>continue<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>LOGERROR('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL>LOGINFO(resp)<EOL>if os.path.exists(lockfile):<EOL><INDENT>os.remove(lockfile)<EOL><DEDENT>return None<EOL><DEDENT><DEDENT>except requests.exceptions.Timeout as e:<EOL><INDENT>LOGERROR('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT:%s>' % repr(inputparams))<EOL>return None<EOL><DEDENT>except Exception as e:<EOL><INDENT>LOGEXCEPTION('<STR_LIT>'<EOL>'<STR_LIT:%s>' % repr(inputparams))<EOL>return None<EOL><DEDENT>finally:<EOL><INDENT>if os.path.exists(lockfile):<EOL><INDENT>os.remove(lockfile)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>if verbose:<EOL><INDENT>LOGINFO('<STR_LIT>'<EOL>'<STR_LIT>' %<EOL>(repr(inputparams)))<EOL><DEDENT>tablefname = cachefname<EOL><DEDENT>resdict = {'<STR_LIT>':inputparams,<EOL>'<STR_LIT>':extraparams,<EOL>'<STR_LIT>':provenance,<EOL>'<STR_LIT>':tablefname}<EOL>return resdict<EOL>
This queries the TRILEGAL model form, downloads results, and parses them.

    Parameters
    ----------

    gal_lon,gal_lat : float
        These are the center galactic longitude and latitude in degrees.

    filtersystem : str
        This is a key in the TRILEGAL_FILTER_SYSTEMS dict. Use the function
        :py:func:`astrobase.services.trilegal.list_trilegal_filtersystems` to
        see a nicely formatted table with the key and description for each of
        these.

    field_deg2 : float
        The area of the simulated field in square degrees.

    usebinaries : bool
        If this is True, binaries will be present in the model results.

    extinction_sigma : float
        This is the applied std dev around the `Av_extinction` value for the
        galactic coordinates requested.

    magnitude_limit : float
        This is the limiting magnitude of the simulation in the
        `maglim_filtercol` band index of the filter system chosen.

    maglim_filtercol : int
        The index in the filter system list of the magnitude limiting band.

    trilegal_version : float
        This is the the version of the TRILEGAL form to use. This can usually be
        left as-is.

    extraparams : dict or None
        This is a dict that can be used to override parameters of the model
        other than the basic ones used for input to this function. All
        parameters are listed in `TRILEGAL_DEFAULT_PARAMS` above. See:

        http://stev.oapd.inaf.it/cgi-bin/trilegal

        for explanations of these parameters.

    forcefetch : bool
        If this is True, the query will be retried even if cached results for
        it exist.

    cachedir : str
        This points to the directory where results will be downloaded.

    verbose : bool
        If True, will indicate progress and warn of any issues.

    timeout : float
        This sets the amount of time in seconds to wait for the service to
        respond to our initial request.

    refresh : float
        This sets the amount of time in seconds to wait before checking if the
        result file is available. If the results file isn't available after
        `refresh` seconds have elapsed, the function will wait for `refresh`
        seconds continuously, until `maxtimeout` is reached or the results file
        becomes available.

    maxtimeout : float
        The maximum amount of time in seconds to wait for a result to become
        available after submitting our query request.

    Returns
    -------

    dict
        This returns a dict of the form::

            {'params':the input param dict used,
             'extraparams':any extra params used,
             'provenance':'cached' or 'new download',
             'tablefile':the path on disk to the downloaded model text file}