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

标签 bash parallel-processing netcdf gnu-parallel

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 的参数

Controller 脚本

# 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="$1"
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)

最佳答案

使用 GNU Parallel:

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

关于bash - 将数组传递给 GNU Parallel 以替换 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50932321/

相关文章:

linux - 创建服务启动

bash - 如何使用双列映射文件将与第二个文件中的一列关联的数据分配给另一个文件中的另一列

regex - 解析 .ssh/config 以获取代理信息

python - 异步处理数组中的多个文件 - Python

python - 如何确保 netcdf 文件在 python 中关闭?

json 与 jq 重映射

c++ - 并行算法寻找K个最近点

python - 如何使用 joblib Python 返回矩阵?

r - netcdf 文件中的变量出现翻转

python - 使用 CDO 计算 ERA5 每日总降水量