#!/usr/bin/perl

use lib "$ENV{'COSMOS_HOME'}/lib/perl";
use lib "$ENV{'COSMOS_HOME'}/lib/perl/i386";
use Tk;

$imacs=$ldss2=$ldss3=$short=0;
$instrument=$camera="";
rset();
$ready=0;

%modeval =
    ('DIRECT'             => 'DIRECT',
     'SPECTROSCOPIC'      => 'SPEC',
     'LONG f/4'           => 'LONG',
     'SHORT f/2'          => 'SHORT');
%graval =
    ('150 l'              => 'IMACS_150A',
     '300 l'              => 'IMACS_300A',
     '600 l'              => 'IMACS_600A',
     '1200 l'             => 'IMACS_1200A',
     'MOE'                => 'MOE');
%grival =
    ('150 l'              => 'IMACS_grism_150',
     '200 l'              => 'IMACS_grism_200',
     '300 l'              => 'IMACS_grism_300',
     '300R l'              => 'IMACS_grism_300R',
     '600 l'              => 'IMACS_grism_600');
%lgrval =
    ('low'                => 'LDSS3_low',
     'med blue'           => 'LDSS3_med_blue',
     'med red'            => 'LDSS3_med_red',
     'high blue'          => 'LDSS3_high_blue',
     'high red'           => 'LDSS3_high_red',
     'VPH red'            => 'LDSS3_VPHred',
     'VPH blue'           => 'LDSS3_VPHblue',
     'VPH all'            => 'LDSS3_VPHall');
%label =
    ('DIRECT'             => 'DIRECT',
     'SPEC'               => 'SPECTROSCOPIC',
     'SHORT'              => 'SHORT f/2',
     'LONG'               => 'LONG f/4',
     'IMACS_150A'         => '150 l',
     'IMACS_300A'         => '300 l',
     'IMACS_600A'         => '600 l',
     'IMACS_1200A'        => '1200 l',
     'MOE'                => 'MOE',
#     'MOE2'               => 'MOE2',
     'IMACS_grism_150'    => '150 l',
     'IMACS_grism_200'    => '200 l',
     'IMACS_grism_300'    => '300 l',
     'IMACS_grism_300R'    => '300R l',
     'IMACS_grism_600'    => '600 l',
     'IMACS_grism_direct' => '',
     'IMACS_direct'       => '',
     'LDSS_direct_grsm'   => '',
     'LDSS_low'           => 'low',
     'LDSS_med_blue'      => 'med blue',
     'LDSS_med_red'       => 'med red',
     'LDSS_high_blue'     => 'high blue',
     'LDSS_high_red'      => 'high red',
     'LDSS3_low'          => 'low',
     'LDSS3_med_blue'     => 'med blue',
     'LDSS3_med_red'      => 'med red',
     'LDSS3_high_blue'    => 'high blue',
     'LDSS3_high_red'     => 'high red',
     'LDSS3_VPHred'       => 'VPH red',
     'LDSS3_VPHblue'      => 'VPH blue',
     'LDSS3_VPHall'       => 'VPH all');

# Main window

$mw=MainWindow->new;
$mw->title('defineobs');

$mw->configure(-background=>'AntiqueWhite3');
$action=1;
$myfont=$mw->fontCreate(
                        -family => 'helvetica',
                        -weight => 'bold',
                        -size   => 12);

#File frame

$filFrame=$mw->Frame(-background=>'AntiqueWhite3')->pack(
                                                         -pady=>15,
                                                         -side=>bottom);

$filFrame->Label(
                 -background=>'AntiqueWhite3',
                 -font=>$myfont,
                 -text=>'Observation Definition File  ')->pack(
                                                              -pady=>1,
                                                              -padx=>10,
                                                              -side=>top);

$filFrame->Entry(	
                  -background=>'AntiqueWhite3',
                  -font=>$myfont,
                  -textvariable=>\$obsfile,
                  -width=>20,
                  -relief=>'sunken')->pack(
                                           -pady=>5,
                                           -side=>top);

$OpenButton=$filFrame->Button(
                              -background=>'AntiqueWhite3',
                              -text=>'Open',
                              -font=>$myfont,
                              -command=>\&openf)->pack(
                                                       -pady=>10,-padx=>10,
                                                       -side=>left);

$SaveButton=$filFrame->Button(
                              -background=>'AntiqueWhite3',
                              -text=>'Save',
                              -font=>$myfont,
                              -command=>\&savef)->pack(
                                                       -pady=>10,-padx=>10,
                                                       -side=>left);

$QuitButton=$filFrame->Button(
                              -background=>'AntiqueWhite3',
                              -text=>'Quit',
                              -font=>$myfont,
                              -command=>sub{exit})->pack(
                                                         -pady=>10,-padx=>10,
                                                         -side=>left);

#Date frame

$dateFrame=$mw->Frame(-background=>'AntiqueWhite3',
                      -relief=>'groove',
                      -borderwidth=>2)->pack(
                                             -fill=>'x',
                                             -pady=>1,
                                             -side=>top);
$dateFrame->Label(
                  -background=>'AntiqueWhite3',
                  -font=>$myfont,
                  -text=>'Observation Date    ')->pack(
                                                                  -pady=>1,
                                                                  -padx=>1,
                                                                  -side=>left);
$dateBox1=$dateFrame->Optionmenu(-background=>'AntiqueWhite3',
                                 -font=>$myfont,
                                 -options=>[["Jan",1],["Feb",2],["Mar",3],
                                   ["Apr",4],["May",5],["Jun",6],
                                   ["Jul",7],["Aug",8],["Sep",9],
                                   ["Oct",10],["Nov",11],["Dec",12]],                              
                                 -textvariable=>\$mon,
                                 -variable=>\$month,   
                                 -width=>2,
                                 -relief=>'sunken')->pack(
                                                          -pady=>7,,
                                                          -side=>left);

$dateBox2=$dateFrame->Optionmenu(	
                              -background=>'AntiqueWhite3',
                              -font=>$myfont,
                              -options=>[2003,2004,2005,2006,2007,2008,2009,
                                         2010,2011,2012],
                              -textvariable=>\$year,
                              -width=>4,
                              -relief=>'sunken')->pack(
                                                       -pady=>7,,
                                                       -side=>left);


#Instrument frame

$instFrame=$mw->Frame(-background=>'AntiqueWhite3',
                      -relief=>'groove',
                      -borderwidth=>2)->pack(-pady=>1,
                                             -fill=>x,
                                             -side=>top);
$instFrame->Label(-background=>'AntiqueWhite3',
                  -font=>$myfont,
                  -text=>'Instrument           ')->pack(-pady=>1,
                                                             -padx=>10,
                                                             -side=>left);

$instBox=$instFrame->Optionmenu(  -command => \&setinstr,
                                  -background=>'AntiqueWhite3',
                                  -foreground=>'AntiqueWhite3',
                                  -font=>$myfont,
                                  -options=>["IMACS","LDSS2","LDSS3"],
                                  -textvariable=>\$instrument,
                                  -width=>4,
                                  -relief=>'sunken')->pack(
                                                           -pady=>7,,
                                                           -side=>left);
$instBox->bind("<Button-1>",sub{$instBox->configure(-foreground=>"black")});

#Mask frame

$maskFrame=$mw->Frame(-background=>'AntiqueWhite3',
                      -relief=>'groove',
                      -borderwidth=>2)->pack(
                                             -fill=>'x',
                                             -pady=>1,
                                             -side=>top);
$maskFrame->Label(
                  -background=>'AntiqueWhite3',
                  -font=>$myfont,
                  -text=>'Mask                ')->pack(
                                         -pady=>1,
                                         -padx=>10,
                                         -side=>left);

$maskBox=$maskFrame->Entry(	
                            -background=>'AntiqueWhite3',
                            -font=>$myfont,
                            -textvariable=>\$obspar{MASK},
                            -width=>26,
                            -relief=>'sunken')->pack(
                                                     -padx=>20,
                                                     -pady=>7,
                                                     -side=>left);


#Dewoff frame

$offFrame=$mw->Frame(-background=>'AntiqueWhite3',
                     -relief=>'groove',
                     -borderwidth=>2)->pack(
                                            -expand=>1,
                                            -fill=>x,
                                            -pady=>1,
                                            -side=>top);
$offFrame->Label(
                 -background=>'AntiqueWhite3',
                 -font=>$myfont,
                 -text=>'Dewar Offset File  ')->pack(
                                                     -pady=>1,
                                                     -padx=>7,
                                                     -side=>left);
$offBox=$offFrame->Entry(	
                          -background=>'AntiqueWhite3',
                          -font=>$myfont,
                          -textvariable=>\$obspar{DEWOFF},
                          -width=>26,
                          -relief=>'sunken')->pack(
                                                   -pady=>7,
                                                   -side=>left);

############################### Camera frame ##################################

$camFrame=$mw->Frame(-background=>'AntiqueWhite3',
                     -relief=>'groove',
                     -borderwidth=>2)->pack(
                                            -pady=>1,
                                            -fill=>x,
                                            -side=>top);

$caml=$camFrame->Label(-background=>'AntiqueWhite3',
                       -foreground=>$camcolor,
                       -font=>$myfont,
                       -text=>'Camera                 ')->pack(-pady=>1,
                                                                 -padx=>10,
                                                                 -side=>left);

#$cff=$camFrame->Frame(-background=>'AntiqueWhite3',
#                      -width=>70)->pack(-side=>right,
#                                        -fill=>x,
#                                        -expand=>1,
#                                        -padx=>0);

$camBox=$camFrame->Optionmenu(-background=>'AntiqueWhite3',
                              -foreground=>$camcolor,
                              -command=>\&set_camera,
                              -font=>$myfont,
                              -options=>["SHORT f/2","LONG f/4"],                              
                              -textvariable=>\$camera,
                              -width=>10,
                              -relief=>'sunken')->pack(
                                                       -pady=>7,,
                                                       -side=>left);
#$camBox->configure(-state=>"disabled");

############################### Mode frame ####################################

$modFrame=$mw->Frame(-background=>'AntiqueWhite3',
                     -relief=>'groove',
                     -borderwidth=>2)->pack(
                                            -pady=>1,
                                            -fill=>x,
                                            -side=>top);
$modFrame->Label(
                 -background=>'AntiqueWhite3',
                 -font=>$myfont,
                 -text=>'Mode                    ')->pack(
                                                  -pady=>1,
                                                  -padx=>10,
                                                  -side=>left);

#$mff=$modFrame->Frame(-background=>'AntiqueWhite3',
#                      -borderwidth=>1,
#                      -width=>20)->pack(
#                                        -side=>right,
#                                        -padx=>0);

$modBox=$modFrame->Optionmenu(	-command => \&set_mode,
                                -background=>'AntiqueWhite3',
                                -foreground=>'AntiqueWhite3',
                                -font=>$myfont,
                                -options=>["DIRECT","SPECTROSCOPIC"],
                                -textvariable=>\$mode,
                                -width=>17,
                                -relief=>'sunken')->pack(
                                                         -pady=>7,,
                                                         -side=>left);
    
$modBox->bind("<Button-1>",sub{$modBox->configure(-foreground=>"black")});


########################### Grating frame #####################################
							
$graFrame=$mw->Frame(-background=>'AntiqueWhite3',
                     -relief=>'groove',
                     -borderwidth=>2)->pack(-pady=>1,
                                            -fill=>x,
                                            -side=>top);

$grl=$graFrame->Label(-background=>'AntiqueWhite3',
                      -foreground=>$grcolor,
                      -font=>$myfont,
                      -text=>$grlabel)->pack(-pady=>1,
                                             -padx=>10,
                                             -     side=>left);

$gff=$graFrame->Frame(-background=>'AntiqueWhite3',
                      -width=>130,
                      -borderwidth=>0)->pack(-side=>right,
                                             -padx=>0);

$gnsl=$gff->Label(-background=>'AntiqueWhite3',
                  -foreground=>$grcolor,
                  -font=>$myfont,
                  -text=>"       N&S")->pack(-pady=>1,
                                      -padx=>10,
                                      -side=>left);

$gnsBut=$gff->Checkbutton(
                  -background=>'AntiqueWhite3',
                  -highlightthickness=>0,
                  -variable=>\$NS)->pack(
                                         -pady=>1,-side=>left);
#$gnsBut->configure(-state=>"disabled");

$graBox=$graFrame->Entry(-background=>'AntiqueWhite3',
                         -foreground=>$grcolor,
                         -font=>$myfont,
                         -textvariable=>\$grating,
                         -width=>12,
                         -relief=>'sunken')->pack(-pady=>7,
                                                  -padx=>0,
                                                  -side=>right);
#$graBox->configure(-state=>"disabled");

$griMenu=$graFrame->Menu(-background=>'AntiqueWhite3',
                         -foreground=>$grcolor,
                         -activeforeground=>$grcolor,
                         -font=>$myfont,
                         -tearoff=>0,
                         -menuitems=>[['command'=> "150 l",
                           -command=>sub{$grating="150 l"}],
                           ['command'=> "200 l",
                             -command=>sub{$grating="200 l"}],
                           ['command'=> "300 l",
                             -command=>sub{$grating="300 l"}],
                           ['command'=> "300R l",
                             -command=>sub{$grating="300R l"}],
                           ['command'=> "600 l",
                             -command=>sub{$grating="600 l"}]]);

$lgroldMenu=$graFrame->Menu(-background=>'AntiqueWhite3',
                            -foreground=>$grcolor,
                            -activeforeground=>$grcolor,
                            -font=>$myfont,
                            -tearoff=>0,
                            -menuitems=>[['command'=> "low",
                                          -command=>sub{$grating="low"}],
                                         ['command'=> "med blue",
                                          -command=>sub{$grating="med blue"}],
                                         ['command'=> "med red",
                                          -command=>sub{$grating="med red"}],
                                         ['command'=> "high blue",
                                          -command=>sub{$grating="high blue"}],
                                         ['command'=> "high red",
                                          -command=>sub{$grating="high red"}]]);

$lgrnewMenu=$graFrame->Menu(-background=>'AntiqueWhite3',
                            -foreground=>$grcolor,
                            -activeforeground=>$grcolor,
                            -font=>$myfont,
                            -tearoff=>0,
                            -menuitems=>[['command'=> "med red",
                                          -command=>sub{$grating="med red"}],
                                         ['command'=> "VPH red",
                                          -command=>sub{$grating="VPH red"}],
                                         ['command'=> "VPH blue",
                                          -command=>sub{$grating="VPH blue"}], 
                                         ['command'=> "high blue",
                                          -command=>sub{$grating="high blue"}],
                                         ['command'=> "high red",
                                        -command=>sub{$grating="high red"}]]);


$graMenu=$graFrame->Menu(-background=>'AntiqueWhite3',
                         -foreground=>$grcolor,
                         -activeforeground=>$grcolor,
                         -font=>$myfont,
                         -tearoff=>0,
                         -menuitems=>[['command'=> "150 l",
                                      -command=>sub{$grating="150 l"}],
                                      ['command'=> "300 l",
                                       -command=>sub{$grating="300 l"}],
                                      ['command'=> "600 l",
                                       -command=>sub{$grating="600 l"}],
                                      ['command'=> "1200 l",
                                       -command=>sub{$grating="1200 l"}],
                                      ['command'=> "MOE",
                                       -command=>sub{$grating="MOE"}]]);


######################### Grating order frame #################################
							
$grordFrame=$mw->Frame(-background=>'AntiqueWhite3',
                       -relief=>'groove',
                       -borderwidth=>2)->pack(
                                              -pady=>1,
                                              -fill=>x,
                                              -side=>top);

$grord=$grordFrame->Label(-background=>'AntiqueWhite3',
                          -foreground=>$grcolor,
                          -font=>$myfont,
                          -text=>'Grating Order')->pack(-pady=>1,
                                                        -padx=>10,
                                                        -side=>left);

$gof=$grordFrame->Frame(-background=>'AntiqueWhite3',
                        -width=>190,
                        -borderwidth=>0)->pack(-side=>right,
                                               -padx=>0);

$grordBox=$grordFrame->Entry(-background=>'AntiqueWhite3',
                             -foreground=>$grcolor,
                             -font=>$myfont,
                             -width=>2,
                             -textvariable=>\$obspar{GR_ORDER})->pack(
                                                                  -pady=>7,
                                                                  -padx=>0,
                                                                  -side=>right);

#$grordBox->configure(-state=>"disabled");

######################### Grating angle frame #################################
							
$grangFrame=$mw->Frame(-background=>'AntiqueWhite3',
                       -relief=>'groove',
                       -borderwidth=>2)->pack(-pady=>1,
                                              -fill=>x,
                                              -side=>top);

$grang=$grangFrame->Label(-background=>'AntiqueWhite3',
                          -foreground=>$ancolor,
                          -font=>$myfont,
                          -text=>'Grating Angle')->pack(-pady=>1,
                                                        -padx=>10,
                                                        -side=>left);

$ganf=$grangFrame->Frame(-background=>'AntiqueWhite3',
                         -width=>165,
                         -borderwidth=>0)->pack(-side=>right,
                                                -padx=>0);

$grangBox=$grangFrame->Entry(-background=>'AntiqueWhite3',
                             -foreground=>$ancolor,
                             -font=>$myfont,
                             -textvariable=>\$obspar{GR_ANGLE},
                             -width=>5,
                             -relief=>'sunken')->pack(-pady=>7,
                                                      -padx=>0,
                                                      -side=>right);

#$grangBox->configure(-state=>"disabled");

######################### D_ALIGNROT frame #################################
							
$dalrotFrame=$mw->Frame(-background=>'AntiqueWhite3',
                       -relief=>'groove',
                       -borderwidth=>2)->pack(-pady=>1,
                                              -fill=>x,
                                              -side=>top);

$dalrot=$dalrotFrame->Label(-background=>'AntiqueWhite3',
                          -foreground=>$dalrotcolor,
                          -font=>$myfont,
                          -text=>'Disperser Misalignment')->pack(-pady=>1,
                                                        -padx=>10,
                                                        -side=>left);

$dalrotf=$dalrotFrame->Frame(-background=>'AntiqueWhite3',
                         -width=>165,
                         -borderwidth=>0)->pack(-side=>right,
                                                -padx=>0);

$dalrotBox=$dalrotFrame->Entry(-background=>'AntiqueWhite3',
                             -foreground=>$dalrotcolor,
                             -font=>$myfont,
                             -textvariable=>\$obspar{D_ALIGNROT},
                             -width=>5,
                             -relief=>'sunken')->pack(-pady=>7,
                                                      -padx=>0,
                                                      -side=>right);


#colorconf();
$ready=1;
$obsfile=shift;
if($obsfile ne ""){
  openf();}
MainLoop;



########################### Subroutines #######################################

sub openf{

  $obsfil=$obsfile.".obsdef";
  if(-e $obsfil){
    open(OBSF,"$obsfil");
    while(<OBSF>){
	    chop;
	    ($par,$value)=split;
	    $obspar{$par}=$value;}
    
    $NS=0;
    $month=1;
    $mon="Jan";
    $year=2003;
    if($obspar{DEWAR} eq "SITE2"){
      $month=9;
      $mon="Sep";
      $year=2005;}
    if($obspar{DEWAR} eq "E2V"){
      $month=4;
      $mon="Apr";
      $year=2008;}
    if($obspar{GRATING} eq "MOE2"){
      $month=12;
      $mon="Dec";
      $year=2005;}
    $instrument=$obspar{INSTRUMENT};
    setinstr();
    $mask=$obspar{MASK};
    $dewoff=$obspar{DEWOFF};
    $camera=$label{$obspar{CAMERA}};
    set_camera();
    $mode=$label{$obspar{MODE}};
    set_mode();
    $gra=$obspar{GRATING};
    $alignrot=$obspar{D_ALIGNROT};
    if($gra=~/_NS/){
      $NS=1;
      $gra=~s/_NS//;}
    if($gra=~/MOE2/){
      $gra=~s/MOE2/MOE/;}
    $grating=$label{$gra};
#    $alignrot=$obspar{D_ALIGNROT};
    close(OBSF);
    colorconf();}
  else{
    die("obsdef file $obsfile cannot be found\n");}
}

sub rset{
  $grlabel="";
  $grcolor="AntiqueWhite3";
  $ancolor="AntiqueWhite3";
  $camcolor="AntiqueWhite3";
  $modcolor="AntiqueWhite3";
  $dalrotcolor="AntiqueWhite3";
  $mode=$camera="";
  $grating=$grism="";
  $direct=$spec=0;}

sub setinstr{
  return(0) if(!$ready);
  $instBox->configure(-foreground=>"black");
  if($instrument eq "IMACS"){
    $imacs=1;
    $ldss2=0;
    $ldss3=0;
    rset();
    $camcolor="black";
    $camera="";
    $grating="";
    $camBox->configure(-state=>"normal");
    colorconf();}	
  if($instrument eq "LDSS2"){
    $ldss2=1;
    $ldss3=0;
    $imacs=0;
#    $camBox->configure(-state=>"disabled");
    rset();
    colorconf();}
  if($instrument eq "LDSS3"){
    $ldss3=1;
    $ldss2=0;
    $imacs=0;
#    $camBox->configure(-state=>"disabled");
    rset();
    colorconf();}
     }

sub set_grism{
  $spec=1;
  $grlabel="Grism";
  $ancolor="AntiqueWhite3";
  $dalrotcolor="black";
  $grcolor="black";
  $grating="";
  if($imacs){
    $graBox->bind("<Button-1>",sub{$griMenu->Popup(-popover=> $graBox)});}
  if($ldss2){
    $graBox->bind("<Button-1>",sub{$lgroldMenu->Popup(-popover=>$graBox)});}
  if($ldss3){
    $graBox->bind("<Button-1>",sub{$lgrnewMenu->Popup(-popover=>$graBox)});}
  colorconf();}

sub set_mode{
  if(!$ready){return(0);}
  $modBox->configure(-foreground=>"black");
  if($mode eq "SPECTROSCOPIC"){
    $grordBox->configure(-state=>"normal");
    $gnsBut->configure(-state=>"normal");
    $dalrotBox->configure(-state=>"normal");
    $spec=1;
    $grcolor="black";
    if($camera=~/LONG/ && $imacs){
      $graBox->configure(-state=>"normal");
      setspec();
      set_grating();}
    else{
      if($imacs || $ldss2 || $ldss3){
#        $graBox->configure(-state=>"disabled");
        setspec();
        set_grism();}
    }
  }
  elsif($mode eq "DIRECT"){
    $spec=0;
    $grlabel="Grism";
    $ancolor="AntiqueWhite3";
    $grcolor="AntiqueWhite3";
    $dalrotcolor="AntiqueWhite3";
    colorconf();
    $obspar{GR_ORDER}="";
#    $grordBox->configure(-state=>"disabled");
#    $graBox->configure(-state=>"disabled");
#    $gnsBut->configure(-state=>"disabled");
#    $grangBox->configure(-state=>"disabled");
    $graBox->bind("<Button-1>",""); 
  }
}

sub set_grating{
  $spec=1;
  $grlabel="Grating";
  $grcolor="black";
  $ancolor="black";
  $dalrotcolor="black";
  $grating="";
  $graBox->bind("<Button-1>",sub{$graMenu->Popup(-popover=> $graBox)});
  colorconf();}

sub set_camera{
  return(0) if(!$ready);
  if($camera eq "LONG f/4"){
    if($short){
      $obspar{GR_ORDER}="";
      $obspar{GR_ANGLE}="";
      $grating="";}
    $short=0;    
    if($spec){
      set_grating();}
  }
  if($camera eq "SHORT f/2"){
    if(!$short){$grating="";}
    $short=1;
    $ancolor="AntiqueWhite3";
    $dalrotcolor="black";
    if($spec){
      set_grism();}
  }
}
  
sub errmess{
$errmessage=shift;
  $nofile=$mw->Toplevel;
  $nofile->title('');
  $nofile->configure(-background=>'AntiqueWhite3');
  $nofile->Label(-text=>"$errmessage",
                 -font=>$myfont,
                 -background=>'AntiqueWhite3')->pack(
                                                     -padx=>20,
                                                     -pady=>20);
  $nofile->Button(
                  -background=>'AntiqueWhite3',
                  -relief=>'groove',
                  -text=>'OK',
                  -font=>$myfont,
                  -borderwidth=>5,
                  -command=>sub{$nofile->destroy})->pack(
                                                         -side=>'left',
                                                         -expand=>1,
                                                         -fill=>'none',
                                                         -pady=>10);}

sub setspec{
  if($imacs){
    if($short){
      $graBox->bind("<Button-1>",sub{$griMenu->Popup(-popover=> $graBox)});
      $gnsBut->configure(-state=>"normal");}
    else{
      $graBox->bind("<Button-1>",sub{$graMenu->Popup(-popover=> $graBox)});}
  }
  else{
    if($instrument eq "LDSS2"){
      $graBox->bind("<Button-1>",sub{$lgroldMenu->Popup(-popover=> $graBox)});}
    else{
      $graBox->bind("<Button-1>",sub{$lgrnewMenu->Popup(-popover=> $graBox)});}
  }
  $grcolor="black";
  if(!$short){
   $grangBox->configure(-state=>"normal");
   $ancolor="black";
   $dalrotcolor="AntiqueWhite3";
  }
  colorconf();}

sub setdir{
  $mode="DIRECT";
  $grating="";
  $obspar{GR_ORDER}="";
  $obspar{GR_ANGLE}="";
  $graBox->bind("<Button-1>",sub{$grcolor="AntiqueWhite3"});
  $grcolor="AntiqueWhite3";
  $ancolor="AntiqueWhite3";
  $dalrotcolor="AntiqueWhite3";
  colorconf();}

sub colorconf{
  $grl->configure(-text=>$grlabel) if(Exists($grl));
  $grl->configure(-foreground=>$grcolor)  if(Exists($grl));
  $caml->configure(-foreground=>$camcolor) if(Exists($caml));
  $camBox->configure(-foreground=>$camcolor) if(Exists($camBox));
  $graBox->configure(-foreground=>$grcolor) if(Exists($graBox));
  $graMenu->configure(-foreground=>$grcolor) if(Exists($graMenu));
  $griMenu->configure(-foreground=>$grcolor) if(Exists($griMenu));
  $lgroldMenu->configure(-foreground=>$grcolor) if(Exists($lgroldMenu));
  $lgrnewMenu->configure(-foreground=>$grcolor) if(Exists($lgrnewMenu));
  $grordMenu->configure(-foreground=>$grcolor) if(Exists($grordMenu));
  $grord->configure(-foreground=>$grcolor) if(Exists($grord));
  $grordBox->configure(-foreground=>$grcolor) if(Exists($grordBox));
  $gnsl->configure(-foreground=>$grcolor) if(Exists($gnsl));
  $grangBox->configure(-foreground=>$ancolor) if(Exists($grangBox));
  $grang->configure(-foreground=>$ancolor) if(Exists($grang));
  $dalrot->configure(-foreground=>$dalrotcolor) if(Exists($dalrot));
  $dalrotBox->configure(-foreground=>$dalrotcolor) if(Exists($dalrotBox));}
   
    
sub null{}

sub savef{

  $dateval=$year+$month/100.;
  $obspar{INSTRUMENT}=$instrument;
  if($instrument eq "IMACS"){
    $obspar{CAMERA}=$modeval{$camera};
    
    if($dateval<2005.08){
      $obspar{DEWAR}="SITE";}#before chip6 change
    else{
      $obspar{DEWAR}="SITE2";}#after chip6 change
      
    if($short && $dateval>2008.03){
      $obspar{DEWAR}="E2V";}#after new short dewar installed
      
    if($short){
      $obspar{GR_ANGLE}="0";
      if($mode eq "DIRECT"){
        $obspar{GR_ORDER}="0";

        $obspar{GRATING}="IMACS_direct_grism";}
      else{
        $obspar{GRATING}=$grival{$grating};
        if($dateval>=2007.05 && $NS){
          $obspar{GRATING}.="_NS";}#after N&S grism rotation implemented
      }
    }
    else{
      if($mode eq "DIRECT"){
        $obspar{GR_ORDER}="0";
        $obspar{GR_ANGLE}="0";
        $obspar{GRATING}="IMACS_direct";}
      else{
        $obspar{GRATING}=$graval{$grating};
        if($obspar{GRATING} eq "MOE" && $dateval>=2005.12){
          $obspar{GRATING}.="2";}
      }
    }
  }
  if($instrument eq "LDSS2"){
    $obspar{CAMERA}="";
    $obspar{INSTRUMENT}="LDSS2";
    $obspar{DEWAR}="LDSS2";
    $obspar{GR_ANGLE}="0";
    if($mode eq "DIRECT"){
	    $obspar{GR_ORDER}="0";
	    $obspar{GRATING}="LDSS_direct_grism";}
    else{
	    $obspar{GRATING}=$lgrval{$grating};
    }
  }
  if($instrument eq "LDSS3"){
    $obspar{CAMERA}="";
    $obspar{INSTRUMENT}="LDSS3";
    $obspar{DEWAR}="LDSS3";
    $obspar{GR_ANGLE}="0";
    if($mode eq "DIRECT"){
	    $obspar{GR_ORDER}="0";
	    $obspar{GRATING}="LDSS_direct_grism";}
    else{
	    $obspar{GRATING}=$lgrval{$grating};
    }
  }
#  if($alignrot eq ""){
#    $obspar{D_ALIGNROT}="0.0";
#  }
    
    $obspar{MASK}=~s/\.SMF//;
      $obspar{DEWOFF}=~s/\.dewoff//;
      $obspar{MODE}=$modeval{$mode};
    
    $obsfil=$obsfile.".obsdef";
    if(-e $obsfil){
      $obsbak=$obsfil."%";
      system "mv $obsfil $obsbak";}
    open(OBSF,">$obsfil");
    while(($key,$val)=each(%obspar)){
      printf OBSF "%-15s %s\n",$key,$val;}
    close(OBSF);}
  
