#!/bin/csh

if ($#argv < 1) then
   echo "   Usage: dy2doy YYYY.DDDD"
   echo "          YYYY.DDDD = decimal year to convert"
   echo "          It outputs YEAR DOY"
   echo "          Cheloni & Avallone (2014/7/9)"
   exit
endif
set dyear = $1
set year  = `echo $dyear | awk '{printf "%i\n",$1}'`
set dec   = `echo $dyear $year | awk '{printf "%6.4f\n", $1-$2}'`
set ldoy  = `date +%j --d=${year}-12-31 | awk '{printf "%i\n", $1}'`
set ddoy  = `echo $dec $ldoy | awk '{print ($1*$2)+1}'`
# rounding to the nearest integer
# the upper limit must be the last doy of the year
set doy = `echo $ddoy | awk '{if (($1-int($1))<0.5) print int($1); else print int($1)+1}' |  awk '{if ($1 > "'$ldoy'") print "'$ldoy'"; else print $1}'`
echo $year $doy
