def phosimPhysics(workdir, outdir, phosim_exe='phosim.py', dryrun=False):
    pars = utensils.PhosimParameters()
    #
    # Disable the background using parameters in the
    # examples/nobackground file in the phosim installation directory.
    #
    my_phosimdir = phosimdir(phosim_exe)
    bg_file = os.path.join(my_phosimdir, 'examples/nobackground')
    pars.read(bg_file)
    #
    # Turn off various physics effects
    #
    pars['telescopemode'] = '0'      # "perfect dummy telescope"
    pars['detectormode'] = '0'       # charge diffusion off
    pars['diffractionmode'] = '0'    # diffraction off
    pars['exptime'] = '15'           # use standard exposure time (s)
    pars['blooming'] = '0'           # turn blooming off
    pars['clearpurturbations'] = '0' # set all perturbations to zero
    pars['cleartracking'] = '0'      # set all tracking to zero
    pars['clearopacity'] = '0'       # set all forms of atmospheric opacity to zero
    pars['clearturbulence'] = '0'    # set seeing to all layers to zero

    parfile = 'my_config.pars'
    pars.write(parfile)

    utensils.makedir(workdir, query=False)
    utensils.makedir(outdir, query=False)

    catalog = os.path.join(my_phosimdir, 'examples/star')
    command = (phosim_exe, catalog, 
               "-c", os.path.abspath(parfile), 
               "-w", os.path.abspath(workdir), 
               "-o", os.path.abspath(outdir))

    print "\nexecuting\n", ' '.join(command)
    if not dryrun:
        subprocess.call(command)
def stargal():

    # We want to run phosim for both stars and galaxies:
    
    for typ in ['msstars', 'bdgals']:
        
        # The input catalogue files are in this directory. 
        
        catfile = "stargal-"+typ+".pars"

        # The data in the input catalogues provided cover this chip.
        # Format: R is the raft coordinate
        #         S is the sensor coordinate
        #         This is the center raft (22), 
        #         and the top center chip (21). 
        
        sensor = "R22_S21" 


        # Get seeing and seeds for the 100 atmospheric realisations.
        # These were generated from opsim, and exist in a dat file in
        # this directory. 
        
        seeings, seeds  = [], []
        for aline in open("stargal-atmos.dat","r"):
            cols = aline.split()
            seeds.append(cols[0])
            seeings.append(cols[1])


        # Loop over the 100 atmospheric realisations: 
        
        for atm in range(1,100):

            # Name your work and output dirs. This allows many jobs to
            # run simultaneously without their working files bumping
            # into each other on the disk.  make sure you put in the
            # full path to the directories, not the relative path. 
            
            workdir = "/path/to/ImageSimulationRecipes/python/recipes/work_"+typ+"_"+sensor+"_atm"+str(atm)
            outdir = "/path/to/ImageSimulationRecipes/python/recipes/output_"+typ+"_"+sensor+"_atm"+str(atm)


            # Point out what we're doing:
            
            print "Running phosim over", typ, \
                  "for atmosphere realisation", atm

            # Check whether the work and output dirs exist. If so, 
            # empty them. If not, make them. phosim will fail if they 
            # don't exist. 
            
            utensils.makedir(workdir, replace=True, query=False)
            utensils.makedir(outdir, replace=True, query=False)

            # Write out a new catalogue file to your workdir.
            # This will contain the same objects as the orginal 
            # catalogue, and the same pointing/rotation angle etc, but 
            # will have new random seed and seeing parameters. 
            
            newcatfilename = workdir+"/cat-"+typ+"-atm"+str(atm)+".dat"
            newcatfile = open(newcatfilename,"w")
            for aline in open(catfile):
                if "SIM_SEED" in aline:
                    print >> newcatfile, "SIM_SEED", seeds[int(atm)]
                elif "Opsim_rawseeing" in aline:
                    print >> newcatfile, "Opsim_rawseeing", seeings[int(atm)]
                else:
                    print >> newcatfile, aline[:-1]                
            newcatfile.close()



            # Now you're all set! Run phosim over this catalogue file.
            # there are many options here depending on your setup/needs.
            # Note that here we specify the configuration file for no 
            # sky background.

            # To run it interactively from your phosim installation
            # directory:
            
            subcmd = ["./phosim", newcatfilename,  "-c", "examples/nobackground", "-s", sensor, "-w", workdir, "-o", outdir]

            # To run it interactively on the slac batch system, you need
            # to specify the architecture to be rhel60. This uses the
            # xlong queue; if you have bright stars you may need xxl.
            # You can probably get away with the long queue as-is.  note
            # that this also uses the SLAC installation of phosim. 

            # SLAC-specific setup: if you haven't already, you will need
            # to load the LSST environment via: source
            # /afs/slac/g/lsst/software/redhat6-x86_64-64bit-gcc44/DMstack/Winter2013-v6_2/loadLSST.csh
            # You may also run into problems with a specific
            # environmental variable that you shoudl unset via: unsetenv
            # LS_COLORS

            # subcmd = ["bsub", "-q", "xlong", "-o", workdir+"/log.log", "-R", "rhel60", "python", "/afs/slac/g/lsst/software/redhat6-x86_64-64bit-gcc44/phoSim/phosim-3.3.2/phosim.py", newcatfilename,  "-c", "examples/nobackground", "-s", sensor, "-w", workdir, "-o", outdir]

            subprocess.call(subcmd)

            # You may want to pause between jobs, especially if you're
            # submitting them to a batch system. 
            
            time.sleep(5)
def simGridStars():
    
    ## making a star catalogue for an entire focal plane 
    ## is a lot of data, so we'll restrict ourselves to one 
    ## CCD. This is easily extended. 

    # Set up some useful things. 
    # You'll need to add the full path to wherever you keep the SED files.
    # These can be downloaded via this command: 
    # curl -O http://lsst-web.ncsa.illinois.edu/lsstdata/summer2012/SEDs/SEDs.tar.gz
    path_to_SEDs = '/path/to/SED/data/' 

    # If phosim is not in your PATH, edit this line to point to it. 
    phosim_path = "/path/to/phosim/installation/"
   
    ## which chip are we going to simulate?
    sensor = 'R01_S00'

    ## output catalog file:
    catfilename = 'grid-msstars.pars'
    catfile = open(catfilename,'w')


    ## We'll use RA=81.18, dec=-12.21 as our pointing, as used in the 
    ## existing catalog stargal-msstars.pars. 
    ## This is convenient, because the astrometric solution has already 
    ## been calculated for the simulated images at this pointing. 
    ## If you don't care about running through the DM stack, you can 
    ## of course chose whatever pointing you want. 
    
    ra = 81.176
    dec = -12.210

    ## copy over the observing params. 
    ## We want to have zero telescope rotation to make it easier to 
    ## figure out which stars to make for which ccd. 
    for line in open('stargal-msstars.pars','r'):
        cols = line.split()
        if len(cols)==2:
            if 'RA'  in line:
                print >> catfile, 'Unrefracted_RA', ra
            elif 'Dec' in line:
                print >> catfile, 'Unrefracted_Dec', dec

            else:
                print >> catfile, line[:-1]

            
    ## And now for the grid of stars! 
    ## we'll make enough stars to cover the entire field of view - 
    ## this is simpler thantrying to figure out whic patch of sky each chip will observe.
    ## The LSST FoV is 3.5' diameter
    ra_start = ra - (3.5/2.0)+0.1 ## a little extra to cover the outer-most CCDs
    ra_stop = ra + (3.5/2.0)+0.1

    dec_start = dec - (3.5/2.0)+0.1
    dec_stop = dec + (3.5/2.0)+0.1
    
    ## Use 1 star per square arcminute. 
    ## We're doing r-band, and we want a star around 17th mag 
    ## (so as not to saturate, which happens at r~16). 
    ## mag_norm in the input file is *not* the same as r-band mag, it's a little higher. 
    ## SED file is chosen at random. 
    print ra_start, ra_stop
    print dec_start, dec_stop

    for i in np.arange(ra_start, ra_stop, 1/60.0):
        for j in np.arange(dec_start, dec_stop, 1/60.0):
            print >> catfile, 'object', str(i*j), str(i), str(j), '17.5', 'starSED/mlt/m4.4Full.dat.gz', '0 0 0 0 0 0 POINT CCM 0.476663142 3.1 none' 

    catfile.close()


    # Now we can run phosim over this file. 
    # We'll specify a work dir, and an output dir. 
    workdir = 'work-gridStars/'
    outdir = 'out-gridStars/'

    utensils.makedir(workdir, replace=True, query=False)
    utensils.makedir(outdir, replace=True, query=False)

    
    # And run phosim over this new file. Note that we've specified 
    # no sky background, to speed things up. 
    subcmd = ['python', phosim_path+"phosim.py", catfilename,  "-c", phosim_path+"examples/nobackground", "-s", sensor, "-w", workdir, "-o", outdir]
    
    subprocess.call(subcmd)
def selectFaintStars():

    ## we'll use the catalog file containing main sequence stars 
    ## from the star/gal example. 
    
    catfile = "stargal-msstars.pars"
    
    # Set up some useful things. 
    # You'll need to add the full path to wherever you keep the SED files.
    # These can be downloaded via this command: 
    # curl -O http://lsst-web.ncsa.illinois.edu/lsstdata/summer2012/SEDs/SEDs.tar.gz
    path_to_SEDs = '/path/to/data/SED/'

    # If phosim is not in your PATH, edit this line to point to it. 
    phosim_path = "/path/to/phosim/installation/" 

    # getPhosimMag need the filter throughput curves. If unspecified, 
    # getPhosimMag will download it from the website *every time* it's called. 
    # It will be far more efficient if we download the file, and pass it to the function. 
    # Possible filters: u g r i z y4
    
    urlfile = 'https://dev.lsstcorp.org/trac/export/29728/sims/throughputs/tags/1.2/baseline/total_r.dat'
    print 'reading throughput curve from {}.'.format(urlfile)
    urllib.urlretrieve(urlfile, 'total_r.dat')
        
    lsst_filter = 'total_r.dat'

    ## Open the file we'll write the output into.
    newcatfilename = "faint-msstars.pars"
    newcatfile = open(newcatfilename,"w")

    for line in open(catfile):
        cols = line.split()
        if len(cols)<3:
            # these are the params that set up the observation. 
            print >> newcatfile, line[:-1]

        else:
            # Star catalog params are:
            # name  id  ra  dec  mag_norm  sed  redshift shear1 shear2 magnification shift_x shift_y  source_type  dust_model_in_rest_frame  internal_ac  internal_rv  dust_model_lab_frame  galactic_av galt=ctic_rv 
            # Stars, of course, have zeros for the shear/magnification/shift parameters, and for the galactic dust model. 

            # getPhosimMag requires the filter, mag_norm, sed name, and internal dust params.

            mag_norm = float(cols[4])
            sed = path_to_SEDs+cols[5]
            redshift = float(cols[6])
            int_dust = cols[13]
            int_Av = float(cols[14])
            int_Rv = float(cols[15])
            
            # Run the utensil to obtain the r-band magnitudes
            mag = utensils.getPhosimMag(lsst_filter, mag_norm, sed, redshift, int_dust, int_Av, int_Rv)
            # Let's discard all stars brighter than 16th mag (which is roughly when saturation occurs in an LSST CCD)
            if mag<16:
                continue
            else:
                print >> newcatfile, line[:-1]

    newcatfile.close()


    # Now we can run phosim over this file. 
    # We'll specify a work dir, and an output dir. 
    workdir = 'work-faintStars/'
    outdir = 'out-faintStars/'

    utensils.makedir(workdir, replace=True, query=False)
    utensils.makedir(outdir, replace=True, query=False)

    # We have stars in this catalog that fall on raft 22, CCD 21. 
    # Specifying this saves some time in the execution so that 
    # phosim doesn't look for objects anywhere else. 
    sensor = 'R22_S21' 
    
    # And run phosim over this new file. Note that we've specified 
    # no sky background, to speed things up. 
    subcmd = ['python', phosim_path+"phosim.py", newcatfilename,  "-c", phosim_path+"examples/nobackground", "-s", sensor, "-w", workdir, "-o", outdir]
    
    subprocess.call(subcmd)