Skip to content

Julia Library

The underlying algorithm of the FAIRSenDD workflow is implemented in the Julia package RQADeforestation.jl. This page shows how to use this package.

NOTE

Documentation about the individual methods of RQADeforestation.jl](https://github.com/EarthyScience/RQADeforestation.jl) are shown here.

Installation

Install the Julia library:

julia
using Pkg
Pkg.add(url="https://github.com/EarthyScience/RQADeforestation.jl.git")

Mock data set

Compute the RQA trend metric of a mock data:

julia
using RQADeforestation
using Dates
using DimensionalData: Ti, X, Y
using Statistics
using YAXArrays
import Random

Random.seed!(1337)
Random.TaskLocalRNG()
julia
mock_axes = (
    Ti(Date("2022-01-01"):Day(1):Date("2022-01-30")),
    X(range(1, 10, length=10)),
    Y(range(1, 5, length=15)),
)
(↓ Ti Date("2022-01-01"):Day(1):Date("2022-01-30"),
→ X  1.0:1.0:10.0,
↗ Y  1.0:0.2857142857142857:5.0)
julia

mock_data = rand(30, 10, 15)
30×10×15 Array{Float64, 3}:
[:, :, 1] =
 0.349231   0.53876    0.878318  …  0.316947   0.459357    0.229587
 0.854846   0.853795   0.682241     0.176719   0.0690835   0.0792883
 0.85342    0.13409    0.865562     0.480683   0.381826    0.528175
 0.733944   0.0061554  0.190508     0.453772   0.915689    0.259446
 0.330468   0.270052   0.828653     0.424396   0.854763    0.798935
 0.869457   0.165995   0.995626  …  0.483512   0.33265     0.547669
 0.870173   0.204482   0.678454     0.475502   0.724284    0.596102
 0.303946   0.206789   0.65602      0.0596838  0.566698    0.0520711
 0.0645162  0.6311     0.492919     0.871871   0.381627    0.0267235
 0.25354    0.910679   0.697177     0.999553   0.644935    0.59105
 ⋮                               ⋱                         
 0.826243   0.129101   0.159458     0.639182   0.44967     0.827472
 0.806943   0.0978743  0.49419      0.852151   0.00287023  0.478299
 0.0278994  0.937788   0.213223     0.72657    0.465386    0.074507
 0.578662   0.366963   0.270639     0.266752   0.591159    0.517302
 0.575956   0.10846    0.583266  …  0.0491781  0.500742    0.946877
 0.577588   0.321257   0.861161     0.748045   0.340308    0.55718
 0.156431   0.174649   0.917735     0.261341   0.419091    0.0870799
 0.412377   0.587156   0.114767     0.679095   0.861415    0.663294
 0.698565   0.16979    0.188686     0.873404   0.297355    0.536483

[:, :, 2] =
 0.719633  0.554803   0.959282  …  0.0701651    0.187597   0.579269
 0.556281  0.945921   0.925642     0.0663647    0.200541   0.142791
 0.658504  0.510017   0.949134     0.30164      0.121371   0.723265
 0.825463  0.204921   0.29263      0.33332      0.525286   0.433413
 0.928991  0.443846   0.349444     0.421129     0.463668   0.0896193
 0.48039   0.239608   0.580068  …  0.000237287  0.479413   0.0780237
 0.994381  0.984162   0.963058     0.881044     0.652395   0.664047
 0.322587  0.293367   0.46344      0.991791     0.905462   0.473695
 0.131677  0.0338542  0.282544     0.813854     0.922409   0.336244
 0.547035  0.591687   0.398932     0.39691      0.708255   0.882775
 ⋮                              ⋱                          
 0.706008  0.602217   0.320035     0.998782     0.82829    0.224237
 0.560212  0.906582   0.735312     0.560713     0.689285   0.963959
 0.370577  0.0451018  0.214521     0.652623     0.195481   0.0757704
 0.572757  0.299181   0.007818     0.290158     0.80139    0.52169
 0.844344  0.579517   0.469722  …  0.736576     0.484784   0.991317
 0.410212  0.137359   0.131968     0.556747     0.52976    0.112784
 0.103006  0.507467   0.527073     0.6739       0.729163   0.624701
 0.42396   0.246394   0.391261     0.0651532    0.0854672  0.479656
 0.548192  0.403857   0.226385     0.594765     0.163033   0.689535

[:, :, 3] =
 0.0289064  0.928035   0.482867   …  0.160399   0.502716    0.955449
 0.659961   0.641217   0.599385      0.495633   0.588       0.496612
 0.283621   0.586305   0.0517645     0.397527   0.5945      0.87561
 0.294237   0.12976    0.355659      0.239232   0.194637    0.894809
 0.310909   0.848024   0.393003      0.29534    0.113107    0.247519
 0.188368   0.689566   0.158619   …  0.893654   0.334303    0.294527
 0.105138   0.757983   0.203031      0.036211   0.0804092   0.503413
 0.651792   0.667045   0.631245      0.114309   0.753078    0.136424
 0.714199   0.784795   0.832674      0.761691   0.172029    0.600892
 0.698484   0.0630183  0.955731      0.0716826  0.771984    0.380222
 ⋮                                ⋱                         
 0.0291846  0.0599276  0.60644       0.430985   0.00863431  0.151264
 0.86761    0.245671   0.0518249     0.893707   0.480923    0.336022
 0.643489   0.680982   0.658327      0.243787   0.239857    0.217585
 0.230068   0.696921   0.282045      0.627627   0.309246    0.812449
 0.407435   0.710805   0.484929   …  0.506167   0.902825    0.864996
 0.807022   0.85447    0.506255      0.145303   0.741847    0.196044
 0.381468   0.235268   0.753993      0.720927   0.873039    0.271128
 0.167371   0.0766603  0.646217      0.651222   0.854436    0.915326
 0.220362   0.240157   0.59031       0.780296   0.338959    0.099293

;;; … 

[:, :, 13] =
 0.713909  0.903516   0.67789   0.987033  …  0.583502   0.399564  0.789163
 0.894058  0.321799   0.606166  0.311672     0.339549   0.518162  0.898924
 0.395494  0.580128   0.211105  0.396929     0.377082   0.561409  0.921842
 0.360432  0.984619   0.799076  0.293696     0.183147   0.327336  0.770052
 0.17857   0.772695   0.381646  0.552411     0.853336   0.43313   0.787305
 0.817192  0.010263   0.06841   0.137673  …  0.445114   0.918452  0.824707
 0.792391  0.645626   0.498344  0.702505     0.252229   0.404009  0.212524
 0.467771  0.46003    0.795607  0.582668     0.0372337  0.716691  0.634013
 0.717692  0.436197   0.826876  0.950637     0.825964   0.100497  0.498408
 0.590578  0.426426   0.923046  0.608403     0.631093   0.487185  0.874344
 ⋮                                        ⋱                       
 0.554688  0.0609281  0.397758  0.470654     0.105955   0.370114  0.456764
 0.240164  0.985459   0.951708  0.537027     0.573347   0.405515  0.512396
 0.182516  0.521515   0.89279   0.610017     0.803759   0.69677   0.14951
 0.69162   0.050001   0.606757  0.196382     0.49241    0.891701  0.459325
 0.281797  0.734469   0.36333   0.581138  …  0.290878   0.560014  0.21403
 0.958465  0.372233   0.978233  0.115218     0.0791348  0.218001  0.0347856
 0.012386  0.530334   0.659164  0.752625     0.959979   0.492046  0.440306
 0.976713  0.631697   0.172045  0.582209     0.793404   0.395602  0.620002
 0.46619   0.86231    0.765553  0.159687     0.214054   0.440731  0.387494

[:, :, 14] =
 0.687001   0.263397   0.229972   …  0.0701951  0.224446   0.800358
 0.551125   0.966771   0.712678      0.291595   0.811207   0.737664
 0.698462   0.206513   0.956208      0.371947   0.405716   0.40435
 0.221142   0.653965   0.921858      0.683735   0.43258    0.404906
 0.3944     0.689613   0.293905      0.358279   0.0374832  0.996498
 0.24508    0.941915   0.800983   …  0.37618    0.66791    0.698731
 0.28369    0.577358   0.884003      0.445662   0.804163   0.944782
 0.1296     0.317574   0.828713      0.786471   0.110987   0.354739
 0.939389   0.708483   0.956031      0.919702   0.690103   0.320357
 0.916606   0.749578   0.829902      0.233021   0.250713   0.978975
 ⋮                                ⋱                        
 0.593984   0.348027   0.113496      0.299738   0.979595   0.743785
 0.540387   0.0898005  0.715995      0.666946   0.0256756  0.526923
 0.0793508  0.781614   0.447696      0.947472   0.575156   0.573902
 0.689955   0.364801   0.814366      0.0768869  0.545915   0.226187
 0.185779   0.200631   0.187115   …  0.806712   0.941441   0.346428
 0.459424   0.655608   0.486937      0.875332   0.222368   0.404671
 0.0315181  0.073493   0.623791      0.783414   0.489978   0.0430407
 0.273703   0.983768   0.989976      0.581092   0.254915   0.875668
 0.0373494  0.16087    0.0948844     0.761532   0.625235   0.236202

[:, :, 15] =
 0.0775874  0.201796   0.332825    …  0.562871    0.637997   0.324169
 0.678526   0.21403    0.120137       0.567513    0.537173   0.194041
 0.943194   0.536137   0.00228048     0.854036    0.882203   0.540476
 0.184119   0.378438   0.493203       0.511813    0.555967   0.107228
 0.385986   0.876526   0.0803014      0.325587    0.802279   0.603256
 0.420886   0.428179   0.250725    …  0.806581    0.590516   0.0489458
 0.308602   0.752626   0.440315       0.072761    0.88224    0.931432
 0.924107   0.577225   0.165051       0.323986    0.274916   0.499565
 0.0903383  0.817168   0.748459       0.836933    0.0429846  0.55796
 0.364819   0.380236   0.932925       0.639297    0.864002   0.589081
 ⋮                                 ⋱                         
 0.460268   0.819742   0.1964         0.00222107  0.646918   0.632462
 0.618858   0.217148   0.684913       0.718405    0.955232   0.507728
 0.521585   0.278905   0.703246       0.661446    0.519281   0.772848
 0.811158   0.974311   0.362553       0.467965    0.791043   0.410073
 0.488559   0.0628066  0.939292    …  0.902711    0.0426006  0.981258
 0.907969   0.95516    0.887501       0.857101    0.512076   0.935349
 0.158002   0.0287899  0.782649       0.060263    0.591802   0.603786
 0.430692   0.254367   0.854912       0.233108    0.63659    0.661488
 0.0756641  0.429091   0.5839         0.0691556   0.889573   0.994705
julia
mock_props = Dict()
Dict{Any, Any}()
julia
mock_cube = YAXArray(mock_axes, mock_data, mock_props)
┌ 30×10×15 YAXArray{Float64, 3} ┐
├───────────────────────────────┴──────────────────────────────────────── dims ┐
  ↓ Ti Sampled{Date} Date("2022-01-01"):Day(1):Date("2022-01-30") ForwardOrdered Regular Points,
  → X  Sampled{Float64} 1.0:1.0:10.0 ForwardOrdered Regular Points,
  ↗ Y  Sampled{Float64} 1.0:0.2857142857142857:5.0 ForwardOrdered Regular Points
├──────────────────────────────────────────────────────────────────── metadata ┤
  Dict{Any, Any}()
├──────────────────────────────────────────────────────────── loaded in memory ┤
  data size: 35.16 KB
└──────────────────────────────────────────────────────────────────────────────┘
julia
mock_trend = rqatrend(mock_cube; thresh=0.5)
outpath = "/tmp/jl_yN2xaH4dWV.zarr"

┌ 10×15 YAXArray{Union{Missing, Float32}, 2} ┐
├────────────────────────────────────────────┴─────────────────────────── dims ┐
  ↓ X Sampled{Float64} 1.0:1.0:10.0 ForwardOrdered Regular Points,
  → Y Sampled{Float64} 1.0:0.2857142857142857:5.0 ForwardOrdered Regular Points
├──────────────────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any} with 1 entry:
  "missing_value" => 1.0f32
├─────────────────────────────────────────────────────────────── loaded lazily ┤
  data size: 600.0 bytes
└──────────────────────────────────────────────────────────────────────────────┘
julia
mock_trend
┌ 10×15 YAXArray{Union{Missing, Float32}, 2} ┐
├────────────────────────────────────────────┴─────────────────────────── dims ┐
  ↓ X Sampled{Float64} 1.0:1.0:10.0 ForwardOrdered Regular Points,
  → Y Sampled{Float64} 1.0:0.2857142857142857:5.0 ForwardOrdered Regular Points
├──────────────────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any} with 1 entry:
  "missing_value" => 1.0f32
├─────────────────────────────────────────────────────────────── loaded lazily ┤
  data size: 600.0 bytes
└──────────────────────────────────────────────────────────────────────────────┘

Real world data set

This example is using Sentinel-1 Sigma Nought backscatter data as processed using Wagner et al. 2021 in Equi7Grid projection. Similar world wide data can be accessed via the EODC STAC catalog. The following test dataset contains only a subset of a tile to make it faster to process.

Download some test data:

julia
using Downloads
using ZipFile

function unzip(file,exdir="")
    fileFullPath = isabspath(file) ?  file : joinpath(pwd(),file)
    basePath = dirname(fileFullPath)
    outPath = (exdir == "" ? basePath : (isabspath(exdir) ? exdir : joinpath(pwd(),exdir)))
    isdir(outPath) ? "" : mkdir(outPath)
    zarchive = ZipFile.Reader(fileFullPath)
    for f in zarchive.files
        fullFilePath = joinpath(outPath,f.name)
        if (endswith(f.name,"/") || endswith(f.name,"\\"))
            mkdir(fullFilePath)
        else
            write(fullFilePath, read(f))
        end
    end
    close(zarchive)
end
unzip (generic function with 2 methods)
julia

url = "https://github.com/EarthyScience/RQADeforestationTestData/archive/refs/tags/v2.0.zip"
"https://github.com/EarthyScience/RQADeforestationTestData/archive/refs/tags/v2.0.zip"
julia
tmp_dir = mktempdir()
"/tmp/jl_8ZuQd1"
julia
zip_path = joinpath(tmp_dir, "downloaded.zip")
"/tmp/jl_8ZuQd1/downloaded.zip"
julia
Downloads.download(url, zip_path)
"/tmp/jl_8ZuQd1/downloaded.zip"
julia
unzip(zip_path)

in_dir = joinpath(tmp_dir, "RQADeforestationTestData-2.0")
"/tmp/jl_8ZuQd1/RQADeforestationTestData-2.0"

Run the workflow:

julia
using RQADeforestation

out_dir = joinpath(tmp_dir, "out")
"/tmp/jl_8ZuQd1/out"
julia
using Zarr
using YAXArrays
using Dates
RQADeforestation.main(;
    tiles=["E051N018T3"],
    continent="EU",
    indir=in_dir,
    start_date=Date("2021-01-01"),
    end_date=Date("2022-01-01"),
    outdir=out_dir
)
relorbits = SubString{String}["022"]
length(dates) = 184
  5.148236 seconds (12.87 M allocations: 644.679 MiB, 1.27% gc time, 99.43% compilation time: <1% of which was recompilation)
path = "/tmp/jl_8ZuQd1/out/E051N018T3_rqatrend_VH_D022_thresh_3.0"
size(cube) = (50, 74, 61)
size(tcube) = (50, 74, 48)
outpath = "/tmp/jl_8ZuQd1/out/E051N018T3_rqatrend_VH_D022_thresh_3.0.zarr"
  7.232062 seconds (19.35 M allocations: 982.303 MiB, 9.68% gc time, 91.06% compilation time)

The directory out_dir forms a Zarr Dataset that can be opened as an n dimensional array in various ways, e.g. xarray in Pxthon and YAXArrays in Julia.

Examine the output data cube using YAXArrays:

julia
a = open_dataset(out_dir * "/E051N018T3_rqatrend_VH_D022_thresh_3.0.zarr").layer
┌ 50×74 YAXArray{Union{Missing, Float32}, 2} ┐
├────────────────────────────────────────────┴─────────────────────────── dims ┐
  ↓ X Sampled{Float64} 5.255e6:20.0:5.25598e6 ForwardOrdered Regular Points,
  → Y Sampled{Float64} 2.03299e6:-20.0:2.03153e6 ReverseOrdered Regular Points
├──────────────────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any} with 3 entries:
  "missing_value" => 1.0e32
  "name"          => "layer"
  "_FillValue"    => 1.0f32
├─────────────────────────────────────────────────────────────── loaded lazily ┤
  data size: 14.45 KB
└──────────────────────────────────────────────────────────────────────────────┘
julia
a
┌ 50×74 YAXArray{Union{Missing, Float32}, 2} ┐
├────────────────────────────────────────────┴─────────────────────────── dims ┐
  ↓ X Sampled{Float64} 5.255e6:20.0:5.25598e6 ForwardOrdered Regular Points,
  → Y Sampled{Float64} 2.03299e6:-20.0:2.03153e6 ReverseOrdered Regular Points
├──────────────────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any} with 3 entries:
  "missing_value" => 1.0e32
  "name"          => "layer"
  "_FillValue"    => 1.0f32
├─────────────────────────────────────────────────────────────── loaded lazily ┤
  data size: 14.45 KB
└──────────────────────────────────────────────────────────────────────────────┘