Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
Oceano2python
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
US191
Oceano2python
Commits
8337999b
Commit
8337999b
authored
5 years ago
by
jacques.grelet_ird.fr
Browse files
Options
Downloads
Patches
Plain Diff
add conversion toolbox
parent
7bfc83db
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
tests/test_tools.py
+23
-0
23 additions, 0 deletions
tests/test_tools.py
tools.py
+131
-0
131 additions, 0 deletions
tools.py
with
154 additions
and
0 deletions
tests/test_tools.py
0 → 100644
+
23
−
0
View file @
8337999b
import
unittest
from
datetime
import
datetime
import
tools
class
ToolsTests
(
unittest
.
TestCase
):
def
test_julian2dt
(
self
):
"""
Tests a simple of julian day 0, origine 1950
"""
dt
=
datetime
(
year
=
1950
,
day
=
1
,
month
=
1
)
self
.
assertAlmostEqual
(
0
,
tools
.
dt2julian
(
dt
))
def
test_dt2julian
(
self
):
dt
=
tools
.
julian2dt
(
0
)
self
.
assertEqual
(
dt
.
year
,
1950
)
self
.
assertEqual
(
dt
.
month
,
1
)
self
.
assertEqual
(
dt
.
day
,
1
)
self
.
assertEqual
(
dt
.
hour
,
0
)
self
.
assertEqual
(
dt
.
minute
,
0
)
self
.
assertEqual
(
dt
.
second
,
0
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
tools.py
0 → 100644
+
131
−
0
View file @
8337999b
# tools.py
import
re
import
math
from
datetime
import
datetime
import
julian
JULIAN
=
33282
def
dateTime2julian
(
month
,
day
,
year
,
hour
,
minute
,
second
):
# initialize datetime object
dt
=
datetime
# format date and time to "May 09 2011 16:33:53"
dateTime
=
"
%s/%s/%s %s:%s:%s
"
%
(
day
,
month
,
year
,
hour
,
minute
,
second
)
# dateTime conversion to "09/05/2011 16:33:53"
dateTime
=
"
%s
"
%
\
(
dt
.
strptime
(
dateTime
,
"
%d/%b/%Y %H:%M:%S
"
).
strftime
(
"
%d/%m/%Y %H:%M:%S
"
))
# conversion to "20110509163353"
epic_date
=
"
%s
"
%
\
(
dt
.
strptime
(
dateTime
,
"
%d/%m/%Y %H:%M:%S
"
).
strftime
(
"
%Y%m%d%H%M%S
"
))
# conversion to julian day
julian
=
float
((
dt
.
strptime
(
dateTime
,
"
%d/%m/%Y %H:%M:%S
"
).
strftime
(
"
%j
"
)))
\
+
((
float
(
hour
)
*
3600.
)
+
(
float
(
minute
)
*
60.
)
+
float
(
second
))
/
86400.
# we use julian day with origine 0
julian
-=
1
return
julian
def
julian2dt
(
jd
):
# see: https://en.wikipedia.org/wiki/Julian_day
# Julian Date 12h Jan 1, 4713 BC
# Modified JD 0h Nov 17, 1858 JD − 2400000.5
# CNES JD 0h Jan 1, 1950 JD − 2433282.5
jd
=
jd
+
JULIAN
dt
=
julian
.
from_jd
(
jd
,
fmt
=
'
mjd
'
)
return
dt
def
dt2julian
(
dt
):
jd
=
julian
.
to_jd
(
dt
,
fmt
=
'
mjd
'
)
jd
=
jd
-
JULIAN
return
jd
def
dateTime2epic
(
month
,
day
,
year
,
hour
,
minute
,
second
):
# initialize datetime object
dt
=
datetime
# format date and time to "May 09 2011 16:33:53"
dateTime
=
"
%s/%s/%s %s:%s:%s
"
%
(
day
,
month
,
year
,
hour
,
minute
,
second
)
# dateTime conversion to "09/05/2011 16:33:53"
dateTime
=
"
%s
"
%
\
(
dt
.
strptime
(
dateTime
,
"
%d/%b/%Y %H:%M:%S
"
).
strftime
(
"
%d/%m/%Y %H:%M:%S
"
))
# conversion to "20110509163353"
epic_date
=
"
%s
"
%
\
(
dt
.
strptime
(
dateTime
,
"
%d/%m/%Y %H:%M:%S
"
).
strftime
(
"
%Y%m%d%H%M%S
"
))
return
epic_date
# Dec2dmc convert decimal position to degree, mim with centieme string,
# hemi = 0 for latitude, 1 for longitude
def
Dec2dmc
(
position
,
hemi
):
if
re
.
match
(
'
[EW]
'
,
hemi
):
neg
=
'
W
'
pos
=
'
E
'
else
:
neg
=
'
S
'
pos
=
'
N
'
if
position
<
0
:
geo
=
neg
else
:
geo
=
pos
# get integer and decimal part
dec
,
intg
=
math
.
modf
(
position
)
dec
=
abs
(
dec
)
intg
=
abs
(
intg
)
if
re
.
match
(
'
[EW]
'
,
hemi
):
str
=
"
{:0>3.0f}°{:0>7.4f} {}
"
.
format
(
intg
,
(
dec
/
100
)
*
6000
,
geo
)
else
:
str
=
"
{:0>2.0f}°{:0>7.4f} {}
"
.
format
(
intg
,
(
dec
/
100
)
*
6000
,
geo
)
return
str
# Dec2dms convert decimal position to degree, mim with second string,
# hemi = 0 for latitude, 1 for longitude
def
Dec2dms
(
position
,
hemi
):
if
re
.
match
(
'
[EW]
'
,
hemi
):
neg
=
'
W
'
pos
=
'
E
'
else
:
neg
=
'
S
'
pos
=
'
N
'
if
position
<
0
:
geo
=
neg
else
:
geo
=
pos
# get integer and decimal part
dec
,
intg
=
math
.
modf
(
position
)
# get integer and decimal part of min.sec
sec
,
min
=
math
.
modf
((
dec
/
100
)
*
6000
)
if
re
.
match
(
'
[EW]
'
,
hemi
):
str
=
"
{:0>3.0f}°{:2.4f} {}
"
.
format
(
intg
,
min
+
sec
/
100
*
60
,
geo
)
else
:
str
=
"
{:0>2.0f}°{:2.4f} {}
"
.
format
(
intg
,
min
+
sec
/
100
*
60
,
geo
)
return
str
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