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:
- https://www.mathworks.com/help/matlab/data_analysis/missing-data-in-matlab.html
- https://www.mathworks.com/help/matlab/date-and-time-operations.html
I hope this helps you!
Thanks.