days_in_month#

Warning

This is not meant to be a standalone notebook. This notebook is part of the process we have for adding entries to the NCL Index and is not meant to be used as tutorial or example code.

Functions covered#

days_in_month

NCL code#

; Adapted from https://www.ncl.ucar.edu/Document/Functions/Built-in/days_in_month.shtml

year  = (/1996, 1997, 1, 1500/)
month = (/ 2, 2, 1, 2/)

year@calendar = "standard"
dim = days_in_month(year,month)  ; dim = (/29,28,31,28/)
print(dim)

year@calendar = "noleap"
dim = days_in_month(year,month)  ; dim = (/28,28,31,28/)
print(dim)

year@calendar = "all_leap"
dim = days_in_month(year,month)  ; dim = (/29,29,31,29/)
print(dim)

year@calendar = "360_day"
dim = days_in_month(year,month)  ; dim = (/30,30,30,30/)
print(dim)

year@calendar = "gregorian"
dim = days_in_month(year,month)  ; dim = (/29,28,31,28/)
print(dim)

year@calendar = "julian"
dim = days_in_month(year,month)  ; dim = (/29,28,31,29/)
print(dim)

year@calendar = "365_day"
dim = days_in_month(year,month)  ; dim = (/28,28,31,28/)
print(dim)

year@calendar = "366_day"
dim = days_in_month(year,month)  ; dim = (/29,29,31,29/)
print(dim)

year@calendar = "none"
dim = days_in_month(year,month)  ; dim = (/29,28,31,28/)
print(dim)
# From the above code snippet
ncl_year = [1996, 1997, 1, 1500]
ncl_month = [2, 2, 1, 2]

# dictionary of calendars to results
ncl_results = {
    "standard": [29, 28, 31, 28],
    "noleap": [28, 28, 31, 28],
    "all_leap": [29, 29, 31, 29],
    "360_day": [30, 30, 30, 30],
    "gregorian": [29, 28, 31, 28],
    "julian": [29, 28, 31, 29],
    "365_day": [28, 28, 31, 28],
    "366_day": [29, 29, 31, 29],
    "none": [29, 28, 31, 28],
}

Python Functionality#

import cftime
from pprint import pprint

day = 1
cals = sorted(list(ncl_results.keys()))
cals.remove("none")
cals.append("proleptic_gregorian")
dim = [
    [
        cftime.datetime(year, month, day, calendar=c).daysinmonth
        for year, month in zip(ncl_year, ncl_month)
    ]
    for c in cals
]
results = {cal: days for cal, days in zip(cals, dim)}

pprint(results)
{'360_day': [30, 30, 30, 30],
 '365_day': [28, 28, 31, 28],
 '366_day': [29, 29, 31, 29],
 'all_leap': [29, 29, 31, 29],
 'gregorian': [29, 28, 31, 29],
 'julian': [29, 28, 31, 29],
 'noleap': [28, 28, 31, 28],
 'proleptic_gregorian': [29, 28, 31, 28],
 'standard': [29, 28, 31, 29]}

Comparison#

for c in ncl_results.keys() & results.keys():
    print(f"{c}: \n\tpython:\t{results[c]}\n\tncl:\t{ncl_results[c]}\n")

for c in ncl_results.keys() - results.keys():
    print(f"{c}: \n\tncl:\t{ncl_results[c]}\n")

for c in results.keys() - ncl_results.keys():
    print(f"{c}: \n\tpython:\t{results[c]}\n")
julian: 
	python:	[29, 28, 31, 29]
	ncl:	[29, 28, 31, 29]

standard: 
	python:	[29, 28, 31, 29]
	ncl:	[29, 28, 31, 28]

360_day: 
	python:	[30, 30, 30, 30]
	ncl:	[30, 30, 30, 30]

365_day: 
	python:	[28, 28, 31, 28]
	ncl:	[28, 28, 31, 28]

noleap: 
	python:	[28, 28, 31, 28]
	ncl:	[28, 28, 31, 28]

366_day: 
	python:	[29, 29, 31, 29]
	ncl:	[29, 29, 31, 29]

all_leap: 
	python:	[29, 29, 31, 29]
	ncl:	[29, 29, 31, 29]

gregorian: 
	python:	[29, 28, 31, 29]
	ncl:	[29, 28, 31, 28]

none: 
	ncl:	[29, 28, 31, 28]

proleptic_gregorian: 
	python:	[29, 28, 31, 28]

Differences#

for c in ncl_results.keys() | results.keys():
    if c in ncl_results.keys() & results.keys() and results[c] != ncl_results[c]:
        print(f"{c}: {results[c]} != {ncl_results[c]}")
    elif c in ncl_results.keys() - results.keys():
        print(f"{c}: calendar not available in python")
    elif c in results.keys() - ncl_results.keys():
        print(f"{c}: calendar not available in ncl")
standard: [29, 28, 31, 29] != [29, 28, 31, 28]
none: calendar not available in python
proleptic_gregorian: calendar not available in ncl
gregorian: [29, 28, 31, 29] != [29, 28, 31, 28]