00001 __copyright__ = """
00002 Copyright 2008 Henryk Modzelewski
00003 """
00004 __license__ = """
00005 This file is part of SLIMpy .
00006
00007 SLIMpy is free software: you can redistribute it and/or modify
00008 it under the terms of the GNU Lesser General Public License as published by
00009 the Free Software Foundation, either version 3 of the License, or
00010 (at your option) any later version.
00011
00012 SLIMpy is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00015 GNU Lesser General Public License for more details.
00016
00017 You should have received a copy of the GNU Lesser General Public License
00018 along with SLIMpy . If not, see <http://www.gnu.org/licenses/>.
00019 """
00020
00021 '''
00022 Set of algorithms for seismic data processing.
00023 '''
00024 import os
00025 import Steppers
00026 import MultiPipe
00027
00028 def GenThreshLandweber(ForwardOper, AdjointOper,
00029 lambdaMax, lambdaMin, lambdaN, InnerN,
00030 DataIn, DataOut,
00031 TMPDIR=None, VERBOSE=False):
00032 '''
00033 Generalized Thresholded Landweber method using RSF commands.
00034
00035 Requires:
00036 RSFROOT environment has to be specified, such that $RSFROOT/bin is the path
00037 to RSF commands. SLIM python modules: Steppers and MultiPipe
00038 Parameters:
00039 ForwardOper: list of strings containing complete definition of forward operator
00040 AdjointOper: list of strings containing complete definition of adjoint operator
00041 (For both above, each string has to be a complete RSF command
00042 working on the stdin and stdout.)
00043 lambdaMax: maximum lambda
00044 lambdaMin: minimum lambda
00045 lambdaN: number of steps in iteration from lambdaMax to lambdaMin
00046 InnerN: number of iterations for any lambda
00047 DataIn: RSF file with input data
00048 DataOut: RSF file for output data
00049 Keyword parameters (optional):
00050 TMPDIR: temporary directory (system default if not specified)
00051 VERBOSE: verbose if set to True
00052 Returns:
00053 0 if successful, positive number otherwise
00054 Notes:
00055 The function generates a number of temporary files that will have to be cleaned
00056 manually if it is forced to stop before it finishes.
00057 Bugs:
00058 None so far, but keep trying.
00059 It is not fool-proof. So, you have to know what you are doing;
00060 i.e., how to use RSF.
00061 '''
00062
00063 if VERBOSE: print '%%%%%% into GenThreshLandweber'
00064 err = 0
00065
00066
00067 try: RSFbin = os.environ['RSFROOT']+'/bin/'
00068 except:
00069 print 'FATAL: missing RSFROOT environment'
00070 return 1
00071 if VERBOSE: print 'RSF binaries in:',RSFbin
00072
00073
00074 x = os.tempnam(TMPDIR,'x.')+'.rsf'
00075 xTmp = os.tempnam(TMPDIR,'xTmp.')+'.rsf'
00076 Coefs = os.tempnam(TMPDIR,'Coefs.')+'.rsf'
00077 if VERBOSE: print 'Temporary files:',x,xTmp,Coefs
00078
00079
00080 MathOper = [RSFbin+'sfmath x=%s output="x-input"' % (Coefs),
00081 RSFbin+'sfmath x=%s output="input+x"' % (x)]
00082 pipeAFM = AdjointOper+ForwardOper+MathOper
00083 pipeF = ForwardOper
00084 pipeStmpl = [RSFbin+'sfsoftth thr=LAMBDA']
00085 pipeA = AdjointOper
00086 if VERBOSE:
00087 print 'Forward Operator:',pipeF
00088 print 'Adjoint Operator:',pipeA
00089 print 'Adjoint+Forward+Math:',pipeAFM
00090 print 'Soft Thresholding:',pipeStmpl
00091 print
00092
00093
00094 err = 0
00095
00096
00097 if VERBOSE:
00098 print 'Initial guess:'
00099 print '<',DataIn,pipeF,'>',Coefs
00100 err += MultiPipe.FileInOut(DataIn,pipeF,Coefs)
00101 pipeS = []
00102 for line in pipeStmpl:
00103 dummy = line.replace('LAMBDA',str(lambdaMax))
00104 pipeS.append(dummy)
00105 if VERBOSE: print '<',Coefs,pipeS,'>',x
00106 err += MultiPipe.FileInOut(Coefs,pipeS,x)
00107
00108
00109 if VERBOSE: print 'Executing loops:'
00110 for i in Steppers.OneDimStepN(lambdaMax,lambdaMin,lambdaN):
00111 for j in Steppers.OneDimStep1(1,InnerN):
00112 if VERBOSE:
00113 print ' lambda=',i,'j=',j
00114 print '<',x,pipeAFM,'>',xTmp
00115 err += MultiPipe.FileInOut(x,pipeAFM,xTmp)
00116 pipeS = []
00117 for line in pipeStmpl:
00118 dummy = line.replace('LAMBDA',str(i))
00119 pipeS.append(dummy)
00120 if VERBOSE: print '<',xTmp,pipeS,'>',x
00121 err += MultiPipe.FileInOut(xTmp,pipeS,x)
00122
00123 if VERBOSE:
00124 print 'Final results:'
00125 print '<',x,pipeA,'>',DataOut
00126 err += MultiPipe.FileInOut(x,pipeA,DataOut)
00127
00128
00129 try: os.remove(xTmp)
00130 except: pass
00131 try: os.remove(xTmp+'@')
00132 except: pass
00133 try: os.remove(Coefs)
00134 except: pass
00135 try: os.remove(Coefs+'@')
00136 except: pass
00137 try: os.remove(x)
00138 except: pass
00139 try: os.remove(x+'@')
00140 except: pass
00141 if VERBOSE: print
00142 if VERBOSE: print '%%%%%% out off GenThreshLandweber with error code=',err
00143
00144 return err