#! /bin/csh -f
#
# ISIS2ENVI
# ---------
# Author: 
#     A. Lucas / lucas@ipgp.fr 
#    
# Contents: 
#    This c-shell script generates envi file with appropriate header from ISIS3 cube
#
# Requierments: 
#     ISIS3 need to be installed on the system (OS X,LINUX,FreeBSD) if cube is not converted in raw binary format. 
#     We assume little endian cpu
#
#
# Current version: available at http://www.ipgp.fr/~lucas/isis2envi.csh 
#
# License/Warranty:
#
#        This program is free software; you can redistribute it and/or modify
#        it under the terms of the GNU General Public License as published by
#        the Free Software Foundation; either version 2 of the License, or
#        (at your option) any later version.
#        
#        This program is distributed in the hope that it will be useful,
#        but WITHOUT ANY WARRANTY; without even the implied warranty of
#        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#        GNU General Public License for more details.
#        
#        You should have received a copy of the GNU General Public License
#        along with this program; if not, write to the Free Software
#        Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#        MA 02110-1301, USA.
#---------------------------------------------------------------------------------------
clear
echo "ISIS2ENVI"
if ($#argv != 1) then
   clear
   echo " "
   echo " Usage: $0 isiscube.cub"   
   goto done
endif
if (-e $1 ) then




set isiscube=$1
set base=`basename $isiscube .cub`
set new=$base.img
set envi_header=$new.hdr
set raw=$base.raw

#
if (-e $new ) then 
else

if (-e $raw ) then 
echo " "
echo "we change the raw extension into img."
/bin/mv $raw $new
else
clear
echo " "
echo "Using the isis2raw program, let's convert cube into binary file."
isis2raw from=$isiscube to=$raw
/bin/mv $raw $new
endif

endif

## Get header info with awk and grep unix commands
# data size and type
set idsample=`catlab from=$isiscube | grep "Samples ="`
set samples=`echo $idsample | awk -F"Samples =" '{print $3}' `
set idline=`catlab from=$isiscube | grep "Lines   ="`
set lines=`echo $idline | awk -F"Lines =" '{print $3}' `
set idband=`catlab from=$isiscube | grep "Bands   ="`
set bands=`echo $idband | awk -F"=" '{print $2}'`
set idtype=`catlab from=$isiscube | grep "Type"`
set type=`echo $idtype | awk -F"= " '{print $2}'`
set type=`echo $type | awk -F" " '{print $1}'`

#intrument and target
set ProductId=`catlab from=$isiscube | grep ProductId`
set InstrumentId=`catlab from=$isiscube | grep InstrumentId`
set TargetName=`catlab from=$isiscube | grep TargetName`
set Target=`echo $TargetName | awk -F"= " '{print $2}'`
set Target=`echo $Target | awk -F" " '{print $1}'`
set Instrument=`echo $InstrumentId | awk -F"= " '{print $2}' `
set Instrument=`echo $Instrument | awk -F" " '{print $1}' `
set bandcenter=`catlab from=$isiscube | grep -A1   "Center"`
set bandcenter=`echo $bandcenter | awk -F"= " '{print $2}'`
set bandcenter=`echo $bandcenter | awk -F"-- " '{print $1}'`
set bandcenter=`echo $bandcenter | tr -d '[a-z-A-Z=><)(] '`

#This line is here because in Envi, MArs ellipsoid is defined as Mars 2001. 
#For other body, user has to check the name used for ellipsoid 
if ($Target == "MARS") then
set ellips="Mars 2001"
else
set ellips=$Target
endif


#map projection
set MappingId=`catlab from=$isiscube | grep -c Mapping`
if ( $MappingId == 1)  then
set ansmap="Yes"
set ProjectionName=`catlab from=$isiscube | grep ProjectionName`
set ProjectionName=`echo $ProjectionName | awk -F"= " '{print $2}' `
set CenterLongitude=`catlab from=$isiscube | grep CenterLongitude`
set CenterLongitude=`echo $CenterLongitude | awk -F"= " '{print $2}' `
set CenterLatitude=`catlab from=$isiscube | grep CenterLatitude`
set CenterLatitude=`echo $CenterLatitude | awk -F"= " '{print $2}' `
set EquatorialRadius=`catlab from=$isiscube | grep EquatorialRadius`
set EquatorialRadius=`echo $EquatorialRadius | awk -F"= " '{print $2}' `
set EquatorialRadius=`echo $EquatorialRadius | awk -F"<" '{print $1}' `
set PolarRadius=`catlab from=$isiscube | grep PolarRadius`
set PolarRadius=`echo $PolarRadius | awk -F"= " '{print $2}' `
set PolarRadius=`echo $PolarRadius | awk -F"<" '{print $1}' `
set UpperLeftCornerX=`catlab from=$isiscube | grep UpperLeftCornerX`
set UpperLeftCornerX=`echo $UpperLeftCornerX | awk -F"= " '{print $2}' `
set UpperLeftCornerX=`echo $UpperLeftCornerX | awk -F"<" '{print $1}' `
set UpperLeftCornerY=`catlab from=$isiscube | grep UpperLeftCornerY`
set UpperLeftCornerY=`echo $UpperLeftCornerY | awk -F"= " '{print $2}' `
set UpperLeftCornerY=`echo $UpperLeftCornerY | awk -F"<" '{print $1}' `
set PixelResolution=`catlab from=$isiscube | grep PixelResolution`
set PixelResolution=`echo $PixelResolution | awk -F"=" '{print $2}' `
set PixelResolution=`echo $PixelResolution | awk -F"<" '{print $1}' `

set mapinfo="$ProjectionName, 1.0000, 1.0000,$UpperLeftCornerX,$UpperLeftCornerY,$PixelResolution,$PixelResolution,$ellips,"units=Meters""

#
if ($ProjectionName == "Sinusoidal") then 
set projnum=16
set projinfo="$projnum, $EquatorialRadius , $CenterLongitude, 0.0, 0.0, $ellips, $ProjectionName, "units=Meters""
endif
if ($ProjectionName == "Equirectangular") then 
set projnum=17
set projinfo="$projnum, $EquatorialRadius , $CenterLatitude,$CenterLongitude, 0.0, 0.0, $ellips, $ProjectionName, "units=Meters""
endif

if ($ProjectionName == "Mercator") then 
set projnum=20
set projinfo="$projnum, $PolarRadius, $EquatorialRadius , $CenterLatitude,$CenterLongitude, 0.0, 0.0, $ellips, $ProjectionName, "units=Meters""
endif

if ($ProjectionName == "Orthographic") then 
set projnum=14
set projinfo="$projnum, $EquatorialRadius , $CenterLatitude,$CenterLongitude, 0.0, 0.0, $ellips, $ProjectionName, "units=Meters""
endif


else
set ansmap="No"
set mapinfo=" "
set projinfo=" "
endif




#let's display info on the screen
clear
echo "informations: "
echo "------------------ "
echo "  Samples:" $samples 
echo "  lines : " $lines
if ( $bands != 1) echo "  Bands : " $bands 
echo "  Type : "  $type
if ( $Target != " ")  echo "  Target:"  $Target
if ( $Instrument != " ")  echo "  Instrument:" $Instrument
echo "  Projected file: "$ansmap
echo " "



#Here we convert data type into IDL/ENVI index for data type (eg., byte=1; float=4...)
if ($type == Real) then
set dtype=4
endif
if ($type == Byte) then
set dtype=1
endif
if (-e $user ) then 
else
set user=`whoami`
endif
#writing the envi header
echo "ENVI" > $envi_header
echo "description = {"  >> $envi_header
echo ""  >> $envi_header
echo " " $ProductId,         >> $envi_header
echo ""  >> $envi_header
echo " " $InstrumentId,      >> $envi_header
echo ""  >> $envi_header
echo "  TargetName= " $Target,        >> $envi_header
echo ""  >> $envi_header
echo "  File Imported from ISIS-3 into ENVI by "$user"}" >> $envi_header
echo "samples = "$samples             >> $envi_header
echo "lines = "$lines                 >> $envi_header
echo "bands = "$bands                 >> $envi_header
echo "header offset = 0"              >> $envi_header
echo "file type =  ENVI Standard"     >> $envi_header
echo "data type = " $dtype            >> $envi_header
echo "interleave = bsq"               >> $envi_header
echo "sensor type = "$Instrument      >> $envi_header
echo "byte order = 0"                 >> $envi_header
echo  " map info = {" $mapinfo "}"    >> $envi_header
echo "projection info = {" $projinfo "}" >> $envi_header
echo "wavelength units = Micrometers"    >> $envi_header
echo " wavelength = {"      >> $envi_header
echo  $bandcenter "}"       >> $envi_header


echo " "
echo "Files generated: " 
echo $new "(ENVI data file)"
echo $envi_header "(ENVI header file)"
echo " "


else
echo " "
echo "  Sorry, file does not exist"
endif
done:
  exit 0



