Пример #1
0
def write_plan(dir,calibs):
    """ write_plan(dir,calibs)

    This reads in the directory passed.  For each file in that
    directory that is in the calibration list, it builts up a string
    for the plan file.  This routine also has all of the header
    information for the plan file.  All of this is written to a file
    in the specified directory (dir) called "plan.par".
    
    """
    fitsfiles = glob.glob(dir + "/*.fits*")
    #    print fitsfiles
    nfiles = len(fitsfiles)
    if nfiles > 0:

        hdrblock = "logfile 'plan.log'   # Log file\n"
        hdrblock += "plotfile 'plan.ps'   # Plot file\n"
        hdrblock += "indir './'   # Raw data directory\n"
        hdrblock += "tempdir Temp     # Temporary (working) directory\n"
        hdrblock += "scidir  Science  # Science output directory\n"
        hdrblock += "maxobj     5  # Maximum number of Objects per slit\n"
        hdrblock += "minslit 20  # Minimum slit width\n"
        hdrblock += "reduxthresh 0.01 # Sets the fraction of the brightest objects on each slit that is reduced\n"
        hdrblock += "idlutilsVersion ''  # Version of idlutils when building plan file\n"
        hdrblock += "LongslitVersion 'NOCVS:'  # Version of Longslit when building plan file\n\n"
        hdrblock += "typedef struct {\n"
        hdrblock += "char filename[24];\n"
        hdrblock += "char flavor[6];\n"
        hdrblock += "char target[13];\n"
        hdrblock += "float exptime;\n"
        hdrblock += "char instrument[5];\n"
        hdrblock += "char grating[10];\n"
        hdrblock += "char wave[10];\n"
        hdrblock += "char maskname[9];\n"
        hdrblock += "} LEXP;\n\n"

        rxidltypes = { "IntFlat" : "bothflat",
                      "Line" : "arc"}
        bxidltypes = { "IntFlat" : "twiflat",
                      "Line" : "arc"}


        planfile = open(os.path.join(dir,"plan.par"),"w")
        planfile.write(hdrblock)


        # note  - I just ASSUME that the files are all the same.

        for fitsfile in fitsfiles:
            for calib in calibs:
                if calib.name in fitsfile:
                    line = Planutil.genXIDLframestr(calib,calib.instrument.name)
                    planfile.write(line)

        planfile.close()

    return(nfiles)
Пример #2
0
def writesens_str(plan,datapath,sens_str,idlenv):
    """This writes out the actual executable for running the
    sensitivity calculation. It builds the path to the final csh
    script and then writes out the string of the idl into that file.

    Obsolete.
    """
    runpath = os.path.join(datapath,plan.finalpath,"sens.csh")
    executable = Planutil.writerunstr(runpath,datapath,sens_str,idlenv)
    return(runpath)
Пример #3
0
def writeplan(plan,datapath,idlenv):
    """ Given a plan object, the datapath and the string containing the csh with
    the idl environment variables, this writes out the data reduction plan
    and returns the string neccessary to execute the idl code.
    
    """

    plan.runstr = genplanflags(plan) # for "legacy" reasons the additional flags for the idl
                                     # procedure long_reduce are stored in the runstr of plan
                                     # This will make it into a form that can be parsed by Planutil.genrunstr()
                                     #
    Planutil.writeplan(plan,datapath)
    runpath = os.path.join(datapath,plan.finalpath, plan.display_name)
    if os.path.isfile(runpath):
        os.remove(runpath)
    # executable = Planutil.writerunstr(runpath,datapath,Planutil.genrunstr(plan,datapath),idlenv)
    runstr = 'source %s;' % idlenv
    executable = Planutil.XIDLmodrunstr(plan,datapath)
    runstr += executable
    plan.started=datetime.datetime.now()
    plan.finished=None
    plan.setstatus(1)
    
    return runstr
Пример #4
0
def genplanflags(plan):
    """genplanflags(plan)

    Generates plan flags from a frame attached to the plan.  The flags
    are then bundled into a str using the dicttostr() function.
    """
    
    
    flagsd = dict()
    for frame in plan.frames:
        if frame.flags != "":
            linearray = frame.flags.split()
            for element in linearray:
                key,value = element.split('=')
                flagsd[key]=value


    flags = Planutil.dicttostr(flagsd)
    return(flags)
Пример #5
0
testfiledir = "r101216_0093"
testcalfile = "r101216_0092.fits.gz"
testdir = os.path.abspath("test_data")

if not os.path.isfile(os.path.join(testdir,testfile)):
	print "file %s does not exist, check test data and setup" % (os.path.join(testdir,testfile))
	sys.exit(-1)
hdr = pyfits.getheader(os.path.join(testdir,testfile),0)
calhdr = pyfits.getheader(os.path.join(testdir,testcalfile),0)
stdhdu = pyfits.open(os.path.join(testdir,testfiledir,'Science',stdfile))

instrument = Frameutil.find_instrument_name(hdr)
obsdate,obstime = Frameutil.getobsdate(hdr)
cobsdate,cobstime = Frameutil.getobsdate(calhdr)
nmsg,stdframe = Frameutil.addframe(testfile,hdr,obsdate,obstime,testfile,os.path.join(testdir,testfiledir),instrument)
nmsg,calframe = Frameutil.addframe(testcalfile,calhdr,cobsdate,cobstime,testcalfile,testdir,instrument)
xidlpipe = Pipeline(display_name="XIDL for LRIS",
		    runstr='idl -e long_reduce',
		    instrument=stdframe.instrument,
		    framelist="Object Line IntFlat Flat DmFlat Trace"                    
	)
pipes = [xidlpipe]
	
flag = dict(redo=False)
correctplan = Planutil.buildplan(stdframe,"test.plan",os.getcwd(),xidlpipe,flag)
correctplan.frames.append(stdframe)

calframes = [calframe]
stars = ["G191B2B","Feige 34","HZ 44","BD+33 2642","BD+28 4211","Feige 110"]
#
Пример #6
0
def buildandrunplan(filename,watchdir,stddir,pipelines,calibs,stars,idlenv,flag):
    """This does what it name says. It requires a filename for a standard image,
    the directory that image lives in, the output directory, a list of pipelines, calibration
    frames, a starlist, a filename for a csh script containing the IDL env information
    and a dictionary of flags.

    This copies the file into the appropiate output directory. It checks the file to
    see if it is a standard star observation and the star is in the starlist. It checks to see
    if a pipeline that can proccess the file is in the pipline list. Then, it finds the appropriate
    calibration frames, builds the final data reduction plan and executes it.

    The routine returns the run data reduction plan and a message string. If the plan is False,
    then the routine failed, the message string should return an error. Otherwise, the string is empty.
    """

    # first, we parse the input file and make the frame instance
    # remember, this copies the file from the current location (watchdir)
    # to the final directory which is stddir/ + date_str/ + filename/ 
    msg,frame= Frameutil.ingestframe(os.path.basename(filename),watchdir,stddir,flag)
    if not frame:
        return(msg,False)

    # second, we check to see if the input file is in the allowed list
    msg = check_if_std_frame(frame,stars)
    if msg:
        # crap - at this point we have already moved the to stddir
        if os.path.isfile(os.path.join(stddir,os.path.basename(filename))):
            os.remove(os.path.join(stddir,os.path.basename(filename)))
        return(msg,False)


    # given an acceptable frame, we make the plan file
    pipeline = find_pipeline(frame,pipelines)
    pipeflags(frame,pipeline)
    #    planname = re.sub(r"\.fit(s?)",r".plan",os.path.basename(filename))
    planname = "plan.par"
    plan = Planutil.buildplan(frame,planname,stddir,pipeline,flag)
    plan.frames.append(frame)
    # now we use the calib file list in the calib directory
    # to find matching calibrations
    #
    # find_calibframes also will copy those frames into the plan
    # directory
    calframes,msg = find_calibframes(frame,plan,calibs,stddir)
    if not calframes or len(calframes) == 0:
        if os.path.isfile(os.path.join(stddir,plan.finalpath,os.path.basename(filename))):
            os.remove(os.path.join(stddir,plan.finalpath,os.path.basename(filename)))
        return(msg,False)
    msg = "" # msg can be filled with information for find_calibframes that is not relevant
             # I should make linkreduced smarter
    plan.frames += calframes
    # update with calibration data frames and write out the plan file
    plan = Planutil.updateplandata(plan,stddir)
    runstr = writeplan(plan,stddir,idlenv)
    # executable = shlex.split(runstr)
    # actually run the pipeline
    cwd = os.path.join(stddir,plan.finalpath)
    outputfile = open(os.path.join(cwd,'longreduce.log'),"wb")
    curproc = None
    try:
        curproc = subprocess.call(runstr,
            cwd=cwd,
            stdout=outputfile,
            stderr=outputfile,
            executable="/bin/tcsh",
            shell=True
            )
            # curproc.wait()
        
    except :
        msg = "Error running process %s" % (runstr)

    return(msg,plan)