将数组传递给 GNU Parallel 以替换 for 循环

Pass in array to GNU Parallel to replace for loop

a) 我想运行 2 个脚本并行

b) 我想在这些脚本中并行执行我的 for 循环。

在我有这段代码之前:

for year in 2000 2001 2002 2003; do

  echo $year" LST data being merged"

  cd $base_data_dir/$year

  # this is the part that takes a long time
  cdo -f nc2 mergetime *.nc $output_dir/LST_$year.nc

done

我想使用 GNU Parallel 来尝试 运行 并行执行此操作。

我尝试了以下内容:

a) 创建一个 'controller' 脚本调用其他脚本

b) 将数组作为参数传递给 GNU parallel

控制器脚本

# 1. Create monthly LST for each year

cd $working_dir
seq 2000 2003 | parallel 'bash create_yearly_LST_files.sh {}'

# 2. Create monthly NDVI for each year

cd $working_dir
seq 2000 2003 | parallel 'bash create_yearly_NDVI_files.sh {}'

这应该是运行同时执行以下操作:

bash create_yearly_LST_files.sh 2000
bash create_yearly_LST_files.sh 2001
...

bash create_yearly_NDVI_files.sh 2000
bash create_yearly_NDVI_files.sh 2001
...

处理脚本(NDVI同)

year=""
echo $year" LST data being merged"
cd $base_data_dir/$year

cdo -f nc2 mergetime *.nc $output_dir/LST_$year.nc

因此命令应为:

cd $base_data_dir/2000
cdo -f nc2 mergetime *.nc $output_dir/LST_2000.nc

cd $base_data_dir/2001
cdo -f nc2 mergetime *.nc $output_dir/LST_2001.nc
...

cd $base_data_dir/2000
cdo -f nc2 mergetime *.nc $output_dir/NDVI_2000.nc

cd $base_data_dir/2001
cdo -f nc2 mergetime *.nc $output_dir/NDVI_2001.nc
...

我的问题:

这些进程在我的新代码中仍然有效,但性能没有提高。

任何人都可以帮助我了解如何通过每年并行 运行 吗?

还有 运行 两个脚本并行(create_yearly_LST_files.shcreate_yearly_NDVI_files.sh

是什么阻止了你做事

for year in 2000 2001 2002 2003; do

  echo $year" LST data being merged"

  cd $base_data_dir/$year

  # this is the part that takes a long time
  cdo -f nc2 mergetime *.nc $output_dir/LST_$year.nc &

done
wait

使用 GNU 并行:

cd $working_dir
parallel 'cd {}; cdo -f nc2 mergetime *.nc xxx/LST_{}.nc' ::: {2000..2003}

也许这会奏效:

doit() {
  cd "$base_data_dir"/""
  cdo -f nc2 mergetime *.nc "$output_dir"/_.nc"
}
export -f doit
export base_data_dir
export output_dir
parallel doit ::: {2000..2018} ::: LST NDVI