#!/bin/csh
#
# Finds month number, day number and leap year for an 
# input of year (4 digit) and julian day
#
set year = $1
set jday = $2
#
# Cumulative day array (1 indicates leap year)
#
set cday0 = (0 31 59 90 120 151 181 212 243 273 304 334 365)
set cday1 = (0 31 60 91 121 152 182 213 244 274 305 335 366)
#
# Test for leap year: leap=1 indicates a leap year
#
if(($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0) then
  @ leap = 1
  if($jday < 1 || $jday > 366) exit 1
else
  @ leap = 0
  if($jday < 1 || $jday > 365) exit 1
endif
#
# Find month number, month name and day of month
#
@ mon = 1
while ($mon <= 12)
  @ mon2 = $mon ; @ mon2++
  if($leap == 1) then
    if($jday > $cday1[$mon] && $jday <= $cday1[$mon2]) then
      @ day = $jday - $cday1[$mon]
      echo $mon $day $leap
      exit 0
    endif
  else
    if($jday > $cday0[$mon] && $jday <= $cday0[$mon2]) then
      @ day = $jday - $cday0[$mon]
      echo $mon $day $leap
      exit 0
    endif
  endif
  @ mon++
end

end