Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Persefone.jl
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Repository analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Persefone
Persefone.jl
Commits
2fccb9f8
Commit
2fccb9f8
authored
11 months ago
by
xo30xoqa
Browse files
Options
Downloads
Patches
Plain Diff
Moved date and unit functions to core/util.jl
parent
70a9efc8
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Persefone.jl
+1
-14
1 addition, 14 deletions
src/Persefone.jl
src/core/utils.jl
+60
-0
60 additions, 0 deletions
src/core/utils.jl
with
61 additions
and
14 deletions
src/Persefone.jl
+
1
−
14
View file @
2fccb9f8
...
@@ -107,20 +107,6 @@ export
...
@@ -107,20 +107,6 @@ export
cropcover
,
cropcover
,
cropyield
cropyield
## Import and define units and dimensions
import
Unitful
:
cm
,
m
,
km
,
ha
,
mg
,
g
,
kg
,
Length
,
Area
,
Mass
const
m²
=
m
^
2
const
km²
=
km
^
2
import
Base
./
# enable division with different length/area unit types
/
(
x
::
S
,
y
::
T
)
where
{
S
<:
Length
,
T
<:
Length
}
=
(
upreferred
(
x
)
/
m
)
/
(
upreferred
(
y
)
/
m
)
/
(
x
::
S
,
y
::
T
)
where
{
S
<:
Area
,
T
<:
Area
}
=
(
upreferred
(
x
)
/
m²
)
/
(
upreferred
(
y
)
/
m²
)
/
(
x
::
S
,
y
::
T
)
where
{
S
<:
Mass
,
T
<:
Mass
}
=
(
upreferred
(
x
)
/
g
)
/
(
upreferred
(
y
)
/
g
)
## Utility type and function for working wth recurring dates
const
AnnualDate
=
Tuple
{
Int64
,
Int64
}
function
thisyear
(
ad
::
AnnualDate
,
model
::
SimulationModel
)
=
Date
(
year
(
model
.
date
),
ad
...
)
function
nextyear
(
ad
::
AnnualDate
,
model
::
SimulationModel
)
=
Date
(
year
(
model
.
date
)
+
Year
(
1
),
ad
...
)
"""
"""
SimulationModel
SimulationModel
...
@@ -141,6 +127,7 @@ function stepagent! end
...
@@ -141,6 +127,7 @@ function stepagent! end
## include all module files (note that the order matters - if file
## include all module files (note that the order matters - if file
## b references something from file a, it must be included later)
## b references something from file a, it must be included later)
include
(
"core/utils.jl"
)
include
(
"core/input.jl"
)
include
(
"core/input.jl"
)
include
(
"core/output.jl"
)
include
(
"core/output.jl"
)
include
(
"analysis/makieplots.jl"
)
include
(
"analysis/makieplots.jl"
)
...
...
This diff is collapsed.
Click to expand it.
src/core/utils.jl
0 → 100644
+
60
−
0
View file @
2fccb9f8
### Persefone.jl - a model of agricultural landscapes and ecosystems in Europe.
###
### This file holds various utility types and functions, especially for dealing with
### units and dates.
###
## Import and define units and dimensions
import
Unitful
:
cm
,
m
,
km
,
ha
,
mg
,
g
,
kg
,
Length
,
Area
,
Mass
const
m²
=
m
^
2
const
km²
=
km
^
2
import
Base
./
# enable division with different length/area unit types
/
(
x
::
S
,
y
::
T
)
where
{
S
<:
Length
,
T
<:
Length
}
=
(
upreferred
(
x
)
/
m
)
/
(
upreferred
(
y
)
/
m
)
/
(
x
::
S
,
y
::
T
)
where
{
S
<:
Area
,
T
<:
Area
}
=
(
upreferred
(
x
)
/
m²
)
/
(
upreferred
(
y
)
/
m²
)
/
(
x
::
S
,
y
::
T
)
where
{
S
<:
Mass
,
T
<:
Mass
}
=
(
upreferred
(
x
)
/
g
)
/
(
upreferred
(
y
)
/
g
)
## Utility type and function for working wth recurring dates
"""
AnnualDate
A type to handle recurring dates (e.g. migration, harvest).
Stores a month and a day, and can be compared against normal dates.
"""
mutable struct
AnnualDate
month
::
Int64
day
::
Int64
# inner constructor to make sure inputs are in range
AnnualDate
(
month
::
Int64
,
day
::
Int64
)
=
if
!
(
0
<
month
<=
12
)
Base
.
error
(
"AnnualDate: month
$
month is out of range."
)
#TODO replace with exception
elseif
!
(
0
<
day
<=
31
)
Base
.
error
(
"AnnualDate: day
$
day is out of range."
)
#TODO replace with exception
else
new
(
month
,
day
)
end
end
# Interface with Dates
AnnualDate
(
date
::
Date
)
=
AnnualDate
(
month
(
date
),
day
(
date
))
Dates
.
month
(
ad
::
AnnualDate
)
=
ad
.
month
Dates
.
day
(
ad
::
AnnualDate
)
=
ad
.
day
# Instantiate a recurring date for a given year
thisyear
(
ad
::
AnnualDate
,
model
::
SimulationModel
)
=
Date
(
year
(
model
.
date
),
ad
.
month
,
ad
.
day
)
nextyear
(
ad
::
AnnualDate
,
model
::
SimulationModel
)
=
Date
(
year
(
model
.
date
)
+
1
,
ad
.
month
,
ad
.
day
)
lastyear
(
ad
::
AnnualDate
,
model
::
SimulationModel
)
=
Date
(
year
(
model
.
date
)
-
1
,
ad
.
month
,
ad
.
day
)
# Comparison between AnnualDates and with Dates
Base
.:
(
==
)(
ad1
::
AnnualDate
,
ad2
::
AnnualDate
)
=
(
month
(
ad1
)
==
month
(
ad2
)
&&
day
(
ad1
)
==
day
(
ad2
))
Base
.:
(
==
)(
ad
::
AnnualDate
,
d
::
Date
)
=
(
month
(
d
)
==
month
(
ad
)
&&
day
(
d
)
==
day
(
ad
))
Base
.:
(
==
)(
d
::
Date
,
ad
::
AnnualDate
)
=
(
ad
==
d
)
Base
.:
(
<
)(
ad1
::
AnnualDate
,
ad2
::
AnnualDate
)
=
(
month
(
ad1
)
<
month
(
ad2
)
||
(
month
(
ad1
)
==
month
(
ad2
)
&&
day
(
ad1
)
<
day
(
ad2
)))
Base
.:
(
<
)(
ad
::
AnnualDate
,
d
::
Date
)
=
(
thisyear
(
ad
)
<
d
)
Base
.:
(
<
)(
d
::
Date
,
ad
::
AnnualDate
)
=
(
d
<
thisyear
(
ad
))
# Addition and subtraction of date periods
Base
.:
(
+
)(
ad
::
AnnualDate
,
time
::
DatePeriod
)
=
AnnualDate
(
thisyear
(
ad
)
+
time
)
Base
.:
(
-
)(
ad
::
AnnualDate
,
time
::
DatePeriod
)
=
AnnualDate
(
thisyear
(
ad
)
-
time
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment