我具有以下创建多个对象的脚本。
我尝试仅在终端中运行它,但是似乎花费了很长时间。如何使用GNU-parallel运行它?
下面的脚本创建一个对象。它通过niy = 1到niy = 800,并且对于niy的每个增量,都会循环njx = 1到675。
#!/bin/csh
set njx = 675 ### Number of grids in X
set niy = 800 ### Number of grids in Y
set ll_x = -337500
set ll_y = -400000 ### (63 / 2) * 1000 ### This is the coordinate at lower right corner
set del_x = 1000
set del_y = 1000
rm -f out.shp
rm -f out.shx
rm -f out.dbf
rm -f out.prj
shpcreate out polygon
dbfcreate out -n ID1 10 0
@ n = 0 ### initilzation of counter (n) to count gridd cells in loop
@ iy = 1 ### initialization of conunter (iy) to count grid cells along north-south direction
echo ### emptly line on screen
while ($iy <= $niy) ### start the loop for norht-south direction
echo ' south-north' $iy '/' $niy ### print a notication on screen
@ jx = 1
while ($jx <= $njx)### start the loop for east-west direction
@ n++
set x = `echo $ll_x $jx $del_x | awk '{print $1 + ($2 - 1) * $3}'`
set y = `echo $ll_y $iy $del_y | awk '{print $1 + ($2 - 1) * $3}'`
set txt = `echo $x $y $del_x $del_y | awk '{print $1, $2, $1, $2 + $4, $1 + $3, $2 + $4, $1 + $3, $2, $1, $2}'`
shpadd out `echo $txt`
dbfadd out $n
@ jx++
end ### close the second loop
@ iy++
end ### close the first loop
echo
### lines below create a projection file for the created shapefile using
cat > out.prj << eof
PROJCS["Asia_Lambert_Conformal_Conic",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Lambert_Conformal_Conic"],PARAMETER["False_Easting",0.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",120.98],PARAMETER["Standard_Parallel_1",5.0],PARAMETER["Standard_Parallel_2",20.0],PARAMETER["Latitude_Of_Origin",14.59998],UNIT["Meter",1.0]]
eof
###
###
###
内部执行了540,000次,每次迭代您调用3个awk
进程进行3个简单的数学运算……这是160万个awks。
而不是,我编写了一个awk来生成所有循环并进行所有数学运算,然后可以将其馈送到bash
或csh
中以实际执行它。
我编写了此文件,并在原始版本通过16%的时间内完全运行了它。我尚未对其进行彻底检查,但您应该可以随时更正任何小错误:
#!/bin/bash
awk -v njx=675 -v niy=800 -v ll_x=-337500 -v ll_y=-400000 '
BEGIN{
print "shpcreate out polygon"
print "dbfcreate out -n ID1 10 0"
n=0
for(iy=1;iy<niy;iy++){
for(jx=1;jx<njx;jx++){
n++
x = llx + (jx-1)*1000
y = lly + (iy-1)*1000
txt = sprintf("%d %d %d %d %d %d %d %d %d %d",x,y,x, y+dely, x+delx, y+dely, x+delx,y,x,y)
print "shpadd out",txt
print "dbfadd out",n
}
}
}' /dev/null
如果输出看起来不错,则可以像这样通过bash
或csh
运行它:
./MyAwk | csh
请注意,我对这些Shapefile(?)工具,shpadd
或dbfadd
工具一无所知。它们可能无法并行运行-如果它们像sqlite
那样并行运行,将无济于事。我想上面的更改足以对您的运行时进行重大改进。如果没有,那么您可以考虑以下其他事项。
您可以在以&
或dbfadd
开头的每一行中附加一个&符号(shpadd
),以便多行并行开始,然后每8行打印一次wait
,以便运行8件事并行并行。
[您可以将脚本的输出直接输入到GNU Parallel,但是我不知道行的顺序是否很关键。
我想这是在创建某种数据库。如果在RAM支持的文件系统(例如/tmp
)上运行它,则它[[may会更快。