-
Notifications
You must be signed in to change notification settings - Fork 0
/
dodate
executable file
·1356 lines (1178 loc) · 60.7 KB
/
dodate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
##################################################################
##################################################################
#
# ForWarn 2 Production Script
#
##################################################################
##################################################################
#
# Low-level bash script for building an entire set of products
# for a single 24-day period (3 MODIS periods).
#
# Author: William Hargrove (EFETAC / USDA)
#
# Maintained By: UNCA's NEMAC ([email protected])
#
# Arguments:
# datestring - a string of the form YYYYJJJ, where
# YYYY is a year and JJJ is a zero-padded julian day (day of the year).
#
# Example: ./dodate 2019009
#
# Data Source: https://gimms.gsfc.nasa.gov/MODIS/
#
#################################################################
#################################################################
#
# Summary of All Precursors
#
# from distal to proximal
# in order of dependency, from the bottom up
#
# Precursors for Prior Year Products:
#
# meanallpriormax.* mean over all prior years, over 3 dates, over 2 sensors
# sumallpriormax.* sum over all prior years, over 3 dates, over 2 sensors
# maxallpriormax.* max over all prior years, over 3 dates, over 2 sensors
#
# Precursors for 1, 3, and 5-year Products
#
# maxMODISmaxmax.* max over all prior years, over 3 dates, over 2 sensors
#
# .std all 3 dates are from std products
# .nrt first 2 dates are from std, third date is nrt
# maxMODISalc.* max over 3 dates, recent priority, over 2 sensors
# maxMODISmax.* max over 3 dates, over 2 sensors
# maxMODIS.* max over 2 sensors, for one date
#
#################################################################
#
# Precursor Intermediate Files Needed for making 1-, 3-, and 5-year Products
#
# maxMODIS nrt DOY3
# maxMODIS std DOY2
# maxMODIS std DOY1*
#
# maxMODISmaxmax std max for this DOY over all 5 prior years*
#
# if not present, this, in turn, needs:
# maxMODISmax std max for this DOY over *each* of the 5 prior years
# maxMODIS std max over each of 3 DOYs in the prior year
#
# * = files should already exist
#
# use cp command something like this
# cp /precursors/maxMODISmaxmax.[135]-yr-baseline.201[76543].DOY.std.img .
# cp /precursors/maxMODISmax.201[76543].DOY.std.img .
# cp /precursors/maxMODIS.2017.0[876][135].std.img .
#
# harvest back the ?
#
#################################################################
#
# Precursor Intermediate Files Needed for making all prior year Products
#
# maxallpriormax.*.DOY.std.img for this DOY for as recent a year as possible*
# made by running doallprior script
#
# finds additional maxes from above using:
# maxMODISmax std for this DOY for every extra prior year since
# the year of the latest available maxallpriormax file*
#
# * = files should already exist
#
# use cp command something like this
# cp /media/disk/fullwrapper2/maxallpriormax.*.DOY.std.img .
# cp /media/disk/fullwrapper2/maxMODISmax.201[76543].081.std.img .
#
# harvest back the maxallpriormax std file that is made for this DOY
#
#################################################################
#
# Precursor Intermediate Files Needed for making Mean all prior year Products
#
# countallpriormax std for this DOY for as recent a year as possible*
# sumallpriormax std for this DOY for as recent a year as possible*
# made by running doallpriormean script
#
# adds up count and sum from above files using:
# maxMODISmax std for this DOY for every extra prior year since
# the year of the latest available countallpriormax and sumallpriormax files*
#
# * = files should already exist
#
# use cp command something like this
# cp /media/disk/fullwrapper2/countallpriormax.*.DOY.std.img .
# cp /media/disk/fullwrapper2/sumallpriormax.*.DOY.std.img .
# cp /media/disk/fullwrapper2/maxMODISmax.201[76543].081.std.img .
#
# harvest back the countallpriormax and sumallpriormax files that are made for this DOY
#
#
#################################################################
#
# SCRIPT STARTS NOW
#
#################################################################
##
# Init
# Report the status of terminated background jobs immediately,
# rather than before the next primary prompt. This is effective
# only when job control is enabled.
set -b
# Exit immediately upon any error
set -e
# Paths to precursors
ALCpath=.
medianallyrmaxpath=.
# Extract the year and julian day from the datestring argument.
YEAR=`echo $1 | cut -c1-4`
DOYWANTED=`echo $1 | cut -c5-7`
# Find the desired day of year (DOY) and three-day interval.
for INTERVALs in 361/353/345 353/345/337 345/337/329 337/329/321 329/321/313 321/313/305 313/305/297 305/297/289 297/289/281 289/281/273 281/273/265 273/265/257 265/257/249 257/249/241 249/241/233 241/233/225 233/225/217 225/217/209 217/209/201 209/201/193 201/193/185 193/185/177 185/177/169 177/169/161 169/161/153 161/153/145 153/145/137 145/137/129 137/129/121 129/121/113 121/113/105 113/105/097 105/097/089 097/089/081 089/081/073 081/073/065 073/065/057 065/057/049 057/049/041 049/041/033 041/033/025 033/025/017 025/017/009 017/009/001 009/001/361 001/361/353
do
DOY=`echo $INTERVALs | awk -F/ '{print $1}'`
if [ $DOY -eq $DOYWANTED ]; then
break
fi
done
echo "NRT DOY to download is " $DOY
# this is the day the 8-day period BEGINS
echo "The three INTERVALs needed are " $INTERVALs
echo "these are the days the 8-day periods BEGIN"
# build output directories if needed
for dirs in 1-yr-max 3-yr-max 5-yr-90 10-yr-90 median-all-yr-max ALC pctprogress
do
if [ ! -d "${dirs}" ];then
echo "Need to make subdirectory " $dirs
mkdir $dirs
fi
done
# get rid of any existing or partial gz downloads
rm -f *.gz* || true
let LASTYEAR=$YEAR-1
# The three julian days to make precursors for
DOY1=`echo $INTERVALs | awk -F"/" '{print $1}'`
DOY2=`echo $INTERVALs | awk -F"/" '{print $2}'`
DOY3=`echo $INTERVALs | awk -F"/" '{print $3}'`
echo $DOY1 $DOY2 $DOY3
# Calculates the correct year for each julian day.
# The first two julian days of the year have at least one day in the
# corresponding interval that occurred in year previous to what was supplied
# in the datestring argument.
case $DOY1 in
001)
YRDOY3=$YEAR/$DOY1
YRDOY2=$LASTYEAR/$DOY2
YRDOY1=$LASTYEAR/$DOY3;;
009)
YRDOY3=$YEAR/$DOY1
YRDOY2=$YEAR/$DOY2
YRDOY1=$LASTYEAR/$DOY3;;
*)
YRDOY3=$YEAR/$DOY1
YRDOY2=$YEAR/$DOY2
YRDOY1=$YEAR/$DOY3;;
esac
echo $YEAR $INTERVALs $YRDOY3 $YRDOY2 $YRDOY1
# parse YRDOY3 YRDOY2 YRDOY1 into paired years and dates
YR3=`echo $YRDOY3 | awk -F/ '{print $1}'`
DOY3=`echo $YRDOY3 | awk -F/ '{print $2}'`
YR2=`echo $YRDOY2 | awk -F/ '{print $1}'`
DOY2=`echo $YRDOY2 | awk -F/ '{print $2}'`
YR1=`echo $YRDOY1 | awk -F/ '{print $1}'`
DOY1=`echo $YRDOY1 | awk -F/ '{print $2}'`
YEAR=$YR3
DOY=$DOY3
SECONDDATETYPE=std
FIRSTDATETYPE=std
THIRDDATETYPE=std
# Assume maxMODIS and maxMODISmax precursors already exist
# Check if std is available first
if [ ! -f maxMODIS.$YR3.$DOY3.std.img ]; then
THIRDDATETYPE=nrt
if [ ! -f maxMODIS.$YR3.$DOY3.nrt.img ]; then
echo "Missing maxMODIS.$YR3.$DOY3.nrt.img... Exiting"
exit 1
fi
fi
if [ ! -f maxMODIS.$YR2.$DOY2.std.img ]; then
echo "Missing maxMODIS.$YR2.$DOY2.std.img... Exiting"
exit 1
fi
if [ ! -f maxMODIS.$YR1.$DOY1.std.img ]; then
echo "Missing maxMODIS.$YR1.$DOY1.std.img... Exiting"
exit 1
fi
#################################################################
#
# maxMODISalc Precursor Generation
#
#################################################################
##
#
# Create the maxMODISalc nrt numerator across the three dates for all products
# consisting of two std dates and one nrt date (ideally).
#
# Find 24-day Adaptive Length Compositing (ALC) current image for 1-year product only.
#
# Raster calculation summary:
#
# make triple DOY maximum value composite
# if ANY of three are 254, then 254 = water
# if ALL three are 255, then 255 = no data
#
# Raster calculation summary:
#
# Output 254 (water):
# - any of the three pixels are 254
#
# Output 255:
# - any of the three pixels are 255
#
# NoData value is 252
echo
echo "***************************************************************"
echo
echo "Creating maxMODISalc for " $YEAR $DOY "..."
gdal_calc.py --debug --calc="\
(C<251)*C\
+((B<251)&(C>250))*B\
+((A<251)&(B>250)&(C>250))*A\
+((A==254)|(B==254)|(C==254))*254\
+((A==255)&(B==255)&(C==255))*255\
" --NoDataValue=252 --format=HFA --co "STATISTICS=YES" --co "COMPRESSED=YES" --outfile=$ALCpath/maxMODISalc.$YR3.$DOY3.$THIRDDATETYPE.img -A maxMODIS.$YR1.$DOY1.$FIRSTDATETYPE.img -B maxMODIS.$YR2.$DOY2.$SECONDDATETYPE.img -C maxMODIS.$YR3.$DOY3.$THIRDDATETYPE.img --type=Byte --overwrite
rm -f $ALCpath/maxMODISalc.$YR3.$DOY3.$THIRDDATETYPE.tif || true
# loop to make 1-, 3-, 5-, and 10-year products
for PRODUCT in 1 3 5 10
do
echo "Making " $PRODUCT"-yr product now ..."
TYPE=std
let MINYEAR=$PRODUCT+2003
echo "MINYEAR is " $MINYEAR
if [ $YEAR -ge $MINYEAR ]; then
echo $PRODUCT "-year max baseline is possible for year " $YEAR
# set denominator file and output subdirectory acc to PRODUCT
if [ $PRODUCT -lt 5 ]; then
subdir=max
denomfile=max
else
subdir=90
denomfile=90
fi
# test whether maxMODISmaxmax std max or maxMODISmax90 over all prior years, over 3 dates,
# and over 2 sensors already exists for denominator baseline
if [ ! -f maxMODISmax$denomfile.$PRODUCT-yr-baseline.$YEAR.$DOY.$TYPE.img ]; then
echo "File does not exist for maxMODISmax"$denomfile"."$PRODUCT"-yr-baseline."$YEAR"."$DOY"."$TYPE".img"
######## make $PRODUCT-year product #########################################
# generate $PRODUCT-year maxMODISmaxmax maximum baseline or maxMODISmax90 percentile baseline over the $PRODUCT prior yrsback years
let BACK10=$YEAR-10
let BACK9=$YEAR-9
let BACK8=$YEAR-8
let BACK7=$YEAR-7
let BACK6=$YEAR-6
let BACK5=$YEAR-5
let BACK4=$YEAR-4
let BACK3=$YEAR-3
let BACK2=$YEAR-2
let BACK1=$YEAR-1
case $PRODUCT in
10)
# generate 90th percentile over prior 10 years
gdal_calc.py --debug --calc="\
percentile([\
(A<251)*A,(B<251)*B,(C<251)*C,\
(D<251)*D,(E<251)*E,(F<251)*F,\
(G<251)*G,(H<251)*H,(I<251)*I,\
(J<251)*J\
],90,axis=0)\
+((A==254)&(B==254)&(C==254)&(D==254)&(E==254)&(F==254)&(G==254)&(H==254)&(I==254)&(J==254))*254\
+( (\
(A!=254)|(B!=254)|(C!=254)|(D!=254)|(E!=254)|(F!=254)|(G!=254)|(H!=254)|(I!=254)|(J!=254)\
)&\
( \
(A>250)&(B>250)&(C>250)&(D>250)&(E>250)&(F>250)&(G>250)&(H>250)&(I>250)&(J>250)\
) )*255\
" --NoDataValue=252 --format=HFA --co "STATISTICS=YES" --co "COMPRESSED=YES" --outfile=maxMODISmax90.$PRODUCT-yr-baseline.$YR3.$DOY3.$TYPE.img -A maxMODISmax.$BACK10.$DOY3.$TYPE.img -B maxMODISmax.$BACK9.$DOY3.$TYPE.img -C maxMODISmax.$BACK8.$DOY3.$TYPE.img -D maxMODISmax.$BACK7.$DOY3.$TYPE.img -E maxMODISmax.$BACK6.$DOY3.$TYPE.img -F maxMODISmax.$BACK5.$DOY3.$TYPE.img -G maxMODISmax.$BACK4.$DOY3.$TYPE.img -H maxMODISmax.$BACK3.$DOY3.$TYPE.img -I maxMODISmax.$BACK2.$DOY3.$TYPE.img -J maxMODISmax.$BACK1.$DOY3.$TYPE.img --type=Byte --overwrite
;;
5)
# generate 90th percentile over prior 5 years
gdal_calc.py --debug --calc="\
percentile([\
(A<251)*A,(B<251)*B,(C<251)*C,\
(D<251)*D,(E<251)*E\
],90,axis=0)\
+((A==254)&(B==254)&(C==254)&(D==254)&(E==254))*254\
+( (\
(A!=254)|(B!=254)|(C!=254)|(D!=254)|(E!=254)\
)&\
( \
(A>250)&(B>250)&(C>250)&(D>250)&(E>250)\
) )*255\
" --NoDataValue=252 --format=HFA --co "STATISTICS=YES" --co "COMPRESSED=YES" --outfile=maxMODISmax90.$PRODUCT-yr-baseline.$YR3.$DOY3.$TYPE.img -A maxMODISmax.$BACK5.$DOY3.$TYPE.img -B maxMODISmax.$BACK4.$DOY3.$TYPE.img -C maxMODISmax.$BACK3.$DOY3.$TYPE.img -D maxMODISmax.$BACK2.$DOY3.$TYPE.img -E maxMODISmax.$BACK1.$DOY3.$TYPE.img --type=Byte --overwrite
;;
3)
# generate maximum over prior 3 years
gdal_calc.py --debug --calc="\
maximum( maximum( (A<251)*A,(B<251)*B ),(C<251)*C )\
+((A==254)|(B==254)|(C==254))*254\
+((A==255)&(B==255)&(C==255))*255\
" --NoDataValue=252 --format=HFA --co "STATISTICS=YES" --co "COMPRESSED=YES" --outfile=maxMODISmaxmax.$PRODUCT-yr-baseline.$YR3.$DOY3.$TYPE.img -A maxMODISmax.$BACK3.$DOY3.$TYPE.img -B maxMODISmax.$BACK2.$DOY3.$TYPE.img -C maxMODISmax.$BACK1.$DOY3.$TYPE.img --type=Byte --overwrite
;;
1)
# generate maximum over prior year
# for 1-yr product, 1-yr max is the same as the max last year
cp maxMODISmax.$BACK1.$DOY3.$TYPE.img maxMODISmaxmax.$PRODUCT-yr-baseline.$YR3.$DOY3.$TYPE.img
;;
esac # over all 1-, 3-, 5-, and 10-year cases
# done with custom calculations to make maxMODISmaxmax or maxMODISmax90 for each case,
else
echo "maxMODISmax"$denomfile"."$PRODUCT"-yr-baseline."$YEAR"."$DOY"."$TYPE".img file already exists"
fi
# now continue with generic PRODUCT calculations
# only if 1-yr product, finish calculating ALC products
if [ $PRODUCT -eq 1 ]; then
#################################################################
#ALC and ALC2, for 1-yr only
#################################################################
# calculate Adaptive Length Compositing (ALC) product
# only for 1-year PRODUCT
# using recent priority values current nrt view
# standard way, (observed-expected)/expected NDVI
# clamp departure values between 2 and 254
# if either current or baseline is 254 water, then 0 water
# if BOTH current AND baseline are 255 no value, then 255 no value
# 252 is nodata coming in
# 1 is nodata going out
# 0 = water
# 255 = no NDVI value
if [ ! -f $ALCpath/ALC/ALC2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.img ];
then
echo "File does not exist for $ALCpath/ALC/ALC2LAEA."$YEAR"."$DOY"."$PRODUCT"-yr-baseline.img"
echo "Calculating ALC product ..."
gdal_calc.py --debug --calc="\
((A<251)&(B<251))*\
round_(( (A.astype(float)-B.astype(float))/ B.clip(1).astype(float)*128+127).clip(2,254)).astype(uint8)\
+((A==254)|(B==254))*0\
+((A==255)&(B==255))*255\
" --NoDataValue=1 --format=HFA --co "STATISTICS=YES" --co "COMPRESSED=YES" --outfile=$ALCpath/ALC/ALC.$YEAR.$DOY.$PRODUCT-yr-baseline.img -A $ALCpath/maxMODISalc.$YEAR.$DOY.$THIRDDATETYPE.img -B maxMODISmaxmax.$PRODUCT-yr-baseline.$YEAR.$DOY.$TYPE.img --type=Byte --overwrite
rm -f $ALCpath/ALC/ALC.$YEAR.$DOY.$PRODUCT-yr-baseline.tif || true
# calculate Adaptive Length Compositing (ALC2) sqrt product
# only for 1-year PRODUCT
# using recent priority values current view
# sqrt of expected, (observed-expected)/sqrt(expected+1)
# must use native NDVI scaling
# clamp departure values between 2 and 254
# if either current or baseline is 254 water, then 0 water
# if BOTH current AND baseline are 255 no value, then 255
# 252 is nodata coming in
# 1 is nodata going out
# 0 = water
# 255 = no NDVI value
echo "Calculating ALC2 sqrt product ..."
gdal_calc.py --debug --calc="\
((A<251)&(B<251))*\
round_(( ((A.astype(float)*0.004)-(B.astype(float)*0.004))/ sqrt((B.astype(float)*0.004).clip(1).astype(float))*128+127).clip(2,254)).astype(uint8)\
+((A==254)|(B==254))*0\
+((A==255)&(B==255))*255\
" --NoDataValue=1 --format=HFA --co "STATISTICS=YES" --co "COMPRESSED=YES" --outfile=$ALCpath/ALC/ALC2.$YEAR.$DOY.$PRODUCT-yr-baseline.img -A $ALCpath/maxMODISalc.$YEAR.$DOY.$THIRDDATETYPE.img -B maxMODISmaxmax.$PRODUCT-yr-baseline.$YEAR.$DOY.$TYPE.img --type=Byte --overwrite
rm -f $ALCpath/ALC/ALC2.$YEAR.$DOY.$PRODUCT-yr-baseline.tif || true
# ALC and ALC2 split, reproject, re-join
# split out two mask categories, 0 water and 255 nodata from ALC product
# use 128 as a fill value, but not as nodata
# 1 is nodata coming in
# 1 is nodata going out
# use 128 as a fill value
echo "Splitting out two mask categories from ALC product ..."
gdal_calc.py --debug --calc="\
((A==0)|(A==255))*A\
+((A>1)&(A<255))*128\
" --NoDataValue=1 --format=HFA --co "STATISTICS=YES" --co "COMPRESSED=YES" --outfile=$ALCpath/ALC/ALC.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img -A $ALCpath/ALC/ALC.$YEAR.$DOY.$PRODUCT-yr-baseline.img --type=Byte --overwrite
rm -f $ALCpath/ALC/ALC.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.tif || true
echo "Splitting out data from two masks in ALC product ..."
# use 1 as a fill value
gdal_calc.py --debug --calc="\
((A>1)&(A<255))*A\
+((A==0)|(A==255))*1\
" --NoDataValue=1 --format=HFA --co "STATISTICS=YES" --co "COMPRESSED=YES" --outfile=$ALCpath/ALC/ALC.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img -A $ALCpath/ALC/ALC.$YEAR.$DOY.$PRODUCT-yr-baseline.img --type=Byte --overwrite
rm -f $ALCpath/ALC/ALC.$YEAR.$DOY.$PRODUCT-yr-baseline.data.tif || true
# reproject ALC product to LAEA
# reproject masks and data separately, and re-join
echo "Warping ALC product data with gdalwarp"
gdalwarp -overwrite -multi -t_srs laea.prj -r bilinear -wm 500 -tr 231.656358264000005 231.656358264000005 -srcnodata 1 -dstnodata None -of HFA -co "STATISTICS=YES" -co "COMPRESSED=YES" $ALCpath/ALC/ALC.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img $ALCpath/ALC/ALCLAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img
rm -f $ALCpath/ALC/ALCLAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.data.tif || true
echo "Warping ALC product masks with gdalwarp"
gdalwarp -overwrite -multi -t_srs laea.prj -wm 500 -tr 231.656358264000005 231.656358264000005 -srcnodata 1 -dstnodata None -of HFA -co "STATISTICS=YES" -co "COMPRESSED=YES" $ALCpath/ALC/ALC.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img $ALCpath/ALC/ALCLAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img
rm -f $ALCpath/ALC/ALCLAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.tif || true
echo "Rejoining ALC product masks with data"
gdal_calc.py --debug --calc="\
((A==0)|(A==255))*A\
+((B>1)&(B<255))*B\
" --format=HFA --co "STATISTICS=YES" --co "COMPRESSED=YES" --outfile=$ALCpath/ALC/ALCLAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.img -A $ALCpath/ALC/ALCLAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img -B $ALCpath/ALC/ALCLAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img --type=Byte --overwrite
rm -f $ALCpath/ALC/ALCLAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.tif || true
# take out the trash here
rm -f $ALCpath/ALC/ALCLAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img* || true
rm -f $ALCpath/ALC/ALCLAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img* || true
rm -f $ALCpath/ALC/ALC.$YEAR.$DOY.$PRODUCT-yr-baseline.img* || true
rm -f $ALCpath/ALC/ALC.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img* || true
rm -f $ALCpath/ALC/ALC.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img* || true
# split out two mask categories, 0 water and 255 nodata from ALC2 sqrt product
# use 128 as a fill value, but not as nodata
# 1 is nodata coming in
# 1 is nodata going out
# use 128 as a fill value
echo "Splitting out two mask categories from ALC2 sqrt product ..."
gdal_calc.py --debug --calc="\
((A==0)|(A==255))*A\
+((A>1)&(A<255))*128\
" --NoDataValue=1 --format=HFA --co "STATISTICS=YES" --co "COMPRESSED=YES" --outfile=$ALCpath/ALC/ALC2.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img -A $ALCpath/ALC/ALC2.$YEAR.$DOY.$PRODUCT-yr-baseline.img --type=Byte --overwrite
rm -f $ALCpath/ALC/ALC2.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.tif || true
echo "Splitting out data from two masks in ALC2 sqrt product ..."
# use 1 as a fill value
gdal_calc.py --debug --calc="\
((A>1)&(A<255))*A\
+((A==0)|(A==255))*1\
" --NoDataValue=1 --format=HFA --co "STATISTICS=YES" --co "COMPRESSED=YES" --outfile=$ALCpath/ALC/ALC2.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img -A $ALCpath/ALC/ALC2.$YEAR.$DOY.$PRODUCT-yr-baseline.img --type=Byte --overwrite
rm -f $ALCpath/ALC/ALC2.$YEAR.$DOY.$PRODUCT-yr-baseline.data.tif || true
# reproject ALC2 sqrt product to LAEA
# reproject masks and data separately, and re-join
echo "Warping ALC2 sqrt product with gdalwarp"
gdalwarp -overwrite -multi -t_srs laea.prj -r bilinear -wm 500 -tr 231.656358264000005 231.656358264000005 -srcnodata 1 -dstnodata None -of HFA -co "STATISTICS=YES" -co "COMPRESSED=YES" $ALCpath/ALC/ALC2.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img $ALCpath/ALC/ALC2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img
rm -f $ALCpath/ALC/ALC2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.data.tif || true
echo "Warping ALC2 sqrt product masks with gdalwarp"
gdalwarp -overwrite -multi -t_srs laea.prj -wm 500 -tr 231.656358264000005 231.656358264000005 -srcnodata 1 -dstnodata None -of HFA -co "STATISTICS=YES" -co "COMPRESSED=YES" $ALCpath/ALC/ALC2.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img $ALCpath/ALC/ALC2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img
rm -f $ALCpath/ALC/ALC2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.tif || true
echo "Rejoining ALC2 sqrt product masks with data"
# probably gdal_calc assigns 255 as default nodata here
gdal_calc.py --debug --calc="\
((A==0)|(A==255))*A\
+((B>1)&(B<255))*B\
" --format=HFA --co "STATISTICS=YES" --co "COMPRESSED=YES" --outfile=$ALCpath/ALC/ALC2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.img -A $ALCpath/ALC/ALC2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img -B $ALCpath/ALC/ALC2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img --type=Byte --overwrite
rm -f $ALCpath/ALC/ALC2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.tif || true
# take out the trash here
rm -f $ALCpath/ALC/ALC2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img* || true
rm -f $ALCpath/ALC/ALC2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img* || true
rm -f $ALCpath/ALC/ALC2.$YEAR.$DOY.$PRODUCT-yr-baseline.img* || true
rm -f $ALCpath/ALC/ALC2.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img* || true
rm -f $ALCpath/ALC/ALC2.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img* || true
else
echo "$ALCpath/ALC/ALC2LAEA."$YEAR"."$DOY"."$PRODUCT"-yr-baseline.img file already exists"
fi
fi # PRODUCT was 1, just calculated ALC and ALC2 products
# generate FW and FW2 divison products for $PRODUCT-year
# split, reproject, re-paste
# calculate $PRODUCT-year ForWarn product
# use nrt version for maxMODISmax current view
# standard way, (observed-expected)/expected NDVI
# clamp departure values between 2 and 254
# if either current or baseline is 254 water, then 0 water
# if BOTH current AND baseline are 255 no value, then 255 no value
if [ ! -f ./$PRODUCT-yr-$subdir/ForWarn2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.img ]
then
echo "File does not exist for ./"$PRODUCT"-yr-"$subdir"/ForWarn2LAEA."$YEAR"."$DOY"."$PRODUCT"-yr-baseline.img"
# 252 is nodata coming in
# 1 is nodata going out
# 0 = water
# 255 = no NDVI value
echo "Calculating ForWarn product ..."
gdal_calc.py --debug --calc="\
((A<251)&(B<251))*\
round_(( (A.astype(float)-B.astype(float))/ B.clip(1).astype(float)*128+127).clip(2,254)).astype(uint8)\
+((A==254)|(B==254))*0\
+((A==255)&(B==255))*255\
" --NoDataValue=1 --format=HFA --co "STATISTICS=YES" --co "COMPRESSED=YES" --outfile=./$PRODUCT-yr-$subdir/ForWarn.$YEAR.$DOY.$PRODUCT-yr-baseline.img -A maxMODISmax.$YEAR.$DOY.$THIRDDATETYPE.img -B maxMODISmax$denomfile.$PRODUCT-yr-baseline.$YEAR.$DOY.$TYPE.img --type=Byte --overwrite
rm -f ./$PRODUCT-yr-$subdir/ForWarn.$YEAR.$DOY.$PRODUCT-yr-baseline.tif || true
# calculate $PRODUCT-year ForWarn sqrt product
# sqrt of expected, (observed-expected)/sqrt(expected+1)
# must use native NDVI scaling
# clamp departure values between 2 and 254
# if either current or baseline is 254 water, then 0 water
# if BOTH current AND baseline are 255 no value, then 255
# 252 is nodata coming in
# 1 is nodata going out
# 0 = water
# 255 = no NDVI value
echo "Calculating ForWarn2 sqrt product ..."
gdal_calc.py --debug --calc="\
((A<251)&(B<251))*\
round_(( ((A.astype(float)*0.004)-(B.astype(float)*0.004))/ sqrt((B.astype(float)*0.004).clip(1).astype(float))*128+127).clip(2,254)).astype(uint8)\
+((A==254)|(B==254))*0\
+((A==255)&(B==255))*255\
" --NoDataValue=1 --format=HFA --co "STATISTICS=YES" --co "COMPRESSED=YES" --outfile=./$PRODUCT-yr-$subdir/ForWarn2.$YEAR.$DOY.$PRODUCT-yr-baseline.img -A maxMODISmax.$YEAR.$DOY.$THIRDDATETYPE.img -B maxMODISmax$denomfile.$PRODUCT-yr-baseline.$YEAR.$DOY.$TYPE.img --type=Byte --overwrite
rm -f ./$PRODUCT-yr-$subdir/ForWarn2.$YEAR.$DOY.$PRODUCT-yr-baseline.tif || true
# FW and FW2 split, reproject, re-join
# split out two mask categories, 0 water and 255 nodata from ForWarn product
# use 128 as a fill value, but not as nodata
# 1 is nodata coming in
# 1 is nodata going out
# use 128 as a fill value
echo "Splitting out two mask categories from ForWarn product ..."
gdal_calc.py --debug --calc="\
((A==0)|(A==255))*A\
+((A>1)&(A<255))*128\
" --NoDataValue=1 --format=HFA --co "STATISTICS=YES" --co "COMPRESSED=YES" --outfile=./$PRODUCT-yr-$subdir/ForWarn.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img -A ./$PRODUCT-yr-$subdir/ForWarn.$YEAR.$DOY.$PRODUCT-yr-baseline.img --type=Byte --overwrite
rm -f ./$PRODUCT-yr-$subdir/ForWarn.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.tif || true
echo "Splitting out data from two masks in ForWarn product ..."
# use 1 as a fill value
gdal_calc.py --debug --calc="\
((A>1)&(A<255))*A\
+((A==0)|(A==255))*1\
" --NoDataValue=1 --format=HFA --co "STATISTICS=YES" --co "COMPRESSED=YES" --outfile=./$PRODUCT-yr-$subdir/ForWarn.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img -A ./$PRODUCT-yr-$subdir/ForWarn.$YEAR.$DOY.$PRODUCT-yr-baseline.img --type=Byte --overwrite
rm -f ./$PRODUCT-yr-$subdir/ForWarn.$YEAR.$DOY.$PRODUCT-yr-baseline.data.tif || true
# reproject ForWarn product to LAEA
# reproject masks and data separately, and re-join
echo "Warping ForWarn product data with gdalwarp"
gdalwarp -overwrite -multi -t_srs laea.prj -r bilinear -wm 500 -tr 231.656358264000005 231.656358264000005 -srcnodata 1 -dstnodata None -of HFA -co "STATISTICS=YES" -co "COMPRESSED=YES" ./$PRODUCT-yr-$subdir/ForWarn.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img ./$PRODUCT-yr-$subdir/ForWarnLAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img
rm -f ./$PRODUCT-yr-$subdir/ForWarnLAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.data.tif || true
echo "Warping ForWarn product masks with gdalwarp"
gdalwarp -overwrite -multi -t_srs laea.prj -wm 500 -tr 231.656358264000005 231.656358264000005 -srcnodata 1 -dstnodata None -of HFA -co "STATISTICS=YES" -co "COMPRESSED=YES" ./$PRODUCT-yr-$subdir/ForWarn.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img ./$PRODUCT-yr-$subdir/ForWarnLAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img
rm -f ./$PRODUCT-yr-$subdir/ForWarnLAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.tif || true
echo "Rejoining ForWarn product masks with data"
gdal_calc.py --debug --calc="\
((A==0)|(A==255))*A\
+((B>1)&(B<255))*B\
" --format=HFA --co "STATISTICS=YES" --co "COMPRESSED=YES" --outfile=./$PRODUCT-yr-$subdir/ForWarnLAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.img -A ./$PRODUCT-yr-$subdir/ForWarnLAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img -B ./$PRODUCT-yr-$subdir/ForWarnLAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img --type=Byte --overwrite
rm -f ./$PRODUCT-yr-$subdir/ForWarnLAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.tif || true
# take out the trash
rm -f ./$PRODUCT-yr-$subdir/ForWarnLAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img* || true
rm -f ./$PRODUCT-yr-$subdir/ForWarnLAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img* || true
rm -f ./$PRODUCT-yr-$subdir/ForWarn.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img* || true
rm -f ./$PRODUCT-yr-$subdir/ForWarn.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img* || true
rm -f ./$PRODUCT-yr-$subdir/ForWarn.$YEAR.$DOY.$PRODUCT-yr-baseline.img* || true
# split out two mask categories, 0 water and 255 nodata from ForWarn2 sqrt product
# use 128 as a fill value, but not as nodata
# 1 is nodata coming in
# 1 is nodata going out
# use 128 as a fill value
echo "Splitting out two mask categories from ForWarn2 sqrt product ..."
gdal_calc.py --debug --calc="\
((A==0)|(A==255))*A\
+((A>1)&(A<255))*128\
" --NoDataValue=1 --format=HFA --co "STATISTICS=YES" --co "COMPRESSED=YES" --outfile=./$PRODUCT-yr-$subdir/ForWarn2.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img -A ./$PRODUCT-yr-$subdir/ForWarn2.$YEAR.$DOY.$PRODUCT-yr-baseline.img --type=Byte --overwrite
rm -f ./$PRODUCT-yr-$subdir/ForWarn2.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.tif || true
echo "Splitting out data from two masks in ForWarn2 sqrt product ..."
# use 1 as a fill value
gdal_calc.py --debug --calc="\
((A>1)&(A<255))*A\
+((A==0)|(A==255))*1\
" --NoDataValue=1 --format=HFA --co "STATISTICS=YES" --co "COMPRESSED=YES" --outfile=./$PRODUCT-yr-$subdir/ForWarn2.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img -A ./$PRODUCT-yr-$subdir/ForWarn2.$YEAR.$DOY.$PRODUCT-yr-baseline.img --type=Byte --overwrite
rm -f ./$PRODUCT-yr-$subdir/ForWarn2.$YEAR.$DOY.$PRODUCT-yr-baseline.data.tif || true
#gdal_translate ./$PRODUCT-yr-$subdir/ForWarn2.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img -of GTiff ./$PRODUCT-yr-$subdir/ForWarn2.$YEAR.$DOY.$PRODUCT-yr-baseline.data.tif
#xv ./$PRODUCT-yr-$subdir/ForWarn2.$YEAR.$DOY.$PRODUCT-yr-baseline.data.tif
#################################################################
# reproject ForWarn2 sqrt product to LAEA
# reproject masks and data separately, and re-join
echo "Warping ForWarn2 sqrt product with gdalwarp"
gdalwarp -overwrite -multi -t_srs laea.prj -r bilinear -wm 500 -tr 231.656358264000005 231.656358264000005 -srcnodata 1 -dstnodata None -of HFA -co "STATISTICS=YES" -co "COMPRESSED=YES" ./$PRODUCT-yr-$subdir/ForWarn2.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img ./$PRODUCT-yr-$subdir/ForWarn2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img
rm -f ./$PRODUCT-yr-$subdir/ForWarn2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.data.tif || true
#gdal_translate ./$PRODUCT-yr-$subdir/ForWarn2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img -of GTiff ./$PRODUCT-yr-$subdir/ForWarn2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.data.tif
#xv ./$PRODUCT-yr-$subdir/ForWarn2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.data.tif
echo "Warping ForWarn2 sqrt product masks with gdalwarp"
gdalwarp -overwrite -multi -t_srs laea.prj -wm 500 -tr 231.656358264000005 231.656358264000005 -srcnodata 1 -dstnodata None -of HFA -co "STATISTICS=YES" -co "COMPRESSED=YES" ./$PRODUCT-yr-$subdir/ForWarn2.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img ./$PRODUCT-yr-$subdir/ForWarn2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img
rm -f ./$PRODUCT-yr-$subdir/ForWarn2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.tif || true
#gdal_translate ./$PRODUCT-yr-$subdir/ForWarn2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img -of GTiff ./$PRODUCT-yr-$subdir/ForWarn2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.tif
#xv ./$PRODUCT-yr-$subdir/ForWarn2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.tif
echo "Rejoining ForWarn2 sqrt product masks with data"
# probably gdal_calc assigns 255 as default nodata here
gdal_calc.py --debug --calc="\
((A==0)|(A==255))*A\
+((B>1)&(B<255))*B\
" --format=HFA --co "STATISTICS=YES" --co "COMPRESSED=YES" --outfile=./$PRODUCT-yr-$subdir/ForWarn2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.img -A ./$PRODUCT-yr-$subdir/ForWarn2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img -B ./$PRODUCT-yr-$subdir/ForWarn2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img --type=Byte --overwrite
rm -f ./$PRODUCT-yr-$subdir/ForWarn2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.tif || true
#gdal_translate ./$PRODUCT-yr-$subdir/ForWarn2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.img -of GTiff ./$PRODUCT-yr-$subdir/ForWarn2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.tif
#xv ./$PRODUCT-yr-$subdir/ForWarn2.$YEAR.$DOY.$PRODUCT-yr-baseline.tif
# take out the trash
rm -f ./$PRODUCT-yr-$subdir/ForWarn2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img* || true
rm -f ./$PRODUCT-yr-$subdir/ForWarn2LAEA.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img* || true
rm -f ./$PRODUCT-yr-$subdir/ForWarn2.$YEAR.$DOY.$PRODUCT-yr-baseline.data.img* || true
rm -f ./$PRODUCT-yr-$subdir/ForWarn2.$YEAR.$DOY.$PRODUCT-yr-baseline.masks.img* || true
rm -f ./$PRODUCT-yr-$subdir/ForWarn2.$YEAR.$DOY.$PRODUCT-yr-baseline.img* || true
else
echo "./$PRODUCT-yr-"$subdir"/ForWarn2LAEA."$YEAR"."$DOY"."$PRODUCT"-yr-baseline.img file already exists"
fi
else
echo "This "$PRODUCT"-year length baseline is not possible for this year "$YEAR" "$DOY
fi # year is earlier than 2003+$PRODUCT, this length baseline is not possible
done # over 1-, 3-, 5- and 10-yr PRODUCTs
echo "1-, 3-, 5- and 10-yr products have been generated!"
##
# calculate 90th, 50th and 10th percentile baselines over all prior years
# for this DOY
#*******************************************************************
# this is code that writes code for gdal_calc for 50th and 90th and 10th percentile of all prior years, over the entire MODIS period until 2028
# much shorter than case statements!
#*******************************************************************
for prioryear in 2003/A 2004/B 2005/C 2006/D 2007/E 2008/F 2009/G 2010/H 2011/I 2012/J 2013/K 2014/L 2015/M 2016/N 2017/O 2018/P 2019/Q 2020/R 2021/S 2022/T 2023/U 2024/V 2025/W 2026/X 2027/Y 2028/Z
do
yr=`echo $prioryear|awk -F/ '{print $1}'`
ltr=`echo $prioryear|awk -F/ '{print $2}'`
yrstring=$yrstring" "$yr
ltrstring=$ltrstring" "$ltr
if [ $yr -eq $LASTYEAR ]; then
break
fi
done
#*******************************************************************
if [ ! -f medianallpriormax.$YEAR.$DOY.$TYPE.img ]
then
echo "File does not exist for medianallpriormax."$YEAR"."$DOY"."$TYPE".img"
first=`
echo -n "percentile(["
for cac in $ltrstring
do
if [ $cac == A ]; then
echo -n "("$cac"<251)*"$cac
else
echo -n ",("$cac"<251)*"$cac
fi
done
echo -en "],50,axis=0)+("
for cac in $ltrstring
do
if [ $cac == A ]; then
echo -n "("$cac"==254)"
else
echo -n "&("$cac"==254)"
fi
done
echo -e ")*0+( ("
for cac in $ltrstring
do
if [ $cac == A ]; then
echo -n "("$cac"!=254)"
else
echo -n "|("$cac"!=254)"
fi
done
echo -n ")&("
for cac in $ltrstring
do
if [ $cac == A ]; then
echo -n "("$cac">250)"
else
echo -n "&("$cac">250)"
fi
done
echo -n ") )*255"
`
second=`
yr=2003
for cac in $ltrstring
do
echo -n " -"$cac" maxMODISmax."$yr".$DOY.$TYPE.img "
let yr=yr+1
done
`
echo $first
echo $second
gdal_calc.py --debug --calc="`echo $first`" `echo $second` --outfile=medianallpriormax.$YEAR.$DOY.$TYPE.img --type=Byte --NoDataValue=252 --format=HFA --co "STATISTICS=YES" --co "COMPRESSED=YES" --overwrite
else
echo "medianallpriormax."$YEAR"."$DOY"."$TYPE".img file already exists"
fi
#*******************************************************************
if [ ! -f 90thallpriormax.$YEAR.$DOY.$TYPE.img ]
then
echo "File does not exist for 90thallpriormax."$YEAR"."$DOY"."$TYPE".img"
first=`
echo -n "percentile(["
for cac in $ltrstring
do
if [ $cac == A ]; then
echo -n "("$cac"<251)*"$cac
else
echo -n ",("$cac"<251)*"$cac
fi
done
echo -en "],90,axis=0)+("
for cac in $ltrstring
do
if [ $cac == A ]; then
echo -n "("$cac"==254)"
else
echo -n "&("$cac"==254)"
fi
done
echo -e ")*0+( ("
for cac in $ltrstring
do
if [ $cac == A ]; then
echo -n "("$cac"!=254)"
else
echo -n "|("$cac"!=254)"
fi
done
echo -n ")&("
for cac in $ltrstring
do
if [ $cac == A ]; then
echo -n "("$cac">250)"
else
echo -n "&("$cac">250)"
fi
done
echo -n ") )*255"
`
second=`
yr=2003
for cac in $ltrstring
do
echo -n " -"$cac" maxMODISmax."$yr".$DOY.$TYPE.img "
let yr=yr+1
done
`
gdal_calc.py --debug --calc="`echo $first`" `echo $second` --outfile=90thallpriormax.$YEAR.$DOY.$TYPE.img --type=Byte --NoDataValue=252 --format=HFA --co "STATISTICS=YES" --co "COMPRESSED=YES" --overwrite
else
echo "90thallpriormax."$YEAR"."$DOY"."$TYPE".img file already exists"
fi
# START 10thallpriormax
if [ ! -f 10thallpriormax.$YEAR.$DOY.$TYPE.img ]
then
echo "File does not exist for 10thallpriormax."$YEAR"."$DOY"."$TYPE".img"
first=`
echo -n "percentile(["
for cac in $ltrstring
do
if [ $cac == A ]; then
echo -n "("$cac"<251)*"$cac
else
echo -n ",("$cac"<251)*"$cac
fi
done
echo -en "],10,axis=0)+("
for cac in $ltrstring
do
if [ $cac == A ]; then
echo -n "("$cac"==254)"
else
echo -n "&("$cac"==254)"
fi
done
echo -e ")*0+( ("
for cac in $ltrstring
do
if [ $cac == A ]; then
echo -n "("$cac"!=254)"
else
echo -n "|("$cac"!=254)"
fi
done
echo -n ")&("
for cac in $ltrstring
do
if [ $cac == A ]; then
echo -n "("$cac">250)"
else
echo -n "&("$cac">250)"
fi
done
echo -n ") )*255"
`
second=`
yr=2003
for cac in $ltrstring
do
echo -n " -"$cac" maxMODISmax."$yr".$DOY.$TYPE.img "
let yr=yr+1
done
`
gdal_calc.py --debug --calc="`echo $first`" `echo $second` --outfile=10thallpriormax.$YEAR.$DOY.$TYPE.img --type=Byte --NoDataValue=252 --format=HFA --co "STATISTICS=YES" --co "COMPRESSED=YES" --overwrite
else
echo "10thallpriormax."$YEAR"."$DOY"."$TYPE".img file already exists"
fi
#*******************************************************************
##### this is an example of the gdal_calc.py --debug code that this code writes ###
#*******************************************************************
# calculate mean, max, 90th and 50th median baselines over all prior years
# for this DOY
#case $numyrsprior in
#16) # 2019 thru P
#
#gdal_calc.py --debug --calc="\
#percentile([\
#(A<251)*A,(B<251)*B,(C<251)*C,\
#(D<251)*D,(E<251)*E,(F<251)*F,\
#(G<251)*G,(H<251)*H,(I<251)*I,\
#(J<251)*J,(K<251)*K,(L<251)*L,\
#(M<251)*M,(N<251)*N,(O<251)*O,\
#(P<251)*P\
#],50,axis=0)\
#+(\
#(A==254)&(B==254)&(C==254)&\
#(D==254)&(E==254)&(F==254)&\
#(G==254)&(H==254)&(I==254)&\
#(J==254)&(K==254)&(L==254)&\
#(M==254)&(N==254)&(O==254)&\
#(P==254)\
#)*0\
#+( (\
#(A!=254)|(B!=254)|(C!=254)|\
#(D!=254)|(E!=254)|(F!=254)|\
#(G!=254)|(H!=254)|(I!=254)|\
#(J!=254)|(K!=254)|(L!=254)|\
#(M!=254)|(N!=254)|(O!=254)|\
#(P!=254)\
#)&\
#( \
#(A>250)&(B>250)&(C>250)&\
#(D>250)&(E>250)&(F>250)&\
#(G>250)&(H>250)&(I>250)&\
#(J>250)&(K>250)&(L>250)&\
#(M>250)&(N>250)&(O>250)&\
#(P>250)\
#) )*255\
#" \
#-A maxMODISmax.2003.$DOY.$TYPE.img -B maxMODISmax.2004.$DOY.$TYPE.img \
#-C maxMODISmax.2005.$DOY.$TYPE.img -D maxMODISmax.2006.$DOY.$TYPE.img \
#-E maxMODISmax.2007.$DOY.$TYPE.img -F maxMODISmax.2008.$DOY.$TYPE.img \
#-G maxMODISmax.2009.$DOY.$TYPE.img -H maxMODISmax.2010.$DOY.$TYPE.img \
#-I maxMODISmax.2011.$DOY.$TYPE.img -J maxMODISmax.2012.$DOY.$TYPE.img \
#-K maxMODISmax.2013.$DOY.$TYPE.img -L maxMODISmax.2014.$DOY.$TYPE.img \
#-M maxMODISmax.2015.$DOY.$TYPE.img -N maxMODISmax.2016.$DOY.$TYPE.img \
#-O maxMODISmax.2017.$DOY.$TYPE.img -P maxMODISmax.2018.$DOY.$TYPE.img \
#--outfile=medianallpriormax.$YEAR.$DOY.$TYPE.img --type=Byte --NoDataValue=252 --format=HFA --co "STATISTICS=YES" --co "COMPRESSED=YES" --overwrite
#
#gdal_translate medianallpriormax.$YEAR.$DOY.$TYPE.img -of GTiff medianallpriormax.$YEAR.$DOY.$TYPE.tif
#rm *.xml
#gdalinfo -stats -hist medianallpriormax.$YEAR.$DOY.$TYPE.img
#xv medianallpriormax.$YEAR.$DOY.$TYPE.tif
#
#
#gdal_calc.py --debug --calc="\
#percentile([\
#(A<251)*A,(B<251)*B,(C<251)*C,\
#(D<251)*D,(E<251)*E,(F<251)*F,\
#(G<251)*G,(H<251)*H,(I<251)*I,\
#(J<251)*J,(K<251)*K,(L<251)*L,\
#(M<251)*M,(N<251)*N,(O<251)*O,\
#(P<251)*P\
#],90,axis=0)\
#+(\
#(A==254)&(B==254)&(C==254)&\
#(D==254)&(E==254)&(F==254)&\
#(G==254)&(H==254)&(I==254)&\
#(J==254)&(K==254)&(L==254)&\
#(M==254)&(N==254)&(O==254)&\
#(P==254)\
#)*0\
#+( (\
#(A!=254)|(B!=254)|(C!=254)|\
#(D!=254)|(E!=254)|(F!=254)|\
#(G!=254)|(H!=254)|(I!=254)|\
#(J!=254)|(K!=254)|(L!=254)|\