def setUp(self): setref() self.gref = irafconvert('mtab$t2605492m_tmg.fits') self.cref = irafconvert('mtab$t260548pm_tmc.fits') setref(graphtable=self.gref, comptable=self.cref) self.pick = getref()
def tearDown(self): # Workaround for #234; TODO: change this back to setref(**startup) when # that issue is fixed # setref(**startup) setref(graphtable=startup['graphtable'], comptable=startup['comptable'], thermtable=startup['thermtable'], area=startup['area']) set_default_waveset()
def testcounts(self): #Area is used to convert to counts. #So, changing the area should change the resulting counts. w=N.arange(1,10) p=units.Photlam() ref=p.ToCounts(w,w) setref(area=10) tst=p.ToCounts(w,w) self.assertTrue(N.all(ref != tst))
def test_observation_mode2(): obsmode = ObservationMode('acs,hrc,f555w', graphtable=GT_FILE_NO) assert obsmode.primary_area == refs.PRIMARY_AREA # this should change the obsmode's area refs.setref(area=1.) assert refs.PRIMARY_AREA == 1. assert obsmode.primary_area == refs.PRIMARY_AREA
def test_obs_bandpass2(): bp = ObsBandpass('acs,hrc,f555w', graphtable=GT_FILE_NO) assert bp.primary_area == refs.PRIMARY_AREA # this should change the bandpass's area refs.setref(area=1.) assert refs.PRIMARY_AREA == 1. assert bp.primary_area == refs.PRIMARY_AREA
def test_linear_num(): refs.set_default_waveset(10, 20, 10, log=False) testref = [10., 11., 12., 13., 14., 15., 16., 17., 18., 19.] nptest.assert_array_equal(refs._default_waveset, testref) refs.setref(waveset=(10, 20, 10, 'linear')) nptest.assert_allclose(testref, refs._default_waveset)
def test_linear_num(): refs.set_default_waveset(10, 20, 10, log=False) testref = [ 10., 11., 12., 13., 14., 15., 16., 17., 18., 19.] nptest.assert_array_equal(refs._default_waveset, testref) refs.setref(waveset=(10, 20, 10, 'linear')) nptest.assert_allclose(testref,refs._default_waveset)
def setUpModule(): global old_comptable global old_graphtable old_comptable = refs.COMPTABLE old_graphtable = refs.GRAPHTABLE refs.setref(comptable='$PYSYN_CDBS/mtab/r1j2146sm_tmc.fits', graphtable='$PYSYN_CDBS/mtab/w3j2015mm_tmg.fits') print("%s:" % os.path.basename(__file__)) print(" Tests are being run with %s" % refs.COMPTABLE) print(" ETC comparison results were computed with r1j2146sm_tmc.fits")
def test_observation_mode1(): obsmode = ObservationMode('acs,hrc,f555w', graphtable=GT_FILE_100) assert obsmode.primary_area != refs.PRIMARY_AREA assert obsmode.primary_area == 100.0 # this should have no effect refs.setref(area=1.) assert refs.PRIMARY_AREA == 1. assert obsmode.primary_area != refs.PRIMARY_AREA assert obsmode.primary_area == 100.0
def test_obs_bandpass1(): bp = ObsBandpass('acs,hrc,f555w', graphtable=GT_FILE_100) assert bp.primary_area != refs.PRIMARY_AREA assert bp.primary_area == 100.0 # this should have no effect refs.setref(area=1.) assert refs.PRIMARY_AREA == 1. assert bp.primary_area != refs.PRIMARY_AREA assert bp.primary_area == 100.0
def test_observation2(): spec = FlatSpectrum(1) bp = ObsBandpass('acs,hrc,f555w', graphtable=GT_FILE_NO) obs = Observation(spec, bp) assert obs.primary_area == refs.PRIMARY_AREA # this should change the observations's area refs.setref(area=1.) assert refs.PRIMARY_AREA == 1. assert obs.primary_area == refs.PRIMARY_AREA
def test_log_num(): refs.set_default_waveset(10, 20, 10) testref = [ 10. , 10.71773463, 11.48698355, 12.31144413, 13.19507911, 14.14213562, 15.15716567, 16.24504793, 17.41101127, 18.66065983] nptest.assert_allclose(testref,refs._default_waveset) refs.setref(waveset=(10, 20, 10)) nptest.assert_allclose(testref,refs._default_waveset) refs.setref(waveset=(10, 20, 10, 'log')) nptest.assert_allclose(testref,refs._default_waveset)
def test_observation1(): spec = FlatSpectrum(1) bp = ObsBandpass('acs,hrc,f555w', graphtable=GT_FILE_100) obs = Observation(spec, bp) assert obs.primary_area != refs.PRIMARY_AREA assert obs.primary_area == 100.0 # this should have no effect refs.setref(area=1.) assert refs.PRIMARY_AREA == 1. assert obs.primary_area != refs.PRIMARY_AREA assert obs.primary_area == 100.0
def test_log_num(): refs.set_default_waveset(10, 20, 10) testref = [ 10., 10.71773463, 11.48698355, 12.31144413, 13.19507911, 14.14213562, 15.15716567, 16.24504793, 17.41101127, 18.66065983 ] nptest.assert_allclose(testref, refs._default_waveset) refs.setref(waveset=(10, 20, 10)) nptest.assert_allclose(testref, refs._default_waveset) refs.setref(waveset=(10, 20, 10, 'log')) nptest.assert_allclose(testref, refs._default_waveset)
def create_table(output, basemode, detector, useafter, tmgtab=None, tmctab=None, tmttab=None, mode_list=[], nmodes=None, clobber=False, verbose=True): """Create an IMPHTTAB reference file for a specified base configuration, ``basemode``. Parameters ---------- output : str Output IMPHTTAB filename. (``_imp.fits`` will be appended if only prefix is given.) basemode : str Base observation mode for which to generate IMPHTTAB (e.g., ``acs,hrc``). This is ignored if ``mode_list`` is given. detector : str Detector name. useafter : str Useafter date in the format of ``MMM DD YYYY HH:MM:SS``. tmgtab, tmctab, tmttab : str, optional Graph (TMG), component (TMC), and thermal component (TMT) tables to use. If `None`, the most recent version in CDBS is used. mode_list : list of str, optional A list of observation modes which should be used to make an IMPHTTAB reference file. If given, ``basemode`` is ignored. nmodes : int, optional Set to limit the number of modes to calculate. This is for testing only, otherwise set to `None`. clobber : bool, optional Overwrite existing IMPHTTAB? verbose : bool, optional Display extra information. """ import pysynphot as S from pysynphot import refs if not output.endswith('_imp.fits'): output = output + '_imp.fits' # If cannot overwrite, raise exception here rather than at the end. if os.path.exists(output) and not clobber: raise IOError('Output file already exists. Please delete/rename ' 'before restarting.') # Define graph and component tables. if tmgtab is None: tmgtab = refs.GRAPHTABLE if tmctab is None: tmctab = refs.COMPTABLE if tmttab is None: tmttab = refs.THERMTABLE refs.setref(graphtable=tmgtab, comptable=tmctab, thermtable=tmttab) x = sgf.read_graphtable(tmgtab, tmctab, tmttab) if len(mode_list) == 0: # start by getting the full list of obsmodes before # expanding the parameterized elements x.get_obsmodes(basemode, prefix=True) obsmodes = x.obsmodes else: obsmodes = mode_list # start building obsmodes for each row if nmodes is not None: nrows = nmodes obsmodes = obsmodes[:nmodes] else: nrows = len(obsmodes) fpars_vals = list() # list of parameterized var names for each obsmode/row npar_vals = list() # number of parameterized vars for each obsmode/row flam_datacol_vals = list() plam_datacol_vals = list() bw_datacol_vals = list() fpars_sz = 1 # Compute 'globally' required values: max num of parameterized variables for filt in obsmodes: # For each filter combination (row in the table)... basename, fpars = parse_filters(filt) # keep track of how many parameterized vars are used in this obsmode npars = len(fpars) npar_vals.append(npars) fpars_vals.append(fpars) fpars_len = [len(f) for f in fpars] if len(fpars_len) == 0: fpars_max = 0 else: fpars_max = max(fpars_len) if fpars_max > fpars_sz: fpars_sz = fpars_max if npars == 0: nstr = '' else: nstr = str(npars) flam_datacol_vals.append('PHOTFLAM' + nstr) plam_datacol_vals.append('PHOTPLAM' + nstr) bw_datacol_vals.append('PHOTBW' + nstr) # At this point, all interpretation for the following cols has been done: # OBSMODE, DATACOL (for all 3 tables), PEDIGREE, DESCRIP # # Start by determining the maximum num of parameters in any given obsmode max_npars = np.array(npar_vals, np.int32).max() log.info('MAX_NPARS: {0} NROWS: {1}'.format(max_npars, nrows)) # Now, define empty lists for NELEM* and PAR*VALUES columns # # nelem_rows[i] for each column i nelem_rows = np.zeros([nrows, max_npars], np.int16) parvals_rows = list() filtdata_set = dict() # create columns for PAR*NAMES parnames_rows = np.chararray([nrows, max_npars], itemsize=fpars_sz) parnames_rows[:] = '' * fpars_sz # initialize with blanks, just to be safe for nr in range(nrows): # create path through graphtab for this obsmode, reading in values for # all parameterized variables as well obspath = x.traverse(obsmodes[nr], verbose=False) filtdata = obspath._params # Create a master set of parameterized vars and their ranges of values for p in filtdata: if (p.upper(), obsmodes[nr]) not in list(filtdata_set.keys()): filtdata_set[(p.upper(), obsmodes[nr])] = filtdata[p] fpars = fpars_vals[nr] npars = npar_vals[nr] pvals = list() # extract entries from 'filtdata' for only the values given in 'fpars' for i in range(max_npars): if len(fpars) == 0: pvals.append(np.array([0])) else: if i < len(fpars): f = fpars[i].upper() nelem = len(filtdata[f]) nelem_rows[nr, i] = nelem if filtdata[f] == '': pvals.append(np.array([0])) else: pvals.append(np.array(filtdata[f])) parnames_rows[nr, i] = f else: pvals.append(np.array([0])) parvals_rows.append(pvals) # All NELEM* and PAR*VALUES cols are correctly populated up to this point # in the code. # # Now, define the values for the actual results columns: PHOT*LAM, PHOTBW # flam_rows = list() plam_rows = list() bw_rows = list() nmode_vals = list() # dictionary to hold optical components # (pysynphot.observationmode._Component objects) component_dict = {} # list to hold skipped obsmodes skipped_obs = [] if verbose: log.info('Computing photmetry values for each row\'s obsmode...') sys.stdout.flush() for nr in range(nrows): # Provide some indication of which row is being worked if verbose: log.info('Row: {0}'.format(nr + 1)) sys.stdout.flush() obsmode = obsmodes[nr] fpars = fpars_vals[nr] npars = npar_vals[nr] filtdict = OrderedDict() lenpars = list() for f in fpars: f = f.upper() filtdict[f] = filtdata_set[(f, obsmode)] lenpars.append(len(filtdict[f])) # Now build up list of all obsmodes with all combinations of # parameterized variables values olist = expand_obsmodes(obsmode, filtdict) nmodes = len(olist) # If there are warnings from here, try updating rules_dict # in graphtab.py if nmodes == 0 and verbose: log.warn('No info for {0}'.format(obsmode)) pflam = np.zeros(nmodes, np.float64) pplam = np.zeros(nmodes, np.float64) pbw = np.zeros(nmodes, np.float64) skip = False for n, fullmode in enumerate(olist): try: value = compute_values(fullmode, component_dict) except ValueError as e: if e.message == 'Integrated flux is <= 0': # integrated flux is zero, skip this obsmode skip = True skipped_obs.append(obsmode) flam_datacol_vals.pop(nr) plam_datacol_vals.pop(nr) bw_datacol_vals.pop(nr) parvals_rows.pop(nr) nelem_rows = np.delete(nelem_rows, nr, 0) parnames_rows = np.delete(parnames_rows, nr, 0) if verbose: log.info('\tSkipping {0}'.format(obsmode)) break elif e.message == 'math domain error': skip = True skipped_obs.append(obsmode) if verbose: log.info('\tSkipping {0}'.format(obsmode)) flam_datacol_vals.pop(nr) plam_datacol_vals.pop(nr) bw_datacol_vals.pop(nr) parvals_rows.pop(nr) nelem_rows = np.delete(nelem_rows, nr, 0) parnames_rows = np.delete(parnames_rows, nr, 0) break else: raise if verbose: log.info('\tPHOTFLAM({0}) = {1}'.format( fullmode, value['PHOTFLAM'])) pflam[n] = value['PHOTFLAM'] pplam[n] = value['PHOTPLAM'] pbw[n] = value['PHOTBW'] if skip is True: continue nmode_vals.append(nmodes) # Re-order results so that fastest varying variable is the last index # when accessed as a numpy array later by the C code photflam = ((pflam.reshape(lenpars)).transpose()).ravel() photplam = ((pplam.reshape(lenpars)).transpose()).ravel() photbw = ((pbw.reshape(lenpars)).transpose()).ravel() fvals = list() pvals = list() bvals = list() if npars == 0: fvals.append(photflam[0]) pvals.append(photplam[0]) bvals.append(photbw[0]) else: fvals.append(0) pvals.append(0) bvals.append(0) for col in range(1, max_npars + 1): if col == npars: fvals.append(np.array(photflam, np.float64)) pvals.append(np.array(photplam, np.float64)) bvals.append(np.array(photbw, np.float64)) else: fvals.append(np.array([0])) pvals.append(np.array([0])) bvals.append(np.array([0])) flam_rows.append(fvals) plam_rows.append(pvals) bw_rows.append(bvals) del photflam, photplam, photbw, filtdict, lenpars del component_dict # remove any skipped obsmodes from the obsmodes list for sk in skipped_obs: obsmodes.remove(sk) # save skipped obs to a file if len(skipped_obs) > 0: save_skipped_obsmodes(output, skipped_obs) del skipped_obs log.info('Creating table columns from photometry values...') # Convert nelem information from row-oriented to column oriented nelem_cols = nelem_rows.transpose() parnames_cols = parnames_rows.transpose() parvals_cols = list() flam_cols = list() plam_cols = list() bw_cols = list() for col in range(max_npars): pvals = list() for row in range(len(parvals_rows)): pvals.append(parvals_rows[row][col]) parvals_cols.append(pvals) for col in range(max_npars+1): fvals = list() plvals = list() bvals = list() for row in range(len(flam_rows)): fvals.append(flam_rows[row][col]) plvals.append(plam_rows[row][col]) bvals.append(bw_rows[row][col]) if col == 0: fvals = np.array(fvals) plvals = np.array(plvals) bvals = np.array(bvals) flam_cols.append(fvals) plam_cols.append(plvals) bw_cols.append(bvals) ped_vals = [fits.getval(tmctab, 'pedigree', 0)] * len(nmode_vals) descrip_str = ( 'Generated {0} from {1}, mkimphttab version {2}, ' 'pysynphot version {3}'.format( get_date(), os.path.basename(tmgtab), __version__, S.__version__)) descrip_vals = [descrip_str] * len(nmode_vals) # Finally, create structures needed to define this row in the FITS table # Define each col in the table based on max_npars which are not different # from one extension to the other obsmode_col = fits.Column(name='OBSMODE', format='40A', array=obsmodes) pedigree_col = fits.Column(name='PEDIGREE', format='30A', array=ped_vals) descrip_col = fits.Column(name='DESCRIP', format='110A', array=descrip_vals) datacol_col = {} datacol_col['PHOTFLAM'] = fits.Column( name='DATACOL', format='12A', array=flam_datacol_vals) datacol_col['PHOTPLAM'] = fits.Column( name='DATACOL', format='12A', array=plam_datacol_vals) datacol_col['PHOTBW'] = fits.Column( name='DATACOL', format='12A', array=bw_datacol_vals) parvals_tabcols = list() nelem_tabcols = list() parnames_tabcols = list() parnames_format = str(fpars_sz) + "A" # for each parameterized element, create a set of columns specifying the # range of values for that parameter and the number of elements covering # that range namely, the PAR<n>VALUES and NELEM<n> columns for p in range(max_npars): nelem_tabcols.append(fits.Column( name="NELEM"+str(p+1), format="I", array=nelem_cols[p])) parvals_tabcols.append(fits.Column( name="PAR"+str(p+1)+"VALUES", format="PD()", array=parvals_cols[p])) parnames_tabcols.append(fits.Column( name="PAR"+str(p+1)+"NAMES", format=parnames_format, array=parnames_cols[p])) # create the set of results columns flam_tabcols = list() plam_tabcols = list() bw_tabcols = list() for p in range(max_npars + 1): if p == 0: format_str = 'D' pstr = '' fcols = flam_cols[p] pcols = plam_cols[p] bcols = bw_cols[p] else: format_str = 'PD()' pstr = str(p) fcols = flam_cols[p] pcols = plam_cols[p] bcols = bw_cols[p] flam_tabcols.append(fits.Column( name='PHOTFLAM'+pstr, format=format_str, array=fcols)) plam_tabcols.append(fits.Column( name='PHOTPLAM'+pstr, format=format_str, array=pcols)) bw_tabcols.append(fits.Column( name='PHOTBW'+pstr, format=format_str, array=bcols)) # Now create the FITS file with the table in each extension log.info('Creating full table: {0}'.format(output)) phdu = make_pri_hdu(output, max_npars, basemode.split(',')[0], detector, ped_vals[0], useafter) flam_tab = fits.BinTableHDU.from_columns( [obsmode_col, datacol_col['PHOTFLAM']] + flam_tabcols + parnames_tabcols + parvals_tabcols + nelem_tabcols + [pedigree_col, descrip_col]) flam_tab.header['EXTNAME'] = ('PHOTFLAM', 'Extension name') flam_tab.header['EXTVER'] = (1, 'Extension number') plam_tab = fits.BinTableHDU.from_columns( [obsmode_col, datacol_col['PHOTPLAM']] + plam_tabcols + parnames_tabcols + parvals_tabcols + nelem_tabcols + [pedigree_col, descrip_col]) plam_tab.header['EXTNAME'] = ('PHOTPLAM', 'Extension name') plam_tab.header['EXTVER'] = (1, 'Extension number') bw_tab = fits.BinTableHDU.from_columns( [obsmode_col, datacol_col['PHOTBW']] + bw_tabcols + parnames_tabcols + parvals_tabcols + nelem_tabcols + [pedigree_col, descrip_col]) bw_tab.header['EXTNAME'] = ('PHOTBW', 'Extension name') bw_tab.header['EXTVER'] = (1, 'Extension number') ftab = fits.HDUList() ftab.append(phdu) ftab.append(flam_tab) ftab.append(plam_tab) ftab.append(bw_tab) ftab.writeto(output, clobber=clobber)
def testchange(self): ref=100 setref(area=ref) tst=refs.PRIMARY_AREA self.assertEqual(ref,tst)
def setUp(self): setref() self.ref='mtab$foobar.fits' self.ttype='graphtable' setref(graphtable=self.ref)
def testreset(self): setref() tst=getref() self.assertEqual(startup,tst, "(ref,tst)=(%s,%s)" % (startup, tst) )
def setUp(self): setref() self.ref='mtab$foobar.fits' self.ttype='comptable' setref(comptable=self.ref)
def tearDown(self): refs.setref(graphtable=self.old_refs['graphtable']) refs.setref(comptable=self.old_refs['comptable'])
def setUp(self): setref() self.ttype = 'area' self.ref = 12345.6 setref(area=self.ref)
def testreset(self): setref() tst=getref() self.assertEqual(startup, tst)