def writeLocations(srcDir,outfile,overwrite=True):
    """Extract the recording locations and write to a csv file"""
    try:
        if overwrite:
            f = open(outfile,'w')
        else:
            f = open(outfile,'w+')
    except IOError as e:
        logger.error('Could not open file:{} for writing. {}'.format(outfile,e))

    recordingCoords = psiSession.getImagingSessionInfo(srcDir)
        
    csv_writer = csv.DictWriter(f,
                                delimiter=',',
                                fieldnames=recordingCoords[0].keys())
    if overwrite:
        csv_writer.writeheader()
        
    for row in recordingCoords:
        csv_writer.writerow(row)

    f.close()
    
    # check the outdir exists
    if args.outDir:
        try:
            os.mkdir(args.outDir)
        except OSError as e:
            if e.errno == 17:
                #already exists
                pass
            else:
                logger.error('Failed to create output dir {}. {}'.format(args.outDir,e))
            
    if args.outDir:
        writeLocations(args.sourceDir,
                       os.path.join(args.outDir,'locations.csv'),
                       args.overwrite)
    else:
        print psiSession.getImagingSessionInfo(args.sourceDir)
        

    #now align the files
    recordings = psiSession.getRecordings(args.sourceDir)
    #for recording in recordings[0]:
    for recording in recordings:
        alignSloFile(args.sourceDir,
                     recording,
                     args.outDir,
                     args.moveLocal, 
                     args.writeStable, 
                     args.writeFrames)