#!/usr/bin/perl # # MAKE1DSPEC constructs an empty 1dspec file to hold 1d spectra extracted # from a 1dspec file using the write command in viewspectra # # USAGE: make1dspec filename # # DATE: 01 Feb 06 use lib "$ENV{'COSMOS_HOME'}/lib/perl"; use lib "$ENV{'COSMOS_HOME'}/lib/perl/i386"; use PDL; use PDL::AutoLoader; use PDL::IO::Misc; use PDL::ImageND; use PDL::NiceSlice; use Astro::FITS::CFITSIO; ########################### default parameters ################################ $file=shift; $file=~s/_2spec//; $twodfile=$file."_2spec.fits"; $onedfile=$file."_1spec.fits"; # open 2dspec file if($fptr){$fptr->close_file($status);} if(!(-e $twodfile)){ die("file $twodfile cannot be found");} $status=0; $fptr=Astro::FITS::CFITSIO::open_file($twodfile,Astro::FITS::CFITSIO::READONLY (),$status); if($status){ die("Error $status opening file");} $fptr->get_img_parm($bitpix,undef,$naxes,$status); ($size,$wid,$npln)=@$naxes; $FOBJ=zeroes($size); if($npln != 2){ die("input file is not a 2dspec file");} $sz=$size-1; $status=0; $fptr->read_key(Astro::FITS::CFITSIO::TFLOAT(),"CRVAL1",$lambda0,undef, $status); if($status){ die("Error reading file $file header CRVAL1");} $fptr->read_key(Astro::FITS::CFITSIO::TFLOAT(),"CDELT1",$dlambda,undef, $status); if($status){ die("Error reading file $file header CDELT1");} $fptr->read_key(Astro::FITS::CFITSIO::TFLOAT(),"EXPTIME",$exptime,undef, $status); if($status){ die("Error reading file $file header EXPTIME");} $fptr->read_key(Astro::FITS::CFITSIO::TFLOAT(),"N_SLITS",$nspec,undef, $status); if($status){ die("Error reading file $file header N_SLITS");} $status=0; # create 1dspec filej $status=0; $optr=Astro::FITS::CFITSIO::create_file($onedfile,$status); if($status){ die("Error $status creating file $file");} # write blank spectra for($nobj=1;$nobj<=$nspec;$nobj++){ #read spectrum parameters $status=0; $fptr->movabs_hdu($nobj,undef,$status); if($status){ die("cannot find spectrum $nobj");} $fptr->get_img_parm($bitpix,undef,$naxes,$status); if($status){ die("Error reading spectrum $nobj parameters");} ($size,$wid,$npln)=@$naxes; $fptr->read_key(Astro::FITS::CFITSIO::TSTRING(),"OBJECT",$objname,undef, $status); $fptr->read_key(Astro::FITS::CFITSIO::TINT(),"SLITNUM",$slitnum,undef, $status); if($status){ die("Error reading spectrum $nobj parameters");} #write blank spectrum @naxes=[$size]; $optr->create_img(Astro::FITS::CFITSIO::FLOAT_IMG(),1,@naxes,$status); if($status){ die("Error $status creating image");} $optr->write_key(Astro::FITS::CFITSIO::TFLOAT(),"CRVAL1",$lambda0,undef, $status); $optr->write_key(Astro::FITS::CFITSIO::TFLOAT(),"CDELT1",$dlambda,undef, $status); $optr->write_key(Astro::FITS::CFITSIO::TSTRING(),"OBJECT",$objname,undef, $status); $optr->write_key(Astro::FITS::CFITSIO::TINT(),"SLITNUM",$slitnum,undef, $status); if($status){printf("Error $status writing file\n");} if($nobj==1){ $optr->write_key(Astro::FITS::CFITSIO::TINT(),"N_SLITS",$nspec,undef, $status); if($status){printf("Error $status writing file\n");} $optr->write_key(Astro::FITS::CFITSIO::TFLOAT(),"EXPTIME",$exptime,undef, $status); if($status){printf("Error $status writing file\n");} } $optr->write_pix(Astro::FITS::CFITSIO::TFLOAT(),[1,1],$size, $FOBJ->get_dataref,$status); if($status) {die("Error $status writing file");} } $optr->close_file($status);