package main

import (
	"os"
	"path/filepath"

	"github.com/airbusgeo/godal"
	"github.com/sirupsen/logrus"
	"github.com/spf13/cobra"
	"starwiz.cn/sjy01/image-proc/pkg/config"
	"starwiz.cn/sjy01/image-proc/pkg/dem"
	"starwiz.cn/sjy01/image-proc/pkg/producer"
)

var (
	warpIn   string
	warpOut  string
	warpMeta string
	warpRPC  string
	warpDEM  string
)

var cmdWarp = &cobra.Command{
	Use:   "warp",
	Short: "L1A to L2: transform the input image using rpc",
	Run: func(cmd *cobra.Command, args []string) {
		godal.RegisterAll()
		dem.Dem1KmLT = dem.NewDem1Km(config.GCONFIG.Dem.Dem1Km)
		warpDEM, _ = filepath.Abs(config.GCONFIG.Dem.Dem1Km)
		err := producer.L1AtoL2(warpIn, warpOut, warpMeta, warpRPC, warpDEM)
		if err != nil {
			logrus.Error("Generate L2 product failed:", err)
			os.Exit(1)
		}
	},
}

func init() {
	rootCmd.AddCommand(cmdWarp)

	cmdWarp.Flags().StringVarP(&warpIn, "input", "i", "", "input image file")
	cmdWarp.Flags().StringVarP(&warpOut, "output", "o", "", "output image file")
	cmdWarp.Flags().StringVarP(&warpMeta, "meta", "m", "", "meta file")
	cmdWarp.Flags().StringVarP(&warpRPC, "rpb", "r", "", "rpb file")
	cmdWarp.Flags().StringVarP(&warpDEM, "dem", "d", "", "dem file")
}
