today=$(date)
echo
echo "    **** Date: $today ****    "
echo
echo "Let's check format files and its extension"
echo
for d in ../archives/*/;do
    echo
    echo "Checking folder $d"
    echo
    if [ "$(ls -A $d*zip)" ];then
        for f in $d*zip;do
            echo
            echo "-----------------------------"
            echo
            echo "Checking file $f"
            echo
            format="$(file -b $f)"
            if [[ ${format,,} == *"data"* ]] && [[ ! ${format,,} == *"zip"* ]];then
                echo
                echo "Seems that is a data file"
                echo "Let's check if it is a grib or a netcdf file"
                echo
                format2="$(cdo sinfo $f | grep "File format")"
                if [[ ${format2,,} == *"netcdf"* ]];then
                    echo
                    echo "File $f have NetCDF format"
                    echo
                    echo "Changing extension to NetCDF (.nc)"
                    mv $f "${f%.zip}.nc"
                    echo
                else
                    echo
                    echo "File $f NO NetCDF"
                    echo
                fi
                if [[ ${format2^^} == *"GRIB"* ]];then
                    echo
                    echo "File $f have GRIB format"
                    echo
                    echo "Changing extension to GRIB (.grib)"
                    mv $f "${f%.zip}.grib"
                    echo
                else
                    echo
                    echo "File $f NO GRIB"
                    echo
                fi
            else
                echo
                echo "File $f is not data type"
                echo
                if [ "$(unzip -l $f)" ];then
                    echo
                    echo "File $f is a ZIP file"
                    echo
                    echo "$(unzip -l $f)"
                    echo
                else
                    echo
                    echo "UNKOWN"
                    echo
                fi
            fi
        done
    else
        echo
        echo "NO ZIP files in the folder"
        echo
    fi
    echo
    echo "*************************************************************************"
    echo
done
