#!/usr/bin/env python

from glob import glob
import os
import re

def make_feedmes():
    # Used to convert all the fit*.galfit files to fit*.diff
    ids = glob('*/')
    for id in ids:
        os.chdir(id)
        feedmes = glob('fit*diff')
        # output starting models
        for f in feedmes:
            template = r'fit(.*)(\d)(n|m){0,1}([ugrizYJHK]{0,1})([abcde]{0,1})'
            matchobj = re.match(template, f)
            if matchobj.group(1) != 'A' or matchobj.group(5) != '':
                cmd = matchobj.expand('patch -o \g<0>.galfit ../A\g<2>/'
                                      'fitA\g<2>\g<4>.galfit \g<0>.diff')
                os.system(cmd)
        os.chdir('..')

if __name__ =='__main__':
    make_feedmes()

@@ -184,29 +184,8 @@
 ds:%0A
-        os.chdir(id)%0A
     
@@ -203,16 +203,19 @@
  = glob(
+id+
 'fit*dif
@@ -302,16 +302,18 @@
 ate = r'
+.*
 fit(.*)(
@@ -537,11 +537,8 @@
 fit 
-../
 A%5Cg%3C
@@ -612,24 +612,24 @@
 g%3C0%3E.diff')%0A
+
             
@@ -650,31 +650,8 @@
 cmd)
-%0A        os.chdir('..')
 %0A%0Aif

Fix to work with new patch