
FILE="CM_garik7.csv"
declare -i i
i=0
while read line; do
i=i+1
 IFS=',' read -ra ADDR <<< "$line"
 URL=${ADDR[7]}
 DIR=${ADDR[0]}
 collection=${ADDR[1]}
 DIR="${DIR/ /}"
 collection="${collection/ /}"
 URL="${URL/ /}"

 if [ -e "$DIR" ]
 then
  echo "$DIR ...yes"
 else
  echo "$DIR ...no"
  mkdir $DIR
 fi
pth="$DIR/$collection"

 if [ -e "$pth" ]
 then
  echo "$pth ...yes"
 else
  echo "$pth ...no"
  mkdir $pth
 fi
dir_nm=$collection"_"$i
pth2="$DIR/$collection/$dir_nm"
 if [ -e "$pth2" ]
 then
  echo "$pth2 ...yes"
 else
  echo "$pth2 ...no"
  mkdir $pth2
 fi
filename=$dir_nm".fits"
pth3=$pth2"/"$filename
cd $pth2
echo $filename $URL
wget -O $filename -c $URL
cd ../../../

echo "$DIR,$pth2/$filename,$pth2,$filename,$line" >> list_fits_files_CM.csv
done < $FILE
