forked from fluxnet/ONEFlux
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python translation of fcDatetick (plus some extra testing for datenum and a fix) #103
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
2ae978b
module names and scope (imports)
dorchard 96b6cfd
fix different notion of indexing
dorchard 0d74f79
convert some utils to not use matlabarrys, just nparrays
dorchard 86606ac
some data changes?
dorchard b2d0bc5
leverage matplotlib; cleanup
dorchard ae49b7b
import
dorchard e08c7ef
Merge conflicts resolved
dorchard f23c012
use the translate datenum
dorchard 88d9656
rename fcDatenum to datenum to allow testing
dorchard b218c28
tests for our datenum against MATLAB
dorchard c9c4bf6
fix to datenum to allow months > 12"
dorchard aaaff07
fixes to translation
dorchard 556f158
tidy up and docs
dorchard 1f08d22
Merge __init__.py
dorchard 886535c
Remooved type 'm' at the beggining of function file
isaacaka 38799c9
Fixed overwritten data
isaacaka 7726f2a
Merge branch 'ustar_cp_refactor_main' into python-fcDatetick
dorchard 1211639
type signatures and use some numpy instead
dorchard 43fd83e
specialise imports
dorchard 2ac7646
add typing
dorchard 567dbe0
use numpy mod
dorchard 48fbf89
fix tests to be more general
dorchard 2e3fadc
more types
dorchard 3da982d
type hints fixed
dorchard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
from oneflux_steps.ustar_cp_python.utils import floor, ceil, dot, arange, xlim, unique | ||
from oneflux_steps.ustar_cp_python.fcDatevec import fcDatevec | ||
from oneflux_steps.ustar_cp_python.fcDatenum import datenum | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
import datetime | ||
|
||
def fcDatetick(t : float | np.ndarray, sFrequency : str, iDateStr : int, fLimits : float): | ||
""" | ||
Generate date ticks for a plot based on the given time vector and frequency. | ||
|
||
This function generates date ticks for a plot based on the provided time vector `t`, | ||
the frequency `sFrequency`, the date string format `iDateStr`, and the plot limits `fLimits`. | ||
It replicates some minimal behavior from the previous codebase for plotting purposes. | ||
|
||
Parameters: | ||
t (array-like): Time vector. | ||
sFrequency (str): Frequency of the ticks. Possible values are "Dy", "14Dy", "Mo". | ||
iDateStr (int): Date string format. | ||
fLimits (float): Limits of the plot. | ||
|
||
Returns: | ||
None | ||
|
||
Notes: | ||
- This function is *not* used in the rest of the Python code. | ||
- It is *not* thoroughly tested and only replicates some minimal behavior from the previous codebase for plotting. | ||
|
||
Examples: | ||
>>> t = list(range(0, 49)) | ||
>>> sFrequency = "Mo" | ||
>>> iDateStr = 4 | ||
>>> fLimits = 1 | ||
>>> fcDatetick(t, sFrequency, iDateStr, fLimits) | ||
""" | ||
|
||
y, m, d, h, mn, s = fcDatevec(t) | ||
iYrs = unique(y) | ||
iSerMos = dot((y - 1), 12) + m | ||
iSerMo1 = min(iSerMos) | ||
iSerMo2 = max(iSerMos) | ||
nSerMos = iSerMo2 - iSerMo1 + 1 | ||
xDates = np.array([]) | ||
|
||
match (sFrequency): | ||
case "Dy": | ||
xDates = t[::48] | ||
|
||
case "14Dy": | ||
iYr1 = floor(iSerMo1 / 12) + 1 | ||
iMo1 = iSerMo1 % 12 | ||
if iMo1 == 0: | ||
iMo1 = 12 | ||
iYr1 = iYr1 - 1 | ||
for iDy in arange(1, 15, 14).reshape(-1): | ||
xDates = [datenum(iYr1, int(month), iDy) for month in arange(iMo1, (iMo1 + nSerMos))] | ||
|
||
case "Mo": | ||
iYr1 = floor(iSerMo1 / 12) + 1 | ||
iMo1 = iSerMo1 % 12 | ||
if iMo1 == 0: | ||
iMo1 = 12 | ||
iYr1 = iYr1 - 1 | ||
# define xDates as the array given my mapping over | ||
# every month in arange(iMo1, (iMo1 + nSerMos)) | ||
# and applying `datenum(iYr1, month, 1)` to it | ||
xDates = [datenum(iYr1, int(month), 1) for month in arange(iMo1, (iMo1 + nSerMos))] | ||
# oneflux_steps/ustar_cp_refactor_wip/fcDatetick.m:36 | ||
# # # datestr(xDates) | ||
# # # datestr([min(t) max(t)]) | ||
# # # pause; | ||
|
||
xDates = unique(xDates) | ||
|
||
# Set current `x` access to have values given by xDates | ||
plt.gca().set_xticks(xDates) | ||
# set label to empty | ||
plt.gca().set_xticklabels([]) | ||
if iDateStr > 0: | ||
# compute a datestring for each xDates based on iDateStr | ||
# TODO: this is different to the original MATLAB code which was | ||
# cDates = datestr(xDates, iDateStr) | ||
|
||
# convert from a date floating-point | ||
# ordinal to a datetime object | ||
cDates = [datetime.datetime.fromordinal(floor(t) + 1).strftime("%Y-%m-%d") for t in xDates] | ||
|
||
plt.gca().set_xticklabels(cDates) | ||
|
||
if fLimits == 1: | ||
xlim([floor(min(xDates)), ceil(max(xDates))]) | ||
# Turn on the grid and box using matplotlib | ||
plt.grid("on") | ||
plt.box("on") | ||
|
||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typehints