Skip to content. | Skip to navigation

Personal tools


You are here: Home / Data / SNooPy / SNooPy Documentation / SNooPy Example 2

SNooPy Example 2

Here's an example of how you might try to fit a bunch of objects in a loop.

Another situation.  You've got a whole bunch of objects and you want to fit them all with the minimal amount of interaction.  Let's say they're all in .txt files in a folder you've created.  You want to fit them with the max_model (solving for maximum magnitude in each band rather than E(B-V) and distance modulus), save the fitted objects to .snpy files and create graphs of the fits.  And let's keep track of the objects that failed miserably.

% snpy
< Welcome message>

In[1]: import glob

In[2]: the_list = glob.glob('*.txt')

I've imported the glob module and created a list of all objects in the folder that end with .txt.  Next, we'll open up a log file to let us know which objects failed:

In[3]: fout = open('failures.log', 'w')

Now, I'm going to create a loop and deal with each object one by one.  I'm enclosing the entire block of the for loop in a try: block, which will catch the exceptions and allow me to log the failures (and keep going on to the next object).  Note that ipython will automatically indent for you, but you have to de-dent for yourself.

In [4]: for file in the_list:
   ...:    try:
   ...:        s = get_sn(file)
   ...:        s.choose_model('max_model', stype='st')
   ...:['B'], kcorr=0)
   ...:        if < 0.5:
   ...:            s.k_version = '91bg'
   ...:,, Tmax=s.Tmax)
   ...:        s.plot("_plot.eps")
   ...:    except:
   ...:        fout.write('Failed on object %s' %
In[5]: fout.close()

The python syntax for 'for' and 'if' loops is that you end the first line with a colon (:) and indent the block.  So breaking it down:

  • Each loop is enclosed in a try:/except: block.  If any command in the try: block generates an exception, it is caught and handled by the except: block, which in this case writes the name of the SN to the log file.
  • Inside the try: block, we first get the SN from the .txt file.  We then choose the max_model model and select stretch (st) as the stretch parameter (default is to use dm15).
  • We then fit the B-band (I'm assuming in this case that there is always a B band) with k-corrections turned off.  I basically just want to quicly solve for Tmax and the stretch.
  • I test to see if the stretch is low enough to switch to the SN1991bg spectral template for doing the k-corrections.
  • I then fit the remaining filters, keeping st and Tmax fixed and k-corrections turned off, to get the maxima of all the other filters.
  • Then I fit all bands with no constraints and turn the k-corrections back on.
  • If all goes well, the plot is saved to a file with the base name of the SN and '_plot.eps' on the end.  Then save to a .snpy file.

I would then inspect the file "failures.log" for any failed SNe and work with them interactively to find out why they're giving SNooPy a hard time.