def test_get_mnemonic_info(): """Test retrieval of mnemonic info.""" from jwql.edb.engineering_database import get_mnemonic_info mnemonic_identifier = 'IMIR_HK_ICE_SEC_VOLT4' info = get_mnemonic_info(mnemonic_identifier) assert 'subsystem' in info.keys()
def get_edb_components(request): """Return dictionary with content needed for the EDB page. Parameters ---------- request : HttpRequest object Incoming request from the webpage Returns ------- edb_components : dict Dictionary with the required components """ mnemonic_name_search_result = {} mnemonic_query_result = {} mnemonic_query_result_plot = None mnemonic_exploration_result = None # If this is a POST request, we need to process the form data if request.method == 'POST': if 'mnemonic_name_search' in request.POST.keys(): # authenticate with astroquery.mast if necessary logged_in = log_into_mast(request) mnemonic_name_search_form = MnemonicSearchForm( request.POST, logged_in=logged_in, prefix='mnemonic_name_search') if mnemonic_name_search_form.is_valid(): mnemonic_identifier = mnemonic_name_search_form[ 'search'].value() if mnemonic_identifier is not None: mnemonic_name_search_result = get_mnemonic_info( mnemonic_identifier) # create forms for search fields not clicked mnemonic_query_form = MnemonicQueryForm(prefix='mnemonic_query') mnemonic_exploration_form = MnemonicExplorationForm( prefix='mnemonic_exploration') elif 'mnemonic_query' in request.POST.keys(): # authenticate with astroquery.mast if necessary logged_in = log_into_mast(request) mnemonic_query_form = MnemonicQueryForm(request.POST, logged_in=logged_in, prefix='mnemonic_query') # proceed only if entries make sense if mnemonic_query_form.is_valid(): mnemonic_identifier = mnemonic_query_form['search'].value() start_time = Time(mnemonic_query_form['start_time'].value(), format='iso') end_time = Time(mnemonic_query_form['end_time'].value(), format='iso') if mnemonic_identifier is not None: mnemonic_query_result = get_mnemonic( mnemonic_identifier, start_time, end_time) mnemonic_query_result_plot = mnemonic_query_result.bokeh_plot( ) # generate table download in web app result_table = mnemonic_query_result.data # save file locally to be available for download static_dir = os.path.join(settings.BASE_DIR, 'static') ensure_dir_exists(static_dir) file_name_root = 'mnemonic_query_result_table' file_for_download = '{}.csv'.format(file_name_root) path_for_download = os.path.join(static_dir, file_for_download) # add meta data to saved table comments = [] comments.append( 'DMS EDB query of {}:'.format(mnemonic_identifier)) for key, value in mnemonic_query_result.info.items(): comments.append('{} = {}'.format(key, str(value))) result_table.meta['comments'] = comments comments.append(' ') comments.append('Start time {}'.format(start_time.isot)) comments.append('End time {}'.format(end_time.isot)) comments.append('Number of rows {}'.format( len(result_table))) comments.append(' ') result_table.write(path_for_download, format='ascii.fixed_width', overwrite=True, delimiter=',', bookend=False) mnemonic_query_result.file_for_download = file_for_download # create forms for search fields not clicked mnemonic_name_search_form = MnemonicSearchForm( prefix='mnemonic_name_search') mnemonic_exploration_form = MnemonicExplorationForm( prefix='mnemonic_exploration') elif 'mnemonic_exploration' in request.POST.keys(): mnemonic_exploration_form = MnemonicExplorationForm( request.POST, prefix='mnemonic_exploration') if mnemonic_exploration_form.is_valid(): mnemonic_exploration_result, meta = mnemonic_inventory() # loop over filled fields and implement simple AND logic for field in mnemonic_exploration_form.fields: field_value = mnemonic_exploration_form[field].value() if field_value != '': column_name = mnemonic_exploration_form[field].label # matching indices in table (case-insensitive) index = [ i for i, item in enumerate( mnemonic_exploration_result[column_name]) if re.search(field_value, item, re.IGNORECASE) ] mnemonic_exploration_result = mnemonic_exploration_result[ index] mnemonic_exploration_result.n_rows = len( mnemonic_exploration_result) # generate tables for display and download in web app display_table = copy.deepcopy(mnemonic_exploration_result) # temporary html file, # see http://docs.astropy.org/en/stable/_modules/astropy/table/ tmpdir = tempfile.mkdtemp() file_name_root = 'mnemonic_exploration_result_table' path_for_html = os.path.join(tmpdir, '{}.html'.format(file_name_root)) with open(path_for_html, 'w') as tmp: display_table.write(tmp, format='jsviewer') mnemonic_exploration_result.html_file_content = open( path_for_html, 'r').read() # pass on meta data to have access to total number of mnemonics mnemonic_exploration_result.meta = meta # save file locally to be available for download static_dir = os.path.join(settings.BASE_DIR, 'static') ensure_dir_exists(static_dir) file_for_download = '{}.csv'.format(file_name_root) path_for_download = os.path.join(static_dir, file_for_download) display_table.write(path_for_download, format='ascii.fixed_width', overwrite=True, delimiter=',', bookend=False) mnemonic_exploration_result.file_for_download = file_for_download if mnemonic_exploration_result.n_rows == 0: mnemonic_exploration_result = 'empty' # create forms for search fields not clicked mnemonic_name_search_form = MnemonicSearchForm( prefix='mnemonic_name_search') mnemonic_query_form = MnemonicQueryForm(prefix='mnemonic_query') else: mnemonic_name_search_form = MnemonicSearchForm( prefix='mnemonic_name_search') mnemonic_query_form = MnemonicQueryForm(prefix='mnemonic_query') mnemonic_exploration_form = MnemonicExplorationForm( prefix='mnemonic_exploration') edb_components = { 'mnemonic_query_form': mnemonic_query_form, 'mnemonic_query_result': mnemonic_query_result, 'mnemonic_query_result_plot': mnemonic_query_result_plot, 'mnemonic_name_search_form': mnemonic_name_search_form, 'mnemonic_name_search_result': mnemonic_name_search_result, 'mnemonic_exploration_form': mnemonic_exploration_form, 'mnemonic_exploration_result': mnemonic_exploration_result } return edb_components
def get_edb_components(request): """Return dictionary with content needed for the EDB page. Parameters ---------- request : HttpRequest object Incoming request from the webpage Returns ------- edb_components : dict Dictionary with the required components """ mnemonic_name_search_result = {} mnemonic_query_result = {} mnemonic_query_result_plot = None mnemonic_exploration_result = None # If this is a POST request, we need to process the form data if request.method == 'POST': if 'mnemonic_name_search' in request.POST.keys(): mnemonic_name_search_form = MnemonicSearchForm( request.POST, prefix='mnemonic_name_search') if mnemonic_name_search_form.is_valid(): mnemonic_identifier = mnemonic_name_search_form[ 'search'].value() if mnemonic_identifier is not None: mnemonic_name_search_result = get_mnemonic_info( mnemonic_identifier) # create forms for search fields not clicked mnemonic_query_form = MnemonicQueryForm(prefix='mnemonic_query') mnemonic_exploration_form = MnemonicExplorationForm( prefix='mnemonic_exploration') elif 'mnemonic_query' in request.POST.keys(): mnemonic_query_form = MnemonicQueryForm(request.POST, prefix='mnemonic_query') # proceed only if entries make sense if mnemonic_query_form.is_valid(): mnemonic_identifier = mnemonic_query_form['search'].value() start_time = Time(mnemonic_query_form['start_time'].value(), format='iso') end_time = Time(mnemonic_query_form['end_time'].value(), format='iso') if mnemonic_identifier is not None: mnemonic_query_result = get_mnemonic( mnemonic_identifier, start_time, end_time) mnemonic_query_result_plot = mnemonic_query_result.bokeh_plot( ) # create forms for search fields not clicked mnemonic_name_search_form = MnemonicSearchForm( prefix='mnemonic_name_search') mnemonic_exploration_form = MnemonicExplorationForm( prefix='mnemonic_exploration') elif 'mnemonic_exploration' in request.POST.keys(): mnemonic_exploration_form = MnemonicExplorationForm( request.POST, prefix='mnemonic_exploration') if mnemonic_exploration_form.is_valid(): mnemonic_exploration_result, meta = mnemonic_inventory() # loop over filled fields and implement simple AND logic for field in mnemonic_exploration_form.fields: field_value = mnemonic_exploration_form[field].value() if field_value != '': column_name = mnemonic_exploration_form[field].label # indices in table for which a match is found (case-insensitive) index = [ i for i, item in enumerate( mnemonic_exploration_result[column_name]) if re.search(field_value, item, re.IGNORECASE) ] mnemonic_exploration_result = mnemonic_exploration_result[ index] mnemonic_exploration_result.n_rows = len( mnemonic_exploration_result) display_table = copy.deepcopy(mnemonic_exploration_result) # temporary html file, see http://docs.astropy.org/en/stable/_modules/astropy/table/ # table.html#Table.show_in_browser tmpdir = tempfile.mkdtemp() path = os.path.join(tmpdir, 'mnemonic_exploration_result_table.html') with open(path, 'w') as tmp: display_table.write(tmp, format='jsviewer') mnemonic_exploration_result.html_file = path mnemonic_exploration_result.html_file_content = open( path, 'r').read() # pass on meta data to have access to total number of mnemonics mnemonic_exploration_result.meta = meta if mnemonic_exploration_result.n_rows == 0: mnemonic_exploration_result = 'empty' # create forms for search fields not clicked mnemonic_name_search_form = MnemonicSearchForm( prefix='mnemonic_name_search') mnemonic_query_form = MnemonicQueryForm(prefix='mnemonic_query') else: mnemonic_name_search_form = MnemonicSearchForm( prefix='mnemonic_name_search') mnemonic_query_form = MnemonicQueryForm(prefix='mnemonic_query') mnemonic_exploration_form = MnemonicExplorationForm( prefix='mnemonic_exploration') edb_components = { 'mnemonic_query_form': mnemonic_query_form, 'mnemonic_query_result': mnemonic_query_result, 'mnemonic_query_result_plot': mnemonic_query_result_plot, 'mnemonic_name_search_form': mnemonic_name_search_form, 'mnemonic_name_search_result': mnemonic_name_search_result, 'mnemonic_exploration_form': mnemonic_exploration_form, 'mnemonic_exploration_result': mnemonic_exploration_result } return edb_components