package doy2;

;# Year, DOY   -->  Month, Day
;# $_[0] $_[1]      $month $day

sub doytoymd {

if ( $_[0] % 4 != 0 ) {
  @nmday = (0,31,59,90,120,151,181,212,243,273,304,334,365);
} else {
  @nmday = (0,31,60,91,121,152,182,213,244,274,305,335,366);
}

;#--- Month
for ($mm = 0; $mm <= 11; $mm++) {
  if (( $_[1] > $nmday[$mm] ) && ( $_[1] <= $nmday[$mm+1] )) { $month = $mm + 1 }
}
  if ( $_[1] <= 0 ) { $month = 1 }
  if ( $_[1] > $nmday[12] ) { $month = 12 }

;#--- Date
$day = $_[1] - $nmday[$month-1];

;#printf("%4.4d/%2.2d/%2.2d %3.3d\n", $_[0], $month, $ day, $_[1]);

return($month,$day);
}
1;
