datenum using lunar calendar question

4 views (last 30 days)
peter huang
peter huang on 11 Apr 2022
Answered: Ashutosh Thakur on 21 Dec 2023
i want using lunar calendar in matlab
and want plot my tide data and using lunar calendar be my timeseries
but lunar calendar sometime have 13 months(leap year)
a leap year in the lunar calendar may have around 380 day
when i use datanum meet 13 months
corresponding time present NAN
How can I include 13 in my timeseries as well
attached is my time and water level information
A column is normal time (Gregorian calendar)
B column is lunar calendar
C column is water level data
Thanks in advance!

Answers (1)

Ashutosh Thakur
Ashutosh Thakur on 21 Dec 2023
Hi Peter,
I can understand that you want to plot your tide data with respect to lunar dates. Also the data you have contains NaN values.
I can suggest an algorithm to approach this issue:
  • First try to clean the data by treating the NaN values. NaN values can be handled using many ways such as removing them, taking average of values etc. It is upto you to decide how to handle NaN values.
  • Second is to create a Custom Mapping function as MATLAB inbuilt functions work on gregorian calendar. To create a custom map you have to decide on the number of months in a leap year, number of days in a month, cycle of a leap year etc and based on that try to convert gregorian date to lunar one. I have also provided a sample code to handle the same.
  • Third, is now to plot these data with respect to water levels by converting the final resultant data into "timeseries" data.
Below is the pseudocode for custom mapping of gregorian to lunar, this code may not handle all the cases and further modification may be required based on the requirements:
function lunarDate = gregorianToLunar(gregorianDate)
% Define the start date of the lunar calendar in Gregorian terms
lunarStart = datenum(2000, 1, 1); % Example start date (January 1, 2000)
% Define the number of days in each lunar month (for simplicity, all the same)
daysInLunarMonth = 29.53; % Average number of days in a lunar month
% Calculate the number of days since the start of the lunar calendar
daysSinceStart = gregorianDate - lunarStart;
% Define the lunar cycle (for example, every 3 years is a leap year)
lunarCycleYears = 3;
daysInLunarYear = 12 * daysInLunarMonth;
daysInLunarLeapYear = 13 * daysInLunarMonth;
% Calculate the current lunar year and the day within that year
cyclePeriod = lunarCycleYears * daysInLunarYear + daysInLunarLeapYear;
cyclesSinceStart = floor(daysSinceStart.Day / cyclePeriod);
dayWithinCycle = mod(daysSinceStart.Day, cyclePeriod);
% Calculate the number of leap years that have passed in the current cycle
leapYearsPassed = floor(dayWithinCycle / (lunarCycleYears * daysInLunarYear));
if leapYearsPassed >= lunarCycleYears
leapYearsPassed = lunarCycleYears - 1;
end
% Calculate the day within the current (leap) year
dayWithinYear = dayWithinCycle - leapYearsPassed * daysInLunarYear;
% Calculate the current lunar year
lunarYear = cyclesSinceStart * lunarCycleYears + leapYearsPassed;
% Calculate the current lunar month and day
lunarMonth = ceil(dayWithinYear / daysInLunarMonth);
lunarDay = mod(dayWithinYear, daysInLunarMonth);
if lunarDay == 0
lunarDay = daysInLunarMonth;
end
% Construct the lunar date as a string or a structure
lunarDate = struct('year', lunarYear, 'month', lunarMonth, 'day', floor(lunarDay));
end
Please take reference from the following documentation links for the reference:
I hope this helps you!
Thanks.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!