我有以下创建多个对象的脚本。
我尝试仅在终端中运行它,但是似乎花费了很长时间。如何使用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
请注意,我不知道这些shape文件(?)工具,任何shpadd
或dbfadd
工具。它们可能无法并行运行-如果它们像sqlite
并行运行那样对您没有多大帮助。我想上面的更改足以对您的运行时进行重大改进。如果没有,您可以考虑以下其他事项。
您可以&
在每个开始的行后面加上一个&符(dbfadd
或shpadd
使多个行并行开始),然后wait
每8行打印一次,以便您并行运行8个代码块。
您可以将脚本的输出直接输入到GNU Parallel中,但是我不知道行的顺序是否很关键。
我认为这是在创建某种数据库。如果在RAM支持的文件系统(例如)上运行它,可能会更快/tmp
。
我注意到有操纵形状文件一个Python模块这里。我忍不住想那还会快很多很多倍。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句