Home > Archive > Fortran > September 2005 > DAY Number in an Year
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
DAY Number in an Year
|
|
| prasad413in@gmail.com 2005-09-28, 9:57 pm |
| Hello,
I am a Newbie to Fortran. I need to write a simple routine in which
current year and day Number in an Year, like today is 272nd day in this
year are known. I need to write routine such that count starts from
which starts from 336th day in 2004 and it goes on.
I tried like
YEAR = 2004
C Condition should be like it should return the Current Year...
IF(DAY_NO .GT. 365) THEN CURRENT_YEAR = YEAR+1 ??????????
It should be a real time run..
Thanks
| |
| ttw@texasairnet.com 2005-09-28, 9:57 pm |
| In 2003, I suggested:
FUNCTION juldate (year,month,day)
c
c Converts DAY/MONTH/YEAR to a Julian date transformed to 2000
c
IMPLICIT NONE
c
INTEGER juldate, day, month, year
c
juldate=367*year-7*(year+(month+9)/12)/4-3*((year+(month-9)/7)/100
1 +1)/4+275*month/9+day-730516
END
The constant 730516 can be adjusted to match any date desired as the
starting point or to change Gregorian dates to Julian.
| |
| John Harper 2005-09-29, 3:57 am |
| In article <1127957350.272566.191140@g14g2000cwa.googlegroups.com>,
<pra 413in@gmail.com> wrote:
>
> IF(DAY_NO .GT. 365) THEN CURRENT_YEAR = YEAR+1 ??????????
>It should be a real time run..
Someone in a certain aluminium smelting company wrote code like that
(I don't know if it was in Fortran) and caused damage costing millions
to repair, because the New Zealand potlines turned themselves off
suddenly at midnight on 30 Dec 1996. Exactly 2 hours later the same
thing happened in Tasmania. More millions gone. But then someone
realised that 1996 had 366 days, and was able to prevent the same
problem hitting the same company's smelter in Queensland 1 hour later.
They were very glad that Qld does not put its clocks forward in summer!
Otherwise the Tas and Qld smelters would have gone down simultaneously.
John Harper, School of Mathematics, Statistics and Computer Science,
Victoria University, PO Box 600, Wellington, New Zealand
e-mail john.harper@vuw.ac.nz phone (+64)(4)463 5341 fax (+64)(4)463 5045
| |
| Rich Townsend 2005-09-29, 3:57 am |
| ttw@texasairnet.com wrote:
> In 2003, I suggested:
> FUNCTION juldate (year,month,day)
> c
> c Converts DAY/MONTH/YEAR to a Julian date transformed to 2000
> c
> IMPLICIT NONE
> c
> INTEGER juldate, day, month, year
> c
>
> juldate=367*year-7*(year+(month+9)/12)/4-3*((year+(month-9)/7)/100
>
>
> 1 +1)/4+275*month/9+day-730516
> END
>
>
> The constant 730516 can be adjusted to match any date desired as the
> starting point or to change Gregorian dates to Julian.
>
Note, however, that Julian days formally begin at 12 noon. So your algorithm has
an off-by-one error for AM vs PM.
cheers,
Rich
PS I'm talking about the astronomical Julian calendar, as created by Julius
Scalinger. For some reason, many programmers refer to the days-since-arbitrary
epoch (say, 1900 or 1970) as the Julian date, but that's just wrong.
| |
| Anton Haumer 2005-09-29, 3:57 am |
| ttw@texasairnet.com schrieb:
>
> In 2003, I suggested:
> FUNCTION juldate (year,month,day)
> c
> c Converts DAY/MONTH/YEAR to a Julian date transformed to 2000
> c
> IMPLICIT NONE
> c
> INTEGER juldate, day, month, year
> c
>
> juldate=367*year-7*(year+(month+9)/12)/4-3*((year+(month-9)/7)/100
>
> 1 +1)/4+275*month/9+day-730516
> END
>
> The constant 730516 can be adjusted to match any date desired as the
> starting point or to change Gregorian dates to Julian.
May i remember that the OP asked for the inverse of your func ...
Toni
| |
| James Giles 2005-09-29, 3:57 am |
| Rich Townsend wrote:
> ttw@texasairnet.com wrote:
>
> Note, however, that Julian days formally begin at 12 noon. So your
> algorithm has an off-by-one error for AM vs PM.
For a one-to-one correspondence with calendar dates,
using round to nearest of the Julian day number is a good
choice. That's what the above produces. (Note that
astronomical day numbers are not integers, the fraction
of the day is part of the value.)
My complaint about the above is that it's difficult to
verify correctness. In the old days of programmable
pocket calculators and the like, saving space using
obscure formulae that didn't require any auxilliary
tables and such might have been appropriate. Today,
it's more important to be legible and verifiably correct.
In the same thread Dr. Warnock mentioned the above,
I recommended:
integer function dateval(m, d, y)
integer, intent(in):: m, d, y
integer, parameter:: ar(12) = (/365, 396, 59, 90, 120, 151, 181, &
212, 243, 273, 304, 334/)
integer :: yr
yr = y
if (m < 3) yr = y-1
dateval = yr*365 + yr/4 - yr/100 + yr/400 +ar(m) + d + 1721060
return
end
Originally, the leap day at the end of Feb. was at the end
of the year (March was the first month). With the change
to January as the first month, leap day was left as the
last of February. It's still easiest to calculate if leap
day is the last of the year however. So that's why the
first two months are adjusted as above. That's the
only confusing part of the algorithm. The rest is
clearly a direct application of the normal rules of
the Gregorian calendar. The 1721060 is the necessary
value for the Julian day number sequence (the first day
of which was chosen to be Jan 1, 4713 BC). Any number
can be chosen if you want a different initial date.
The above obviously doesn't check for valid dates. If the
day of the month or the year are impossible or way out of
range, the returned value is meaningless. The month
is not checked either unless array bounds checking is turned
on. On the other hand, for valid Gregorian dates (more
recent then Oct 15, 1582 and for as far in the future as
I expect to survive - at least assuming default INTEGERs
are at least 23 bits) the returned result should be correct.
To get the fractional Julian day number, take the time of
day (in hours), subtract 12, divide by 24.0 and add to the
above calculated integer.
> PS I'm talking about the astronomical Julian calendar, as created by Julius
> Scalinger. For some reason, many programmers refer to the days-since-arbitrary
> epoch (say, 1900 or 1970) as the Julian date, but that's just wrong.
Actually it was invented by Jacob Scalinger and named for his
father, whose name was Julius.
--
J. Giles
"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare
| |
| Herman D. Knoble 2005-09-29, 7:57 am |
| See the date routines and sample driver at:
http://ftp.cac.psu.edu/pub/ger/fortran/hdk/datesub.f90
These were published in CACM back in 1972:-)
(or Fortran 77 at:
http://ftp.cac.psu.edu/pub/ger/fortran/hdk/datesub.for )
Skip Knoble
On 28 Sep 2005 18:29:10 -0700, pra 413in@gmail.com wrote:
-|Hello,
-| I am a Newbie to Fortran. I need to write a simple routine in which
-|current year and day Number in an Year, like today is 272nd day in this
-|year are known. I need to write routine such that count starts from
-|which starts from 336th day in 2004 and it goes on.
-|I tried like
-|
-|
-|YEAR = 2004
-|C Condition should be like it should return the Current Year...
-|
-| IF(DAY_NO .GT. 365) THEN CURRENT_YEAR = YEAR+1 ??????????
-|It should be a real time run..
-|
-|Thanks
| |
| Paul Van Delst 2005-09-29, 6:59 pm |
| ttw@texasairnet.com wrote:
> In 2003, I suggested:
> FUNCTION juldate (year,month,day)
> c
> c Converts DAY/MONTH/YEAR to a Julian date transformed to 2000
> c
> IMPLICIT NONE
> c
> INTEGER juldate, day, month, year
> c
>
> juldate=367*year-7*(year+(month+9)/12)/4-3*((year+(month-9)/7)/100
>
>
> 1 +1)/4+275*month/9+day-730516
> END
>
>
> The constant 730516 can be adjusted to match any date desired as the
> starting point or to change Gregorian dates to Julian.
But the OP uses day-of-year, not Julian date. I thought they were totally different beasties?
cheers,
paulv
--
Paul van Delst
CIMSS @ NOAA/NCEP/EMC
| |
| Paul Van Delst 2005-09-29, 6:59 pm |
| pra 413in@gmail.com wrote:
> Hello,
> I am a Newbie to Fortran. I need to write a simple routine in which
> current year and day Number in an Year, like today is 272nd day in this
> year are known. I need to write routine such that count starts from
> which starts from 336th day in 2004 and it goes on.
Apologies in advance, but I found your question a little confusing. What exactly do you
want? What are your inputs to your routine, and what exactly are your required outputs?
cheers,
paulv
> I tried like
>
>
> YEAR = 2004
> C Condition should be like it should return the Current Year...
>
> IF(DAY_NO .GT. 365) THEN CURRENT_YEAR = YEAR+1 ??????????
> It should be a real time run..
>
> Thanks
>
--
Paul van Delst
CIMSS @ NOAA/NCEP/EMC
| |
| prasad413in@gmail.com 2005-09-29, 6:59 pm |
| hello Paul..
Excuse me for being not precise in my question.. Actually what i
need is.. I am using lynx in UNIX to fetch the Files from the web..
Those files are updated in real time.. The file format is
GA762_2005269.SAO where 2005 and 269 are variabless. I need to download
that file for the given Day number of the year and YEAR.. link
../lynx_fetch 2005 269 should download that file and..
../lynx_fetch 2005 270 downloads other file...........
similarly ./lynx_fetch 2004 360 downloads file..
I wrote the script for lynx_fetch.. I need to write the Fortran code
for this Day_NO and Year ...
Any suggestions
Pra
| |
| Paul Van Delst 2005-09-29, 6:59 pm |
| pra 413in@gmail.com wrote:
> hello Paul..
> Excuse me for being not precise in my question.. Actually what i
> need is.. I am using lynx in UNIX to fetch the Files from the web..
> Those files are updated in real time.. The file format is
> GA762_2005269.SAO where 2005 and 269 are variabless. I need to download
> that file for the given Day number of the year and YEAR.. link
>
> ./lynx_fetch 2005 269 should download that file and..
> ./lynx_fetch 2005 270 downloads other file...........
> similarly ./lynx_fetch 2004 360 downloads file..
>
>
> I wrote the script for lynx_fetch.. I need to write the Fortran code
> for this Day_NO and Year ...
So you provide the day number and the year, and your "lynx_fetch" script constructs a
filename from that? Fair enough. So what do you need the Fortran code for? To check the
day number and the year input? If this is so, then the first thing you'll want to do is
test if the year is a leap year or not. I do it like so:
FUNCTION Is_Leap_Year( Year ) RESULT ( Its_a_Leap_Year )
INTEGER, INTENT( IN ) :: Year
LOGICAL :: Its_a_Leap_Year
Its_a_Leap_Year = .FALSE.
IF ( ( MOD( Year, 4 ) == 0 .AND. MOD( Year, 100 ) /= 0 ) .OR. &
MOD( Year, 400 ) == 0 ) Its_a_Leap_Year = .TRUE.
END FUNCTION Is_Leap_Year
Then, if your year is a leap year, you'll know that 366 is a valid day of year. If the
year is not a leap year, then 366 is invalid and you can either:
a) exit with an error flagged, or
b) increment the year, and change the day of year (doy) to doy=doy-365. That is, Dec 32rd
of the current year would become Jan 1st of the next year. Dunno if that makes sense or
not, though.
If I've still got it wrong, I again apologise for my denseness in understanding your
problem (I'm gun-shy right now. I've misunderstood several colleagues' questions over the
last couple of days due to me assuming that what they said was what they meant... :o)
cheers,
paulv
--
Paul van Delst
CIMSS @ NOAA/NCEP/EMC
|
|
|
|
|