在 bash 脚本中使用 for 循环遍历 csv 文件中的扩展名以从 NASA 下载数据

Using a for loop in a bash scrip to iterate over extentions held in a csv file to download data from NASA

我目前正在尝试从 (https://ladsweb.modaps.eosdis.nasa.gov/search/order). They provide a bash file (https://ladsweb.modaps.eosdis.nasa.gov/tools-and-services/data-download-scripts/) 下载数据,我需要对其进行尝试和编辑。我有一个 .csv 文件,其中包含我需要下载的所有文件扩展名(具体来说,我需要从 2015 年到现在的所有中国 VNP46A1 数据。

我想以伪代码形式添加以下内容:

FOR url_path IN url_list:
recurse "https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/5000/VNP46A1/“+”$url_path"+”.h5" “your_directory”+”$url_path" "TOKEN_HERE"

我需要编辑此 bash 以遍历 csv 中的文件并将它们下载到文件夹中以备后用。

bash文件如下:

#!/bin/bash

function usage {
  echo "Usage:"
  echo "  [=12=] [options]"
  echo ""
  echo "Description:"
  echo "  This script will recursively download all files if they don't exist"
  echo "  from a LAADS URL and stores them to the specified path"
  echo ""
  echo "Options:"
  echo "    -s|--source [URL]         Recursively download files at [URL]"
  echo "    -d|--destination [path]   Store directory structure to [path]"
  echo "    -t|--token [token]        Use app token [token] to authenticate"
  echo ""
  echo "Dependencies:"
  echo "  Requires 'jq' which is available as a standalone executable from"
  echo "  https://stedolan.github.io/jq/download/"
}

function recurse {
  local src=
  local dest=
  local token=

  echo "Querying ${src}.json"

  for dir in $(curl -s -H "Authorization: Bearer ${token}" ${src}.json | jq '.[] | select(.size==0) | .name' | tr -d '"')
  do
    echo "Creating ${dest}/${dir}"
    mkdir -p "${dest}/${dir}"
    echo "Recursing ${src}/${dir}/ for ${dest}/${dir}"
    recurse "${src}/${dir}/" "${dest}/${dir}"
  done

  for file in $(curl -s -H "Authorization: Bearer ${token}" ${src}.json | jq '.[] | select(.size!=0) | .name' | tr -d '"')
  do
    if [ ! -f ${dest}/${file} ] 
    then
      echo "Downloading $file to ${dest}"
      # replace '-s' with '-#' below for download progress bars
      curl -s -H "Authorization: Bearer ${token}" ${src}/${file} -o ${dest}/${file}
    else
      echo "Skipping $file ..."
    fi
  done
}

POSITIONAL=()
while [[ $# -gt 0 ]]
do
  key=""

  case $key in
    -s|--source)
    src=""
    shift # past argument
    shift # past value
    ;;
    -d|--destination)
    dest=""
    shift # past argument
    shift # past value
    ;;
    -t|--token)
    token=""
    shift # past argument
    shift # past value
    ;;
    *)    # unknown option
    POSITIONAL+=("") # save it in an array for later
    shift # past argument
    ;;
  esac
done

if [ -z ${src+x} ]
then 
  echo "Source is not specified"
  usage
  exit 1
fi

if [ -z ${dest+x} ]
then 
  echo "Destination is not specified"
  usage
  exit 1
fi

if [ -z ${token+x} ]
then 
  echo "Token is not specified"
  usage
  exit 1
fi

recurse "$src" "$dest" "$token"

一个缩短的(用于测试目的)csv 文件如下:

/archive/allData/5000/VNP46A1/2015/001/VNP46A1.A2015001.h30v05.001.2019135185504.h5
/archive/allData/5000/VNP46A1/2015/002/VNP46A1.A2015002.h30v05.001.2019136091632.h5
/archive/allData/5000/VNP46A1/2015/003/VNP46A1.A2015003.h30v05.001.2019136075625.h5
/archive/allData/5000/VNP46A1/2015/004/VNP46A1.A2015004.h30v05.001.2019136081706.h5
/archive/allData/5000/VNP46A1/2015/005/VNP46A1.A2015005.h30v05.001.2019136084155.h5
/archive/allData/5000/VNP46A1/2015/006/VNP46A1.A2015006.h30v05.001.2019136084128.h5
/archive/allData/5000/VNP46A1/2015/007/VNP46A1.A2015007.h30v05.001.2019136085336.h5
/archive/allData/5000/VNP46A1/2015/008/VNP46A1.A2015008.h30v05.001.2019136103147.h5
/archive/allData/5000/VNP46A1/2015/009/VNP46A1.A2015009.h30v05.001.2019136100110.h5

如有任何帮助或建议,我们将不胜感激。

亲切的问候

您想使用 NASA 提供的脚本创建一个 bash 脚本,该脚本将遍历每一行以下载您需要的数据。

例如,假设下面的脚本是一个名为 save-data.sh:

的文件
#!/bin/bash
while read p; do
    ./laads-data-download.sh -s $P -d "destination" -t "token"
    echo "$p"
done <paths.txt

示例树结构:

nasa-satellite-data
├── laads-data-download.sh
├── paths.txt
└── save-data.sh