head	1.1;
access;
symbols;
locks
	jmj:1.1; strict;
comment	@# @;

1.1
date	98.08.25.20.19.50;	author jmj;	state Exp;
branches;
next	;

desc
@@

1.1
log
@Initial revision
@
text
@#!/packages/bin/perl 

#-------------------------------------------------
#
#   ep_summary_plots.cgi - LANL database web browser
#
#   Joerg-Micha Jahn, NIS-2, LANL, January 1998
#
#   This script allows browsing of the LANL database in the spirit
#   of the CCR data browser. Main difference to the CCR browser is the use of
#   the CGI.pm module to make forms processing easier, and of date5.pm for
#   elegant date calculations. 
#   
#   Modification History:
#
#   
#-------------------------------------------------

###   add location of modules to the path

BEGIN {push@@INC, "/n/toaster/u/jmj/public_html/cgi-bin/test/";} 

use CGI;
use date5; 

###   default values we will use

@@day_name = ("Sunday","Monday","Tuesday","Wednesday","Thursday",
		"Friday","Saturday");
@@month_name = ("January","February","March","April","May","June","July",
		  "August","September","October","November","December");
$plot_root_dir = '../../ep_summary_plots';
$a_one = 1; 

###   start of main script

$query = new CGI;       # create a new CGI object

###   first we need to scan the URL. The reason is that we need to identify
###   optional input parameters. Furthermore we need to deal with calls from
###   other browsers (namely CCR and MPA), since those will call with a date
###   using a '/' rather than a '?'

$caller_url = $query->self_url;  ###  this gets the complete(!) URL

if ($caller_url =~ /[\?|\/]/) {     ###  here we have a query style case - good
    @@keywords = $query->keywords; ### this gets the optional input

    if (($#keywords > -1) && ($keywords[0] =~ /[0-9]/)) {  # there was a input
	
	@@components = ();
	$i=0;
	while (1) { 
	    $temp = substr($keywords[0],$i,1); 
	    if ($temp =~ /[0-9]/) {push(@@components,$temp)} else {last;}
	    $i++;
	}
	if ($i == 5) {        ###  yyDDD
	    $year  = "$components[0]$components[1]";
	    if (int $year >= 76) {$year = "19$year";} else {$year = "20$year";}
	    $DOY = "$components[2]$components[3]$components[4]";
	    $jday = &Date5::jday($a_one,$a_one,$year);
	    ($month,$day,$year,$weekday) = &Date5::jdate($jday+$DOY-1);
	} elsif($i == 6) {    ###  yymmdd
	    $year  = "$components[0]$components[1]";
	    $month = "$components[2]$components[3]";
	    $day   = "$components[4]$components[5]";
	    if (int $year >= 76) {$year = "19$year";} else {$year = "20$year";}
	} elsif($i == 7) {    ###  yyyyDDD
	    $year  = "$components[0]$components[1]$components[2]$components[3]";
	    $DOY = "$components[4]$components[5]$components[6]";
	    $jday = &Date5::jday($a_one,$a_one,$year);
	    ($month,$day,$year,$weekday) = &Date5::jdate($jday+$DOY-1);
	} elsif($i == 8) {    ###  yyyymmdd
	    $year  = "$components[0]$components[1]$components[2]$components[3]";
	    $month = "$components[4]$components[5]";
	    $day   = "$components[6]$components[7]";
	}
	
	if ($month < 10) {$month = "0$month";}
	if ($day < 10) {$day = "0$day";}
	
	$query->param(-name=>'year', -values=>$year);
	$query->param(-name=>'month', -values=>$month);
	$query->param(-name=>'day', -values=>$day);
    }
    
    
    if (($#keywords > -1) && (($keywords[1] == 'Both') || 
			      ($keywords[1] == 'Electrons') ||
			      ($keywords[1] == 'Protons'))) {    
	$plot_type = $keywords[1];
	$query->param(-name=>'plot_type', -values=>$plot_type);
    }
    
}
### determine today's date - needed for some of the links (Dst, AE indices);
### this is reasonably fast, so we don't bother with passing those values via
### hidden fields (and save some code that way)

$today_julian_day =  &Date5::today();
($today_month,$today_day,$today_year,$today_weekday) = 
    &Date5::jdate($today_julian_day);
if ($today_month < 10) {$today_month = "0$today_month";}
if ($today_day < 10) {$today_day = "0$today_day";}
$today_date = "${today_year}${today_month}${today_day}";

### for the Dst index we need the info for the previous month; the
### corresponding year is called "prevous_year", though a bit confusing...
 
$previous_month = $today_month - 1;
$previous_year = $today_year;
if ($previous_month == 0) {
    $previous_month = 12;
    $previous_year--;
}
if ($previous_month < 10) {$previous_month = "0$previous_month";}

$query->import_names('Q');  ###   import name space to update form - we need
                            ###   this since the form is NOT updated when
                            ###   we use clickable image buttons or the "jump
                            ###   to latest" button.

if ($Q::year ne '') {       ###   this means there was user input ...

    ### calculate Julian day from input, and then date from this resulting
    ### julian day - this assures that senseless input (like Feb 31) will
    ### result in a sensible date - although not quite the one the user
    ### intended 

    $julian_day = &Date5::jday($Q::month,$Q::day,$Q::year);
    ($curr_month,$curr_day,$curr_year,$curr_weekday) = 
	&Date5::jdate($julian_day);

    ###  get the day of year (DOY) - THIS IS NOT FINAL YET

    $j_day_first = &Date5::jday($a_one,$a_one,$curr_year);
    $DOY = $julian_day - $j_day_first + 1;
    if ($DOY < 100) { $DOY = "0$DOY"; }
    if ($DOY < 10)  { $DOY = "0$DOY"; }

    ### save value of image buttons for further use

    $next_day = $query->param('next_day.x');
    $previous_day = $query->param('previous_day.x');

    ###   'jump to latest' or image button chosen. We need to pass parameters
    ###   since all "SELECT" fields were not changed for such a request

    if ($Q::latest) {
	opendir(DIR, "$plot_root_dir/"); 
        $check_jday = $today_julian_day;
        while (1) {
	    ($mm,$dd,$yy,$wd) = 
		&Date5::jdate($check_jday);
	    if ($mm < 10) {$mm = "0$mm";}
	    if ($dd < 10) {$dd = "0$dd";}
	    $check_date = "${yy}${mm}${dd}";	     
	    $e_filename = "$plot_root_dir/${check_date}_loe.gif";
	    if (-e $e_filename) {last;};
            $check_jday--;
	}
        $e_last_date = $check_date;
	opendir(DIR, "$plot_root_dir/"); 
        $check_jday = $today_julian_day;
        while (1) {
	    ($mm,$dd,$yy,$wd) = 
		&Date5::jdate($check_jday);
	    if ($mm < 10) {$mm = "0$mm";}
	    if ($dd < 10) {$dd = "0$dd";}
	    $check_date = "${yy}${mm}${dd}";	     
	    $p_filename = "$plot_root_dir/${check_date}_lop.gif";
	    if (-e $p_filename) {last;};
            $check_jday--;
	}
        $p_last_date = $check_date;

        if ($Q::plot_type eq 'Electrons') {
	    $latest_date = $e_last_date;
	} elsif ($Q::plot_type eq 'Protons') {
	    $latest_date = $p_last_date;
	} else {
	    if ($e_last_date >= $p_last_date) {
		$latest_date = $e_last_date;
	    } else {
		$latest_date = $p_last_date;
	    } 
	}
        $curr_year  = substr($latest_date,0,4);
        $curr_month = int substr($latest_date,4,2);
        $curr_day   = int substr($latest_date,6,2);
        $keywords[0] = '';
    }
    
    ### "next" and "previous" day buttons were pressed; calculate the new date

    if ($next_day) { 
	$julian_day++;     
	($curr_month,$curr_day,$curr_year,$curr_weekday) = 
	    &Date5::jdate($julian_day);
    }
    if ($previous_day) { 
	$julian_day--; 	
	($curr_month,$curr_day,$curr_year,$curr_weekday) = 
	    &Date5::jdate($julian_day);
    }

    ###   pass all parameters to object; needed to update form, since the form
    ###   is NOT updated when non-select fields are pressed (i.e. the image
    ###   buttons, and the "latest" field
    ###   NOTE: ONLY object parameters will be saved by the script!

    $query->param(-name=>'year', -values=>$curr_year);
    $query->param(-name=>'month', -values=>$curr_month);
    $query->param(-name=>'day', -values=>$curr_day);

    ###   assure 2-digit month/day - needed to construct file name
    
    if ($curr_month < 10) {$curr_month = "0$curr_month";}  
    if ($curr_day < 10) {$curr_day = "0$curr_day";}        
    $curr_date = "${curr_year}${curr_month}${curr_day}";
    $curr_short_date = substr($curr_date,2,6);

    ($month,$day,$year,$weekday) = &Date5::jdate($jday+$DOY-1);

    ### determine the name of the image file; NOTE: 4-digit years!!

    $e_image_filename = 
	"$plot_root_dir/$curr_year$curr_month${curr_day}_loe.gif";
    if (! -e $e_image_filename) {
	$e_image_filename = "/lanl_ep_data/images/no_electrons.gif";
    }
    $p_image_filename = 
	"$plot_root_dir/$curr_year$curr_month${curr_day}_lop.gif";
    if (! -e $p_image_filename) {
	$p_image_filename = "/lanl_ep_data/images/no_protons.gif";
    }

}  ### end of if (Q::year != '')

$query->import_names('Q');  ###   reimport UPDATED (!!) name space; needed
                            ###   to update form

$query->param(-name=>'year', -values=>$$Q::_year);
$query->param(-name=>'month', -values=>$$Q::_month);
$query->param(-name=>'day', -values=>$$Q::_day);
$query->param(-name=>'plot_type', -values=>$plot_type);

$query->import_names('Q');  ###   reimport UPDATED (!!) name space; needed

$julian_day = &Date5::jday($Q::month,$Q::day,$Q::year);
($curr_month,$curr_day,$curr_year,$curr_weekday) = 
    &Date5::jdate($julian_day);
if ($curr_month < 10) {$curr_month = "0$curr_month";}  
if ($curr_day < 10) {$curr_day = "0$curr_day";}        
$curr_date = "${curr_year}${curr_month}${curr_day}";
$curr_short_date = substr($curr_date,2,6);

###   start building the HTML document/form; the little redirect thing is
###   currently a clootch - but iot was the only way I could think o fright
###   now which allows to incorporate an updated (!) current date in the web
###   page address as it is shown by the browser

if (($keywords[0] eq '') && ($Q::year ne '')) {
    print $query->redirect("http://leadbelly.lanl.gov/lanl_ep_data/cgi-bin/ep_plot_choose_2.cgi?$curr_date+$Q::plot_type");
} else {
    print $query->header();
}
print $query->start_html(-title=>'CPA/SOPA Summary Plots');

#######   begin of form   #######

print $query->startform;

#print "$caller_url<BR>";

###  if script is called for the first time, print a special entry page

if ($Q::year eq '') {
    print qq{<H1>
	     <IMG SRC="../images/ep_halfhead.gif" BORDER="0" ALIGN="MIDDLE" 
	     ALT="CCR Logo"> Los Alamos Energetic Particle Summary Plots</H1><P>
             <H3> Choose a 24-hour summary plot type:<BR> </H3>};
    print qq{<H4>};
    print $query->radio_group(-name=>'plot_type',
			      -linebreak=>'true',
			      -values=>['Electrons', 'Protons', 'Both'],
			      -default=>'Both',
			      -labels=>{'Electrons'=>' Electrons: 
                                                      50-315 keV (SOPA) or 
                                                      30-300 keV (CPA)',
				        'Protons'=>' Protons: 50-400 
                                                    keV (SOPA) or approx. 
                                                    100-600 keV (CPA)',
				        'Both'=>' Both Electrons and 
                                                 Protons'});

    print qq{</H4>};

    print "<BR><H3>Choose a date and submit your request: </H3>";
    print qq{<H4>};
} else {   ###  print the following for all subsequent pages
    print "<CENTER>";
    print "<H2>$day_name[$curr_weekday], $month_name[$Q::month-1] $Q::day, ",
          "$Q::year ($Q::month/$Q::day/$Q::year, $Q::year$DOY)</H2><P>";
    print $query->image_button(-name=>'previous_day',
			       -src=>'../images/left_arrow.gif',
			       -align=>'MIDDLE'), " ";        
    print $query->image_button(-name=>'next_day',
			       -src=>'../images/right_arrow.gif',
			       -align=>'MIDDLE'), "<P></CENTER>"; 
    ###   location of survey plot
    print qq{<CENTER>};
    if ($Q::plot_type ne 'Protons') {
        print qq{<A HREF="${e_image_filename}">
                 <IMG SRC="${e_image_filename}" BORDER="0" 
	          ALIGN="CENTER"    
	          ALT="CPA/SOPA electron data for $curr_year/$curr_month/$curr_day"></A>}; 
    }
    if ($Q::plot_type ne 'Electrons') {
        print qq{<A HREF="${p_image_filename}">
	         <IMG SRC="${p_image_filename}" BORDER="0" 
	          ALIGN="CENTER"    
	          ALT="CPA/SOPA proton data for $curr_year/$curr_month/$curr_day"></A>};
    }
    print qq{</CENTER>};
    print "<P>";

###  Input section, select date, plot type, etc.
    print qq{<H4>};
    print $query->popup_menu(-name=>'plot_type',
			     -values=>['Electrons', 'Protons', 'Both'],
			     -default=>'Both',
			     -labels=>{'Electrons'=>'CPA/SOPA Electrons 
                                                     (spin-averaged)',
			               'Protons'=>'CPA/SOPA Protons 
                                                   (spin-averaged)',
			               'Both'=>'CPA/SOPA Electrons and
                                                Protons (spin-averaged)'});
}

print $query->popup_menu(-name=>'month',
			 -values=>['1','2','3','4','5','6','7',
				   '8','9','10','11','12'],
			 -default=>'1',
			 -labels=>{'1'=>'January','2'=>'February',
				   '3'=>'March','4'=>'April',
				   '5'=>'May','6'=>'June','7'=>'July',
				   '8'=>'August','9'=>'September',
				   '10'=>'October','11'=>'November',
				   '12'=>'December'});
print $query->popup_menu(-name=>'day',
			 -values=>['1','2','3','4','5','6','7','8',
				   '9','10',
				   '11','12','13','14','15','16','17','18',
				   ,'19','20','21','22','23','24','25','26',
				   '27','28','29','30','31'],
			 -default=>'10');
print $query->popup_menu(-name=>'year',
			 -values=>['1976','1977','1978','1979',
				   '1980','1981','1982',
				   '1983','1984','1985','1986',
				   '1987','1988','1989',
				   '1990','1991','1992','1993',
				   '1994','1995','1996',
				   '1997','1998','1999','2000',
				   '2001','2002','2003','2004'],
			 -default=>'1997');
print  $query->submit('select_date','Submit');
print " or ";
print  $query->submit('latest','Jump to latest available plot'),
  "</H4><P>";

print $query->endform;

#######   end of form   #######  NOT end of HTML !!!!

### links at the bottom of the page

if ($Q::year eq '') {
    print qq{ <P>&quot;Low Energy&quot; particle summary plots can be 
              viewed and browsed on line. Use this form to choose the date you
              want to start on. After that each plot page has a 
              &quot;forward&quot; and &quot;back&quot; arrow and
              a &quot;choose date&quot; button that will bring you back to
              this page.  
              <BR>
              <A HREF="/lanl_ep_data/information/summary_plot_format.html">
              <h3>About the Plot Format</A></H3>};
    print qq{<H3 ALIGN=CENTER><HR>[
	      <A HREF="/lanl_ep_data/lanl_ep.html">EP Home</A> | 
              <A HREF="/lanl_ep_data/ep_request.html">Data Requests</A> |
              <A HREF="/lanl_ep_data/ep_projects.html">Campaigns & Projects</A> | 
              <A HREF="/lanl_ep_data/ep_information.html">Information</A>]
		  </H3>};
} else {
    print "<CENTER>";
    printf qq{<H3 ALIGN=LEFT>LANL Energetic Particles:
	      <A HREF="/lanl_ep_data/lanl_ep.html">
               Home Page</A> |
              <A HREF="/lanl_ep_data/information/summary_plot_format.html">
		 About the Plot Format</A> \| 
              <A HREF="/lanl_ep_data/ep_request.html">
                 Request Digital Data</A> </H3>};
 
    if (($curr_short_date + 0) >= 890922) {    
	printf qq{<H3 ALIGN=LEFT>Additional Data Sets: };
	if (($curr_date + 0) >= 19960228) {
	    printf qq{<A HREF="/ccr/cgi-bin/ccr_plot_choose.cgi/$curr_date/ips_spec">POLAR EP Data</A> |};
	}
	printf qq{<A HREF="http://nis-www.lanl.gov/nis-projects/mpa/cgi-bin/search.cgi/$curr_short_date/0/5/0/">LANL MPA Plots</A> </H3> };
    }

    ### determine proper Dst file from Japan
    printf qq{<H3 ALIGN=LEFT>Indices: };
    $final_Dst_flag = 0;
    if (($curr_year eq $today_year)&&($curr_month eq $today_month)){ 
	$Dst_id = "dstdir/dst1/q/Dstqthism.html";
    } elsif (($curr_year eq $previous_year)&&($curr_month eq $previous_month)){
	$Dst_id = "dstdir/dst1/q/Dstqlastm.html";
    } elsif (($curr_year + 1) >= ($today_year + 0)) {
	$Dst_id = "dstdir/dst1/p/dstprov${curr_year}${curr_month}.html";
    } else {
	$Dst_id = "dstdir/dst1/f/dstfinal${curr_year}${curr_month}.html";
	$final_Dst_flag = 1;
    }

    printf qq{<A HREF="http://swdcdb.kugi.kyoto-u.ac.jp/${Dst_id}"
	       TARGET="display">};
    if (!$final_Dst_flag) {printf qq{Provisional };} 
    printf qq{Dst</A>};

    printf qq{ | <A
	HREF="http://swdcdb.kugi.kyoto-u.ac.jp/cgi-bin/aeasy-cgi/${curr_date}">
	    AE-ASY/SYM</A>};
    if (($curr_date + 0) >= 19970101) {
	printf qq{ | <A
	HREF="http://swdcdb.kugi.kyoto-u.ac.jp/aedir/ae1/q/RAE.${curr_short_date}.html"
	TARGET="display">Quick-Look AE Plots</A> </H3> };
    }
    print qq{</CENTER>};

    printf qq{<HR>
              <CENTER><H2>Not for Publication or Public Presentation</H2>
              </CENTER></CENTER>

	      <b>These summary plots should <i>not</i> be used for publication.
              Higher quality plots which have been checked for errors are
              available on request.  The data presented here <i>do</i> have
	      known instrumental effects, "bugs" and "glitches" and are 
              provided};
    if ($Q::plot_type ne 'Electrons') {
        printf qq{"as is" 
        (see <A HREF="/lanl_ep_data/information/proton_caveats.html"> 
        Proton caveats</A>).\n}; 
    } else {
        printf qq{"as is". \n};
    }
    printf qq{Keeping the <A HREF="/lanl_ep_data/information/ep_team.html">
              LANL EP team</A>
              involved in your analysis will help all of us. 
              If you would like
              to request publishable plots or data or if you have questions
              about the data please contact:<br>
              <UL> 
                 <LI><A HREF="http://sst.lanl.gov/~belian/">Dick Belian</A> 
                 (<a href="mailto:rbelian\@@lanl.gov">rbelian\@@lanl.gov</A>) 
                 who is PI for the LANL EP instruments
              <BR>or
                 <LI><A HREF="/reeves.html">Geoff Reeves</A> 
                 (<a href="mailto:reeves\@@lanl.gov">reeves\@@lanl.gov</A>) 
                 who made these data available here and is responsible for 
                 LANL energetic particle analysis and this web site.
              </UL>};
}

###   adress information - this is always displayed

print qq{<HR> <ADDRESS>};
#print qq{<A HREF="http://leadbelly.lanl.gov/~jmj">
#         <IMG SRC="../images/header_tsmall.gif" WIDTH = "100"
#         BORDER="0" ALIGN="LEFT" ALT="J&ouml;rg-Micha Jahn"></A> 
#         Produced and Maintains the SEPS Data Browser.<BR>
#	     Last updated April 22, 1998.<BR CLEAR="ALL"></ADDRESS>};

if ($Q::year eq '') {
    print qq{<FONT SIZE="+1"><A HREF="http://leadbelly.lanl.gov/reeves.html">
	     <IMG SRC="../images/geoff_reeves.gif" WIDTH="106" HEIGHT="34" 
	      ALIGN="LEFT" ALT="Geoff Reeves" NATURALSIZEFLAG="0"></A> 
	      Produced and Maintains the LANL EP Web Pages.<BR>};
}
print qq{<CENTER><h4>Please report problems to
             <a href="mailto:jmj\@@lanl.gov">jmj\@@lanl.gov</a>. <BR>
	     </h4></CENTER><HR>};
print qq{Last updated August 03, 1998, by J.-M. Jahn
	 (<A HREF="mailto:jmj\@@lanl.gov">jmj\@@lanl.gov</A>)</ADDRESS></FORM>};

print $query->end_html;

#######   end of HTML, and of script   #######

#*******************************************************************

sub get_doy{
# first extract proper date in yyyymmdd
    $first_month = 1;
    $first_day   = 1;
    local($mm, $dd, $yy, $weekday) = &Date5::jdate($i);
    $j_day_first = &Date5::jday($first_month,$first_day,$yy);
    local($doy) = $i - $j_day_first + 1;
#    if ($doy < 100) { $doy = "0$doy"; }
#    if ($doy < 10)  { $doy = "0$doy"; }
    return $doy;
}

#*******************************************************************

@