[Thread Prev][Thread Next][Index]

Re: [ferret_users] Calculating monthly mean from NOLEAP daily mean



Hi Ansley and Patrick,

Patrick's script is great for normal use in generating monthly axis. I
was just curious how to calculate the calendar days in ferret. Now I
have figured out a way to mimic Days1900() to calculate the day count
from 01-jan-1900 for any date on any calendar.

Attached is my script days1900_mc.jnl. Its usage is quite like the
function Days1900() and one can generate a time axis with the method in
user guide using the output variable, according to the calendar(s)
he/she want to use.

BTW, I made up the Julian calendar for Patrick's script (also attached).

Thank you very much for your help!
Tony

On Thu, 2011-07-28 at 23:54 +0200, Brockmann Patrick wrote:
> Jian Ma a écrit :
> > Hi Ansley,
> >
> > Thanks.
> >
> > Maybe I did not state clear my purpose. It is a multi-year daily data
> > and I want to make a multi-year monthly mean, not climatology. Then how
> > can I define the days count for Noleap calendar?
> >   
> Hi,
> 
> Have a look to the attached script. I think that it will do what you want.
> 
> yes? go def_monthaxis_days.jnl NOLEAP 2002 2005 mytaxis
> yes? show axis/t mytaxis
> 
> Patrick
> 
! days1900_mc.jnl
! (DAYS1900 in Multiple Calendar)

\cancel mode verify

!**************************************************************
! Description: Computes the number of days since 1 Jan 1900.
!              This script is useful in converting dates to
!              days on multiple calendars. If the year is prior
!              to 1900 a negative number is returned. The output
!              has the same dimension with the input.
!
! Usage: go days1900_mc.jnl year month day [calendar] [method]
!
! Arguments:
! 		year       =    variable containing years
!
!               month      =    variable containing months
!
!               day        =    variable containing days
!
!               calendar   =	STANDARD or GREGORIAN
!                               JULIAN
!				NOLEAP   or 365_DAY
!				ALL_LEAP or 366_DAY
!				360_DAY
!                               ALL (default)
!
!		method     =    1 (default) or 2 (and/or other number)
!                               choose a calculation method for the first 3 calendars above
! 
!
! Examples:
!            go days1900_mc.jnl year month day JULIAN 1
!            go days1900_mc.jnl l[l=1970:2054] 1 1 NOLEAP 2
!            go days1900_mc.jnl 1949 l[l=1:12] 20 360_DAY 1
!
!
! To define a time axis with the output:
!
! DEFINE AXIS/UNITS=days/T0=1-jan-1900/cal=??? truemonth = days1900_???
!
!
! Calls:
!
! Author: Jian Ma
! Contact: jianma@xxxxxxxxxx
! $Date: $ 07/28/2011
! $Name: $
! $Revision: $
! History:
! Modification:
!
!**************************************************************

let calendar=UPCASE("($4%all%)")
let method=($5%1%)

say
say "   Computing the number of days since 1 Jan 1900 on `calendar` calendar(s)"
say

IF `(method EQ 1)` THEN

!==================================METHOD #1============================================
say "   Using method 1"
say

let monthdaycount=DAYS1900(1900,l[l=1:12],1)

let monthday_0=0
repeat/range=1:12/name=aa let monthday_`aa`=if $2 eq `aa` then monthdaycount[l=`aa`] else monthday_`aa-1`
let monthday=monthday_12

let days1900_NLP=($1-1900)*365+monthday+$3-1

let JLN=days1900_NLP+int($1/4)-int(1900/4)
let days1900_JLN=if mod($1,4) eq 0 and $2 le 2 then JLN else JLN+1

let GRG=days1900_JLN-int($1/100)+int($1/400)+19-int(1900/400)
let days1900_GRG=if mod($1,100) eq 0 and mod($1,400) ne 0 and $2 le 2 then GRG else GRG-1

ELSE

!==================================METHOD #2============================================
say "   Using method 2"
say

let days1900_GRG=days1900($1,$2,$3)

let JLN=days1900_GRG+int($1/100)-int($1/400)-19+int(1900/400)
let days1900_JLN=if mod($1,100) eq 0 and mod($1,400) ne 0 and $2 le 2 then JLN else JLN+1

let NLP=days1900_JLN-int($1/4)+int(1900/4)
let days1900_NLP=if mod($1,4) eq 0 and $2 le 2 then NLP else NLP-1

ENDIF

!==================================Other calendars======================================

let ALP=days1900_NLP+$1-1900
let days1900_ALP=if $2 le 2 then ALP else ALP+1

let days1900_360=($1-1900)*360+($2-1)*30+$3-1

IF `(calendar EQ "STANDARD") OR (calendar EQ "GREGORIAN") OR (calendar EQ "ALL")` THEN say "   output  =  days1900_GRG for GREGORIAN calendar"
IF `(calendar EQ "JULIAN") OR (calendar EQ "ALL")` THEN say "   output  =  days1900_JLN for JULIAN calendar"
IF `(calendar EQ "NOLEAP") OR (calendar EQ "365_DAY") OR (calendar EQ "ALL")` THEN say "   output  =  days1900_NLP for NOLEAP calendar"
IF `(calendar EQ "ALL_LEAP") OR (calendar EQ "366_DAY") OR (calendar EQ "ALL")` THEN say "   output  =  days1900_ALP for ALL_LEAP calendar"
IF `calendar EQ "360_DAY" OR (calendar EQ "ALL")` THEN say "   output  =  days1900_360 for 360_DAY calendar"

say
!**************************************************************
set mode/last verify
! def_monthaxis_days.jnl

\cancel mode verify

!**************************************************************
! Description: Define a time axis in days in specific calendar 
!              Each grid cell begins at first of the month
!              Axis coordinates are at the middle of each month
!
! Usage: go def_monthaxis_days.jnl calendar year_start year_end [axis_name] [year_ref]
!
! Arguments:
! 		calendar(case insensitive) =	
!				STANDARD or GREGORIAN
!                               JULIAN
!				NOLEAP   or 365_DAY
!				ALL_LEAP or 366_DAY
!				360_DAY
!
!		year_start =    start year for axis
!
!		year_end =      start year for axis
!
!		axis_name =     if not specifed, monthaxis is used
!
!		year_ref =      if not specified, set to year_start
! 
! Examples:
!            go def_monthaxis_days.jnl NOLEAP 2002 2005 mytaxis 
!            go def_monthaxis_days.jnl ALL_LEAP 2002 2005 mytaxis
!            go def_monthaxis_days.jnl 360_DAY 2002 2005 mytaxis 
!
!
! Calls:
!
! Author: Patrick Brockmann
! Contact: Patrick.Brockmann@xxxxxx
! $Date: $
! $Name: $
! $Revision: $ Jian Ma added Julian calendar on 07/28/2011
! History:
! Modification:
!
!**************************************************************

!=====================================================
let calendar=UPCASE("($01)")
let year_start=($02)
let year_end=($03)
def sym axisname=($04%monthaxis%)
let year_ref=($05%($02)%)

let nbyears=year_end-year_start+1

!=====================================================
IF `(calendar EQ "STANDARD") OR (calendar EQ "GREGORIAN")` THEN
	let indices = L[L=1:`1+nbyears*12`]
	let month = MOD(indices-1,12)+1
	let year = year_start + INT((indices-1)/12)
	define axis/units="days"/T0="01-JAN-`year_ref`"/edges/cal="GREGORIAN" ($axisname) = DAYS1900(year,month,1)-DAYS1900(`year_ref`,1,1)

!=====================================================
ELIF `(calendar EQ "JULIAN")` THEN
	let indices = L[L=1:`1+nbyears*12`]
	let month = MOD(indices-1,12)+1
	let year = year_start + INT((indices-1)/12)

	let JLN=DAYS1900(year,month,1)-DAYS1900(`year_ref`,1,1)+int(year/100)-int(year/400)-int(`year_ref`/100)+int(`year_ref`/400)
	let days1900_JLN=if mod(year,100) eq 0 and mod(year,400) ne 0 and month le 2 then JLN else JLN+1
	define axis/units="days"/T0="01-JAN-`year_ref`"/edges/cal="JULIAN" ($axisname) = days1900_JLN

!=====================================================
ELIF `(calendar EQ "NOLEAP") OR (calendar EQ "365_DAY")` THEN
	let nbdays={31,28,31,30,31,30,31,31,30,31,30,31}
	define axis/units="days"/T0="01-JAN-`year_ref`"/edges/cal="NOLEAP" ($axisname) = `(year_start-year_ref)*365`+xcat(0,xsequence(365*(j[j=1:`nbyears`]-1)+nbdays[i=@rsum]))

!=====================================================
ELIF `(calendar EQ "ALL_LEAP") OR (calendar EQ "366_DAY")` THEN
	let nbdays={31,29,31,30,31,30,31,31,30,31,30,31}
	define axis/units="days"/T0="01-JAN-`year_ref`"/edges/cal="ALL_LEAP" ($axisname) = `(year_start-year_ref)*366`+xcat(0,xsequence(366*(j[j=1:`nbyears`]-1)+nbdays[i=@rsum]))

!=====================================================
ELIF `calendar EQ "360_DAY"` THEN
	define axis/units="days"/T0="01-JAN-`year_ref`"/edges/cal="360_DAY"/t="01-JAN-`year_start`":"30-DEC-`year_end`":30 ($axisname)

!=====================================================
ELSE
 	say "`calendar` not known"
 	say "Possible calendars are : STANDARD (or GREGORIAN), JULIAN, NOLEAP (or 365_DAY), ALL_LEAP (or 366_DAY), 360_DAY"
	exit

!=====================================================
ENDIF

show axis ($axisname)

!**************************************************************
set mode/last verify


[Thread Prev][Thread Next][Index]
Contact Us
Dept of Commerce / NOAA / OAR / PMEL / Ferret

Privacy Policy | Disclaimer | Accessibility Statement