#!/usr/local/bin/perl # # ep_plot_choose_3.cgi # - perl script for LANL EP summary plot browser # This version makes heavier use of Dynamic HTML. # # Michael G. Henderson -- Mar 15, 2000. # # require "cgi-lib.pl"; require "ctime.pl"; $script = $ENV{'SCRIPT_NAME'}; $root_dir = $script; $root_dir =~ s+(.*)/.*$+\1+; $HTTP_REFERER = $ENV{'HTTP_REFERER'}; $PATH_INFO = $ENV{'PATH_INFO'}; $browser = $ENV{'HTTP_USER_AGENT'}; $server = $ENV{'SERVER_NAME'}; # # Check to see if we have Java2 with LiveConnect Enabled. # There really ought to be a better way than this! # (For now, this assumes that IE doesnt support it. Fix later.) # if (($browser =~ 'Netscape6') || ($browser =~ 'Gecko')) { $HaveJava2 = 1; $HaveSanePngSupport = 1;} elsif ($browser =~ 'MSIE 6') { $HaveJava2 = 1; $HaveSanePngSupport = 1;} else { $HaveJava2 = 0; $HaveSanePngSupport = 0;} # Check to see if this is running on avatar or not. if ($server =~ 'avatar') { $Avatar = 1; } else { $Avatar = 0; } # # Read in the plot description files to set all of the appropriate variables... # Set flag to include external plots. # $IncludeThisPlot = 1; require "ep_plot_descriptions.pl"; $remote = $ENV{"REMOTE_HOST"} || $ENV{"REMOTE_ADDR"}; if ($remote !~ /\./) { $remote .= ".lanl.gov"; } if (&ReadParse(*input)) { # # We are in here because there was cgi info to process # # print &PrintHeader, &PrintVariables(%input); $year = $input{"Year"}; $month = $input{"Month"}; $day = $input{"Day"}; $size = $input{"PlotSize"}; $EphemType = $input{"EphemType"}; $PlotType = $input{"PlotType"}; $Action2 = $input{"Action2"}; $date = $year*10000 + $month*100 + $day; # # some of the plot types have + and - in the name. "escapify" all such occurences # for doing the string comps # $p = $PlotType; if ($p eq "loe+lop") { $p = "loe" }; $p =~ s/\+/\\+/; $p =~ s/\-/\\-/; if ($Action2 =~ /Latest/){ # then jump to latest date available &FindLatest($p); $date = $latest_file; $date =~ s/^(\d{8})\S*/\1/; $year = $date; $month = $date; $day = $date; $year =~ s/(\d{4})(\d{2})(\d{2})/\1/; $month =~ s/(\d{4})(\d{2})(\d{2})/\2/; $day =~ s/(\d{4})(\d{2})(\d{2})/\3/; } # # paste the page up to the browser # if (IsValidDate($date)){ &DoPage($year, $month, $day, $PlotType, $size, $EphemType); } else { &WarnOfInvalidDate($date, $year, $month, $day); } } elsif ($PATH_INFO ne "") { # # The user has called the script with PATH_INFO extensions.... # interpret the path_info and process # $PATH_INFO =~ s/^\///; ($date, $PlotType, $size, $EphemType) = split("/", $PATH_INFO); if ($size eq ""){ $size = 2; } # # some of the plot types have + and - in the name. "escapify" all such occurences # for doing the string comps # $p = $PlotType; $p =~ s/\+/\\+/; $p =~ s/\-/\\-/; if ($date =~ /Latest/i){ # then jump to latest date available &FindLatest($p); printf qq| latest_file = $latest_file\n|; $date = $latest_file; $date =~ s/^(\d{8})\S*/\1/; $year = $date; $month = $date; $day = $date; $year =~ s/(\d{4})(\d{2})(\d{2})/\1/; $month =~ s/(\d{4})(\d{2})(\d{2})/\2/; $month += 0; $day =~ s/(\d{4})(\d{2})(\d{2})/\3/; $day += 0; $year = $year; } else { ($dow, $monstr, $year, $month, $day, $DOY) = Doy($date); # $year = int($date/10000); # $tmp = $date - $year*10000; # $month = int($tmp/100); # $day = $tmp - $month*100; } if (IsValidDate($date)){ &DoPage($year, $month, $day, $PlotType, $size, $EphemType); } else { &WarnOfInvalidDate($date, $year, $month, $day); } } else{ # # The user has just typed in the name of the script. # No info obtained either though stdin or via the PATH_INFO technique. # post defaults. # print &PrintHeader; printf qq|\n|; printf qq|\n|; printf qq|LANL Geosynchronous Energetic Particle Summary Plots\n|; printf qq|\n|; printf qq|\n|; if ($HaveSanePngSupport){ printf qq|\n\n\n|; printf qq|

\n|; } else { printf qq|\n\n\n|; printf qq|

\n|; } printf qq|
Welcome to the LANL Geosynchronous Energetic Particle Summary Plot Browser\n|; printf qq|(v3.0). This utility provides access to all of our pre-generated \"Low\n|; printf qq|Energy\" proton and electron summary plots. These plots show spin-averaged\n|; printf qq|fluxes from each satellite for each day since July 4, 1976. There is one plot\n|; printf qq|for electrons and one plot for protons.

\n|; # printf qq|Over this time period, 2 different instruments were used to acquire the data --\n|; # printf qq|satellites launched prior to 1988 all carried \"CPA\" detectors,\n|; # printf qq|while those launched after 1988 all carried \"SOPA\" detectors (post-1988\n|; # printf qq|sats also carry the MPA plasma instrument). You can easily determine which is which\n|; # printf qq|directly from the spacecraft name since the satellites are designated by their\n|; # printf qq|international identifier numbers which automatically gives the launch year.\n|; # printf qq|For example, \"1989-046\" means that this spacecraft was the 46th\n|; # printf qq|launch in 1989 and therfore must carry a SOPA detector, while\n|; # printf qq|\"1984-129\" was the 129th lauch in 1984 and must therefore carry a\n|; # printf qq|CPA detector.

\n|; # # printf qq|It is important to note that the low energy channels span slightly different\n|; # printf qq|energy ranges for CPA versus SOPA. The ranges are given approximately as follows;

\n|; # printf qq|
\n|; # printf qq| Electrons: 50-315 keV (SOPA) or 30-300 keV (CPA)
\n|; # printf qq| Protons: 50-400 keV (SOPA) or approx. 100-600 keV (CPA)\n|; # printf qq|


\n|; printf qq|Use the form below to choose the desired: plot type, size, and date.\n|; printf qq|Hit the \"Go\" button to take you to that date. If you are only interested in\n|; printf qq|the latest available plot, then hit the \"Latest\" button (the date is ignored in this case -- the\n|; printf qq|latest date will be found for you).\n|; printf qq|
\n|; &chooser(19980310, "LANL_GEO_LoE_00-24", 1, 1, $EphemType); printf qq|

[ Info About the Plots \| \n|; printf qq|Info About the Plot Browser \|
\n|; printf qq| SOPA and CPA Instrument Designs \|
\n|; printf qq|EP Home \| \n|; printf qq|Data Requests \|\n|; printf qq|Campaigns \& Projects \| \n|; printf qq|Information]

\n|; printf qq|
\n|; printf qq|
\n|; printf qq|
\n|; printf qq|
\n|; printf qq|Report problems to Mike Henderson\n|; printf qq|who wrote and maintains the EP summary plot browser.
\n|; printf qq|http://nis-www.lanl.gov/~mgh\n|; printf qq|

\n|; printf qq|
\n|; $tx = printf qq|Or report problems to Geoff Reeves\n|; printf qq|who maintains the EP Web Pages.
\n|; printf qq|http://leadbelly.lanl.gov/reeves.html\n|; printf qq|

\n|; printf qq|
\n|; printf qq|\n|; printf qq|\n|; } sub numerically { $a <=> $b }; # # This subroutine creates the pulldown menus for selecting Date, Plot type etc.. # sub chooser { local($date, $PlotType, $size, $flag, $EphemType) = @_; local($i, @SELECTED1, @SELECTED2, @SELECTED3, @SELECTED4, @SELECTED5); local($year, $month, $day); ($c_year, $_c_month) = year_month(); $ny = $c_year - 1976 + 2; # # Figure out which values to select # # # First set all of the SELECTED variables to blanks # for ($i = 0; $i < $nPType; $i++) { $SELECTED1[$i] = " "; } for ($i = 0; $i < 12; $i++) { $SELECTED2[$i] = " "; } for ($i = 0; $i < 31; $i++) { $SELECTED3[$i] = " "; } for ($i = 0; $i < 50; $i++) { $SELECTED4[$i] = " "; } for ($i = 0; $i < 4; $i++) { $SELECTED5[$i] = " "; } for ($i = 0; $i < 2; $i++) { $SELECTED6[$i] = " "; } # # Change selections to what we get from arg list # $year = $date ; $month = $date ; $day = $date ; $year =~ s/^([0-9]{4})[0-9]{4}/\1/; $month =~ s/^[0-9]{4}([0-9]{2})[0-9]{2}/\1/; $day =~ s/^[0-9]{4}[0-9]{2}([0-9]{2})/\1/; $SELECTED1[$PType{$PlotType}] = "SELECTED"; $SELECTED2[$month-1] = "SELECTED"; $SELECTED3[$day-1] = "SELECTED"; $SELECTED4[$year-1976] = "SELECTED"; $SELECTED5[$size-1] = "SELECTED"; $SELECTED6[$EphemType-1] = "SELECTED"; printf qq|
\n|; printf qq|\n|; printf qq|\n|; if ($flag) { printf qq|\n|; } printf qq|\n|; printf qq|\n|; if ($flag) { printf qq|\n|; } printf qq|\n|; printf qq|\n|; printf qq|\n|; if ($flag) { printf qq|\n|; } printf qq|\n|; if ( $HaveJava2 ) { printf qq|\n|; if ($flag) { printf qq|\n|; } printf qq|\n|; } printf qq|\n|; if ($flag) { printf qq|\n|; } printf qq|\n|; printf qq|
Choose Plot Type
Choose Plot Size:
Choose Date:\n|; printf qq|\n|; printf qq|
Choose Ephemeris Display Format:
Submit Request:\n|; printf qq|\n|; printf qq|
\n|; printf qq|

\n|; } sub WarnOfInvalidDate{ local($date, $year, $month, $day) = @_; local($dow, $monstr, $YYYY, $MM, $DD, $DOY); ($dow, $monstr, $YYYY, $MM, $DD, $DOY) = Doy($date); printf("Content-type: text/html\n\n"); printf qq|\n\n|; printf qq|LANL GEO EP Summary Plot for: $dow, $monstr $DD, $YYYY\n|; printf qq|\n|; printf qq|\n|; if ($HaveSanePngSupport){ printf qq|\n\n\n|; } else { printf qq|\n\n\n|; } printf qq|
You Entered an Invalid Date
\n|; printf qq|( Cannot recognize $date as a date )


\n|; printf qq|Try yyyymmdd or yyyyddd
\n|; printf qq|or if the date is in the 20th century try yyddd or yymmdd
\n|; printf qq|or make sure you dont have an impossible day-of-month value (e.g. 19990229)



\n|; printf qq|
\n|; printf qq|
\n|; printf qq|
\n|; printf qq|Report problems to Mike Henderson\n|; printf qq|who wrote and maintains the EP summary plot browser.
\n|; printf qq|http://nis-www.lanl.gov/~mgh\n|; printf qq|

\n|; printf qq|\n\n|; } sub DoPage { local($year, $month, $day, $PlotType, $size, $EphemType) = @_; local($date, $dow, $monstr, $YY, $MM, $DD, $DOY, $yyyyddd); local($days, $next_DOY, $next_year, $prev_DOY, $prev_year); local($prev_date, $next_date, $tdow, $tmonstr, $tYY, $tMM, $tDD, $tDOY); local($nn, $down_PlotType, $up_PlotType); @month2 = ("jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"); $date = $year*1e4 + $month*100 + $day; ($dow, $monstr, $YYYY, $MM, $DD, $DOY) = Doy($date); $yyyyddd = $YYYY*1000 + $DOY; $NextDOY = $DOY+1; if ($NextDOY > 365+LeapYear($YYYY)) { $NextYYYY = $YYYY+1; $NextDOY = 1; } else { $NextYYYY = $YYYY; $NextDOY = $DOY+1; } $MpaDate = sprintf("%02d-%s-%4d", $DD, $month2[$MM-1], $YYYY); $SixDigitDate = $date - 1900*10000; $SixDigitDate =~ s/.*(\d{6})$/$1/; $TwoDigitMonth = $date; $TwoDigitMonth =~ s/.*\d{2}(\d{2})\d{2}$/$1/; $a = ${YYYY}*100000 + ${DOY}*100; $b = ${NextYYYY}*100000 + ${NextDOY}*100; $SPOFDate = "${a}-${b}"; # figure out what next date and previous date ought to be $days = 365+LeapYear($YYYY); $next_DOY = $DOY + 1; $next_year = $YYYY; if ($next_DOY > $days) { $next_DOY -= $days; $next_year += 1; } $prev_DOY = $DOY - 1; $prev_year = $YYYY; if ($prev_DOY <= 0) { $prev_year -= 1; $prev_DOY = 365 + LeapYear($prev_year); } $prev_date = $prev_year*1000 + $prev_DOY; ($tdow, $tmonstr, $tYYYY, $tMM, $tDD, $tDOY) = Doy($prev_date); $prev_date = $tYYYY*1e4 + $tMM*100 + $tDD; $next_date = $next_year*1000 + $next_DOY; ($tdow, $tmonstr, $tYYYY, $tMM, $tDD, $tDOY) = Doy($next_date); $next_date = $tYYYY*1e4 + $tMM*100 + $tDD; # # set the up and down PlotTypes # if ($PlotType eq "") { $PlotType = "loe+lop"; } $nn = $PType{$PlotType} + 1; if ($nn >= $nPType) { $nn = 0; } $down_PlotType = $PType{$nn}; $nn = $PType{$PlotType} - 1; if ($nn < 0) { $nn = $nPType - 1; } $up_PlotType = $PType{$nn}; $OffX = 40; $OffY = 10; $GapX = 2; $nn = $PType{$PlotType}; $UTMin = $PMinUT[$nn]; $UTMax = $PMaxUT[$nn]; $FluxMin = $PMinFlux[$nn]; $FluxMax = $PMaxFlux[$nn]; if ($nn == 2) { $NP = 2; } else { $NP = 1; }; if ($size == 1){ $left = $PDataUpperLeftX_1[$nn] + $OffX; $right = $PDataLowerRightX_1[$nn] + $OffX; $height = $PDataLowerRightY_1[$nn] - $PDataUpperLeftY_1[$nn]; $top = $PDataUpperLeftY_1[$nn] + $OffY; $bottom = $top + $height; $width = $PDataLowerRightX_1[$nn] - $PDataUpperLeftX_1[$nn]; $pwidth = $PSizeX_1[$nn]; $pheight = $PSizeY_1[$nn]; $TX1 = $PSizeX_1[$nn]*$NP + 55; $TY1 = $OffY; $TX2 = $PSizeX_1[$nn]*$NP + 55; $TY2 = $OffY + 80; } elsif ($size == 2){ $left = $PDataUpperLeftX_2[$nn] + $OffX; $right = $PDataLowerRightX_2[$nn] + $OffX; $height = $PDataLowerRightY_2[$nn] - $PDataUpperLeftY_2[$nn]; $top = $PDataUpperLeftY_2[$nn] + $OffY; $bottom = $top + $height; $width = $PDataLowerRightX_2[$nn] - $PDataUpperLeftX_2[$nn]; $pwidth = $PSizeX_2[$nn]; $pheight = $PSizeY_2[$nn]; $TX1 = $PSizeX_2[$nn]*$NP + 55; $TY1 = $OffY; $TX2 = $PSizeX_2[$nn]*$NP + 55; $TY2 = $OffY + 80; } else { $left = $PDataUpperLeftX_3[$nn] + $OffX; $right = $PDataLowerRightX_3[$nn] + $OffX; $height = $PDataLowerRightY_3[$nn] - $PDataUpperLeftY_3[$nn]; $top = $PDataUpperLeftY_3[$nn] + $OffY; $bottom = $top + $height; $width = $PDataLowerRightX_3[$nn] - $PDataUpperLeftX_3[$nn]; $pwidth = $PSizeX_3[$nn]; $pheight = $PSizeY_3[$nn]; $TX1 = $PSizeX_3[$nn]*$NP + 55; $TY1 = $OffY; $TX2 = $PSizeX_3[$nn]*$NP + 55; $TY2 = $OffY + 80; } $loe_name1 = "../LanlGeoSummaryPlots/${year}/${date}/${date}_loe.gif"; $loe_name2 = "$root_dir/../LanlGeoSummaryPlots/${year}/${date}/${date}_loe.gif"; $loe_psname = "$root_dir/../LanlGeoSummaryPlots/${year}/${date}/${date}_loe.ps"; $lop_name1 = "../LanlGeoSummaryPlots/${year}/${date}/${date}_lop.gif"; $lop_name2 = "$root_dir/../LanlGeoSummaryPlots/${year}/${date}/${date}_lop.gif"; $lop_psname = "$root_dir/../LanlGeoSummaryPlots/${year}/${date}/${date}_lop.ps"; $jsephem = "$root_dir/../LanlGeoSummaryPlots/${year}/${date}/JsGeoEphem_${date}.js"; $jsephemf = "../LanlGeoSummaryPlots/${year}/${date}/JsGeoEphem_${date}.js"; $ephemfile = 1; $jskp = "$root_dir/../LanlGeoSummaryPlots/${year}/${date}/Kp_${date}.js"; $jskpf = "../LanlGeoSummaryPlots/${year}/${date}/Kp_${date}.js"; if ( !(-e $jsephemf) ) { $ephemfile = 0; $jsephem = "$root_dir/../LanlGeoSummaryPlots/JsGeoEphem_null.js"; } if ( !(-e $jskpf) ) { $jskp = "$root_dir/../JsKp/JsKp_null.js"; } $jsdst = "$root_dir/../LanlGeoSummaryPlots/${year}/${date}/Dst_${date}.js"; $jsdstf = "../LanlGeoSummaryPlots/${year}/${date}/Dst_${date}.js"; $dstfile = 1; if ( !(-e $jsdstf) ) { $dstfile = 0; $jsdst = "$root_dir/../LanlGeoSummaryPlots/Dst_null.js"; } if ( $PlotType eq "loe+lop" ){ $PT = $PlotType; $PlotType = "loe"; $nn1 = $PType{$PlotType}; $SP1_Raster_Path = eval "qq{$PRasterPath[$nn1]}"; $SP1_Raster_URL = eval "qq{$PRasterURL[$nn1]}"; $SP1_PS_URL = eval "qq{$PPostScriptURL[$nn1]}"; $PlotType = "lop"; $nn2 = $PType{$PlotType}; $SP2_Raster_Path = eval "qq{$PRasterPath[$nn2]}"; $SP2_Raster_URL = eval "qq{$PRasterURL[$nn2]}"; $SP2_PS_URL = eval "qq{$PPostScriptURL[$nn2]}"; $SpPageWidth = $pwidth*2; $SP1_left = 0; $SP1_top = 0; $SP1_width = $pwidth; $SP2_left = 0 + $pwidth; $SP2_top = 0; $SP2_width = $pwidth; $NSP = 2; $PlotType = $PT; } else { # # The eval "qq{ }" construct variable-interpolates its argument. I.e. its a way to variable # interpolate literal strings. # $SP1_Raster_Path = eval "qq{$PRasterPath[$nn]}"; $SP1_Raster_URL = eval "qq{$PRasterURL[$nn]}"; $SP1_PS_URL = eval "qq{$PPostScriptURL[$nn]}"; $SpPageWidth = $pwidth; $SP1_left = 0; $SP1_top = 0; $SP1_width = $pwidth; $NSP = 1; } $SpPageHeight = $pheight; $tmpx1 = $pwidth; $tmpy1 = $SpPageHeight; $Mag = 2; $tmpx2 = $pwidth * $Mag; $tmpy2 = $SpPageHeight * $Mag; $OffXp = $OffX +$pwidth; $leftp = $left +$pwidth; $rightp = $right +$pwidth; $clipp = $leftp+1; $WarningArea_yOff = $SpPageHeight + 20; $tmp = $pwidth*2; # # Read in the GEO locations # This is the oldstyle way of doing things..... # Basically, these files were produced awhile back and are no longer produced. # BUT, the do go back further than anything else.... # SO, we eventually want to nuke all these files and use the JsEphem stuff, but # for now, lets just leave this bit of code in and check anyway.... # $GEO_LOCS_Path = "../../ep_geo_locs/${date}_locs.dat"; @tt = (-999.0, -999.0, -999.0, -999.0, -999.0, -999.0); @sn = ("", "", "", "", "", ""); if (-s ${GEO_LOCS_Path}) { $ti = 0; open(IN, $GEO_LOCS_Path); while(){ chop; ($sn[$ti], $tt[$ti]) = split(); ++$ti; } close(IN); } printf("Content-type: text/html\n\n"); printf qq| LANL GEO EP Summary Plot for: $dow, $monstr $DD, $YYYY \n|; printf qq|\n|; printf qq|\n|; printf qq|\n|; printf qq| |; # printf qq|
\n|; # printf qq|
\n|; # if ( $NSP > 1 ) { # printf qq|
\n|; # } # printf qq|
\n|; printf qq|
\n|; printf qq|
\n|; if (-e ${SP1_Raster_Path} || $PRasterPath[$nn] eq "EXTERNAL") { if (-z ${SP1_Raster_Path}) { printf qq|\n|; } else { $tWidth = $pwidth; $tHeight = $pheight; printf qq|\n|; } } else { printf qq|\n|; } printf qq|
\n|; if ( $NSP > 1 ) { printf qq|
\n|; if (-e ${SP2_Raster_Path}) { if (-z ${SP2_Raster_Path}) { printf qq|\n|; } else { $tWidth = $pwidth; $tHeight = $pheight; printf qq|\n|; } } else { #printf qq|${SP2_Raster_Path}
\n|; if ($HaveSanePngSupport){ printf qq|\n|; } else { printf qq|\n|; } } printf qq|
\n|; } printf qq|
\n|; printf qq|
\n|; printf qq|
\n\n|; printf qq|

Not for Publication or Public Presentation

\n|; printf qq|These summary plots should not be used for publication. Higher\n|; printf qq|quality plots which have been checked for errors are available on request.\n|; printf qq|The data presented here do have known instrumental effects, "bugs"\n|; printf qq|and "glitches" and are provided "as is" (see Proton caveats).\n|; printf qq|Keeping the LANL EP\n|; printf qq|team involved in your analysis will help all of us. If you would\n|; printf qq|like to request publishable plots or data or if you have questions\n|; printf qq|about the data please contact a member of the EP team:

\n|; printf qq|
\n|; printf qq| Dick Belian, rbelian\@lanl.gov) -- CPA & SOPA PI
\n|; printf qq| Geoff Reeves, reeves\@lanl.gov
\n|; printf qq| Mike Henderson, mghenderson\@lanl.gov
\n|; printf qq| Reiner Friedel, friedel\@lanl.gov
\n|; printf qq| Tom Cayton, tcayton\@lanl.gov
\n|; printf qq| Chuck Ingraham, cingraham\@lanl.gov
\n|; printf qq|
\n|; printf qq|
\n|; printf qq|

\n|; if ( $SpPageWidth < 200 ) { printf qq|
\n|; printf qq|Report problems to Mike Henderson\n|; } else { printf qq|Report problems to Mike Henderson\n|; } printf qq|who wrote and maintains the EP summary plot browser.
\n|; printf qq|




\n|; printf qq|
\n|; printf qq|
\n|; printf qq|
\n|; if ( $NSP > 1 ) { printf qq|
\n|; } printf qq|
\n|; printf qq|
UT: 0.00 (00:00:00)
\n|; printf qq|
\n|; printf qq| $PName[$PType{$PlotType}]
\n|; printf qq| $dow, $monstr $DD, $YYYY ( $date / $yyyyddd )
\n|; printf qq| \n|; printf qq| \n|; printf qq| \n|; printf qq|
\n|; if ($HaveJava2){ # # OK, a bug has been pestering me for ages. Looks Like Ive finally found a work-around! # The bug was such that if you hit the "GO" button on the form to bring up a new page, # the Java applet came up fine. But if you hit an arrow it didnt load (bad magic number error # -- which means the ClassLoader didnt think the file was a Java class!) It seems to # work OK if you add the CODEBASE tag as well as the other tags though.... # #printf qq| \n|; printf qq|
\n|; if ($EphemType == 2){ printf qq|\n|;} } printf qq| \n|; printf qq|
\n|; printf qq|\n|; printf qq|\n|; } # # Routine converts between day number and mmdd formats. Input date can be in any of the following # formats; # yyddd (where yy is assumed to be a year in the 20th century) # yymmdd (where yy is assumed to be a year in the 20th century) # yyyyddd (where yyyy is assumed to be between 1000 A.D. and 9999 A.D.) # yyyymmdd (where yyyy is assumed to be between 1000 A.D. and 9999 A.D.) # # Should be good enough for space physics! Need separate routines or add a flag or something # if you want it to be completely general.... Its not completely general now but it is # convenient. # # sub Doy { local($date) = @_; local(@days1, @days2, @month, $YY, $MM, $DD, $DOY, $ddd, $n, $f, $i, @Returns); local($dow); @days1 = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); @days2 = (31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); @month = ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); if (($date < 1e5)||(($date < 1e7)&&($date > 1e6))){ # then the date is in yyddd or yyyyddd format $YY = int($date/1000.0); $DOY = $date - $YY * 1000; if ($date < 1e5) { $YY += 1900; } $ddd = 0, $n = 0; if (LeapYear($YY) == 1){ while ( ($ddd + $days2[$n]) < $DOY) { $ddd += $days2[$n]; ++$n; } } else{ while ( ($ddd + $days1[$n]) < $DOY) { $ddd += $days1[$n]; ++$n; } } $MM = $n + 1; $DD = $DOY - $ddd; } else{ # then the date is in yymmdd or yyyymmdd format $f = $date/10000.0; $YY = int($f); $f = $date - $YY * 10000; $MM = int($f/100.0); $m = $MM - 1; $f = $f - $MM * 100; $DD = int($f); if (LeapYear( ($date < 1e7) ? 1900 + $YY : $YY ) == 1){ for ($i=0, $ddd=0; $i<$m; ++$i) { $ddd += $days2[$i]; } } else{ for ($i=0, $ddd=0; $i<$m; ++$i) { $ddd += $days1[$i]; } } $DOY = $ddd + $DD; if ($date < 1e7) { $YY += 1900; } } $dow = DayofWeek($YY, $MM, $DD); @Returns = ($dow, $month[$MM-1], $YY, $MM, $DD, $DOY); return @Returns; } sub LeapYear { local($year) = @_; local($L); if (($year%100 == 0)&&($year%400 != 0)) { $L = 0; } elsif ($year%4 == 0) { $L = 1; } else { $L = 0; } return $L; } sub DayofWeek { local($year, $month, $day) = @_; local(@dowstr, $UT, $JD, $A, $iA, $Afrac, $n); @dowstr = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); $UT = 0.0; $JD = jd($year, $month, $day, $UT); $A = ($JD + 1.5)/7.0; $iA = int($A); $Afrac = $A - $iA; $n = int($Afrac*7.0 + 0.5); $dowstr[$n]; } # # Compute the Julian Day number for the given date. # Julian Date is the number of days since noon of Jan 1 4713 B.C. # sub jd { local($ny, $nm, $nd, $UT) = @_; local($day, $A, $B, $C, $D, $JD); $day = $nd + $UT/24.0; if (($nm == 1) || ($nm == 2)){ $ny = $ny - 1; $nm = $nm + 12; } if (($ny+$nm/12.0+$day/365.25)>=(1582.0+10.0/12.0+15.0/365.25)){ $A = (int($ny / 100.0)); $B = 2.0 - $A + int($A/4.0); } else{ $B = 0.0; } if ($ny < 0.0){ $C = int((365.25*$ny) - 0.75); } else{ $C = int(365.25*$ny); } $D = int(30.6001*($nm+1)); $JD = $B + $C + $D + $day + 1720994.5; } # # Compute the Julian Day number for the given date. # Julian Date is the number of days since noon of Jan 1 4713 B.C. # sub jd_to_ymdh { local($JD) = @_; local($A, $B, $C, $D, $E, $F, $G, $I, $d, $m, $y); local($year, $month, $day, $UT); $JD += 0.5; $I = int( $JD ); $F = $JD - $I; if ( $I > 2299160 ) { $A = int( ($I-1867216.25)/36524.25 ); $B = $I + 1 + $A - int( $A/4 ); } else { $B = $I; } $C = $B + 1524; $D = int( ($C - 122.1)/365.25 ); $E = int( 365.25 * $D ); $G = int( ($C-$E)/30.6001 ); $d = $C - $E + $F - int( $G * 30.6001 ); if ( $G < 13.5 ) { $m = $G - 1; } else { $m = $G - 13; } if ( $m > 2.5 ) { $y = $D - 4716; } else { $y = $D - 4715; } $day = int( $d ); $UT = ($d - $day)*24.0; $month = int( $m ); $year = $y; ($year, $month, $day, $UT); } sub year_month{ local($ldate_string, $ldow, $lmon, $ldom, $ltim, $lyear, %lmonstr, $tzone, @lval); $ldate_string = &ctime(time); ($ldow, $lmon, $ldom, $ltim, $tzone, $lyear) = split(" ", $ldate_string); # linux doesnt seem to have tzone (its just missing). if ($lyear == '') { $lyear = $tzone; $tzone=''; } %lmonstr = ( "Jan", 1, "Feb", 2, "Mar", 3, "Apr", 4, "May", 5, "Jun", 6, "Jul", 7, "Aug", 8, "Sep", 9, "Oct", 10, "Nov", 11, "Dec", 12); @lval = ($lyear, $lmonstr{$lmon}); } sub IsValidDate(Date){ local($Date) = @_; local($dow, $monstr, $yyyy, $mm, $dd, $doy); @mdays = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); @mdays2 = (31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); if ($Date < 0) { return(0) }; ($dow, $monstr, $yyyy, $mm, $dd, $doy) = Doy($Date); if (($mm < 1)||($mm > 12)) { return(0) }; if ($dd < 1) { return(0) }; if (LeapYear($yyyy)){ if ($dd > $mdays2[$mm-1]) { return(0) }; } else { if ($dd > $mdays[$mm-1]) { return(0) }; } # Must be a valid date (at least in terms of month and day) return(1); } sub FindLZ { local($date) = @_; local(@cep_file_list, @cam_file_list, $filename); local(@sorted_cep_file_list, @sorted_cam_file_list, $found_cep, $found_cam); local($date2, $found_cep_in_cdhf, $found_cam_in_cdhf); local($CammiceLZ, $CeppadLZ); $date2 = $date - 19000000; # # look in /n/tempest/polar/LevelZeroFiles first # opendir(DIR, "/n/tempest/polar/LevelZeroFiles/cep"); # || die print "Error: Could not open "/n/tempest/polar/LevelZeroFiles/cep"; @cep_file_list = (), $found_cep = 0; while($filename = readdir(DIR)){ if ($filename =~ /^${date2}\d{2}\.dat$/) { push(@cep_file_list, $filename); $found_cep = 1; } } closedir(DIR); # # look in /n/tempest/polar/CDHF next # if ($found_cep == 0){ opendir(DIR, "/n/tempest/polar/CDHF"); # || die print "Error: Could not open "/n/tempest/polar/CDHF"; @cep_file_list = (), $found_cep_in_cdhf = 0; while($filename = readdir(DIR)){ if ($filename =~ /^po_lz_cep_${date}_v\d{2}\.dat$/) { push(@cep_file_list, $filename); $found_cep_in_cdhf = 1; } } closedir(DIR); } @sorted_cep_file_list = sort numerically @cep_file_list; # # CAMMICE # look in /n/tempest/polar/LevelZeroFiles first # opendir(DIR, "/n/tempest/polar/LevelZeroFiles/cam"); # || die print "Error: Could not open "/n/tempest/polar/LevelZeroFiles/cam"; @cam_file_list = (), $found_cam = 0; while($filename = readdir(DIR)){ if ($filename =~ /^${date2}\d{2}\.dat$/) { push(@cam_file_list, $filename); $found_cam = 1; } } closedir(DIR); # # look in /n/tempest/polar/CDHF next # if ($found_cam == 0){ opendir(DIR, "/n/tempest/polar/CDHF"); # || die print "Error: Could not open "/n/tempest/polar/CDHF"; @cam_file_list = (), $found_cam_in_cdhf = 0; while($filename = readdir(DIR)){ if ($filename =~ /^po_lz_cam_${date}_v\d{2}\.dat$/) { push(@cam_file_list, $filename); $found_cam_in_cdhf = 1; } } closedir(DIR); } @sorted_cam_file_list = sort numerically @cam_file_list; if ($found_cep) { $CeppadLZ = "CeppadLevelZero/$sorted_cep_file_list[0]"; } elsif ($found_cep_in_cdhf) { $CeppadLZ = "CDHFLevelZero/$sorted_cep_file_list[0]"; } else { $CeppadLZ = ""; } if ($found_cam) { $CammiceLZ = "CammiceLevelZero/$sorted_cam_file_list[0]"; } elsif ($found_cam_in_cdhf) { $CammiceLZ = "CDHFLevelZero/$sorted_cam_file_list[0]"; } else { $CammiceLZ = ""; } ($CeppadLZ, $CammiceLZ); } sub FindLatestInAYear { local($year, $p) = @_; $YearDir = "../LanlGeoSummaryPlots/${year}"; opendir(YEARDIR, $YearDir); @file_list = (); while($DateDir = readdir(YEARDIR)){ if ($DateDir =~ /^\d{8}$/) { $d = $YearDir . "/" . $DateDir; opendir(DATEDIR, $d); while($filename = readdir(DATEDIR)){ if ($filename =~ /^\d{8}_${p}/) { push(@file_list, $filename); } } closedir(DATEDIR); } } closedir(YEARDIR); @sorted_file_list = sort numerically @file_list; $latest_file = $sorted_file_list[$#sorted_file_list]; $date = $latest_file; $date =~ s/^(\d{8})\S*/\1/; $year = $date; $month = $date; $day = $date; $year =~ s/(\d{4})(\d{2})(\d{2})/\1/; $month =~ s/(\d{4})(\d{2})(\d{2})/\2/; $day =~ s/(\d{4})(\d{2})(\d{2})/\3/; ($latest_file); } sub FindLatest { local($p) = @_; local($c_year, $c_month, $y); $latest_file = ""; ($c_year, $c_month) = year_month(); # scan backwards in time for ($y = $c_year; $y >= 1976; --$y){ &FindLatestInAYear( $y, $p ); if ( !($latest_file eq "") ) { last; } } ( $lastest_file ); }