#!/usr/bin/perl
#
use Statistics::OLS;
use Math::FFT;
use Math::Stat;
use Math::Spline qw(spline linsearch binsearch);
use Math::Derivative qw(Derivative2);
use Math::Approx;
use Astro::FITS::CFITSIO qw( :longnames :constants );
use PDL;
use PDL::Fit::Polynomial; 
use PDL::Filter::Linear;
use PGPLOT;  # Load PGPLOT module
use PDL::Fit::Gaussian;
use PDL::Slatec;
use PDL::Image2D;
#use PDL::Matrix;

#$F3D=$ENV{HIIEXPLORER_PATH};  $F3D=$ENV{HIIEXPLORER_PATH};  require("$F3D/my.pl"); #("$F3D/scripts/my.pl"); # "/work1/ssa/perl/MY/my.pl";
$F3D=$ENV{HIIEXPLORER_PATH};  $F3D=$ENV{HIIEXPLORER_PATH};  require("$F3D/my.pl"); #("$F3D/scripts/my.pl"); # "/home/sanchez/sda2/code/R3D/my.pl";


if ($#ARGV<2) {
    print "USE: recal_cube_map.pl CUBE.fits MAP.fits OUTPUTCUBE.fits\n";
    exit;
}

$infile=$ARGV[0];
$ratio_file=$ARGV[1];
$outfile=$ARGV[2];


$pdl=rfits($infile);
($nx,$ny,$nz)=$pdl->dims;
$h=$pdl->gethdr;

$ratio=rfits($ratio_file);
($Mnx,$Mny)=$ratio->dims;
if (($Mnx!=$nx)||($Mny!=$ny)) {
    print "Cube and map dimesions do not match\n";
    exit;
}
$pdl=$pdl*$ratio;
$pdl->sethdr($h);
$pdl->wfits($outfile);

exit;
