+ All Categories
Home > Documents > 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions...

1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions...

Date post: 01-May-2018
Category:
Upload: vuongtuong
View: 226 times
Download: 5 times
Share this document with a friend
26
Geophysical Computing HW Solns-1 Homework Solutions – Lectures 1-8 1. Homework Solutions – Lecture 1: Basic Unix Homework just involves creating a couple of text files with an editor. 2. Homework Solutions – Lecture 2: Awk, cut, paste, etc. Problem 2-1 1) Consider two files given below that each contain a set of Cartesian coordinates. Write an awk script to compute the distance between these pairs of points. Feel free to use any of the other commands we learned in this lecture as well. x1 y1 0.0 0.0 0.5 0.1 0.75 0.2 1.0 0.3 x2 y2 0.0 0.0 -0.25 0.1 -0.5 0.2 -1.0 0.3 >> paste file1 file2 | awk ‘NR > 1 {print sqrt( ($1-$3)*($1-$3) + ($2-$4)*($2-$4) ) }’ 0 0.75 1.25 2. Problem 2-2 2) Below is a table of S-wave velocities at the coordinates given by the indicated latitude, and longitude (φ) in degrees. Create a file exactly as shown below, and write an awk command that will convert the longitudes given in the file below from the interval: -180° φ 180° to the interval: 0° φ 360°. Note: longitudes from 0° to 180° in the original file should not change. Lon Lat dVs -180 -10 2.3 -135 -10 2.4 -90 -10 2.0 -45 -10 1.8 0 -10 0.0 45 -10 -0.3 90 -10 -1.2 135 -10 -1.5 180 -10 0.0 -180 10 2.4 -135 10 2.6 -90 10 2.1 -45 10 1.6
Transcript
Page 1: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-1

Homework Solutions – Lectures 1-8 1. Homework Solutions – Lecture 1: Basic Unix Homework just involves creating a couple of text files with an editor. 2. Homework Solutions – Lecture 2: Awk, cut, paste, etc. Problem 2-1 1) Consider two files given below that each contain a set of Cartesian coordinates. Write an awk script to compute the distance between these pairs of points. Feel free to use any of the other commands we learned in this lecture as well.

x1 y1 0.0 0.0 0.5 0.1 0.75 0.2 1.0 0.3

x2 y2 0.0 0.0 -0.25 0.1 -0.5 0.2 -1.0 0.3

>> paste file1 file2 | awk ‘NR > 1 {print sqrt( ($1-$3)*($1-$3) + ($2-$4)*($2-$4) ) }’ 0 0.75 1.25 2. Problem 2-2 2) Below is a table of S-wave velocities at the coordinates given by the indicated latitude, and longitude (φ) in degrees. Create a file exactly as shown below, and write an awk command that will convert the longitudes given in the file below from the interval: -180° ≤ φ ≤ 180° to the interval: 0° ≤ φ ≤ 360°. Note: longitudes from 0° to 180° in the original file should not change.

Lon Lat dVs -180 -10 2.3 -135 -10 2.4 -90 -10 2.0 -45 -10 1.8 0 -10 0.0

45 -10 -0.3 90 -10 -1.2

135 -10 -1.5 180 -10 0.0 -180 10 2.4 -135 10 2.6 -90 10 2.1 -45 10 1.6

Page 2: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-2

0 10 -0.1 45 10 -0.4 90 10 -1.0

135 10 -1.0 180 10 0.3

>> awk ‘{if ($1 < 0) print ($1+360), $2, $3; else print $1, $2, $3}’ file Problem 2-3 3) Consider a file that looks as follows:

Vs Vp Rho Vs Vp Rho Vs

write an awk command that will print the total number of lines that contain the string Vs. >> awk ‘$1 ~ /Vs/ {nlines = nlines + 1} END {print nlines}’ file Problem 2-4 4) I have a group of SAC files named as follows: >> HRU.UU.EHZ NOQ.UU.HHZ GMU.UU.EHZ CTU.UU.EHZ Using awk, how can we change the names of all of these files so that the EHZ or HHZ is replaced by just Z. So, for example the first file is renamed as: HRU.UU.Z >> ls *UU* | awk ‘BEGIN {FS=”.”} {print “mv “ $1”.”$2”.”$3, $1”.”$2”.Z”}’ | csh Problem 2-5 5) Write an awk command that will print the sum and average of column #1 of a file. The output should look like: >> Sum is: X; Average is: X >> awk ‘{s += $1} END { print “Sum is: “, s “; Average is: “, s/NR}’ file

Page 3: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-3

3. Homework Solutions – Lecture 3: C Shell Scripting Part 1 Problem 3-1 1) Write a C Shell script that will allow you to set the name of an input postscript file and desired output name of a jpg file, and then using ImageMagick’s convert command will perform a nice job of converting a postscript file into a jpeg image. E.g., At the very least I should be enter, either by the command line or by interactive input the name of an input .ps file, and desired name of output .jpg file and the script will automatically create the .jpg file. #!/bin/csh echo "Convert to JPEG ..." # User input #:----:----:----:----:----:----:----:----:----:----:----:----:# echo "Enter Name of file to convert to JPEG (e.g., input.ps) ..." set in = $< echo "Enter Name of output jpeg file (e.g., output.jpg) ..." set out = $< echo "Enter JPEG resolution (e.g., 300) ..." set res = $< convert -compress Lossless -density ${res}x${res} ${in} ${out} Problem 3-2 2) Write a C Shell script that will add the current date to the end of a filename. E.g., if today is Dec 25, 2010, then the shell script should change the filename to: filename.20101225 The script should read the filename from the command line. Hence, if we named this script adddate then execution of this command should look like: >> addate filename Utility: addate #!/bin/csh set ifile = $argv[1] #take input file name from command line set od = `date +%Y%m%d` #set format of date string set ofile = ${ifile}.${od} #set name of output file echo “moving file $ifile to $ofile” mv $ifile ${ifile}.${od}

Page 4: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-4

Problem 3-3 3) Write a C Shell script that will remove dates added with the script written in Problem #2. Note: this script should also work when there is a dot in the filename. E.g., the code should work for any filename of the form… foo.20101225 foo.foo.20101225 foo.foo.foo.20101225 foo.foo.foo.*.20101225 Output file names for the examples above should be: foo foo.foo foo.foo.foo etc. Utility: rmdate #!/bin/csh set ifile = $argv[1] #determine total number of fields to keep @ lf = `echo $ifile | awk ‘BEGIN {FS=”.”} END {print (NF-1)}’` set ofile = `echo $ifile | cut –d. –f1-${lf}` echo “moving file $ifile to $ofile” mv $ifile $ofile Problem 3-4 4) Write a script that will replace spaces in file names with underscores. E.g., if the input file is named: My File.txt , then the output file should be named My_File.txt. This one can be tricky to deal with. The key is in the proper use of quotes. The result is my utility: noblanks

Page 5: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-5

#!/bin/csh # Script to remove blanks in a filename replacing them # with underscores. # # Usage: >> noblanks My File.txt # # output: My_File.txt # # by: [email protected] if ($#argv < 1) goto usage set ifile = “$argv[1]” set ofile = `echo $ifile | awk ‘{gsub(/ /, “_”)}; 1’` echo “moving file $ifile to $ofile” mv “$ifile” $ofile exit 1 usage: echo “Usage: noblanks My File.txt” echo “ output will be: My_File.txt” 4. Homework Solutions – Lecture 4: C Shell Scripting Part 2 Problem 4-1 1) Write a C Shell script that will loop through a list of files, and add a counter to the beginning of the filename. For example, if I have 10 files named: a.txt b.txt c.txt … j.txt The code should move the files to be named: 01_a.txt 02_b.txt 03_c.txt … 10_j.txt This kind of utility is often needed in naming files. Especially, as we will see in later lectures when automatically generating animations or movie files.

Page 6: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-6

#!/bin/csh ls *.txt >! flist @ nr = `awk ‘END {print NR}’ flist` @ n = 1 while ($n <= $nr) set if = `awk ‘NR == ‘$n’ {print $1}’ flist` if ($n < 10) then set prefix = 0${n} else set prefix = $n endif mv $if ${prefix}_${if} @ n = $n + 1 end rm flist Problem 4-2 2) Write a C Shell script that will repeat a command many times. We will call this script: forever. For example, sometimes I want to see if a job I submitted to the supercomputer has started yet. To do so I would type qstat –a. Well, I’m anxious to see if it starts, so I will keep typing qstat –a until I get confirmation that indeed the job did start. Instead I want to type forever qstat –a, and what should happen is that qstat –a keeps getting invoked (after a couple of second delay) until I decide to cancel it. Your script should be able to take any Unix command as input. For example, it should work as forever ls, or forever ls –la, or forever cat inputfile, etc. Not an incredibly elegant script, but has worked for me for years. Could easily make this script more generalized, and also use onintr to make it exit smoothly. #!/bin/csh @ nargs = $#argv set cmd = $argv[1] if ($nargs == 2) then set argument = $argv[2] endif @ count = 1000 @ n = 1 while ($n <= $count)

Page 7: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-7

if ($nargs == 1) then $cmd else $cmd $argument endif #simple way to delay calling $cmd @t = 1 while ($t <= 5000) #can set counter for longer/shorter delay @ t = $t + 1 end echo “ “ @ n = $n + 1 end Problem 4-3 3) In the C Shell one can not do floating point operations. That is, you can not do math with real numbers. However, it is sometimes necessary to do so. A quick work around is to do the math inside a program like the basic calculater (e.g., use: bc -l). Write a shell script that will allow you to do a simple calculation on floating point numbers. Take as input a coordinate position in polar coordinates (Radius, and angle theta in degrees) and output the equivalent Cartesian coordinate position. Doing math in a C Shell isn’t elegant, but sometimes useful! #!/bin/csh set r = 5.0 #input radius set theta = -10.0 #input theta, in deg bc –l << eff >! ans.txt $r*s(($theta*3.141592654/180)) eff set x = `cat ans.txt` bc –l << eff >! ans.txt $r*c(($theta*3.141592654/180)) eff set y = `cat ans.txt` #Report output echo “X= $x” echo “Y= $y” rm ans.txt

Page 8: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-8

Problem 4-4 4) Write a C Shell script using the dialog utility to create a menu box. The menu box should provide several options of actions you want to carry out on a seismogram. For example, the menu box may have options as follows: Please choose an Action to be performed on Seismogram: 1 Flip Polarity of Seismogram 2 Low Pass Filter Seismogram 3 Make Time Picks on Seismogram 4 Discard Seismogram The script doesn’t actually have to perform any actions on a seismogram file, but is aimed at getting you to write a script using the dialog utility. Output, in the form of some kind of recognition of which option was chosen should be provided in the code. #!/bin/csh dialog --title “----- Menu Example -----“ \ --menu “Please choose an option: “ 15 55 5 \ 1 “Flip Polarity of Seismogram” \ 2 “Low Pass Filter Seismogram” \ 3 “Make Time Picks on Seismogram” \ 4 “Discard Seismogram” \ --stdout >! temp_menu.txt set exit_status = $? #get dialog utility exit status set input = `cat temp_menu.txt` switch ($exit_status) case 0: # A choice of some sort was made if ($input == 1) then echo “Flipping polarity…” else if ($input == 2) then echo “Low Pass Filtering Seismogram…” else if ($input == 3) then echo “Making Time Picks…” else if ($input == 4) then echo “Discarding Seismogram…” endif breaksw case 1: #cancel button was pressed” breaksw

Page 9: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-9

case 255: #escape key was pressed breaksw endsw rm temp_menu.txt 5. Homework Solutions – Lecture 5: GMT Part 1 Problem 5-1 1) Create a global map showing the major plate boundaries (an x,y table of plate boundaries are given on the web page) using a Mollweide projection and centered on the Pacific Plate. #!/bin/csh pscoast –R0/360/-85/85 –JM180/0/6i –Bg30 –Df –A8000 \ –G100/200/10 -W3/10/10/10 –P –K >! plot.ps psxy plate_boundaries.xy –m –R –JM –P –O –W3/0/0/255 >> plot.ps gs –sDEVICE=x11 plot.ps Resulting plot:

Page 10: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-10

Problem 5-2 2) Create a map that will plot the locations of all Global Seismic Network (GSN) stations using a Winkel projection. Put the station code (e.g., AAK) next to the plot symbol for the station. The station names and locations are provided in a file on the web page. #!/bin/csh # Awk out lon, lat of stations awk '{print $3, $2}' gsn_stations.txt >! stats.xy # Make a text file with station names awk '{print $3, $2, "8 0 1 1", $1}' gsn_stations.txt >! text.xy pscoast -R-180/180/-90/90 -JR0/7.5i -Df -B0 \ -W4/100/100/100 -P -K -A40000 -X.5 -V >! statmap.ps psxy stats.xy -JR -R -P -O -K -Si.125i -W2/0/0/0 \ -G0/0/205 >> statmap.ps pstext text.xy -JR -R -P -O -N >> statmap.ps rm stats.xy rm text.xy gs -sDEVICE=x11 statmap.ps Problem 5-3 3) The data file: envelope.xy is given on the web page. This data file contains the envelope of a seismogram recorded at Eilson Airforce Base in Alaska with the direct P-wave arrival aligned at 0 sec. The envelope is given here to show the exponential decay of the coda waves. Make a plot of this seismogram in two separate panels. In the top panel show the raw seismogram as provided in the file envelope.xy. Anytime a quantity displays exponential behavior it is customary to plot this as the natural log of the amplitudes instead of the raw amplitude. Such a plot would then show a linear decay instead of exponential decay, and the exponential decay can then be estimated by fitting a straight line to the linear region. Make the bottom panel of the plot should be the natural log of the amplitude of the seismogram, and show which region immediately following the direct P-arrival can be fit with a straight line. #!/bin/csh psxy envelope.xy -R-50/500/0/0.25 -JX6i/3i -P -K -W3/0/0/255 \ -B50g100000f10/0.2g10000nSeW -X1.5 -Y6i >! plot.ps awk '{print $1, log($2)}' envelope.xy >! logenv.xy psxy logenv.xy -R-50/500/-13.5/-1 -JX6i/3i -P -O -W3/0/0/255 \ -B50g100000f10/2g10000nSeW -Y-4i -V >> plot.ps # One should at this point add some text describing the axes... gs -sDEVICE=x11 plot.ps

Page 11: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-11

Problem 5-4 4) This will be one of the most useful scripts you will ever write. When doing any type of scientific work one always generates tabular data (i.e., x, y pairs of data). It is really nice to quickly be able to take a glance at these data without having to write a special GMT script every time. For a finalized plot of data you probably will want to write a special script, but sometimes its really, really useful to just be able to take a quick glance at it. Write a generic script that will plot a 2D group of data. Make an option for plotting solid lines or symbols. Name the script xyplot. The plot should contain a title that contains the name of the file being plotted. The script should run as: >> xyplot input.xy s where, if the s option is given, the data will be plotted with filled in circles. Hint: Use GMT’s minmax and gmtmath commands to help in determining the plot range, and spacing between tickmarks for the axes. #!/bin/csh #-------------------------------------------------------------# # # XYPLOT # # crude script for visualizing x,y pairs of data # #-------------------------------------------------------------# # Read command line arguments #-------------------------------------------------------------# if ($#argv < 1) goto usage set ifile = $argv[1] if ($#argv == 2) then set switch = $argv[2] else set switch = 'd' endif #-------------------------------------------------------------# # Determine range of data #-------------------------------------------------------------# set minx = `minmax -C $ifile | awk '{print $1}'` set maxx = `minmax -C $ifile | awk '{print $2}'` set miny = `minmax -C $ifile | awk '{print $3}'` set maxy = `minmax -C $ifile | awk '{print $4}'` #-------------------------------------------------------------# # Determine tick marks #-------------------------------------------------------------# # probably a much better way to do this that will # produce more readable axis labels...

Page 12: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-12

set xticks = `gmtmath -Q $maxx $minx SUB 6 DIV =` set yticks = `gmtmath -Q $maxy $miny SUB 5 DIV =` #-------------------------------------------------------------# # Plot data #-------------------------------------------------------------# if ($switch == 'd') then psxy $ifile -JX6.5i/4i -R${minx}/${maxx}/${miny}/${maxy} -P \ -B${xticks}g10000000/${yticks}g1000000nSeW -W3/0/0/255 \ -X1. -Y5.0 -K >! xyplot.ps else if ($switch == 's') then psxy $ifile -JX6.5i/4i -R${minx}/${maxx}/${miny}/${maxy} -P \ -B${xticks}g10000000/${yticks}g1000000nSeW -W1/0/0/0 \ -Sc.1i -G0/0/255 -X1. -Y5.0 -K >! xyplot.ps else echo "Unsupported switch specified..." goto usage endif #-------------------------------------------------------------# # Add a text label #-------------------------------------------------------------# pstext -R0/8/0/11 -JX8i/11i -P -O -N << END >> xyplot.ps 0.0 4.25 16 0 1 1 File: $ifile END #-------------------------------------------------------------# # Display with GMT #-------------------------------------------------------------# gs -sDEVICE=x11 xyplot.ps #-------------------------------------------------------------# # Usage #-------------------------------------------------------------# exit 1 usage: echo "Usage: xyplot input.xy [s]" echo " [s] flag implies plot as symbols and not lines." #-------------------------------------------------------------# 6. Homework Solutions – Lecture 6: GMT Part 2 Problem 6-1 1) Write a C Shell script utility that one can use to visualize a color palette table. That is it should use the psscale command to generate a plot that one can quickly see what the .cpt file looks like. If we name the script viewcpt then we should be able to run it as: >> viewcpt cptfile where cptfile is the name of the color palette table we are interested in seeing.

Page 13: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-13

This is actually not a bad script, and quite handy as well #!/bin/csh #--------------------------------------------------------# # # VIEWCPT # # crude script for visualizing .cpt tables # #--------------------------------------------------------# # Read command line arguments #--------------------------------------------------------# if ($#argv < 1) goto usage set ifile = $argv[1] if ($#argv == 2) then set ticks = $argv[2] endif #--------------------------------------------------------# # Generate Plot #--------------------------------------------------------# if ($#argv == 1) then psscale -D2.0i/1.75i/5.5i/.3ih -C${ifile} -P -X2 \ >! viewcpt.ps else if ($#argv == 2) then psscale -D2.0i/1.75i/5.5i/.3ih -C${ifile} -P -X2 \ -B${ticks} >! viewcpt.ps endif #--------------------------------------------------------# # Display with GMT #--------------------------------------------------------# gs -sDEVICE=x11 viewcpt.ps #--------------------------------------------------------# # Usage #--------------------------------------------------------# exit 1 usage: echo "Usage: viewcpt cptfile [ticks]" echo " if 'ticks' are given then the cpt file will " echo " be shown with the supplied tickmark interval" #--------------------------------------------------------#

Page 14: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-14

Problem 6-2 2) Using the Etopo1 grid file create a world map to emulate the first plot shown in Section 4. Using the Etopo1 dataset it is quite simple to make beautiful images of global topography. #!/bin/csh set if = ETOPO1_Ice_g_gmt4.grd makecpt -Crelief -Z -T-11000/11000/100 >! elev.cpt grdimage $if -R0/360/-80/80 -JM180/0/5.25i -B0 -Celev.cpt \ -P -Y5.5 -X1.75 -V >! map.ps rm elev.cpt gs -sDEVICE=x11 map.ps Resulting Image:

Page 15: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-15

Problem 6-3 3) Make a global plot showing elevation of land masses using the Etopo1 elevations. Instead of using bathymetry for the oceans use the ocean ages and overlay the plate boundaries we used in the last homework. This problem will require clipping. Refer to the GMT Example #17 for hints on how to do this. Hint: Elevations will look best here as gray scale. #!/bin/csh # consult GMT example #17 for help makecpt -Cgray -Z -T-11000/11000/100 >! colors.cpt makecpt -Crainbow -Z -I -T0/28000/1000 >! colors2.cpt # first plot the ocean ages grdimage -R0/360/-90/90 -JG-111/45/4.5i -B30 age.3.6.nc \ -P -K -Ccolors2.cpt >! num2.ps # start a clipping path pscoast -Rage.3.6.nc -J -O -K -Dl -Gc >> num2.ps # now add the topography grdimage ETOPO1_Ice_g_gmt4.grd -J -R -Ccolors.cpt -O -K \ >> num2.ps # turn of clipping and plot coastlines pscoast -R -J -Q -Df -P -O -K >> num2.ps pscoast -R -J -Dc -W4/0/0/0 -P -O -K >> num2.ps # add the plate boundaries psxy plate_boundaries.xy -m -R -J -P -O -K -W5/0/0/255 >> num2.ps rm colors.cpt colors2.cpt gs -sDEVICE=x11 num2.ps Resulting image from the above script:

Page 16: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-16

Problem 6-4 4) The webpage contains a file: Tomography_TXBW.xyz. This file has longitude, latitude, and variations of S-wave velocity based on the tomographic inversion of Steve Grand (University of Texas at Austin). The velocities are given for a layer just above the core mantle boundary. Convert this file into a .grd file and plot it in two panels centered on (a) the central Pacific Ocean region and (b) Africa. It is customary to plot the lowest values (low velocities) with red colors and the highest values (high velocities) with blue colors. Make the color palette table a continuous table. Also plot world hot spot locations as triangles (to emulate volcanoes) on the maps. Be sure to include a scale bar for the colors. Is there any relation between deep mantle seismic velocities and hot spots locations? #!/bin/csh set if = Tomography_TXBW.xyz # Grid up Grand Tomography model and make Color palette table #:----:----:----:----:----:----:----:----:----:----:----:----:----:---- awk '{if ($1 < 0) print ($1+360.), $2, $3; \ else print $1, $2, $3}' $if >! temp.xyz xyz2grd temp.xyz -Gtxbw.grd -Rg -I1/1 -V #xyz to grid file makecpt -Cpolar -T-2.5/2.5/.1 -Z -I >! TXBW_tomo.cpt #color palette #:----:----:----:----:----:----:----:----:----:----:----:----:----:---- # make table of hot spots in same coord system #:----:----:----:----:----:----:----:----:----:----:----:----:----:---- awk '{if ($1 < 0) print ($1+360.), $2; \ else print $1, $2}' hotspots.txt >! hot.xy #:----:----:----:----:----:----:----:----:----:----:----:----:----:----

Page 17: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-17

# Plot top panel centered on Pacific #:----:----:----:----:----:----:----:----:----:----:----:----:----:---- grdimage -Rg -JR180/5i txbw.grd -K -P -Sc -Ei300 \ -CTXBW_tomo.cpt -X2.0 -Y6.0 >! txbw.ps pscoast -Rg -J -B90g1000/30g1000f10nSeW -P -O -K \ -Dc -W2 -A8000 >> txbw.ps psxy hot.xy -R -J -K -O -P -St.1i -W1/0/0/0 \ -G0/200/0 >> txbw.ps #:----:----:----:----:----:----:----:----:----:----:----:----:----:---- # Plot bottom panel centered on Africa #:----:----:----:----:----:----:----:----:----:----:----:----:----:---- grdimage -R -JR360/5i txbw.grd -K -O -P -Sc -Ei300 \ -CTXBW_tomo.cpt -Y-3.5 >> txbw.ps pscoast -R -JR -B90g1000/30g1000f10nSeW -P -O -K \ -Dc -W2 -A8000 >> txbw.ps psxy hot.xy -R -J -K -O -P -St.1i -W1/0/0/0 \ -G0/200/0 >> txbw.ps #:----:----:----:----:----:----:----:----:----:----:----:----:----:---- # Add a scale bar for the tomography #:----:----:----:----:----:----:----:----:----:----:----:----:----:---- psscale -D2.5i/-.5i/3.5i/.3ih -O -CTXBW_tomo.cpt \ -B.5:dVs:/:%: >> txbw.ps #:----:----:----:----:----:----:----:----:----:----:----:----:----:---- # remove extra files #:----:----:----:----:----:----:----:----:----:----:----:----:----:---- rm txbw.grd rm temp.xyz rm TXBW_tomo.cpt rm hot.xy #:----:----:----:----:----:----:----:----:----:----:----:----:----:---- gs -sDEVICE=x11 txbw.ps

Page 18: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-18

Page 19: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-19

7. Homework Solutions – Lecture 7: GMT Part 3 Problem 7-1 In this homework and in the next we will make a variety of plots of Antelope Island. Hence, whatever work you do for this weeks HW should be saved for future use. Download 5-m DEM data covering the Antelope Island region. Convert these elevation data into a netCDF format grid file, and generate a series of maps of Antelope Island. Make three separate plots of the island showing: (A) just colored elevations, (B) elevations colored with intensity shading, (C) bi-modal color (e.g., light brown for land masses) and blue (for water) with intensity shading. Provide a scale bar for the maps for both color (elevation in feet) and distance (in miles). Be sure to label the axes and to label each plot. Bring a printed copy of your map to the next class. We will vote on the best plot with the winner receiving a prize. #!/bin/csh # Set Map Boundaries #---------------------------------------------------------------------# set xmin = 390000 set xmax = 405000 set ymin = 4520000 set ymax = 4550000 # Plot #1 - Colored Elevations #---------------------------------------------------------------------# # Make cpt -- set water level at 4200 feet, and make background blue gmtset COLOR_BACKGROUND 198/226/255 makecpt -CColor_DEM.cpt -M -Z -T4200/6600/100 >! antelope.cpt grdimage Antelope_Island.grd -R${xmin}/${xmax}/${ymin}/${ymax} \ -Jx1:175000 -B5000g1000000/5000g1000000nSeW -Cantelope.cpt \ -V -P -K -X2 -Y2 -Sb -E300i >! Antelope_01.ps # map project UTM coords onto lon, lat set lon1 = `echo $xmin $ymin | mapproject -Ju12T/1:1 -F -C -I | awk '{print $1}'` set lat1 = `echo $xmin $ymin | mapproject -Ju12T/1:1 -F -C -I | awk '{print $2}'` set lon2 = `echo $xmax $ymax | mapproject -Ju12T/1:1 -F -C -I | awk '{print $1}'` set lat2 = `echo $xmax $ymax | mapproject -Ju12T/1:1 -F -C -I | awk '{print $2}'` # reset the colors gmtset COLOR_BACKGROUND 0/0/0 gmtset COLOR_FOREGROUND 255/255/255 # add distance scale bar pscoast -Ju12T/1:175000 -R${lon1}/${lon2}/${lat1}/${lat2} -Df -V -P \ -O -K -W2 -N1 -A100000 -Lf-112.24/40.84/${lat1}/5m+l1:175000+u \ --LABEL_FONT_SIZE=10 >> Antelope_01.ps

Page 20: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-20

# add color scale bar psscale -D3.8i/1.75i/3.5i/.3i -O -K -Cantelope.cpt \ -B200:Elevation:/:feet: >> Antelope_01.ps # add some text pstext -R0/8/0/11 -JX8i/11i -P -O -N << END >> Antelope_01.ps 1.1 -0.75 16 0 1 1 Easting (m) -1.1 2.8 16 90 1 1 Northing (m) 0.0 7.0 16 0 1 1 (A) Antelope Island - Elevations END #---------------------------------------------------------------------# # Plot #2 - Add intensities #---------------------------------------------------------------------# # make intensity file grdgradient Antelope_Island.grd -A0/270 -GAntelope_Island.gradients \ -Ne0.3 grdimage Antelope_Island.grd -R${xmin}/${xmax}/${ymin}/${ymax} -Jx1:175000 \ -B5000g1000000/5000g1000000nSeW -Cantelope.cpt -V -P -K -X2 -Y2 \ -Sb -E300i -IAntelope_Island.gradients >! Antelope_02.ps # map project UTM coords onto lon, lat set lon1 = `echo $xmin $ymin | mapproject -Ju12T/1:1 -F -C -I | awk '{print $1}'` set lat1 = `echo $xmin $ymin | mapproject -Ju12T/1:1 -F -C -I | awk '{print $2}'` set lon2 = `echo $xmax $ymax | mapproject -Ju12T/1:1 -F -C -I | awk '{print $1}'` set lat2 = `echo $xmax $ymax | mapproject -Ju12T/1:1 -F -C -I | awk '{print $2}'` # reset the colors gmtset COLOR_BACKGROUND 0/0/0 gmtset COLOR_FOREGROUND 255/255/255 # add distance scale bar pscoast -Ju12T/1:175000 -R${lon1}/${lon2}/${lat1}/${lat2} -Df -V -P \ -O -K -W2 -N1 -A100000 \ -Lf-112.24/40.84/${lat1}/5m+l1:175000+u --LABEL_FONT_SIZE=10 >> Antelope_02.ps # add color scale bar psscale -D3.8i/1.75i/3.5i/.3i -O -K -Cantelope.cpt \ -B200:Elevation:/:feet: >> Antelope_02.ps # add some text pstext -R0/8/0/11 -JX8i/11i -P -O -N << END >> Antelope_02.ps 1.1 -0.75 16 0 1 1 Easting (m) -1.1 2.8 16 90 1 1 Northing (m) 0.0 7.0 16 0 1 1 (B) Antelope Island - Intensities END # Plot #3 - two tone #---------------------------------------------------------------------#

Page 21: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-21

#make a new .cpt file echo "4000 198 226 255 4200 198 226 255" >! antelope.cpt echo "4200 222 184 135 6600 222 184 135" >> antelope.cpt grdimage Antelope_Island.grd -R${xmin}/${xmax}/${ymin}/${ymax} -Jx1:175000 \ -B5000g1000000/5000g1000000nSeW -Cantelope.cpt -V -P -K -X2 -Y2 \ -Sb -E300i -IAntelope_Island.gradients >! Antelope_03.ps # map project UTM coords onto lon, lat set lon1 = `echo $xmin $ymin | mapproject -Ju12T/1:1 -F -C -I | awk '{print $1}'` set lat1 = `echo $xmin $ymin | mapproject -Ju12T/1:1 -F -C -I | awk '{print $2}'` set lon2 = `echo $xmax $ymax | mapproject -Ju12T/1:1 -F -C -I | awk '{print $1}'` set lat2 = `echo $xmax $ymax | mapproject -Ju12T/1:1 -F -C -I | awk '{print $2}'` # reset the colors gmtset COLOR_BACKGROUND 0/0/0 gmtset COLOR_FOREGROUND 255/255/255 # add distance scale bar pscoast -Ju12T/1:175000 -R${lon1}/${lon2}/${lat1}/${lat2} -Df -V -P \ -O -K -W2 -N1 -A100000 \ -Lf-112.24/40.84/${lat1}/5m+l1:175000+u --LABEL_FONT_SIZE=10 >> Antelope_03.ps # add color scale bar psscale -D3.8i/1.75i/3.5i/.3i -O -K -Cantelope.cpt \ -B200:Elevation:/:feet: >> Antelope_03.ps # add some text pstext -R0/8/0/11 -JX8i/11i -P -O -N << END >> Antelope_03.ps 1.1 -0.75 16 0 1 1 Easting (m) -1.1 2.8 16 90 1 1 Northing (m) 0.0 7.0 16 0 1 1 (C) Antelope Island - Two Tone END # Remove Temporary Files #---------------------------------------------------------------------# rm antelope.cpt rm Antelope_Island.gradients gs -sDEVICE=x11 Antelope_0*.ps

Page 22: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-22

Page 23: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-23

8. Homework Solutions – Lecture 8: GMT Part 4 Problem 8-1 1) Generate a plot showing 3D perspective views of Antelope Island. The plot should contain two panels showing the island from two different viewpoints. As always be sure to include all relevant scale bars. #!/bin/csh # Set map boundaries set xmin = 390000 set xmax = 405000 set ymin = 4520000 set ymax = 4550000 # Make .cpt and .gradient file gmtset COLOR_BACKGROUND 198/226/255 makecpt -CColor_DEM.cpt -M -Z -T4200/6600/100 >! antelope.cpt grdgradient Antelope_Island.grd -A0/270 \ -GAntelope_Island.gradients -Ne0.3 # View #1 Azimuth=100, Elevation=20 grdview Antelope_Island.grd -R${xmin}/${xmax}/${ymin}/${ymax} \

Page 24: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-24

-Jx1:175000 -JZ1i -B5000g10000000nSeW -Cantelope.cpt \ -IAntelope_Island.gradients -Qs -E100/20 -P -K >! HW8_Prob1.ps # View #2 Azimuth=180, Elevation=40 grdview Antelope_Island.grd -R -Jx -JZ -B -Cantelope.cpt \ -IAntelope_Island.gradients -Qs -P -O -K -E180/40 \ -Y3i >> HW8_Prob1.ps # Scalebar psscale -D3.8i/1.75i/3.5i/.3i -O -Cantelope.cpt \ -B200:Elevation:/:feet: >> HW8_Prob1.ps rm Antelope_Island.gradients rm antelope.cpt gs -sDEVICE=x11 HW8_Prob1.ps

Page 25: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-25

Problem 8-2 2) Pick an appropriate 3D perspective view of Antelope Island and generate an animation that mimics changing illumination from sunrise to sunset. That is, the illumination azimuth should start from the East and move to the West. #!/bin/csh # Set map boundaries set xmin = 390000 set xmax = 405000 set ymin = 4520000 set ymax = 4550000 # Make .cpt and .gradient file gmtset COLOR_BACKGROUND 198/226/255 makecpt -CColor_DEM.cpt -M -Z -T4200/6600/100 >! antelope.cpt @ azi = 70 while ($azi <= 290) if ($azi < 100) then set ofile = ante_0${azi}.ps set jfile = ante_0${azi}.jpg else set ofile = ante_${azi}.ps set jfile = ante_${azi}.jpg endif grdgradient Antelope_Island.grd -A${azi} \ -GAntelope_Island.gradients -Ne0.3 # View: Azimuth=180, Elevation=40 grdview Antelope_Island.grd -R${xmin}/${xmax}/${ymin}/${ymax} -Jx1:175000 -JZ1i -B5000g10000000nSeW -Cantelope.cpt \ -IAntelope_Island.gradients \ -Qs -E180/40 -P -K -Y3.0 -X1.5 >! $ofile # Scalebar psscale -D3.8i/1.75i/3.5i/.3i -O -Cantelope.cpt \ -B200:Elevation:/:feet: >> $ofile convert -compress Lossless -density 100x100 $ofile $jfile rm $ofile rm Antelope_Island.gradients @ azi = $azi + 5 end convert -adjoin -delay 10 -loop 0 ante*.jpg antelope.gif

Page 26: 1. Homework Solutions – Lecture 1: Basic Unix 2. Homework Solutions …web.utah.edu/thorne/computing/Homework_Solutions_… ·  · 2010-07-30Homework Solutions – Lecture 1: Basic

Geophysical Computing HW Solns-26

rm antelope.cpt gs -sDEVICE=x11 ante.ps


Recommended