164
MADAGASCAR DOCUMENTATION Maurice Aye-Aye, Sergey Fomel, Gilles Hennenfent, and Paul Sava http://ahay.org/

MADAGASCAR DOCUMENTATIONahay.org/RSF/book/rsf/book.pdf · Madagascar Documentation Tutorial 3 Fetch, Flow, Plot, and Result are de ned in Madagascar’s rsf.proj package, which extends

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

  • MADAGASCAR DOCUMENTATION

    Maurice Aye-Aye, Sergey Fomel, Gilles Hennenfent, and Paul Sava

    http://ahay.org/

    http://ahay.org/

  • Copyright c© 2011-12

    by Madagascar Community

  • i

    RSF — TABLE OF CONTENTS

    Maurice the Aye-Aye, Madagascar tutorial: Field data processing . . . . . . . 1

    Paul Sava, Seismic Imaging Tutorial: “exploding reflector” modeling/mi-gration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

    Maurice the Aye-Aye, Madagascar tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

    Sergey Fomel, Guide to Madagascar programs . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

    Sergey Fomel, Guide to RSF format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

    Sergey Fomel, Revisiting SEP tour with Madagascar and SCons . . . . . . . . . 103

    Sergey Fomel, Guide to RSF API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

    Paul Sava, Guide to programming using RSF . . . . . . . . . . . . . . . . . . . . . . . . . . . 135

    Sergey Fomel and Gilles Hennenfent, Reproducible computational experi-ments using SCons. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

  • ii

  • Madagascar Documentation, RSF, July 19, 2012

    Madagascar tutorial: Field data processing

    Maurice the Aye-Aye

    ABSTRACT

    In this tutorial, you will learn about multiple attenuation using parabolic Radontransform (Hampson, 1986). You will first go through an example that explainsthe process step by step. You will be asked to change some parameters and addmissing few lines. In the next part of the tutorial, you will be asked to apply thesame workflow to another CMP gather. The CMP gathers used in the tutorialare from the Canterbury data set (Lu et al., 2003). By the end of this tutorial,you should have learned to:

    1. apply NMO and inverse NMO for a CMP gather,

    2. apply forward and inverse parabloic Radon transform,

    3. design a mute function that preserves multiples in the Radon domain,

    4. subtract multiples from the data,

    5. create a semblance scan for a CMP gather.

    PREREQUISITES

    Completing this tutorial requires

    • Madagascar software environment available fromhttp://www.ahay.org

    • LATEX environment with SEGTeX available fromhttp://www.ahay.org/wiki/SEGTeX

    To do the assignment on your personal computer, you need to install the requiredenvironments. An Internet connection is required for access to the data repository.

    The tutorial itself is available from the Madagascar repository by running

    svn co https://rsf.svn.sourceforge.net/svnroot/rsf/trunk/book/rsf/school2012

    1

    http://www.ahay.orghttp://www.ahay.org/wiki/SEGTeX

  • 2 Maurice Madagascar Documentation

    INTRODUCTION

    In this tutorial, you will be asked to run commands from the Unix shell (identifiedby bash$) and to edit files in a text editor. Different editors are available in a typicalUnix environment (vi, emacs, nedit, etc.)

    Your first assignment:

    1. Open a Unix shell.

    2. Change directory to the tutorial directory

    bash$ cd $RSFSRC/book/rsf/school2012

    3. Open the tutorial.tex file in your favorite editor, for example by running

    bash$ nedit tutorial.tex &

    4. Look at the first line in the file and change the author name from Maurice theAye-Aye to your name (first things first).

    DEMO

    Part One

    1. Change directory to the demo directory

    bash$ cd demo

    2. Run

    bash$ scons cmp.view

    in the Unix shell. A number of commands will appear in the shell followed byFigure 3(a) appearing on your screen.

    3. To understand the commands, examine the script that generated them by open-ing the SConstruct file in a text editor. Notice that, instead of Shell commands,the script contains rules.

    • The first rule, Fetch, allows the script to download the input data filecmp1.rsf from the data server.

    • Other rules have the form Flow(target,source,command) for generatingdata files or Plot and Result for generating picture files.

  • Madagascar Documentation Tutorial 3

    • Fetch, Flow, Plot, and Result are defined in Madagascar’s rsf.projpackage, which extends the functionality of SCons .

    4. To better understand how rules translate into commands, run

    bash$ scons -c cmp.rsf

    The -c flag tells scons to remove the cmp.rsf file and all its dependencies.

    5. Next, run

    bash$ scons -n cmp.rsf

    The -n flag tells scons not to run the command but simply to display it on thescreen. Identify the lines in the SConstruct file that generate the output yousee on the screen.

    6. Run

    bash$ scons cmp.rsf

    Examine the file cmp.rsf both by opening it in a text editor and by running

    bash$ sfin cmp.rsf

    Part Two

    Figure 3(a) shows a CMP gather from Canterbury data set Line 12. The multipleenergy appears at time around 2.25 s. Figure 6 shows the same gather after applyingNMO correction with veloctiy equals to 1500 m/s. The multiple events starting ataround 2.25 s and below are flatened while primary events , e.g at 2 s, are overcorrected. The difference in move-out between the primaries and multiples , hence,can be used in Radon domain to attenuate multiple energy. Figure 2(a) is generated byforward parabolic Radon transform while Figure 1(d) is generated by inverse parabloicRadon transform. The purpose was to make sure that forward and inverse transformsdo not cause any data loss.

    Figure 2(a) shows the Radon transform of the CMP gather in Figure 3(a) whileFigure 2(b) shows in the Radon domain the multiple energy only after mutting theprimary energy. The protected multiples can be taken back to the time-offset domainand are subtracted from the data.

    CMP gather before multiple attenuation is shown in Figure 3(a) and the core-sponding semblance scan is shown in Figure 3(c). The CMP gather after multipleattenuation is shown in Figure 3(b) and the coresponding semblance scan is shown inFigure 3(d). The semblance scans show how multiple energy is reduced for the CMPgather after multiple attenuation.

    http://www.scons.org

  • 4 Maurice Madagascar Documentation

    Figure 1: CMP gather from Canterbury dataset before applying NMO (a), after ap-plying NMO (b), after Forward parabolic Radon transfrom (c), after applying inverseparabolic Radon transform (d). The forward and inverse parabolic Radon transformsare applied in sequence to examine the parameters of the process and to ensure thatno events are lost during the process school2012/demo cmp,nmo,taup,nmo2

  • Madagascar Documentation Tutorial 5

    Figure 2: Forward Radon transform of the gather (a). Mute is applied to preservemultiples (b); so that multiples can be transformed to time-offset domain for subtrac-

    tion from the CMP gather. school2012/demo taup,taupmult

    1. To examine the forward and inverse Radon transform, Run

    bash$ scons taup_qc.view

    2. Edit the SConstruct file to modify the reference offset x0 for sfradon program.To get more details about sfradon parameters, run

    bash$ sfradon

    in a Unix shell. Check your result by running

    scons taup_qc.view

    3. Edit the SConstruct file to modify the starting time t0 for sfmutter. To getmore details about sfmutter parameters, run sfmutter in a Unix shell. Checkyour result by running

    scons taup_mult.view

    4. Edit the SConstruct file to modify the starting time v0 for sfmutter. Checkyour result by running

  • 6 Maurice Madagascar Documentation

    Figure 3: CMP gather before multiple attenuation (a). CMP gather af-ter multiple attenuation (b). Gather in (a) is used to generated semblancescan in (c). Gather in (b) is used to generate semblance scan in (d).

    school2012/demo cmp,signal2,vscan-cmp,vscan-signal2

  • Madagascar Documentation Tutorial 7

    scons taup_mult.view

    5. Edit the SConstruct file and find the line that says ADD CODE to createsignal2.vpl. To get more details about sfgrey parameters, run sfgrey in aUnix shell. Add your code and create the vpl file by running

    scons signal2.vpl

    6. Edit the SConstruct file and find the line that says ADD CODE to displaycmp.vpl and signal2.vpl. Add your code and view the file by running

    scons cmp_signal2.view

    7. Edit the SConstruct file and find the line that says ADD CODE to displayvscan-cmp.vpl and vscan-signal2.vpl. Add your code and view the file byrunning

    scons v_cmp_signal2.view

    1 from r s f . p ro j import ∗2

    3 # download cmp1 . r s f from the s e r v e r4 Fetch ( ’ cmp1 . r s f ’ , ’ cant12 ’ )5

    6 # conver t to na t i v e format7 Flow ( ’cmp ’ , ’ cmp1 ’ , ’ dd form=nat ive ’ )8

    9 # crea t e cmp . vp l f i l e10 Plot ( ’cmp ’ , ’ grey t i t l e=CMP ’ )11

    12 # water v e l o c i t y 1500 m/s13 wvel=150014

    15 # NMO with water v e l o c i t y16 Flow ( ’nmo ’ , ’cmp ’ , ’ nmostretch h a l f=n v0=%g ’%wvel )17

    18 # crea t e nmo. vp l19 Plot ( ’nmo ’ , ’ grey t i t l e=NMO’ )20

    21 # crea t e cmp nmo . vp l f i l e under Fig d i r e c t o r y22 # cmp . vp l and nmo. vp l c r ea t ed e a r l i e r us ing P lo t23 # command w i l l be p l o t e d s i d e by s i d e24 Result ( ’ cmp nmo ’ , ’cmp nmo ’ , ’ SideBySideAniso ’ )25

    26 ####################

  • 8 Maurice Madagascar Documentation

    27 # radon parameters28 ####################29 ox=29.2530 nx=6031 dx=2532 #−−−−−−−−−−−−−−−−−−−−−33 x0=800 # CHANGE ME34 #−−−−−−−−−−−−−−−−−−−−−35 p0=−.0536 dp=.000537 np=20138

    39 # forward Radon opera tor40 radono=’ ’ ’41 radon np=%d p0=%f dp=%f x0=%d parab=y42 ’ ’ ’ %(np , p0 , dp , x0 )43

    44 # inve r s e Radon opera tor45 radonoinv=’ ’ ’46 radon adj=n nx=%d ox=%g dx=%d x0=%d parab=y47 ’ ’ ’ %(nx , ox , dx , x0 )48

    49 # Test radon parameters , app ly forward and50 # inve r s e Radon Transform , and QC r e s u l t s51 #########################################52 Flow ( ’ taup ’ , ’nmo ’ , radono )53

    54 # p l o t55 Plot ( ’ taup ’ , ’ grey t i t l e=forward RT’ )56

    57 # Inver se58 Flow ( ’nmo2 ’ , ’ taup ’ , radonoinv )59

    60 # p l o t61 Plot ( ’nmo2 ’ , ’ grey t i t l e=i n v e r s e RT’ )62

    63 # Disp lay t h r e e f i g u r e s to QC Radon parameters64 # Check t ha t forward and inv e r s e Radon trans forms65 # do not change the data i . e even t s are pre served .66

    67 Result ( ’ taup qc ’ , ’nmo taup nmo2 ’ , ’ SideBySideAniso ’ )68

    69 ######################################70 # des ign a mute func t i on t ha t p r o t e c t s71 # mu l t i p l e s in the Radon domain

  • Madagascar Documentation Tutorial 9

    72 ######################################73 #−−−−−−−−−−−−−−−−−−−−−−−−−−−−74 t0 =1.2 # CHANGE ME ; t r y 1 .575 #−−−−−−−−−−−−−−−−−−−−−−−−−−−−76 # v e r t i c a l p o s i t i o n o f the t r i a n g l e v e r t i x77

    78 #−−−−−−−−−−−−−−−−−−−−−−−−−−−−−79 v0=.03 # CHANGE ME ; t r y .01580 #−−−−−−−−−−−−−−−−−−−−−−−−−−−−−81 # s l ope o f the t r i a n g l e82

    83 Flow ( ’ taupmult ’ , ’ taup ’ , ’ mutter t0=%g v0=%g ’%(t0 , v0 ) )84 Plot ( ’ taupmult ’ , ’ grey t i t l e =”mu l t i p l e s in Radon domain” ’ )85

    86 # Disp lay taup . v p l and taupmult . v p l87 # This d i s p l a y a l l ow s a f l i p between88 # the two f i g u r e s89 Result ( ’ taup mult ’ , ’ taup taupmult ’ ,90 ’ ’ ’91 cat a x i s=3 ${SOURCES[ 1 ] }92 | grey93 ’ ’ ’ )94

    95 # Transform mu l i t p l e s from Radon domain to time−o f f s e t domain96 Flow ( ’ mu l t ip l e ’ , ’ taupmult ’ , radonoinv )97

    98 # crea t e mu l t i p l e . v p l99 Plot ( ’ mu l t ip l e ’ , ’ grey t i t l e =”mu l t i p l e s ” ’ )

    100

    101 # p l o t CMP and mu l t i p l e s s i d e by s i d e102 Result ( ’ cmp mult ’ , ’nmo2 mul t ip l e ’ , ’ SideBySideAniso ’ )103

    104 # Sub t rac t mu l t i p l e s from the CMP105 Flow ( ’ s i g n a l ’ , ’ mu l t ip l e nmo2 ’ ,106 ’ ’ ’107 add s c a l e =−1,1 ${SOURCES[ 1 ] }108 ’ ’ ’ )109

    110 # inve r s e NMO111 Flow ( ’ s i g n a l 2 ’ , ’ s i g n a l ’ ,112 ’ ’ ’113 nmostretch inv=y h a l f=n v0=%g114 | mutter v0=1900 x0=200115 ’ ’ ’%wvel )116

  • 10 Maurice Madagascar Documentation

    117 #−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−118 # ADD CODE to c rea t e s i g na l 2 . v p l119 #−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−120

    121

    122 #−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−123 # ADD CODE to d i s p l a y cmp . vp l and s i g na l 2 . vp l ,124 # make the f i g u r e s f l i p back and f o r t h so you125 # can examine the the r e s u l t s o f mu l t i p l e126 # at t enua t i on . Let us c a l l the output f i l e127 # cmp signa l2128 #−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−129

    130

    131 ####################132 # Semblance Scan133 ####################134 dv=10135 nv=251136 v0=1400137 vscan=’ vscan v0=%d dv=%d nv=%d semblance=y h a l f=n ’%(v0 , dv , nv )138 pick=’ p ick r e c t 1 =150 r e c t 2 =50 gate=20 ’139

    140 # semblance scan141 Flow ( ’ vscan−cmp ’ , ’cmp ’ , vscan )142

    143 # semblance scan144 Flow ( ’ vscan−s i g n a l 2 ’ , ’ s i g n a l 2 ’ , vscan )145

    146 Plot ( ’ vscan−cmp ’ ,147 ’ ’ ’148 grey c o l o r=j a l l p o s=y149 t i t l e =”Ve loc i ty Scan − CMP”150 ’ ’ ’ )151

    152 Plot ( ’ vscan−s i g n a l 2 ’ ,153 ’ ’ ’154 grey c o l o r=j a l l p o s=y155 t i t l e =”Ve loc i ty Scan − a f t e r demul t ip l e ”156 ’ ’ ’ )157

    158 #−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−159 # ADD CODE to d i s p l a y the two f i g u r e s160 # vscan−cmp . vp l and vscan−s i g na l 2 . v p l161 # s id e by s i d e . Let us c a l l the output

  • Madagascar Documentation Tutorial 11

    162 # f i l e vcmp−s i g na l 2163 #−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−164

    165

    166

    167 ###################################################168 # This par t i s to c r ea t e f i g u r e s f o r t u t o r i a l . pd f169 ###################################################170 # de f i n e grey commands f o r f i g u r e s to be inc luded171 # in t u t o r i a l . pd f172 grey=’ ’ ’173 grey w a n t t i t l e=n l a b e l f a t =2 t i t l e f a t =2174 x l l =2 y l l =1.5 yur=9 xur=6175 ’ ’ ’176

    177 greyc=’ ’ ’178 grey w a n t t i t l e=n l a b e l f a t =2 t i t l e f a t =2179 x l l =2 y l l =1.5 yur=9 xur=6180 c o l o r=j a l l p o s=y181 ’ ’ ’182 # crea t e p l o t s183 Result ( ’cmp ’ , grey )184 Result ( ’nmo ’ , grey )185 Result ( ’ taup ’ , grey )186 Result ( ’nmo2 ’ , grey )187 Result ( ’ taupmult ’ , grey )188 Result ( ’ s i g n a l 2 ’ , grey )189 Result ( ’ vscan−cmp ’ , greyc )190 Result ( ’ vscan−s i g n a l 2 ’ , greyc )191

    192 End ( )

    EXERCISE

    In this part, your task is to apply the workflow explained above to a different CMPgather that requires different parameters. The same workflow should work here, butyou need to observe that the CMP gather used for this exercise has shallow events.This means that, after applying NMO correction, amplitudes at far offstes of theshallow events get stretched. Therefore, an additional step is required for this CMP.We need to mute the distorted amplitudes. The mute is already applied in theSConstruct.

    1. Display the CMP gather after NMO with and without mute applied by running

  • 12 Maurice Madagascar Documentation

    scons nmo1_nmo.view

    2. Your task is to add the necessary code to attenuate multiples for this CMP.The same work flow used in the SConstruct under demo directory should workhere with only changes to

    • x0• t0• v0

    where it says CHANGE ME in the comments

    WRITING A REPORT

    1. Change directory to the parent directory

    bash$ cd ..

    This should be the directory that contains tutorial.tex.

    2. Run

    bash$ sftour scons lock

    The sftour command visits all subdirectories and runs scons lock, whichcopies result files to a different location so that they do not get modified untilfurther notice.

    3. You can also run

    bash$ sftour scons -c

    to clean intermediate results.

    4. Edit the file paper.tex to include your additional results. If you have not usedLATEX before, no worries. It is a descriptive language. Study the file, and itshould become evident by example how to include figures.

    5. Run

    bash$ scons tutorial.pdf

    and open tutorial.pdf with a PDF viewing program such as Acrobat Reader.

    6. If you have LATEX2HTML installed, you can also generate an HTML version ofyour paper by running

    bash$ scons tutorial.html

    and opening tutorial_html/index.html in a web browser.

  • Madagascar Documentation Tutorial 13

    REFERENCES

    Hampson, D., 1986, Inverse velocity stacking for multiple elimination: J. Can. Soc.Expl. Geophys, 22, 44–55.

    Lu, H., C. S. Fulthorpe, and P. Mann, 2003, Three-dimensional architecture of shelf-building sediment drifts in the offshore canterbury basin, new zealand: MarineGeology, 193, 19 – 47.

  • 14 Maurice Madagascar Documentation

  • Madagascar Documentation, RSF, July 19, 2012

    Seismic Imaging Tutorial:

    “exploding reflector” modeling/migration

    Paul SavaCenter for Wave PhenomenaColorado School of Mines1

    ABSTRACT

    This document demonstrates how reproducible numeric experiments constructedusing the madagascar software package can be integrated into a documentgenerated using the LATEXtypesetting program. I use a simple modeling/migra-tion exercise based on the exploding reflector model to illustrate reproducibledocument generation.

    INTRODUCTION

    Acoustic modeling and migration can be implemented using numeric solutions to theacoustic wave-equation (Clærbout, 1985):

    1

    v2Ẅ − ρ∇ ·

    (1

    ρ∇W

    )= f . (1)

    In Equation 1, W (x, t) represents the acoustic wavefield, v (x) and ρ (x) representthe velocity and density of the medium, respectively, and f (x, t) represents a sourcefunction.

    • In modeling, we use the distributed source f (x, t) to generate the wavefieldW (x, t) at all positions and all times by wave propagation forward in time.The data represent a subset of the wavefield observed at receivers distributedin the medium: D (r, t) = W (x = r, t).

    • In migration, we use the observed data D (r, t) to generate the wavefield W (x, t)at all positions and all times by wave propagation backward in time. The imagerepresents a subset of the wavefield at time zero: R (x) = W (x =, t = 0).

    In both cases, we solve Equation 1 with different initial conditions, but with the samemodel, v (x) and ρ (x) and with the same boundary conditions.

    1e-mail: [email protected]

    15

  • 16 Sava Madagascar Documentation

    EXAMPLE

    I illustrate the method using the Sigsbee 2A synthetic model. This model is based onthe Sigsbee structure in the Gulf of Mexico and the velocity model is illustrated inFigure 1. The model is characterized by a massive salt body close to the water bottomand surrounded by sediments. The salt velocity is 4.5 km/s and the surroundingsediment velocity ranges from approximately 1.5 to 3.25 km/s.

    Figure 1: StratigraphicSigsbee 2A velocity modelschool/sigsbee vstr

    In this experiment, I consider sources distributed uniformly in the subsalt regionof the model. The data are acquired in a borehole array, located at x = 8.5 km anda horizontal array located at z = 1.5 km. In order to avoid multiple scattering in thesubsurface, I simulate waves in a smooth version of the Sigsbee model, illustrated inFigure 2, and constant density.

    Figure 2: Smooth Sigs-bee 2A velocity modelschool/sigsbee vsmo

    Using the madagascar program sfawefd2d, we can simulate wavefields from thedistributed sources. Figures 3(a)-3(h) show wavefield snapshots in order of increasingtimes. We can observe waves propagating from all subsalt sources, interacting withthe variable velocity medium and arriving at the vertical and horizontal arrays.

    Figures 4(a) and 4(b) show the data observed at the horizontal array in variabledensity and wiggle plotting formats, respectively. Similarly, Figures 5(a) and 5(b)show the data observed in the vertical array using the same plotting formats. Thedata are just subsets of the same wavefields at the respective receiver positions andcapture the complications observed in the wavefield, i.e. triplications due to lateralvelocity variation.

  • Madagascar Documentation WSI tutorial 17

    Figure 3: Wavefield snapshots at increasing times.school/sigsbee wfld-01,wfld-03,wfld-05,wfld-07,wfld-09,wfld-11,wfld-13,wfld-15

  • 18 Sava Madagascar Documentation

    Figure 4: Acoustic data observed in the horizontal array. school/sigsbee datH,wigH

  • Madagascar Documentation WSI tutorial 19

    Figure 5: Acoustic data observed in the vertical array. school/sigsbee datV,wigV

  • 20 Sava Madagascar Documentation

    In zero-offset migration, we use the observed data to backprogate the wavefieldsusing the data as boundary conditions. The image is the wavefield at time zero.Since we can acquire data at different locations in space, the reconstructed wavefieldsdepend on the acquisition geometry, thus limiting the illumination in the subsurface.Therefore, the migrated images depend on the acquisition array, as illustrated inFigures 6(a) and 6(b) for the horizontal and vertical arrays, respectively. We canalso obtain images by migrating the data observed in both the horizontal and verticalarrays, as illustrated in Figure 7, thus increasing the acquisition aperture and thesubsurface illumination.

    CONCLUSIONS

    The combination of LATEX and madagascar allows geoscientists to generate repro-ducible documents where the numeric examples can be verified by any user with thesame computer setup. This allows for transparent peer-review, for recursive develop-ment and for transfer of technology between collaborative research groups.

    ACKNOWLEDGMENTS

    The reproducible numeric examples in this paper use the madagascar open-sourcesoftware package freely available from http://www.reproducibility.org.

    REFERENCES

    Clærbout, J. F., 1985, Imaging the Earth’s interior: Blackwell Scientific Publications.

    http://www.reproducibility.org

  • Madagascar Documentation WSI tutorial 21

    Figure 6: Migrated images for data acquired in (a) the horizontal array and (b) the

    vertical array. school/sigsbee imgH,imgV

  • 22 Sava Madagascar Documentation

    Figure 7: Migrated image for data acquired in the horizontal and the vertical arrays.school/sigsbee imgA

  • Madagascar Documentation, RSF, July 19, 2012

    Madagascar tutorial

    Maurice the Aye-Aye1

    ABSTRACT

    In this tutorial, you will go through different steps required for writing a researchpaper with reproducible examples. In particular, you will

    1. identify a research problem,

    2. suggest a solution,

    3. test your solution using a synthetic example,

    4. apply your solution to field data,

    5. write a report about your work.

    PREREQUISITES

    Completing this tutorial requires

    • Madagascar software environment available fromhttp://www.ahay.org

    • LATEX environment with SEGTeX available fromhttp://www.ahay.org/wiki/SEGTeX

    To do the assignment on your personal computer, you need to install the requiredenvironments. An Internet connection is required for access to the data repository.

    The tutorial itself is available from the Madagascar repository by running

    svn co https://rsf.svn.sourceforge.net/svnroot/rsf/trunk/book/rsf/school2009

    INTRODUCTION

    In this tutorial, you will be asked to run commands from the Unix shell (identifiedby bash$) and to edit files in a text editor. Different editors are available in a typicalUnix environment (vi, emacs, nedit, etc.)

    1e-mail: [email protected]

    23

    http://www.ahay.orghttp://www.ahay.org/wiki/SEGTeX

  • 24 Maurice Madagascar Documentation

    Your first assignment:

    1. Open a Unix shell.

    2. Change directory to the tutorial directory

    bash$ cd $RSFSRC/book/rsf/school2009

    3. Open the paper.tex file in your favorite editor, for example by running

    bash$ nedit paper.tex &

    4. Look at the first line in the file and change the author name from Maurice theAye-Aye to your name (first things first).

    PROBLEM

    Figure 1: Depth slice from 3-D seismic (left) and output of edge detection (right).

    school2009/channel horizon

    The left plot in Figure 1 shows a depth slice from a 3-D seismic volume2. Younotice a channel structure and decide to extract it using and edge detection algorithmfrom the image processing literature (Canny, 1986). In a nutshell, Canny’s edgedetector picks areas of high gradient that seem to be aligned along an edge. Theextracted edges are shown in the right plot of Figure 1. The initial result is not tooclear, because it is affected by random fluctuations in seismic amplitudes. The goalof your research project is to achieve a better result in automatic channel extraction.

    1. Change directory to the project directory

    2Courtesy of Matt Hall (ConocoPhillips Canada Ltd.)

  • Madagascar Documentation Tutorial 25

    bash$ cd channel

    2. Run

    bash$ scons horizon.view

    in the Unix shell. A number of commands will appear in the shell followed byFigure 1 appearing on your screen.

    3. To understand the commands, examine the script that generated them by open-ing the SConstruct file in a text editor. Notice that, instead of Shell commands,the script contains rules.

    • The first rule, Fetch, allows the script to download the input data filehorizon.asc from the data server.

    • Other rules have the form Flow(target,source,command) for generatingdata files or Plot and Result for generating picture files.

    • Fetch, Flow, Plot, and Result are defined in Madagascar’s rsf.projpackage, which extends the functionality of SCons (Fomel and Hennenfent,2007).

    4. To better understand how rules translate into commands, run

    bash$ scons -c horizon.rsf

    The -c flag tells scons to remove the horizon.rsf file and all its dependencies.

    5. Next, run

    bash$ scons -n horizon.rsf

    The -n flag tells scons not to run the command but simply to display it on thescreen. Identify the lines in the SConstruct file that generate the output yousee on the screen.

    6. Run

    bash$ scons horizon.rsf

    Examine the file horizon.rsf both by opening it in a text editor and by running

    bash$ sfin horizon.rsf

    How many different Madagascar modules were used to create this file? Whatare the file dimensions? Where is the actual data stored?

    http://www.scons.org

  • 26 Maurice Madagascar Documentation

    7. Run

    bash$ scons smoothed.rsf

    Notice that the horizon.rsf file is not being rebuilt.

    8. What does the sfsmooth module do? Find it out by running

    bash$ sfsmooth

    without arguments. Has sfsmooth been used in any other Madagascar ex-amples?

    9. What other Madagascar modules perform smoothing? To find out, run

    bash$ sfdoc -k smooth

    10. Notice that Figure 1 does not make a very good use of the color scale. Toimprove the scale, find the mean value of the data by running

    bash$ sfattr < horizon.rsf

    and insert it as a new value for the bias= parameter in the SConstruct file.Does smoothing by sfsmooth change the mean value?

    11. Save the SConstruct file and run

    bash$ scons view

    to view improved images. Notice that horizon.rsf and smoothed.rsf files arenot being rebuilt. SCons is smart enough to know that only the part affectedby your changes needs to be updated.

    As shown in Figure 2, smoothing removes random amplitude fluctuations but atthe same broadens the channel and thus makes the channel edge detection unreliable.In the next part of this tutorial, you will try to find a better solution by examininga simple one-dimensional synthetic example.

    1 from r s f . p ro j import ∗2

    3 # Download data4 Fetch ( ’ hor i zon . asc ’ , ’ h a l l ’ )5

    6 # Convert format7 Flow ( ’ hor i zon ’ , ’ hor i zon . asc ’ ,8 ’ ’ ’

  • Madagascar Documentation Tutorial 27

    Figure 2: Depth slice from Figure 1 after smoothing (left) and output of edge detection

    (right). school2009/channel smoothed

    9 echo in=$SOURCE data format=a s c i i f l o a t n1=3 n2=57036 |10 dd form=nat ive | window n1=1 f1=−1 |11 put12 n1=196 o1 =33.139 d1=0.01 l a b e l 1=y unit1=km13 n2=291 o2 =35.031 d2=0.01 l a b e l 2=x unit2=km14 ’ ’ ’ )15

    16 # Triang le smoothing17 Flow ( ’ smoothed ’ , ’ hor i zon ’ , ’ smooth r e c t 1 =20 r e c t 2 =20 ’ )18

    19 # Disp lay r e s u l t s20 for hor i zon in ( ’ hor i zon ’ , ’ smoothed ’ ) :21 # −−− CHANGE BELOW −−−22 Plot ( hor izon , ’ grey c o l o r=j b i a s=0 yr eve r s e=n w a n t t i t l e=n ’ )23 edge = ’ edge− ’+hor izon24 Flow ( edge , hor izon , ’ canny max=98 | dd type=f l o a t ’ )25 Plot ( edge , ’ grey a l l p o s=y yr eve r s e=n w a n t t i t l e=n ’ )26 Result ( hor izon , [ hor izon , edge ] , ’ S ideBySideIso ’ )27

    28 End ( )

    1-D SYNTHETIC

    To better understand the effect of smoothing, you decide to create a one-dimensionalsynthetic example shown in Figure 3(a). The synthetic contains both sharp edges andrandom noise. The output of conventional triangle smoothing is shown in Figure 3(b).We see an effect similar to the one in the real data example: random noise gets

  • 28 Maurice Madagascar Documentation

    Figure 3: (a) 1-D synthetic to test edge-preserving smoothing. (b) Output of conven-

    tional triangle smoothing. school2009/local step,smooth

    removed by smoothing at the expense of blurring the edges. Can you do better?

    Figure 4: (a) Input synthetic trace duplicated multiple times. (b) Duplicated tracesshifted so that each data sample gets surrounded by its neighbors. The original traceis in the middle. school2009/local spray,local

    To better understand what is happening in the process of smoothing, let us convert1-D signal into a 2-D signal by first replicating the trace several times and then shiftingthe replicated traces with respect to the original trace (Figure 4). This creates a 2-D dataset, where each sample on the original trace is surrounded by samples fromneighboring traces.

    Every local filtering operation can be understood as stacking traces from Fig-ure 4(b) multiplied by weights that correspond to the filter coefficients.

    1. Change directory to the project directory

    bash$ cd ../local

  • Madagascar Documentation Tutorial 29

    2. Verify the claim above by running

    bash$ scons smooth.view smooth2.view

    Open the SConstruct file in a text editor to verify that the first image iscomputed by sfsmooth and the second image is computed by applying triangleweights and stacking. To compare the two images by flipping between them,run

    bash$ sfpen Fig/smooth.vpl Fig/smooth2.vpl

    3. Edit SConstruct to change the weight from triangle

    WT (x) = 1−|x|x0

    (1)

    to Gaussian

    WG(x) = exp

    (−α |x|

    2

    x20

    )(2)

    Repeat the previous computation. Does the result change? What is a goodvalue for α?

    4. Thinking about this problem, you invent an idea3. Why not apply non-linearfilter weights that would discriminate between points not only based on theirdistance from the center point but also on the difference in function valuesbetween the points. That is, instead of filtering by

    g(x) =

    ∫f(y)W (x− y) dy , (3)

    where f(x) is input, g(y) is output, and W (x) is a linear weight, you decide tofilter by

    g(x) =

    ∫f(y) Ŵ (x− y, f(x)− f(y)) dy , (4)

    where and Ŵ (x, z) is a non-linear weight. Compare the two weights by running

    bash$ scons triangle.view similarity.view

    The results should look similar to Figure 5.

    5. The final output is Figure 6. By examining SConstruct, find how to reproducethis figure.

    6. EXTRA CREDIT If you are familiar with programming in C, add 1-D non-local filtering as a new Madagascar module sfnonloc. Ask the instructorfor further instructions.

  • 30 Maurice Madagascar Documentation

    Figure 5: (a) Linear and stationary triangle weights. (b) Non-linear and non-stationary weights reflecting both distance between data points and similarity indata values. school2009/local triangle,similarity

    Figure 6: Output ofnon-local smoothingschool2009/local nlsmooth

  • Madagascar Documentation Tutorial 31

    Figure 6 shows that non-linear filtering can eliminate random noise while preserv-ing the edges. The problem is solved! Now let us apply the result to our originalproblem.

    1 /∗ Non−l o c a l smoothing . ∗/2 #include < r s f . h>3

    4 int main ( int argc , char ∗argv [ ] )5 {6 int n1 , n2 , i1 , i2 , i s , ns ;7 f loat ∗ t race , ∗ trace2 , ax , ay , t ;8 s f f i l e inp , out ;9

    10 /∗ i n i t i a l i z e ∗/11 s f i n i t ( argc , argv ) ;12

    13 /∗ s e t input and output f i l e s ∗/14 inp = s f i n p u t ( ” in ” ) ;15 out = s f o u t p u t ( ”out” ) ;16

    17 /∗ ge t input dimensions ∗/18 i f ( ! s f h i s t i n t ( inp , ”n1” ,&n1 ) )19 s f e r r o r ( ”No n1= in input ” ) ;20 n2 = s f l e f t s i z e ( inp , 1 ) ;21

    22 /∗ ge t command−l i n e parameters ∗/23 i f ( ! s f g e t i n t ( ”ns” ,&ns ) ) s f e r r o r ( ”Need ns=” ) ;24 /∗ spray rad ius ∗/25

    26 i f ( ! s f g e t f l o a t ( ”ax” ,&ax ) ) s f e r r o r ( ”Need ax=” ) ;27 /∗ e xponen t i a l we igh t f o r the coord ina te d i s t ance ∗/28

    29 t r a c e = s f f l o a t a l l o c ( n1 ) ;30 t ra ce2 = s f f l o a t a l l o c ( n1 ) ;31

    32 /∗ l oop over t r a c e s ∗/33 for ( i 2 =0; i 2 < n2 ; i 2++) {34 /∗ read input ∗/35 s f f l o a t r e a d ( trace , n1 , inp ) ;36

    37 /∗ l oop over samples ∗/38 for ( i 1 =0; i 1 < n1 ; i 1++) {39 t = 0 . ;

    3Actually, you reinvent the idea of bilateral or non-local filters (Tomasi and Manduchi, 1998;Gilboa and Osher, 2008).

  • 32 Maurice Madagascar Documentation

    40

    41 /∗ accumulate s h i f t s ∗/42 for ( i s=−ns ; i s = 0 && i 1+i s < n1 ) {44

    45 /∗ ! ! !MODIFY THE NEXT LINE ! ! ! ∗/46 t += t ra c e [ i 1+i s ]∗ expf(−ax∗ i s ∗ i s ) ;47 }48 }49

    50 t ra ce2 [ i 1 ] = t ;51 }52

    53 /∗ wr i t e output ∗/54 s f f l o a t w r i t e ( trace2 , n1 , out ) ;55 }56

    57 /∗ c l ean up ∗/58 s f f i l e c l o s e ( inp ) ;59 e x i t ( 0 ) ;60 }

    SOLUTION

    1. Change directory to the project directory

    bash$ cd ../channel2

    2. By now, you should know what to do next.

    3. Two-dimensional shifts generate a four-dimensional volume. Verify it by run-ning

    bash$ scons local.rsf

    and

    bash$ sfin local.rsf

    View a movie of different shifts by running

    bash$ scons local.vpl

    4. Modify the filter weights by editing SConstruct in a text editor. Observe yourfinal result by running

  • Madagascar Documentation Tutorial 33

    bash$ scons smoothed2.view

    5. The file norm.rsf contains the non-linear weights stacked over different shifts.Add a Result statement to SConstruct that would display the contents ofnorm.rsf in a figure. Do you notice anything interesting?

    6. Apply the Canny edge detection to your final result and display it in a figure.

    7. EXTRA CREDIT Change directory to ../mona and apply your method tothe image of Mona Lisa. Can you extract her smile?

    1 from r s f . p ro j import ∗2

    3 # Download data4 Fetch ( ’ hor i zon . asc ’ , ’ h a l l ’ )5

    6 # Convert format7 Flow ( ’ hor izon2 ’ , ’ hor i zon . asc ’ ,8 ’ ’ ’9 echo in=$SOURCE data format=a s c i i f l o a t n1=3 n2=57036 |

    10 dd form=nat ive | window n1=1 f1=−1 |11 add add=−65 | put12 n1=196 o1 =33.139 d1=0.01 l a b e l 1=y unit1=km13 n2=291 o2 =35.031 d2=0.01 l a b e l 2=x unit2=km14 ’ ’ ’ , s t d in =0)15 Result ( ’ hor izon2 ’ , ’ grey y r eve r s e=n c o l o r=j t i t l e=Input ’ )16

    17 # Spray18 Flow ( ’ spray ’ , ’ hor i zon2 ’ ,19 ’ ’ ’20 spray a x i s=3 n=21 o=−0.1 d=0.01 |21 spray a x i s=4 n=21 o=−0.1 d=0.0122 ’ ’ ’ )23

    24 # Sh i f t25 Flow ( ’ s h i f t 1 ’ , ’ spray ’ , ’ window n1=1 | math output=x2 ’ )26 Flow ( ’ s h i f t 2 ’ , ’ spray ’ , ’ window n2=1 | math output=x3 ’ )27

    28 Flow ( ’ l o c a l ’ , ’ spray s h i f t 1 s h i f t 2 ’ ,29 ’ ’ ’30 d a t s t r e t c h datum=${SOURCES[ 1 ] } | transp |31 d a t s t r e t c h datum=${SOURCES[ 2 ] } | transp32 ’ ’ ’ )33 Plot ( ’ l o c a l ’ , ’ window j3=4 j4=4 | grey c o l o r=j ’ , view=1)34

  • 34 Maurice Madagascar Documentation

    35 # −−− CHANGE BELOW −−−36 # try ”exp (−0.1∗( input−l o c )ˆ2−200∗( x3ˆ2+x4 ˆ2))”37 Flow ( ’ s i m i l ’ , ’ spray l o c a l ’ ,38 ’ ’ ’39 math l o c=${SOURCES[ 1 ] } output=140 ’ ’ ’ )41

    42 Flow ( ’norm ’ , ’ s i m i l ’ ,43 ’ s tack a x i s=4 | s tack a x i s=3 ’ )44

    45 Flow ( ’ smoothed2 ’ , ’ l o c a l s i m i l norm ’ ,46 ’ ’ ’47 add mode=p ${SOURCES[ 1 ] } |48 s tack a x i s=4 | s tack a x i s=3 |49 add mode=d ${SOURCES[ 2 ] }50 ’ ’ ’ )51 Result ( ’ smoothed2 ’ , ’ grey y r eve r s e=n c o l o r=j t i t l e=Output ’ )52

    53 End ( )

    Figure 7: Your final result.school2009/channel2 smoothed2

    1 from r s f . p ro j import ∗2

    3 # Download data4 Fetch ( ’mona . img ’ , ’ imgs ’ )5

    6 # Convert to s tandard format7 Flow ( ’mona ’ , ’mona . img ’ ,8 ’ ’ ’9 echo n1=512 n2=513 in=$SOURCE data format=nat ive uchar |

    10 dd type=f l o a t11 ’ ’ ’ , s t d in =0)12

  • Madagascar Documentation Tutorial 35

    Figure 8: Can you applyyour algorithm to Mona Lisa?school2009/mona mona

    13 Result ( ’mona ’ ,14 ’ ’ ’15 grey transp=n a l l p o s=y t i t l e =”Mona Lisa ”16 c o l o r=b s c r e e n r a t i o=1 wantaxis=n17 ’ ’ ’ )18

    19 End ( )

    WRITING A REPORT

    1. Change directory to the parent directory

    bash$ cd ..

    This should be the directory that contains paper.tex.

    2. Run

    bash$ sftour scons lock

    The sftour command visits all subdirectories and runs scons lock, whichcopies result files to a different location so that they do not get modified untilfurther notice.

    3. You can also run

    bash$ sftour scons -c

  • 36 Maurice Madagascar Documentation

    to clean intermediate results.

    4. Edit the file paper.tex to include your additional results. If you have not usedLATEX before, no worries. It is a descriptive language. Study the file, and itshould become evident by example how to include figures.

    5. Run

    bash$ scons paper.pdf

    and open paper.pdf with a PDF viewing program such as Acrobat Reader.

    6. Want to submit your paper to Geophysics? Edit SConstruct in the paperdirectory to add option=manuscript to the End statement. Then run

    bash$ scons paper.pdf

    again and display the result.

    7. If you have LATEX2HTML installed, you can also generate an HTML version ofyour paper by running

    bash$ scons html

    and opening paper_html/index.html in a web browser.

    REFERENCES

    Canny, J., 1986, A computational approach to edge detection: IEEE Trans. PatternAnalysis and Machine Intelligence, 8, 679–714.

    Fomel, S., and G. Hennenfent, 2007, Reproducible computational experiments usingSCons: 32nd International Conference on Acoustics, Speech, and Signal Processing(ICASSP), IEEE, 1257–1260.

    Gilboa, G., and S. Osher, 2008, Nonlocal operators with applications to image pro-cessing: Multiscale Model & Simulation, 7, 1005–1028.

    Tomasi, C., and R. Manduchi, 1998, Bilateral filtering for gray and color images:Proceedings of IEEE International Conference on Computer Vision, IEEE, 836–846.

  • Madagascar Documentation, RSF, July 19, 2012

    Guide to Madagascar programs

    Sergey Fomel1

    ABSTRACT

    This guide introduces some of the most used madagascar programs and illustratestheir usage with examples.

    MAIN PROGRAMS

    The source files for these programs can be found under system/main in the Mada-gascar distribution.

    1e-mail: [email protected]

    37

    http://rsf.svn.sourceforge.net/viewvc/rsf/trunk/system/main/

  • 38 Fomel Madagascar Documentation

    sfadd: Add, multiply, or divide RSF datasets.

    sfadd > out.rsf scale= add= sqrt= abs= log= exp= mode= [< file0.rsf] file1.rsf

    file2.rsf ...

    The various operations, if selected, occur in the following order:

    (1) Take absolute value, abs=

    (2) Add a scalar, add=

    (3) Take the natural logarithm, log=

    (4) Take the square root, sqrt=

    (5) Multiply by a scalar, scale=

    (6) Compute the base-e exponential, exp=

    (7) Add, multiply, or divide the data sets, mode=

    sfadd operates on integer, float, or complex data, but all the input

    and output files must be of the same data type.

    An alternative to sfadd is sfmath, which is more versatile, but may be

    less efficient.

    bools abs= If true take absolute value [nin]floats add= Scalar values to add to each dataset [nin]bools exp= If true compute exponential [nin]bools log= If true take logarithm [nin]string mode= ’a’ means add (default), ’p’ or ’m’ means

    multiply, ’d’ means divide

    floats scale= Scalar values to multiply each datasetwith [nin]

    bools sqrt= If true take square root [nin]

    sfadd is useful for combining (adding, dividing, or multiplying) several datasets.What if you want to subtract two datasets? Easy. Use the scale parameter asfollows:

    bash$ sfadd data1.rsf data2.rsf scale=1,-1 > diff.rsf

    or

    bash$ sfadd < data1.rsf data2.rsf scale=1,-1 > diff.rsf

    The same task can be accomplished with the more general sfmath program:

    bash$ sfmath one=data1.rsf two=data2.rsf output=’one-two’ > diff.rsf

    or

  • Madagascar Documentation Madagascar programs 39

    bash$ sfmath < data1.rsf two=data2.rsf output=’input-two’ > diff.rsf

    In both cases, the size and shape of data1.rsf and data2.rsf hypercubes should bethe same, and a warning message is printed out if the the axis sampling parameters(such as o1 or d1) in these files are different.

    Implementation: system/main/add.c

    The first input file is either in the list or in the standard input.

    system/main/add.c

    103 /∗ f i n d number o f input f i l e s ∗/104 i f ( i s a t t y ( f i l e n o ( s td in ) ) ) {105 /∗ no input f i l e in s td in ∗/106 nin =0;107 } else {108 in [ 0 ] = s f i n p u t ( ” in ” ) ;109 nin =1;110 }

    Collect input files in the in array from all command-line parameters that don’tcontain an “=” sign. The total number of input files is nin.

    system/main/add.c

    112 for ( i =1; i< argc ; i++) { /∗ c o l l e c t inputs ∗/113 i f (NULL != s t r c h r ( argv [ i ] , ’= ’ ) ) continue ;114 in [ nin ] = s f i n p u t ( argv [ i ] ) ;115 nin++;116 }117 i f (0==nin ) s f e r r o r ( ”no input ” ) ;118 /∗ nin = no o f input f i l e s ∗/

    A helper function check compat checks the compatibility of input files.

    Finally, we enter the main loop, where the input data are getting read buffer bybuffer and combined in the total product depending on the data type.

    The data combination program for floating point numbers is add float.

    http://rsf.svn.sourceforge.net/viewvc/rsf/trunk/system/main/add.c?view=markup

  • 40 Fomel Madagascar Documentation

    system/main/add.c

    424 check compat ( s f d a t a t y p e type /∗ data type ∗/ ,425 s i z e t nin /∗ number o f f i l e s ∗/ ,426 s f f i l e ∗ in /∗ input f i l e s [ nin ] ∗/ ,427 i n t dim /∗ f i l e d imens i ona l i t y ∗/ ,428 const o f f t ∗ n /∗ dimensions [ dim ] ∗/)429 /∗ Check that the input f i l e s are compatible .430 I s s u e e r r o r for type mismatch or s i z e mismatch .431 I s s u e warning for g r id parameters mismatch . ∗/432 {433 i n t ni , id ;434 s i z e t i ;435 f l o a t d , di , o , o i ;436 char key [ 3 ] ;437 const f l o a t t o l =1.e−5; /∗ t o l e r a n c e for comparison ∗/438

    439 for ( i =1; i < nin ; i++) {440 i f ( s f g e t t y p e ( in [ i ] ) != type )441 s f e r r o r ( ” type mismatch : need %d” , type ) ;442 for ( id =1; id t o l ∗ f a b s f (d ) ) )456 s f warn ing ( ”%s mismatch : need %g” , key , d ) ;457 } else {458 d = 1 . ;459 }460 ( void ) s n p r i n t f ( key , 3 , ”o%d” , id ) ;461 i f ( s f h i s t f l o a t ( in [ 0 ] , key ,&o ) &&462 ( ! s f h i s t f l o a t ( in [ i ] , key ,& o i ) | |463 ( f a b s f ( oi−o ) > t o l ∗ f a b s f (d ) ) ) )464 s f warn ing ( ”%s mismatch : need %g” , key , o ) ;465 }466 }467 }

  • Madagascar Documentation Madagascar programs 41

    system/main/add.c

    183 for ( nbuf /= s f e s i z e ( in [ 0 ] ) ; n s i z > 0 ; n s i z −= nbuf ) {184 i f ( nbuf > n s i z ) nbuf=n s i z ;185

    186 for ( j =0; j < nin ; j++) {187 c o l l e c t = ( bool ) ( j != 0 ) ;188 switch ( type ) {189 case SF FLOAT:190 s f f l o a t r e a d ( ( f l o a t ∗) buf i ,191 nbuf ,192 in [ j ] ) ;193 a d d f l o a t ( c o l l e c t ,194 nbuf ,195 ( f l o a t ∗) buf ,196 ( const f l o a t ∗) buf i ,197 cmode ,198 s c a l e [ j ] ,199 add [ j ] ,200 a b s f l a g [ j ] ,201 l o g f l a g [ j ] ,202 s q r t f l a g [ j ] ,203 e x p f l a g [ j ] ) ;

  • 42 Fomel Madagascar Documentation

    system/main/add.c

    264 s t a t i c void a d d f l o a t ( bool c o l l e c t , /∗ i f c o l l e c t ∗/265 s i z e t nbuf , /∗ b u f f e r s i z e ∗/266 f l o a t ∗ buf , /∗ output [ nbuf ] ∗/267 const f l o a t ∗ buf i , /∗ input [ nbuf ] ∗/268 char cmode , /∗ opera t i on ∗/269 f l o a t s ca l e , /∗ s c a l e f a c t o r ∗/270 f l o a t add , /∗ add f a c t o r ∗/271 bool a b s f l a g , /∗ i f abs ∗/272 bool l o g f l a g , /∗ i f l og ∗/273 bool s q r t f l a g , /∗ i f s q r t ∗/274 bool e x p f l a g /∗ i f exp ∗/)275 /∗ Add f l o a t i n g po int numbers ∗/276 {277 s i z e t j ;278 f l o a t f ;279

    280 for ( j =0; j < nbuf ; j++) {281 f = b u f i [ j ] ;282 i f ( a b s f l a g ) f = f a b s f ( f ) ;283 f += add ;284 i f ( l o g f l a g ) f = l o g f ( f ) ;285 i f ( s q r t f l a g ) f = s q r t f ( f ) ;286 i f ( 1 . != s c a l e ) f ∗= s c a l e ;287 i f ( e x p f l a g ) f = expf ( f ) ;288 i f ( c o l l e c t ) {289 switch ( cmode ) {290 case ’p ’ : /∗ product ∗/291 case ’m’ : /∗ mult ip ly ∗/292 buf [ j ] ∗= f ;293 break ;294 case ’d ’ : /∗ d e l e t e ∗/295 i f ( f != 0 . ) buf [ j ] /= f ;296 break ;297 d e f a u l t : /∗ add ∗/298 buf [ j ] += f ;299 break ;300 }301 } else {302 buf [ j ] = f ;303 }304 }305 }

  • Madagascar Documentation Madagascar programs 43

    sfattr: Display dataset attributes.

    sfattr < in.rsf lval=2 want=

    Sample output from "sfspike n1=100 | sfbandpass fhi=60 | sfattr"

    *******************************************

    rms = 0.992354

    mean = 0.987576

    2-norm = 9.92354

    variance = 0.00955481

    std dev = 0.0977487

    max = 1.12735 at 97

    min = 0.151392 at 100

    nonzero samples = 100

    total samples = 100

    *******************************************

    rms = sqrt[ sum(data^2) / n ]

    mean = sum(data) / n

    norm = sum(abs(data)^lval)^(1/lval)

    variance = [ sum(data^2) - n*mean^2 ] / [ n-1 ]

    standard deviation = sqrt [ variance ]

    int lval=2 norm option, lval is a non-negative inte-ger, computes the vector lval-norm

    string want= ’all’(default), ’rms’, ’mean’, ’norm’, ’var’,’std’, ’max’, ’min’, ’nonzero’, ’sam-ples’, ’short’ want= ’rms’ displays theroot mean square want= ’norm’ displaysthe square norm, otherwise specified bylval. want= ’var’ displays the variancewant= ’std’ displays the standard devi-ation want= ’nonzero’ displays number ofnonzero samples want= ’samples’ displaystotal number of samples want= ’short’displays a short one-line version

    sfattr is a useful diagnostic program. It reports certain statistical values for anRSF dataset: RMS (root-mean-square) amplitude, mean value, norm value, variance,standard deviation, maximum and minimum values, number of nonzero samples, andthe total number of samples.

    If we denote data values as di for i = 0, 1, 2, . . . , n, then the RMS value is√1n

    n∑i=0

    d2i , the mean value is1n

    n∑i=0

    di, the L2-norm value is

    √n∑

    i=0

    d2i , the variance

    is 1n−1

    [n∑

    i=0

    d2i − 1n

    (n∑

    i=0

    di

    )2], and the standard deviation is the square root of the

    variance. Using sfattr is a quick way to see the distribution of data values andcheck it for anomalies.

  • 44 Fomel Madagascar Documentation

    Implementation: system/main/attr.c

    Computations start by finding the input data (in) size (nsiz) and dimensions (dim).

    system/main/attr.c

    81 dim = ( s i z e t ) s f l a r g e f i l e d i m s ( in , n ) ;82 for ( n s i z =1, i =0; i < dim ; i++) {83 n s i z ∗= n [ i ] ;84 }

    In the main loop, we read the input data buffer by buffer.

    system/main/attr.c

    100 for ( n l e f t=n s i z ; n l e f t > 0 ; n l e f t −= nbuf ) {101 nbuf = ( b u f s i z < n l e f t )? b u f s i z : n l e f t ;102 switch ( type ) {103 case SF FLOAT:104 s f f l o a t r e a d ( ( f l o a t ∗) buf , nbuf , in ) ;105 break ;106 case SF INT :107 s f i n t r e a d ( ( i n t ∗) buf , nbuf , in ) ;108 break ;109 case SF SHORT:110 s f s h o r t r e a d ( ( shor t ∗) buf , nbuf , in ) ;111 break ;112 case SF COMPLEX:113 s f complexread ( ( s f complex ∗) buf , nbuf , in ) ;114 break ;115 case SF UCHAR:116 s f u cha r r ead ( ( unsigned char ∗) buf , nbuf , in ) ;117 break ;118 case SF CHAR:119 d e f a u l t :120 s f c h a r r e a d ( buf , nbuf , in ) ;121 break ;122 }

    The data attributes are accumulated in corresponding double-precision variables.

    Finally, the attributes are reduced and printed out.

    http://rsf.svn.sourceforge.net/viewvc/rsf/trunk/system/main/attr.c?view=markup

  • Madagascar Documentation Madagascar programs 45

    system/main/attr.c

    146 fsum += f ;147 f s q r += ( double ) f ∗ f ;

    system/main/attr.c

    180 fmean = fsum/ n s i z ;181 i f ( l v a l ==2) fnorm = s q r t ( f s q r ) ;182 else i f ( l v a l ==0) fnorm = ns iz−nzero ;183 else fnorm = pow( f l v a l , 1 . / l v a l ) ;184 frms = s q r t ( f s q r / n s i z ) ;185 i f ( n s i z > 1) f va r = fabs ( f sq r−n s i z ∗ fmean∗ fmean )/ ( ns i z −1);186 else f va r = 0 . 0 ;187 f s t d = s q r t ( f va r ) ;

    system/main/attr.c

    194 i f (NULL==want | | 0==strcmp ( want , ”rms” ) )195 p r i n t f ( ” rms = %13.6g \n” , ( f l o a t ) frms ) ;196 i f (NULL==want | | 0==strcmp ( want , ”mean” ) )197 p r i n t f ( ” mean = %13.6g \n” , ( f l o a t ) fmean ) ;198 i f (NULL==want | | 0==strcmp ( want , ”norm” ) )199 p r i n t f ( ” %d−norm = %13.6g \n” , l va l , ( f l o a t ) fnorm ) ;200 i f (NULL==want | | 0==strcmp ( want , ” var ” ) )201 p r i n t f ( ” var i ance = %13.6g \n” , ( f l o a t ) f va r ) ;202 i f (NULL==want | | 0==strcmp ( want , ” std ” ) )203 p r i n t f ( ” std dev = %13.6g \n” , ( f l o a t ) f s t d ) ;

  • 46 Fomel Madagascar Documentation

    sfcat: Concatenate datasets.

    sfcat > out.rsf order= space= axis=3 nspace=(int) (ni/(20*nin) + 1) o= d=

    [ one.rsf

    bash$ sfin one.rsf

    one.rsf:

    in="/tmp/one.rsf@"

    esize=4 type=float form=native

    n1=2 d1=0.004 o1=0 label1="Time" unit1="s"

    n2=3 d2=0.1 o2=0 label2="Distance" unit2="km"

    6 elements 24 bytes

    bash$ sfcat one.rsf one.rsf axis=1 > two.rsf

    bash$ sfin two.rsf

    two.rsf:

    in="/tmp/two.rsf@"

    esize=4 type=float form=native

    n1=4 d1=0.004 o1=0 label1="Time" unit1="s"

    n2=3 d2=0.1 o2=0 label2="Distance" unit2="km"

    12 elements 48 bytes

    Example of sfmerge:

    bash$ sfmerge one.rsf one.rsf axis=2 > two.rsf

    bash$ sfin two.rsf

    two.rsf:

    in="/tmp/two.rsf@"

  • Madagascar Documentation Madagascar programs 47

    esize=4 type=float form=native

    n1=2 d1=0.004 o1=0 label1="Time" unit1="s"

    n2=7 d2=0.1 o2=0 label2="Distance" unit2="km"

    14 elements 56 bytes

    In this case, an extra empty trace is inserted between the two merged files.

    The axes that are not being merged are checked for consistency:

    bash$ sfcat one.rsf two.rsf > three.rsf

    sfcat: n2 mismatch: need 3

    Implementation: system/main/cat.c

    The first input file is either in the list or in the standard input.

    system/main/cat.c

    64 i f ( ! s f s t d i n ( ) ) { /∗ no input f i l e in s td in ∗/65 nin =0;66 } else {67 f i l ename [ 0 ] = ” in ” ;68 nin =1;69 }

    Everything on the command line that does not contain a “=” sign is treated as afile name, and the corresponding file object is added to the list.

    system/main/cat.c

    71 for ( i =1; i< argc ; i++) { /∗ c o l l e c t inputs ∗/72 i f (NULL != s t r c h r ( argv [ i ] , ’= ’ ) )73 continue ; /∗ not a f i l e ∗/74 f i l ename [ nin ] = argv [ i ] ;75 nin++;76 }77 i f (0==nin ) s f e r r o r ( ”no input ” ) ;

    As explained above, if the space= parameter is not set, it is inferred from theprogram name: sfmerge corresponds to space=y and sfcat corresponds to space=n.

    Find the axis for the merging (from the command line axis= argument) and figureout two sizes: n1 for everything after the axis and n2 for everything before the axis.

    http://rsf.svn.sourceforge.net/viewvc/rsf/trunk/system/main/cat.c?view=markup

  • 48 Fomel Madagascar Documentation

    system/main/cat.c

    99 i f ( ! s f g e t b o o l ( ” space ” ,& space ) ) {100 /∗ I n s e r t a d d i t i o n a l space .101 y i s d e f a u l t for sfmerge , n i s d e f a u l t for s f c a t ∗/102 prog = s f g e t p r o g ( ) ;103 i f (NULL != s t r s t r ( prog , ”merge” ) ) {104 space = true ;105 } else i f (NULL != s t r s t r ( prog , ” cat ” ) ) {106 space = f a l s e ;107 } else {108 s f warn ing ( ”%s i s n e i t h e r merge nor cat , ”109 ” assume merge” , prog ) ;110 space = true ;111 }112 }

    system/main/cat.c

    132 n1=1;133 n2=1;134 for ( i =1; i a x i s ) n2 ∗= n [ i −1] ;137 }

  • Madagascar Documentation Madagascar programs 49

    In the output, the selected axis will get extended.

    system/main/cat.c

    149 /∗ f i g u r e out the l ength o f extended a x i s ∗/150 ni = 0 ;151 for ( j =0; j < nin ; j++) {152 ni += nax i s [ j ] ;153 }154

    155 i f ( space ) {156 i f ( ! s f g e t i n t ( ” nspace ” ,&nspace ) )157 nspace = ( i n t ) ( n i /(20∗ nin ) + 1 ) ;158 /∗ i f space=y , number o f t r a c e s to i n s e r t ∗/159 ni += nspace ∗( nin −1);160 }161

    162 ( void ) s n p r i n t f ( key , 3 , ”n%d” , a x i s ) ;163 s f p u t i n t ( out , key , ( i n t ) n i ) ;

    The rest is simple: loop through the datasets reading and writing the data inbuffer-size chunks and adding extra empty chunks if space=y.

    sfcmplx: Create a complex dataset from its real and imaginaryparts.

    sfcmplx < real.rsf > cmplx.rsf real.rsf imag.rsf

    There has to be only two input files specified and no additional parameters.

    sfcmplx simply creates a complex dataset from its real and imaginary parts. Thereverse operation can be accomplished with sfreal and sfimag.

    Example of sfcmplx:

    bash$ sfspike n1=2 n2=3 > one.rsf

    bash$ sfin one.rsf

    one.rsf:

    in="/tmp/one.rsf@"

    esize=4 type=float form=native

    n1=2 d1=0.004 o1=0 label1="Time" unit1="s"

    n2=3 d2=0.1 o2=0 label2="Distance" unit2="km"

    6 elements 24 bytes

  • 50 Fomel Madagascar Documentation

    system/main/cat.c

    184 for ( i 2 =0; i 2 < n2 ; i 2++) {185 for ( j =0; j < nin ; j++) {186 k = order [ j ] ;187 for ( n i = n1∗ nax i s [ k ]∗ e s i z e ; n i > 0 ; n i −= nbuf ) {188 nbuf = (BUFSIZ < ni )? BUFSIZ : n i ;189 s f c h a r r e a d ( buf , nbuf , in [ k ] ) ;190 s f c h a r w r i t e ( buf , nbuf , out ) ;191 }192 i f ( ! space | | j == nin−1) continue ;193 /∗ Add spaces ∗/194 memset ( buf , 0 , BUFSIZ ) ;195 for ( n i = n1∗nspace∗ e s i z e ; n i > 0 ; n i −= nbuf ) {196 nbuf = (BUFSIZ < ni )? BUFSIZ : n i ;197 s f c h a r w r i t e ( buf , nbuf , out ) ;198 }199 }200 }

    bash$ sfcmplx one.rsf one.rsf > cmplx.rsf

    bash$ sfin cmplx.rsf

    cmplx.rsf:

    in="/tmp/cmplx.rsf@"

    esize=8 type=complex form=native

    n1=2 d1=0.004 o1=0 label1="Time" unit1="s"

    n2=3 d2=0.1 o2=0 label2="Distance" unit2="km"

    6 elements 48 bytes

    Implementation: system/main/cmplx.c

    The program flow is simple. First, get the names of the input files.

    The main part of the program reads the real and imaginary parts buffer by bufferand assembles and writes out the complex input.

    sfconjgrad: Generic conjugate-gradient solver for linear inver-sion

    sfconjgrad < dat.rsf mod=mod.rsf > to.rsf < from.rsf > out.rsf niter=1

    file mod= auxiliary input file nameint niter=1 number of iterations

    http://rsf.svn.sourceforge.net/viewvc/rsf/trunk/system/main/cmplx.c?view=markup

  • Madagascar Documentation Madagascar programs 51

    system/main/cmplx.c

    41 /∗ the f i r s t two non−parameters are r e a l and imag f i l e s ∗/42 for ( i =1; i< argc ; i++) {43 i f (NULL == s t r c h r ( argv [ i ] , ’= ’ ) ) {44 i f (NULL == r e a l ) {45 r e a l = s f i n p u t ( argv [ i ] ) ;46 } else {47 imag = s f i n p u t ( argv [ i ] ) ;48 break ;49 }50 }51 }52 i f (NULL == imag ) {53 i f (NULL == r e a l ) s f e r r o r ( ”not enough input ” ) ;54 /∗ i f only one input , r e a l i s in s td in ∗/55 imag = r e a l ;56 r e a l = s f i n p u t ( ” in ” ) ;57 }

    system/main/cmplx.c

    81 for ( n l e f t= ( s i z e t ) ( r s i z e ∗ r e s i z e ) ;82 n l e f t > 0 ; n l e f t −= nbuf ) {83 nbuf = (BUFSIZ < n l e f t )? BUFSIZ : n l e f t ;84 s f c h a r r e a d ( rbuf , nbuf , r e a l ) ;85 s f c h a r r e a d ( ibuf , nbuf , imag ) ;86 for ( i =0; i < nbuf ; i += r e s i z e ) {87 memcpy( cbuf+2∗ i , rbuf+i , ( s i z e t ) r e s i z e ) ;88 memcpy( cbuf+2∗ i+r e s i z e , i b u f+i , ( s i z e t ) r e s i z e ) ;89 }90 s f c h a r w r i t e ( cbuf , 2∗ nbuf , cmplx ) ;91 }

  • 52 Fomel Madagascar Documentation

    sfconjgrad is a generic program for least-squares linear inversion with the conjugate-gradient method. Suppose you have an executable program that takes an RSFfile from the standard input and produces an RSF file in the standard output. It maytake any number of additional parameters but one of them must be adj= that sets theforward (adj=0) or adjoint (adj=1) operations. The program is typically anRSF program but it could be anything (a script, a multiprocessor MPI program, etc.)as long as it implements a linear operator L and its adjoint. There are no restrictionson the data size or shape. You can easily test the adjointness with sfdottest. Thesfconjgrad program searches for a vector m that minimizes the least-square misfit‖d− L m‖2 for the given input data vector d.

    Here is an example. The sfhelicon program implements Claerbout’s multidi-mensional helical filtering (Claerbout, 1998). It requires a filter to be specified inaddition to the input and output vectors. We create a helical 2-D filter using theUnix echo command.

    bash$ echo 1 19 20 n1=3 n=20,20 data_format=ascii_int in=lag.rsf > lag.rsf

    bash$ echo 1 1 1 a0=-3 n1=3 data_format=ascii_float in=flt.rsf > flt.rsf

    Next, we create an example 2-D model and data vector with sfspike.

    bash$ sfspike n1=50 n2=50 > vec.rsf

    The sfdottest program can perform the dot product test to check that the adjointmode works correctly.

    bash$ sfdottest sfhelicon filt=flt.rsf lag=lag.rsf \

    mod=vec.rsf dat=vec.rsf

    sfdottest: L[m]*d=5.28394

    sfdottest: L’[d]*m=5.28394

    Your numbers may be different because sfdottest generates new random input oneach run. Next, let us make some random data with sfnoise.

    bash$ sfnoise seed=2005 rep=y < vec.rsf > dat.rsf

    and try to invert the filtering operation using sfconjgrad:

    bash$ sfconjgrad sfhelicon filt=flt.rsf lag=lag.rsf \

    mod=vec.rsf < dat.rsf > mod.rsf niter=10

    sfconjgrad: iter 1 of 10

    sfconjgrad: grad=3253.65

    sfconjgrad: iter 2 of 10

    sfconjgrad: grad=289.421

  • Madagascar Documentation Madagascar programs 53

    sfconjgrad: iter 3 of 10

    sfconjgrad: grad=92.3481

    sfconjgrad: iter 4 of 10

    sfconjgrad: grad=36.9417

    sfconjgrad: iter 5 of 10

    sfconjgrad: grad=18.7228

    sfconjgrad: iter 6 of 10

    sfconjgrad: grad=11.1794

    sfconjgrad: iter 7 of 10

    sfconjgrad: grad=7.26941

    sfconjgrad: iter 8 of 10

    sfconjgrad: grad=5.15945

    sfconjgrad: iter 9 of 10

    sfconjgrad: grad=4.23055

    sfconjgrad: iter 10 of 10

    sfconjgrad: grad=3.57495

    The output shows that, in 10 iterations, the norm of the gradient vector decreases byalmost 1000. We can check the residual misfit before

    bash$ < dat.rsf sfattr want=norm

    norm value = 49.7801

    and after

    bash$ sfhelicon filt=flt.rsf lag=lag.rsf < mod.rsf | \

    sfadd scale=1,-1 dat.rsf | sfattr want=norm

    norm value = 5.73563

    In 10 iterations, the misfit decreased by an order of magnitude. The result can beimproved by running the program for more iterations.

    Implementation: system/main/conjgrad.c

    sfcp: Copy or move a dataset.

    sfcp < in.rsf > out.rsf in.rsf out.rsf

    sfcp - copy, sfmv - move.

    Mimics standard Unix commands.

    The sfcp and sfmv command imitate the Unix cp and mv commands and servefor copying and moving RSF files. Example:

    http://rsf.svn.sourceforge.net/viewvc/rsf/trunk/system/main/conjgrad.c?view=markup

  • 54 Fomel Madagascar Documentation

    bash$ sfspike n1=2 n2=3 > one.rsf

    bash$ sfin one.rsf

    one.rsf:

    in="/tmp/one.rsf@"

    esize=4 type=float form=native

    n1=2 d1=0.004 o1=0 label1="Time" unit1="s"

    n2=3 d2=0.1 o2=0 label2="Distance" unit2="km"

    6 elements 24 bytes

    bash$ sfcp one.rsf two.rsf

    bash$ sfin two.rsf

    two.rsf:

    in="/tmp/two.rsf@"

    esize=4 type=float form=native

    n1=2 d1=0.004 o1=0 label1="Time" unit1="s"

    n2=3 d2=0.1 o2=0 label2="Distance" unit2="km"

    6 elements 24 bytes

    Implementation: system/main/cp.c

    First, we look for the two first command-line arguments that don’t have the “=”character in them and consider them as the names of the input and the output files.

    system/main/cp.c

    47 /∗ the f i r s t two non−parameters are in and out f i l e s ∗/48 for ( i =1; i< argc ; i++) {49 i f (NULL == s t r c h r ( argv [ i ] , ’= ’ ) ) {50 i f (NULL == in ) {51 i n f i l e = argv [ i ] ;52 in = s f i n p u t ( i n f i l e ) ;53 } else {54 out = s f o u t pu t ( argv [ i ] ) ;55 break ;56 }57 }58 }

    Next, we use library functions sf_cp and sf_mv to do the actual work.

    http://rsf.svn.sourceforge.net/viewvc/rsf/trunk/system/main/cp.c?view=markup

  • Madagascar Documentation Madagascar programs 55

    system/main/cp.c

    66 s f c p ( in , out ) ;67 i f (NULL != s t r s t r ( prog , ”mv” ) )68 s f rm ( i n f i l e , f a l s e , f a l s e , f a l s e ) ;

    sfcut: Zero a portion of the dataset.

    sfcut < in.rsf > out.rsf verb=n j#=(1,...) d#=(d1,d2,...) f#=(0,...)

    min#=(o1,o2,,...) n#=(0,...) max#=(o1+(n1-1)*d1,o2+(n1-1)*d2,,...)

    Reverse of window.

    float d#=(d1,d2,...) sampling in #-th dimensionlargeint f#=(0,...) window start in #-th dimensionint j#=(1,...) jump in #-th dimensionfloat max#=(o1+(n1-

    1)*d1,o2+(n1-1)*d2,,...)

    maximum in #-th dimension

    float min#=(o1,o2,,...) minimum in #-th dimensionint n#=(0,...) window size in #-th dimensionbool verb=n [y/n] Verbosity flag

    The sfcut command is related to sfwindow and has the same set of argumentsonly instead of extracting the selected window, it fills it with zeroes. The size of theinput data is preserved.

    Examples:

    bash$ sfspike n1=5 n2=5 > in.rsf

    bash$ < in.rsf sfdisfil

    0: 1 1 1 1 1

    5: 1 1 1 1 1

    10: 1 1 1 1 1

    15: 1 1 1 1 1

    20: 1 1 1 1 1

    bash$ < in.rsf sfcut n1=2 f1=1 n2=3 f2=2 | sfdisfil

    0: 1 1 1 1 1

    5: 1 1 1 1 1

    10: 1 0 0 1 1

    15: 1 0 0 1 1

    20: 1 0 0 1 1

    bash$ < in.rsf sfcut j1=2 | sfdisfil

    0: 0 1 0 1 0

    5: 0 1 0 1 0

  • 56 Fomel Madagascar Documentation

    10: 0 1 0 1 0

    15: 0 1 0 1 0

    20: 0 1 0 1 0

    sfdd: Convert between different formats.

    sfdd < in.rsf > out.rsf trunc=n line=8 ibm=n form= type= format=

    string form= ascii, native, xdrstring format= Element format (for conversion to ASCII)bool ibm=n [y/n] Special case - assume integers actually

    represent IBM floatsint line=8 Number of numbers per line (for conver-

    sion to ASCII)bool trunc=n [y/n] Truncate or round to nearest when con-

    verting from float to int/shortstring type= int, float, complex, short

    The sfdd program is used to change either the form (ascii, xdr, native) or thetype (complex, float, int, char) of the input dataset.

    In the example below, we create a plain text (ASCII) file with numbers and thenuse sfdd to generate an RSF file in xdr form with complex numbers.

    bash$ cat test.txt

    1 2 3 4 5 6

    bash$ echo n1=6 data_format=ascii_int in=test.txt > test.rsf

    bash$ sfin test.rsf

    test.rsf:

    in="test.txt"

    esize=0 type=int form=ascii

    n1=6 d1=? o1=?

    6 elements

    bash$ sfdd < test.rsf form=xdr type=complex > test2.rsf

    bash$ sfin test2.rsf

    test2.rsf:

    in="/tmp/test2.rsf@"

    esize=8 type=complex form=xdr

    n1=3 d1=? o1=?

    3 elements 24 bytes

    bash$ sfdisfil < test2.rsf

    0: 1, 2i 3, 4i 5, 6i

    To learn more about the RSF data format, consult the guide to RSF format.

    http://www.reproducibility.org/RSF/book/rsf/rsf/format_html/

  • Madagascar Documentation Madagascar programs 57

    sfdisfil: Print out data values.

    sfdisfil < in.rsf number=y col=0 format= header= trailer=

    Alternatively, use sfdd and convert to ASCII form.

    int col=0 Number of columns. The default dependson the data type: 10 for int and char, 5for float, 3 for complex

    string format= Format for numbers (printf-style). Thedefault depends on the data type: ”””

    string header= Optional header string to output beforedata

    bool number=y [y/n] If number the elementsstring trailer= Optional trailer string to output after

    data

    The sfdisfil program simply dumps the data contents to the standard outputin a text form. It is used mostly for debugging purposes to quickly examine RSF files.Here is an example:

    bash$ sfmath o1=0 d1=2 n1=12 output=x1 > test.rsf

    bash$ < test.rsf sfdisfil

    0: 0 2 4 6 8

    5: 10 12 14 16 18

    10: 20 22

    The output format is easily configurable.

    bash$ < test.rsf sfdisfil col=6 number=n format="%5.1f"

    0.0 2.0 4.0 6.0 8.0 10.0

    12.0 14.0 16.0 18.0 20.0 22.0

    Along with sfdd, sfdisfil provides a simple way to convert RSF data to an ASCIIform.

    sfdottest: Generic dot-product test for linear operators withadjoints

    sfdottest mod=mod.rsf dat=dat.rsf > pip.rsf

    file dat= auxiliary input file namefile mod= auxiliary input file name

    sfdottest is a generic dot-product test program for testing linear operators. Sup-pose there is an executable program that takes an RSF file from the standard

  • 58 Fomel Madagascar Documentation

    input and produces an RSF file in the standard output. It may take any number ofadditional parameters but one of them must be adj= that sets the forward (adj=0)or adjoint (adj=1) operations. The program is typically an RSF programbut it could be anything (a script, a multiprocessor MPI program, etc.) as long asit implements a linear operator L and its adjoint LT . The sfdottest program istesting the equality

    dT L m = mT LT d (1)

    by using random vectors m and d. You can invoke it with

    bash$ sfdottest [optional aruments] mod=mod.rsf dat=dat.rsf

    where mod.rsf and dat.rsf are RSF files that represent vectors from the model anddata spaces. sfdottest does not create any temporary files and does not have anyrestrictive limitations on the size of the vectors.

    Here is an example. We first setup a vector with 100 elements using sfspike andthen run sfdottest to test the sfcausint program. sfcausint implements a linearoperator of causal integration and its adjoint, the anti-causal integration.

    bash$ sfspike n1=100 > vec.rsf

    bash$ sfdottest sfcausint mod=vec.rsf dat=vec.rsf

    sfdottest: L[m]*d=1410.2

    sfdottest: L’[d]*m=1410.2

    bash$ sfdottest sfcausint mod=vec.rsf dat=vec.rsf

    sfdottest: L[m]*d=1165.87

    sfdottest: L’[d]*m=1165.87

    The numbers are different on subsequent runs because of changing seed in the randomnumber generator.

    Here is a somewhat more complicated example. The sfhelicon program imple-ments Claerbout’s multidimensional helical filtering (Claerbout, 1998). It requires afilter to be specified in addition to the input and output vectors. We create a helical2-D filter using the Unix echo command.

    bash$ echo 1 19 20 n1=3 n=20,20 data_format=ascii_int in=lag.rsf > lag.rsf

    bash$ echo 1 1 1 a0=-3 n1=3 data_format=ascii_float in=flt.rsf > flt.rsf

    Next, we create an example 2-D model and data vector with sfspike.

    bash$ sfspike n1=50 n2=50 > vec.rsf

    Now the sfdottest program can perform the dot product test.

  • Madagascar Documentation Madagascar programs 59

    bash$ sfdottest sfhelicon filt=flt.rsf lag=lag.rsf \

    > mod=vec.rsf dat=vec.rsf

    sfdottest: L[m]*d=8.97375

    sfdottest: L’[d]*m=8.97375

    Here is the same program tested in the inverse filtering mode:

    bash$ sfdottest sfhelicon filt=flt.rsf lag=lag.rsf \

    > mod=vec.rsf dat=vec.rsf div=y

    sfdottest: L[m]*d=15.0222

    sfdottest: L’[d]*m=15.0222

    sfget: Output parameters from the header.

    sfget parform=y all=n par1 par2 ...

    bool all=n [y/n] If output all values.bool parform=y [y/n] If y, print out parameter=value. If n,

    print out value.

    The sfget program extracts a parameter value from an RSF file. It is usefulmostly for scripting. Here is, for example, a quick calculation of the maximum valueon the first axis in an RSF dataset (the output of sfspike) using the standard Unixbc calculator.

    bash$ ( sfspike n1=100 | sfget n1 d1 o1; echo "o1+(n1-1)*d1" ) | bc

    .396

    See also sfput.

    Implementation: system/main/get.c

    The implementation is trivial. Loop through all command-line parameters that con-tain the “=” character.

    system/main/get.c

    41 i f ( ! s f g e t b o o l ( ” a l l ” ,& a l l ) ) a l l=f a l s e ;42 /∗ I f output a l l va lue s . ∗/

    Get the parameter value (as string) and output it as either key=value or value,depending on the parform parameter.

    http://rsf.svn.sourceforge.net/viewvc/rsf/trunk/system/main/get.c?view=markup

  • 60 Fomel Madagascar Documentation

    system/main/get.c

    44 t ab l e = s f s i m t a b i n i t ( t a b s i z e ) ;45 s f s i m t a b i n p u t ( tab le , s td in , NULL) ;46

    47 i f ( a l l ) {48 s f s imtab output ( tab le , s tdout ) ;49 } else {50 for ( i = 1 ; i < argc ; i++) {

    sfheadercut: Zero a portion of a dataset based on a headermask.

    sfheadercut mask=head.rsf < in.rsf > out.rsf

    The input data is a collection of traces n1xn2,

    mask is an integer array of size n2.

    file mask= auxiliary input file name

    sfheadercut is close to sfheaderwindow but instead of windowing the dataset,it fills the traces specified by the header mask with zeroes. The size of the input datais preserved.

    Here is an example of using sfheaderwindow for zeroing every other trace in theinput file. First, let us create an input file with ten traces:

    bash$ sfmath n1=5 n2=10 output=x2+1 > input.rsf

    bash$ < input.rsf sfdisfil

    0: 1 1 1 1 1

    5: 2 2 2 2 2

    10: 3 3 3 3 3

    15: 4 4 4 4 4

    20: 5 5 5 5 5

    25: 6 6 6 6 6

    30: 7 7 7 7 7

    35: 8 8 8 8 8

    40: 9 9 9 9 9

    45: 10 10 10 10 10

    Next, we can create a mask with alternating ones and zeros using sfinterleave.

    bash$ sfspike n1=5 mag=1 | sfdd type=int > ones.rsf

  • Madagascar Documentation Madagascar programs 61

    bash$ sfspike n1=5 mag=0 | sfdd type=int > zeros.rsf

    bash$ sfinterleave axis=1 ones.rsf zeros.rsf > mask.rsf

    bash$ sfdisfil < mask.rsf

    0: 1 0 1 0 1 0 1 0 1 0

    Finally, sfheadercut zeros the input traces.

    bash$ sfheadercut < input.rsf mask=mask.rsf > output.rsf

    bash$ sfdisfil < output.rsf

    0: 1 1 1 1 1

    5: 0 0 0 0 0

    10: 3 3 3 3 3

    15: 0 0 0 0 0

    20: 5 5 5 5 5

    25: 0 0 0 0 0

    30: 7 7 7 7 7

    35: 0 0 0 0 0

    40: 9 9 9 9 9

    45: 0 0 0 0 0

    sfheadersort: Sort a dataset according to a header key.

    sfheadersort < in.rsf > out.rsf head=

    string head= header file

    sfheadersort is used to sort traces in the input file according to trace headerinformation.

    Here is an example of using sfheadersort for randomly shuffling traces in theinput file. First, let us create an input file with seven traces:

    bash$ sfmath n1=5 n2=7 output=x2+1 > input.rsf

    bash$ < input.rsf sfdisfil

    0: 1 1 1 1 1

    5: 2 2 2 2 2

    10: 3 3 3 3 3

    15: 4 4 4 4 4

    20: 5 5 5 5 5

    25: 6 6 6 6 6

    30: 7 7 7 7 7

    Next, we can create a random file with seven header values using sfnoise.

  • 62 Fomel Madagascar Documentation

    bash$ sfspike n1=7 | sfnoise rep=y type=n > random.rsf

    bash$ < random.rsf sfdisfil

    0: 0.05256 -0.2879 0.1487 0.4097 0.1548

    5: 0.4501 0.2836

    If you reproduce this example, your numbers will most likely be different, because,in the absence of seed= parameter, sfnoise uses a random seed value to generatepseudo-random numbers. Finally, we apply sfheadersort to shuffle the input traces.

    bash$ < input.rsf sfheadersort head=random.rsf > output.rsf

    bash$ < output.rsf sfdisfil

    0: 2 2 2 2 2

    5: 1 1 1 1 1

    10: 3 3 3 3 3

    15: 5 5 5 5 5

    20: 7 7 7 7 7

    25: 4 4 4 4 4

    30: 6 6 6 6 6

    As expected, the order of traces in the output file corresponds to the order of valuesin the header. Thanks to the separation between headers and data, the operation ofsfheadersort is optimally efficient. It first sorts the headers and only then accessesthe data, reading each data trace only once.

    sfheaderwindow: Window a dataset based on a header mask.

    sfheaderwindow mask=head.rsf < in.rsf > out.rsf

    The input data is a collection of traces n1xn2,

    mask is an integer array os size n2, windowed is n1xm2,

    where m2 is the number of nonzero elements in mask.

    file mask= auxiliary input file name

    sfheaderwindow is used to window traces in the input file according to traceheader information.

    Here is an example of using sfheaderwindow for randomly selecting part of thetraces in the input file. First, let us create an input file with ten traces:

    bash$ sfmath n1=5 n2=10 output=x2+1 > input.rsf

    bash$ < input.rsf sfdisfil

    0: 1 1 1 1 1

    5: 2 2 2 2 2

    10: 3 3 3 3 3

  • Madagascar Documentation Madagascar programs 63

    15: 4 4 4 4 4

    20: 5 5 5 5 5

    25: 6 6 6 6 6

    30: 7 7 7 7 7

    35: 8 8 8 8 8

    40: 9 9 9 9 9

    45: 10 10 10 10 10

    Next, we can create a random file with ten header values using sfnoise.

    bash$ sfspike n1=10 | sfnoise rep=y type=n > random.rsf

    bash$ < random.rsf sfdisfil

    0: -0.005768 0.02258 -0.04331 -0.4129 -0.3909

    5: -0.03582 0.4595 -0.3326 0.498 -0.3517

    If you reproduce this example, your numbers will most likely be different, because,in the absence of seed= parameter, sfnoise uses a random seed value to generatepseudo-random numbers. Finally, we apply sfheaderwindow to window the inputtraces selecting only those for which the header is greater than zero.

    bash$ < random.rsf sfmask min=0 > mask.rsf

    bash$ < mask.rsf sfdisfil

    0: 0 1 0 0 0 0 1 0 1 0

    bash$ < input.rsf sfheaderwindow mask=mask.rsf > output.rsf

    bash$ < output.rsf sfdisfil

    0: 2 2 2 2 2

    5: 7 7 7 7 7

    10: 9 9 9 9 9

    In this case, only three traces are selected for the output. Thanks to the separationbetween headers and data, the operation of sfheaderwindow is optimally efficient.

    sfin: Display basic information about RSF files.

    sfin info=y check=2. trail=y [

  • 64 Fomel Madagascar Documentation

    sfin is one of the most useful programs for operating with RSF files. It producesquick information on the file hypercube dimensions and checks the consistency of theassociated data file.

    Here is an example. Let us create an RSF file and examine it with sfin.

    bash$ sfspike n1=100 n2=20 > spike.rsf

    bash$ sfin spike.rsf

    spike.rsf:

    in="/tmp/spike.rsf@"

    esize=4 type=float form=native

    n1=100 d1=0.004 o1=0 label1="Time" unit1="s"

    n2=20 d2=0.1 o2=0 label2="Distance" unit2="km"

    2000 elements 8000 bytes

    sfin reports the following information:

    • location of the data file (/tmp/spike.rsf)

    • element size (4 bytes)

    • element type (floating point)

    • element form (native)

    • hypercube dimensions (100 by 20)

    • axes scale (0.004 and 0.1)

    • axes origin (0 and 0)

    • axes labels

    • axes units

    • total number of elements

    • total number of bytes in the data file

    Suppose that the file got corrupted by a buggy program and reports incorrectdimensions. The sfin program should be able to catch the discrepancy.

    bash$ echo n2=100 >> spike.rsf

    bash$ sfin spike.rsf > /dev/null

    sfin: Actually 8000 bytes, 20% of expected.

    sfin also checks the first records in the file for zeros.

  • Madagascar Documentation Madagascar programs 65

    bash$ sfspike n1=100 n2=100 k2=99 > spike2.rsf

    bash$ sfin spike2.rsf >/dev/null

    sfin: The first 32768 bytes are all zeros

    The number of bytes to check is adjustable

    bash$ sfin spike2.rsf check=0.01 >/dev/null

    sfin: The first 16384 bytes are all zeros

    You can also output only the location of the data file. This is sometimes handyin scripts.

    bash$ sfin spike.rsf spike2.rsf info=n

    /tmp/spike.rsf@ /tmp/spike2.rsf@

    An alternative is to use sfget, as follows:

    bash$ sfget parform=n in < spike.rsf

    /tmp/spike.rsf@

    sfinterleave: Combine several datasets by interleaving.

    sfinterleave > out.rsf axis=3 [< file0.rsf] file1.rsf file2.rsf ...

    int axis=3 Axis for interleaving

    sfinterleave combines two or more datasets by interleaving them on one of theaxes. Here is a quick example:

    bash$ sfspike n1=5 n2=5 > one.rsf

    bash$ sfdisfil < one.rsf

    0: 1 1 1 1 1

    5: 1 1 1 1 1

    10: 1 1 1 1 1

    15: 1 1 1 1 1

    20: 1 1 1 1 1

    bash$ sfscale < one.rsf dscale=2 > two.rsf

    bash$ sfdisfil < two.rsf

    0: 2 2 2 2 2

    5: 2 2 2 2 2

    10: 2 2 2 2 2

    15: 2 2 2 2 2

    20: 2 2 2 2 2

  • 66 Fomel Madagascar Documentation

    bash$ sfinterleave one.rsf two.rsf axis=1 | sfdisfil

    0: 1 2 1 2 1

    5: 2 1 2 1 2

    10: 1 2 1 2 1

    15: 2 1 2 1 2

    20: 1 2 1 2 1

    25: 2 1 2 1 2

    30: 1 2 1 2 1

    35: 2 1 2 1 2

    40: 1 2 1 2 1

    45: 2 1 2 1 2

    bash$ sfinterleave < one.rsf two.rsf axis=2 | sfdisfil

    0: 1 1 1 1 1

    5: 2 2 2 2 2

    10: 1 1 1 1 1

    15: 2 2 2 2 2

    20: 1 1 1 1 1

    25: 2 2 2 2 2

    30: 1 1 1 1 1

    35: 2 2 2 2 2

    40: 1 1 1 1 1

    45: 2 2 2 2 2

    sfmask: Create a mask.

    sfmask < in.rsf > out.rsf min= max= min= max=

    Mask is an integer data with ones and zeros.

    Ones correspond to input values between min and max.

    The output can be used with sfheaderwindow.

    int max= maximum header valueint min= minimum header value

    sfmask creates an integer output of ones and zeros comparing the values of theinput data to specified min= and max= parameters. It is useful for sfheaderwindowand in many other applications. Here is a quick example:

    bash$ sfmath n1=10 output="sin(x1)" > sin.rsf

    bash$ < sin.rsf sfdisfil

    0: 0 0.8415 0.9093 0.1411 -0.7568

    5: -0.9589 -0.2794 0.657 0.9894 0.4121

    bash$ < sin.rsf sfmask min=-0.5 max=0.5 | sfdisfil

    0: 1 0 0 1 0 0 1 0 0 1

  • Madagascar Documentation Madagascar programs 67

    sfmath: Mathematical operations on data files.

    sfmath > out.rsf n#= d#=(1,1,...) o#=(0,0,...) label#= unit#= type= label= unit=

    output=

    Known functions:

    cos, sin, tan, acos, asin, atan,

    cosh, sinh, tanh, acosh, asinh, atanh,

    exp, log, sqrt, abs,

    erf, erfc (for float data),

    arg, conj, real, imag (for complex data).

    sfmath will work on float or complex data, but all the input and output

    files must be of the same data type.

    An alternative to sfmath is sfadd, which may be more efficient, but is

    less versatile.

    Examples:

    sfmath x=file1.rsf y=file2.rsf power=file3.rsf output=’sin((x+2*y)^power)’ > out.rsf

    sfmath < file1.rsf tau=file2.rsf output=’exp(tau*input)’ > out.rsf

    sfmath n1=100 type=complex output="exp(I*x1)" > out.rsf

    Arguments which are not treated as variables in mathematical expressions:

    datapath=, type=, out=

    See also: sfheadermath.

    float d#=(1,1,...) sampling on #-th axisstring label= data labelstring label#= label on #-th axislargeint n#= size of #-th axisfloat o#=(0,0,...) origin on #-th axisstring output= Mathematical description of the outputstring type= output data type [float,complex]string unit= data unitstring unit#= unit on #-th axis

    sfmath is a versatile program for mathematical operations with RSF files. It canoperate with several input file, all of the same dimensions and data type. The datatype can be real (floating point) or complex. Here is an example that demonstratesseveral features of sfmath.

    bash$ sfmath n1=629 d1=0.01 o1=0 n2=40 d2=1 o2=5 \

    output="x2*(8+sin(6*x1+x2/10))" > rad.rsf

    bash$ < rad.rsf sfrtoc | sfmath output="input*exp(I*x1)" > rose.rsf

    bash$ < rose.rsf sfgraph title=Rose screenratio=1 wantaxis=n | sfpen

    The first line creates a 2-D dataset that consists of 40 traces 629 samples each. The

  • 68 Fomel Madagascar Documentation

    values of the data are computed with the formula "x2*(8+sin(6*x1+x2/10))", wherex1 refers to the coordinate on the first axis, and x2 is the coordinate of the secondaxis. In the second line, we convert the data from real to complex using sfrtoc andproduce a complex dataset using formula "input*exp(I*x1)", where input refers tothe input file. Finally, we plot the complex data as a collection of parametric curvesusing sfgraph and display the result using sfpen. The plot appearing on your screenshould look similar to Figure 1.

    Figure 1: This figure was created with sfmath. rsf/sfmath rose

    One possible alternative to the second line above is

    bash$ < rad.rsf sfmath output=x1 > ang.rsf

    bash$ sfmath r=rad.rsf a=ang.rsf output="r*cos(a)" > cos.rsf

    bash$ sfmath r=rad.rsf a=ang.rsf output="r*sin(a)" > sin.rsf

    bash$ sfcmplx cos.rsf sin.rsf > rose.rsf

    Here we refer to input files by names (r and a) and combine the names in a formula.

  • Madagascar Documentation Madagascar programs 69

    sfpad: Pad a dataset with zeros.

    sfpad < in.rsf > out.rsf beg#=0 end#=0

    n\#out is equivalent to n\#, both of them overwrite end\#.

    int beg#=0 the number of zeros to add before the be-ginning of #-th axis

    int end#=0 the number of zeros to add after the endof #-th axis

    pad increases the dimensions of the input dataset by padding the data with zeroes.Here are some simple examples.

    bash$ sfspike n1=5 n2=3 > one.rsf

    bash$ sfdisfil < one.rsf

    0: 1 1 1 1 1

    5: 1 1 1 1 1

    10: 1 1 1 1 1

    bash$ < one.rsf sfpad n2=5 | sfdisfil

    0: 1 1 1 1 1

    5: 1 1 1 1 1

    10: 1 1 1 1 1

    15: 0 0 0 0 0

    20: 0 0 0 0 0

    bash$ < one.rsf sfpad beg2=2 | sfdisfil

    0: 0 0 0 0 0

    5: 0 0 0 0 0

    10: 1 1 1 1 1

    15: 1 1 1 1 1

    20: 1 1 1 1 1

    bash$ < one.rsf sfpad beg2=1 end2=1 | sfdisfil

    0: 0 0 0 0 0

    5: 1 1 1 1 1

    10: 1 1 1 1 1

    15: 1 1 1 1 1

    20: 0 0 0 0 0

    bash$ < one.rsf sfwindow n1=3 | sfpad n1=5 n2=5 beg1=1 beg2=1 | sfdisfil

    0: 0 0 0 0 0

    5: 0 1 1 1 0

    10: 0 1 1 1 0

    15: 0 1 1 1 0

    20: 0 0 0 0 0

    You can use sfcat to pad data with values other than zeroes.

  • 70 Fomel Madagascar Documentation

    sfput: Input parameters into a header.

    sfput < in.rsf > out.rsf