Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
ModSpa
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
CESBIO
Modélisation
ModSpa
Commits
0bd7068e
Commit
0bd7068e
authored
1 year ago
by
Jeremy Auclair
Browse files
Options
Downloads
Patches
Plain Diff
Few new additions
parent
7cfcd030
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
input/calculate_ndvi.py
+69
-12
69 additions, 12 deletions
input/calculate_ndvi.py
input/download_S2.py
+1
-1
1 addition, 1 deletion
input/download_S2.py
with
70 additions
and
13 deletions
input/calculate_ndvi.py
+
69
−
12
View file @
0bd7068e
...
@@ -9,30 +9,87 @@ Calculate NDVI images with xarray
...
@@ -9,30 +9,87 @@ Calculate NDVI images with xarray
import
os
# for path exploration
import
os
# for path exploration
import
csv
# open csv files
import
csv
# open csv files
from
fnmatch
import
fnmatch
# for character string comparison
from
typing
import
List
,
Union
# to declare variables
from
typing
import
List
,
Union
# to declare variables
import
xarray
as
xr
# to manage dataset
import
xarray
as
xr
# to manage dataset
import
dask.array
as
da
# dask xarray
import
pandas
as
pd
# to manage dataframes
from
dask.distributed
import
Client
,
LocalCluster
# to parallelise calculations
# import dask.array as da # dask xarray
from
code.toolbox
import
product_str_to_datetime
def
calculate_ndvi
(
downloa
d_path
:
Union
[
List
[
str
],
str
])
->
str
:
def
calculate_ndvi
(
extracte
d_path
s
:
Union
[
List
[
str
],
str
]
,
save_dir
:
str
,
resolution
:
int
=
20
,
chunk_size
:
dict
=
{
'
x
'
:
4000
,
'
y
'
:
4000
,
'
time
'
:
2
},
acorvi_corr
:
int
=
500
)
->
str
:
# Start local cluster client
client
=
Client
()
client
# Check resolution for Sentinel-2
if
not
resolution
in
[
10
,
20
]:
print
(
'
Resolution should be equal to 10 or 20 meters for sentinel-2
'
)
return
None
# If a file name is provided instead of a list of paths, load the csv file that contains the list of paths
# If a file name is provided instead of a list of paths, load the csv file that contains the list of paths
if
type
(
downloa
d_path
)
==
str
:
if
type
(
extracte
d_path
s
)
==
str
:
with
open
(
downloa
d_path
,
'
r
'
)
as
file
:
with
open
(
extracte
d_path
s
,
'
r
'
)
as
file
:
downloa
d_path
=
[]
extracte
d_path
s
=
[]
csvreader
=
csv
.
reader
(
file
,
delimiter
=
'
\n
'
)
csvreader
=
csv
.
reader
(
file
,
delimiter
=
'
\n
'
)
for
row
in
csvreader
:
for
row
in
csvreader
:
download_path
.
append
(
row
[
0
])
extracted_paths
.
append
(
row
[
0
])
# Sort by band
red_paths
=
[]
nir_paths
=
[]
mask_paths
=
[]
if
resolution
==
10
:
for
product
in
extracted_paths
:
if
fnmatch
(
product
,
'
*_B04_10m*
'
):
red_paths
.
append
(
product
)
elif
fnmatch
(
product
,
'
*_B8A_20m*
'
):
nir_paths
.
append
(
product
)
elif
fnmatch
(
product
,
'
*_SCL_20m*
'
):
mask_paths
.
append
(
product
)
else
:
for
product
in
extracted_paths
:
if
fnmatch
(
product
,
'
*_B04_10m*
'
):
red_paths
.
append
(
product
)
elif
fnmatch
(
product
,
'
*_B08_10m*
'
):
nir_paths
.
append
(
product
)
elif
fnmatch
(
product
,
'
*_SCL_20m*
'
):
mask_paths
.
append
(
product
)
# Sort and get dates
red_paths
.
sort
()
nir_paths
.
sort
()
mask_paths
.
sort
()
dates
=
[
product_str_to_datetime
(
prod
)
for
prod
in
red_paths
]
products
=
xr
.
open_mfdataset
(
download_path
,
parallel
=
True
)
#, chunks = {'latitude': 100, 'longitude': 100})
# Open datasets with xarray
red
=
xr
.
open_mfdataset
(
red_paths
,
combine
=
'
nested
'
,
concat_dim
=
'
time
'
,
chunks
=
chunk_size
).
squeeze
(
dim
=
[
'
band
'
],
drop
=
True
).
rename
({
'
band_data
'
:
'
red
'
}).
astype
(
'
f4
'
)
nir
=
xr
.
open_mfdataset
(
nir_paths
,
combine
=
'
nested
'
,
concat_dim
=
'
time
'
,
chunks
=
chunk_size
).
squeeze
(
dim
=
[
'
band
'
],
drop
=
True
).
rename
({
'
band_data
'
:
'
nir
'
}).
astype
(
'
f4
'
)
mask
=
xr
.
open_mfdataset
(
mask_paths
,
combine
=
'
nested
'
,
concat_dim
=
'
time
'
,
chunks
=
chunk_size
).
squeeze
(
dim
=
[
'
band
'
],
drop
=
True
).
rename
({
'
band_data
'
:
'
mask
'
}).
astype
(
'
f4
'
)
if
resolution
==
10
:
mask
=
xr
.
where
((
mask
==
4
)
|
(
mask
==
5
),
1
,
0
).
interp
(
x
=
red
.
coords
[
'
x
'
],
y
=
red
.
coords
[
'
y
'
],
method
=
'
nearest
'
)
else
:
mask
=
xr
.
where
((
mask
==
4
)
|
(
mask
==
5
),
1
,
0
)
# Set time coordinate
red
[
'
time
'
]
=
pd
.
to_datetime
(
dates
)
nir
[
'
time
'
]
=
pd
.
to_datetime
(
dates
)
mask
[
'
time
'
]
=
pd
.
to_datetime
(
dates
)
# Create ndvi dataset and calculate ndvi
ndvi
=
red
ndvi
=
ndvi
.
drop
(
'
red
'
)
ndvi
[
'
ndvi
'
]
=
(((
nir
.
nir
-
red
.
red
-
acorvi_corr
)
/
(
nir
.
nir
+
red
.
red
+
acorvi_corr
))
*
mask
.
mask
)
del
red
,
nir
,
mask
# Mask and scale ndvi
ndvi
[
'
ndvi
'
]
=
xr
.
where
(
ndvi
.
ndvi
<
0
,
0
,
ndvi
.
ndvi
)
ndvi
[
'
ndvi
'
]
=
xr
.
where
(
ndvi
.
ndvi
>
1
,
1
,
ndvi
.
ndvi
)
ndvi
[
'
ndvi
'
]
=
ndvi
.
ndvi
*
255
# Create save path
ndvi_cube_path
=
save_dir
+
os
.
sep
+
'
NDVI_precube_
'
+
dates
[
0
].
strftime
(
'
%d-%m-%Y
'
)
+
'
_
'
+
dates
[
-
1
].
strftime
(
'
%d-%m-%Y
'
)
+
'
.nc
'
ndvi_cube_path
=
''
# Save NDVI cude to netcdf
ndvi
.
to_netcdf
(
ndvi_cube_path
,
encoding
=
{
"
ndvi
"
:
{
"
dtype
"
:
"
u8
"
,
"
_FillValue
"
:
0
}})
ndvi
.
close
()
return
ndvi_cube_path
return
ndvi_cube_path
\ No newline at end of file
This diff is collapsed.
Click to expand it.
input/download_S2.py
+
1
−
1
View file @
0bd7068e
...
@@ -130,7 +130,7 @@ def extract_zip_archives(download_path: str, list_paths: List[str], bands_to_ext
...
@@ -130,7 +130,7 @@ def extract_zip_archives(download_path: str, list_paths: List[str], bands_to_ext
for
file_path
in
list_paths
:
for
file_path
in
list_paths
:
# Change progress bar to print current file
# Change progress bar to print current file
progress_bar
.
set_description_str
(
desc
=
'
Extracting
'
+
os
.
path
.
basename
(
file_path
)
+
'
\n
total progress
'
)
progress_bar
.
set_description_str
(
desc
=
'
\r
Extracting
'
+
os
.
path
.
basename
(
file_path
)
+
'
\n
total progress
'
)
# Get path in which to extract the archive
# Get path in which to extract the archive
extract_path
=
download_path
+
os
.
sep
+
os
.
path
.
basename
(
file_path
)[:
-
4
]
extract_path
=
download_path
+
os
.
sep
+
os
.
path
.
basename
(
file_path
)[:
-
4
]
...
...
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