11using ArgParse
22using YAXArrays: YAXDefaults
3- using ArchGDAL: ArchGDAL
4- using PyramidScheme
5- using AWSS3: global_aws_config, S3Path
6- using FilePathsBase: exists
73
84
9- # using YAXArrays, Zarr
10- using Minio: MinioConfig
11-
12- global_aws_config (MinioConfig (" http://s3.fairsendd.eodchosting.eu" ,region= " us-east-1" ))
13-
145const argparsesettings = ArgParseSettings ()
156
167ArgParse. parse_item (:: Type{Date} , x:: AbstractString ) = Date (x)
@@ -78,121 +69,72 @@ function main(;
7869 tiles:: Vector{String} ,
7970 continent:: String ,
8071 indir:: String ,
81- outstore= Zarr. S3Store (" europe-forest-change" ),
82- outdir= " results" ,
83- tempfolder = S3Path (outstore. bucket, " intermediates/" ),
72+ outdir= " out.zarr" ,
8473 start_date:: Date ,
8574 end_date:: Date ,
8675 polarisation= " VH" ,
8776 orbit= " D" ,
8877 threshold= 3.0 ,
8978 folders= [" V1M0R1" , " V1M1R1" , " V1M1R2" ],
90- stack= :dae ,
91- postprocess= true ,
92- forestdir= " data/forest20m_new" ,
93- delete_intermediate= false
79+ stack= :dae
9480)
95- # global_aws_config(MinioConfig("http://s3.fairsendd.eodchosting.eu",region="us-east-1",username="ufew8gJku5hRY7VD6jbEjRi8VnvDfeEv",password="dqZdzWCLB7a9gTshL29AnQWGqL3krwnS"))
9681 in (orbit, [" A" , " D" ]) || error (" Orbit needs to be either A or D" )
9782 if isdir (indir) && isempty (indir)
9883 error (" Input directory $indir must not be empty" )
9984 end
100-
101- if isdir (tempfolder)
85+ if isdir (outdir)
10286 @warn " Resume from existing output directory"
10387 else
104- mkdir (tempfolder, recursive = true )
88+ mkdir (outdir )
10589 @info " Write output to $outdir "
10690 end
91+
10792 if monthday (start_date) != monthday (end_date)
10893 @warn " Selected time series does not include a multiple of whole years. This might introduce seasonal bias."
10994 end
11095
111- YAXDefaults. workdir[] = tempfolder
112- @show typeof (tempfolder)
96+ YAXDefaults. workdir[] = outdir
11397
114- corruptedfiles = open ( " corrupted_tiles.txt" , " w " )
98+ corruptedfiles = " corrupted_tiles.txt"
11599 # TODO save the corrupt files to a txt for investigation
116100 for tilefolder in tiles
117- @show tilefolder
118- outpath = joinpath (outdir, " postprocess_$tilefolder .zarr/" )
119- @show outpath
120- if outpath in Zarr. subdirs (outstore, outdir)
121- println (" Skip already processed tile $tilefolder " )
122- continue
123- end
124- sub = first (folders)
125- # @show glob("$(sub)/*$(continent)*20M/$(tilefolder)*/*$(polarisation)_$(orbit)*.tif", indir)
126- filenamelist = [glob (" $(sub) /*$(continent) *20M/$(tilefolder) */*$(polarisation) _$(orbit) *.tif" , indir) for sub in folders]
101+ filenamelist = [glob (" $(sub) /*$(continent) *20M/$(tilefolder) /*$(polarisation) _$(orbit) *.tif" , indir) for sub in folders]
127102 allfilenames = collect (Iterators. flatten (filenamelist))
128- # @show allfilenames
103+
129104 relorbits = unique ([split (basename (x), " _" )[5 ][2 : end ] for x in allfilenames])
130105 @show relorbits
131106
132107 for relorbit in relorbits
133- path = S3Path (joinpath (YAXDefaults. workdir[], " $(tilefolder) _rqatrend_$(polarisation) _$(orbit)$(relorbit) _thresh_$(threshold) " ))
134- # s3path = "s3://"*joinpath(outstore.bucket, path)
135- @show path
136- exists (path * " .done" ) && continue
137- exists (path * " _zerotimesteps.done" ) && continue
138108 filenames = allfilenames[findall (contains (" $(relorbit) _E" ), allfilenames)]
139109 @time cube = gdalcube (filenames, stack)
140-
141110
111+ path = joinpath (YAXDefaults. workdir[], " $(tilefolder) _rqatrend_$(polarisation) _$(orbit)$(relorbit) _thresh_$(threshold) " )
112+ @show path
113+ ispath (path * " .done" ) && continue
114+ ispath (path * " _zerotimesteps.done" ) && continue
142115
143116 tcube = cube[Time= start_date .. end_date]
144117 @show size (cube)
145118 @show size (tcube)
146119 if size (tcube, 3 ) == 0
147- touch (S3Path ( path * " _zerotimesteps.done" ) )
120+ touch (path * " _zerotimesteps.done" )
148121 continue
149122 end
150123 try
151- orbitoutpath = string (path * " .zarr/" )
152- # This is only necessary because overwrite=true doesn't work on S3 based Zarr files in YAXArrays
153- # See https://github.com/JuliaDataCubes/YAXArrays.jl/issues/511
154- if exists (S3Path (orbitoutpath))
155- println (" Deleting path $orbitoutpath " )
156- rm (S3Path (orbitoutpath), recursive= true )
157- end
158- @show orbitoutpath
159- # This seems to ignore the overwrite keyword when the outpath point to S3.
160- @time rqatrend (tcube; thresh= threshold, outpath= orbitoutpath, overwrite= true )
161- if delete_intermediate == false
162- PyramidScheme. buildpyramids (orbitoutpath)
163- Zarr. consolidate_metadata (orbitoutpath)
164- end
124+ outpath = path * " .zarr"
125+ @time rqatrend (tcube; thresh= threshold, outpath= outpath, overwrite= true )
126+ Zarr. consolidate_metadata (outpath)
165127 catch e
166128
167129 if hasproperty (e, :captured ) && e. captured. ex isa ArchGDAL. GDAL. GDALError
168- println (corruptedfiles, " Found GDALError:" )
169- println (corruptedfiles, e. captured. ex. msg)
130+ println (" Found GDALError:" )
131+ println (e. captured. ex. msg)
170132 continue
171133 else
172134 rethrow (e)
173135 end
174136 end
175- donepath = path * " .done"
176- @show donepath
177- touch (S3Path (path * " .done" ))
178- end
179- if postprocess
180- @show outpath
181- DD. STRICT_BROADCAST_CHECKS[] = false
182-
183- RQADeforestation. postprocess (tilefolder, tempfolder, outpath, forestdir)
184- Zarr. consolidate_metadata (outpath)
185- DD. STRICT_BROADCAST_CHECKS[] = true
186- # base = basename(outpath)
187- # @show base
188- # command = `aws --endpoint-url http://s3.fairsendd.eodchosting.eu s3 cp --recursive $outpath s3://europe-forest-change/$base`
189-
190- # run(command)
137+ touch (path * " .done" )
191138 end
192- if delete_intermediate
193- rm (tempfolder, force= true , recursive= true )
194- end
195-
196-
197139 end
198140end
0 commit comments