#!/bin/sh

# BEGIN DESCRIPTION
# SYNOPSYS
#     $program [DIR]
#
# DESCRIPTION
#     This program patches hscpipe 8.x so that it will be able to process
#     new narrow band filters.
#
#     If hscpipe 8.x has been set up (or to be specific, if \$OBS_SUBARU_DIR
#     has been set), DIR can be omitted.
#     If DIR is specified, it must be the path to hscpipe 8.x. For example,
#
#         $program /opt/hscpipe/8.4
# END DESCRIPTION

if [ "$1" = "-h" ] || [ "$1" = "--help" ]
then
    program=$(basename "$0")
    eval echo \""$(
        sed -n -e '/^# BEGIN DESCRIPTION/,/^# END DESCRIPTION/{s/^\(# \|#\)//;p}' "$0" |
        sed -n -e '1d;$d;p'
    )"\"

    exit 0
fi

if [ "$#" -eq 0 ] && [ -z "$OBS_SUBARU_DIR" ]
then
    echo "Please setup hscpipe 8.x first,"
    echo "or specify where hscpipe 8.x has been installed."
    echo ""
    echo "See --help."
    exit 1
fi

if [ "$#" -gt 1 ]
then
    shift
    echo "error: Unrecognized arguments:" "$@"
    echo ""
    echo "See --help."
    exit 1
fi

if [ "$#" -eq 1 ]
then
    echo "Searching $1 for 'obs_subaru' installation ..."
    found="$(find "$1" -path "*/obs_subaru" -exec find "{}" -type d -name "python" ";")"

    if [ -z "$found" ]
    then
        echo "error: Directory 'obs_subaru' is not found."
        exit 1
    fi
    if [ "$(echo "$found" | wc -l)" -ne 1 ]
    then
        echo "error: More than one 'obs_subaru' installation are found:"
        echo "$found"
        exit 1
    fi

    OBS_SUBARU_DIR="$(dirname "$found")"
    echo "Found OBS_SUBARU_DIR=$OBS_SUBARU_DIR"
fi

if ! cd "$OBS_SUBARU_DIR"
then
    echo "error: Cannot change directory to: \$OBS_SUBARU_DIR=$OBS_SUBARU_DIR"
    exit 1
fi

if [ "$(cksum config/hsc/colorterms.py)" != "778888616 8985 config/hsc/colorterms.py" ]
then
    echo "warning: File checksum indicates $OBS_SUBARU_DIR has already been modified."
    echo "Applying patches to this directory may mess it up. Proceed anyway?"
    answer="n"
    read -p "[y/N]" answer
    if [ -z "$(echo "$answer" | grep '^y')" ]
    then
        echo "Canceled."
        exit 1
    fi
fi

echo "Applying patches to $OBS_SUBARU_DIR..."

patch -p7 << "__eof__"
diff -Nur 8.4/lsst_home/stack/current/Linux64/obs_subaru/8.4-hsc/config/hsc/colorterms.py 8.4/lsst_home/stack/current/Linux64/obs_subaru/8.4-hsc/config/hsc/colorterms.py
--- 8.4/lsst_home/stack/current/Linux64/obs_subaru/8.4-hsc/config/hsc/colorterms.py	2020-08-27 03:46:59.000000000 +0000
+++ 8.4/lsst_home/stack/current/Linux64/obs_subaru/8.4-hsc/config/hsc/colorterms.py	2021-09-28 09:11:18.000000000 +0000
@@ -74,7 +74,10 @@
         'y': Colorterm(primary="y", secondary="z", c0=-0.002055, c1=0.209680, c2=0.227296),
         'I945': Colorterm(primary="y", secondary="z", c0=0.005275, c1=-0.194285, c2=-0.125424),
         'N387': Colorterm(primary="g", secondary="r", c0=0.427879, c1=1.869068, c2=0.540580),
+        'N391': Colorterm(primary="g", secondary="r",c0=0.386888, c1=1.688354, c2=0.525252),
+        'N395': Colorterm(primary="g", secondary="r", c0=0.326378, c1=1.477711, c2=0.483298),
         'N400': Colorterm(primary="g", secondary="r", c0=0.176542, c1=1.127055, c2=0.505502),
+        'N430': Colorterm(primary="g", secondary="r",c0=0.121335, c1=0.809929, c2=0.126250),
         'N468': Colorterm(primary="g", secondary="r", c0=-0.042240, c1=0.121756, c2=0.027599),
         'N515': Colorterm(primary="g", secondary="r", c0=-0.021913, c1=-0.253159, c2=0.151553),
         'N527': Colorterm(primary="g", secondary="r", c0=-0.020641, c1=-0.366167, c2=0.038497),
@@ -96,7 +99,10 @@
         'HSC-Y': Colorterm(primary="y", secondary="z", c0=-0.002055, c1=0.209680, c2=0.227296),
         'IB0945': Colorterm(primary="y", secondary="z", c0=0.005275, c1=-0.194285, c2=-0.125424),
         'NB0387': Colorterm(primary="g", secondary="r", c0=0.427879, c1=1.869068, c2=0.540580),
+        'NB0391': Colorterm(primary="g", secondary="r",c0=0.386888, c1=1.688354, c2=0.525252),
+        'NB0395': Colorterm(primary="g", secondary="r", c0=0.326378, c1=1.477711, c2=0.483298),
         'NB0400': Colorterm(primary="g", secondary="r", c0=0.176542, c1=1.127055, c2=0.505502),
+        'NB0430': Colorterm(primary="g", secondary="r",c0=0.121335, c1=0.809929, c2=0.126250),
         'NB0468': Colorterm(primary="g", secondary="r", c0=-0.042240, c1=0.121756, c2=0.027599),
         'NB0515': Colorterm(primary="g", secondary="r", c0=-0.021913, c1=-0.253159, c2=0.151553),
         'NB0527': Colorterm(primary="g", secondary="r", c0=-0.020641, c1=-0.366167, c2=0.038497),
diff -Nur 8.4/lsst_home/stack/current/Linux64/obs_subaru/8.4-hsc/config/hsc/filterMap.py 8.4/lsst_home/stack/current/Linux64/obs_subaru/8.4-hsc/config/hsc/filterMap.py
--- 8.4/lsst_home/stack/current/Linux64/obs_subaru/8.4-hsc/config/hsc/filterMap.py	2020-08-27 03:46:59.000000000 +0000
+++ 8.4/lsst_home/stack/current/Linux64/obs_subaru/8.4-hsc/config/hsc/filterMap.py	2021-09-28 08:59:59.000000000 +0000
@@ -5,7 +5,12 @@
         ("i2", "i"),
         # Narrow bands
         ('N387', 'g'),
+        ('N391', 'g'),
+        ('N395', 'g'),
+        ('N400', 'g'),
+        ('N430', 'g'),
         ('N468', 'g'),
+        ('N497', 'g'),
         ('N515', 'g'),
         ('N527', 'g'),
         ('N656', 'r'),
@@ -30,7 +35,11 @@
         ("HSC-Y", "y"),
         # Narrow bands
         ('NB0387', 'g'),
+        ('NB0391', 'g'),
+        ('NB0395', 'g'),
+        ('NB0430', 'g'),
         ('NB0468', 'g'),
+        ('NB0497', 'g'),
         ('NB0515', 'g'),
         ('NB0527', 'g'),
         ('NB0656', 'r'),
diff -Nur 8.4/lsst_home/stack/current/Linux64/obs_subaru/8.4-hsc/hsc/transmission/wHSC-NB391.txt 8.4/lsst_home/stack/current/Linux64/obs_subaru/8.4-hsc/hsc/transmission/wHSC-NB391.txt
--- 8.4/lsst_home/stack/current/Linux64/obs_subaru/8.4-hsc/hsc/transmission/wHSC-NB391.txt	1970-01-01 00:00:00.000000000 +0000
+++ 8.4/lsst_home/stack/current/Linux64/obs_subaru/8.4-hsc/hsc/transmission/wHSC-NB391.txt	2021-01-28 07:20:06.000000000 +0000
@@ -0,0 +1,100 @@
+# wavelength[angstrom]  transmittance
+3701.5  0.0098
+3705.5  0.0094
+3709.6  0.0088
+3713.6  0.0080
+3717.6  0.0071
+3721.6  0.0073
+3725.7  0.0073
+3729.7  0.0073
+3733.7  0.0077
+3737.8  0.0079
+3741.8  0.0071
+3745.8  0.0062
+3749.9  0.0057
+3753.9  0.0058
+3757.9  0.0059
+3762.0  0.0058
+3766.0  0.0057
+3770.0  0.0053
+3774.1  0.0051
+3778.1  0.0049
+3782.1  0.0049
+3786.2  0.0049
+3790.2  0.0048
+3794.2  0.0048
+3798.3  0.0050
+3802.3  0.0050
+3806.4  0.0050
+3810.4  0.0048
+3814.4  0.0048
+3818.5  0.0051
+3822.5  0.0055
+3826.6  0.0060
+3830.6  0.0066
+3834.7  0.0074
+3838.7  0.0090
+3842.7  0.0125
+3846.8  0.0200
+3850.8  0.0352
+3854.9  0.0627
+3858.9  0.1072
+3863.0  0.1709
+3867.0  0.2519
+3871.1  0.3463
+3875.1  0.4447
+3879.2  0.5384
+3883.2  0.6191
+3887.2  0.6831
+3891.3  0.7292
+3895.3  0.7571
+3899.4  0.7684
+3903.4  0.7729
+3907.5  0.7774
+3911.5  0.7843
+3915.6  0.7905
+3919.7  0.7880
+3923.7  0.7834
+3927.8  0.7781
+3931.8  0.7751
+3935.9  0.7802
+3939.9  0.7783
+3944.0  0.7540
+3948.0  0.7166
+3952.1  0.6667
+3956.1  0.5976
+3960.2  0.5155
+3964.3  0.4294
+3968.3  0.3383
+3972.4  0.2485
+3976.4  0.1703
+3980.5  0.1091
+3984.6  0.0655
+3988.6  0.0376
+3992.7  0.0213
+3996.7  0.0122
+4000.8  0.0072
+4004.9  0.0045
+4008.9  0.0032
+4013.0  0.0025
+4017.1  0.0021
+4021.1  0.0017
+4025.2  0.0015
+4029.2  0.0014
+4033.3  0.0013
+4037.4  0.0013
+4041.4  0.0013
+4045.5  0.0014
+4049.6  0.0014
+4053.6  0.0012
+4057.7  0.0011
+4061.8  0.0012
+4065.9  0.0013
+4069.9  0.0012
+4074.0  0.0011
+4078.1  0.0011
+4082.1  0.0011
+4086.2  0.0011
+4090.3  0.0011
+4094.4  0.0012
+4098.4  0.0011
diff -Nur 8.4/lsst_home/stack/current/Linux64/obs_subaru/8.4-hsc/hsc/transmission/wHSC-NB395.txt 8.4/lsst_home/stack/current/Linux64/obs_subaru/8.4-hsc/hsc/transmission/wHSC-NB395.txt
--- 8.4/lsst_home/stack/current/Linux64/obs_subaru/8.4-hsc/hsc/transmission/wHSC-NB395.txt	1970-01-01 00:00:00.000000000 +0000
+++ 8.4/lsst_home/stack/current/Linux64/obs_subaru/8.4-hsc/hsc/transmission/wHSC-NB395.txt	2021-01-28 07:20:06.000000000 +0000
@@ -0,0 +1,124 @@
+# wavelength[angstrom]  transmittance
+3701.5  0.0126
+3705.5  0.0115
+3709.6  0.0099
+3713.6  0.0109
+3717.6  0.0111
+3721.6  0.0091
+3725.7  0.0095
+3729.7  0.0094
+3733.7  0.0082
+3737.8  0.0084
+3741.8  0.0074
+3745.8  0.0069
+3749.9  0.0068
+3753.9  0.0064
+3757.9  0.0068
+3762.0  0.0062
+3766.0  0.0061
+3770.0  0.0062
+3774.1  0.0059
+3778.1  0.0055
+3782.1  0.0052
+3786.2  0.0050
+3790.2  0.0046
+3794.2  0.0047
+3798.3  0.0050
+3802.3  0.0047
+3806.4  0.0042
+3810.4  0.0041
+3814.4  0.0039
+3818.5  0.0042
+3822.5  0.0041
+3826.6  0.0043
+3830.6  0.0040
+3834.7  0.0043
+3838.7  0.0051
+3842.7  0.0057
+3846.8  0.0069
+3850.8  0.0086
+3854.9  0.0102
+3858.9  0.0118
+3863.0  0.0152
+3867.0  0.0206
+3871.1  0.0285
+3875.1  0.0412
+3879.2  0.0618
+3883.2  0.0917
+3887.2  0.1298
+3891.3  0.1763
+3895.3  0.2362
+3899.4  0.3098
+3903.4  0.3891
+3907.5  0.4675
+3911.5  0.5408
+3915.6  0.6086
+3919.7  0.6774
+3923.7  0.7302
+3927.8  0.7566
+3931.8  0.7669
+3935.9  0.7770
+3939.9  0.7969
+3944.0  0.8211
+3948.0  0.8302
+3952.1  0.8291
+3956.1  0.8416
+3960.2  0.8613
+3964.3  0.8634
+3968.3  0.8460
+3972.4  0.8293
+3976.4  0.8079
+3980.5  0.7619
+3984.6  0.7061
+3988.6  0.6459
+3992.7  0.5760
+3996.7  0.5019
+4000.8  0.4272
+4004.9  0.3571
+4008.9  0.2929
+4013.0  0.2374
+4017.1  0.1936
+4021.1  0.1583
+4025.2  0.1293
+4029.2  0.1032
+4033.3  0.0779
+4037.4  0.0551
+4041.4  0.0369
+4045.5  0.0241
+4049.6  0.0163
+4053.6  0.0121
+4057.7  0.0098
+4061.8  0.0085
+4065.9  0.0074
+4069.9  0.0062
+4074.0  0.0050
+4078.1  0.0039
+4082.1  0.0033
+4086.2  0.0028
+4090.3  0.0027
+4094.4  0.0026
+4098.4  0.0025
+4102.5  0.0024
+4106.6  0.0024
+4110.6  0.0024
+4114.7  0.0024
+4118.8  0.0024
+4122.9  0.0026
+4127.0  0.0025
+4131.0  0.0023
+4135.1  0.0023
+4139.2  0.0026
+4143.3  0.0027
+4147.3  0.0024
+4151.4  0.0025
+4155.5  0.0027
+4159.6  0.0025
+4163.7  0.0025
+4167.7  0.0024
+4171.8  0.0025
+4175.9  0.0026
+4180.0  0.0025
+4184.1  0.0025
+4188.2  0.0028
+4192.2  0.0028
+4196.3  0.0027
diff -Nur 8.4/lsst_home/stack/current/Linux64/obs_subaru/8.4-hsc/hsc/transmission/wHSC-NB430.txt 8.4/lsst_home/stack/current/Linux64/obs_subaru/8.4-hsc/hsc/transmission/wHSC-NB430.txt
--- 8.4/lsst_home/stack/current/Linux64/obs_subaru/8.4-hsc/hsc/transmission/wHSC-NB430.txt	1970-01-01 00:00:00.000000000 +0000
+++ 8.4/lsst_home/stack/current/Linux64/obs_subaru/8.4-hsc/hsc/transmission/wHSC-NB430.txt	2019-07-30 21:08:29.000000000 +0000
@@ -0,0 +1,99 @@
+# wavelength[angstrom]  transmittance
+4102.5  0.0100
+4106.6  0.0102
+4110.6  0.0099
+4114.7  0.0110
+4118.8  0.0104
+4122.9  0.0106
+4127.0  0.0102
+4131.0  0.0104
+4135.1  0.0105
+4139.2  0.0106
+4143.3  0.0108
+4147.3  0.0095
+4151.4  0.0105
+4155.5  0.0107
+4159.6  0.0103
+4163.7  0.0100
+4167.7  0.0095
+4171.8  0.0098
+4175.9  0.0098
+4180.0  0.0102
+4184.1  0.0103
+4188.2  0.0113
+4192.2  0.0096
+4196.3  0.0097
+4200.4  0.0096
+4204.5  0.0094
+4208.6  0.0100
+4212.7  0.0087
+4216.7  0.0097
+4220.8  0.0098
+4224.9  0.0088
+4229.0  0.0089
+4233.1  0.0095
+4237.2  0.0096
+4241.3  0.0096
+4245.4  0.0097
+4249.5  0.0100
+4253.5  0.0119
+4257.6  0.0168
+4261.7  0.0317
+4265.8  0.0666
+4269.9  0.1363
+4274.0  0.2473
+4278.1  0.3879
+4282.2  0.5396
+4286.3  0.6884
+4290.4  0.8202
+4294.5  0.9139
+4298.6  0.9603
+4302.7  0.9672
+4306.8  0.9655
+4310.9  0.9536
+4315.0  0.9070
+4319.1  0.8219
+4323.2  0.7004
+4327.3  0.5527
+4331.4  0.3943
+4335.5  0.2431
+4339.6  0.1265
+4343.7  0.0578
+4347.8  0.0260
+4351.9  0.0139
+4356.0  0.0101
+4360.1  0.0092
+4364.2  0.0085
+4368.3  0.0077
+4372.4  0.0076
+4376.5  0.0073
+4380.6  0.0077
+4384.7  0.0064
+4388.8  0.0070
+4392.9  0.0073
+4397.0  0.0064
+4401.1  0.0072
+4405.2  0.0060
+4409.3  0.0065
+4413.4  0.0067
+4417.6  0.0074
+4421.7  0.0071
+4425.8  0.0073
+4429.9  0.0062
+4434.0  0.0063
+4438.1  0.0067
+4442.2  0.0061
+4446.3  0.0063
+4450.4  0.0067
+4454.6  0.0067
+4458.7  0.0063
+4462.8  0.0057
+4466.9  0.0052
+4471.0  0.0061
+4475.1  0.0057
+4479.2  0.0064
+4483.4  0.0063
+4487.5  0.0059
+4491.6  0.0064
+4495.7  0.0061
+4499.8  0.0048
diff -Nur 8.4/lsst_home/stack/current/Linux64/obs_subaru/8.4-hsc/hsc/transmission/wHSC-NB497.txt 8.4/lsst_home/stack/current/Linux64/obs_subaru/8.4-hsc/hsc/transmission/wHSC-NB497.txt
--- 8.4/lsst_home/stack/current/Linux64/obs_subaru/8.4-hsc/hsc/transmission/wHSC-NB497.txt	1970-01-01 00:00:00.000000000 +0000
+++ 8.4/lsst_home/stack/current/Linux64/obs_subaru/8.4-hsc/hsc/transmission/wHSC-NB497.txt	2020-01-23 05:18:30.000000000 +0000
@@ -0,0 +1,61 @@
+# wavelength[angstrom] transmittance
+4851.5 0.0024
+4855.7 0.0022
+4859.8 0.0027
+4864.0 0.0024
+4868.1 0.0024
+4872.3 0.0024
+4876.5 0.0028
+4880.6 0.0032
+4884.8 0.0031
+4888.9 0.0049
+4893.1 0.0085
+4897.3 0.0141
+4901.4 0.0234
+4905.6 0.0396
+4909.8 0.0683
+4913.9 0.1177
+4918.1 0.1941
+4922.2 0.2935
+4926.4 0.4057
+4930.6 0.5222
+4934.7 0.6337
+4938.9 0.7351
+4943.1 0.8169
+4947.2 0.8675
+4951.4 0.8887
+4955.6 0.8953
+4959.7 0.9025
+4963.9 0.9132
+4968.1 0.9200
+4972.2 0.9175
+4976.4 0.8852
+4980.6 0.8181
+4984.8 0.7415
+4988.9 0.6492
+4993.1 0.5380
+4997.3 0.4153
+5001.4 0.2892
+5005.6 0.1783
+5009.8 0.1007
+5014.0 0.0558
+5018.1 0.0320
+5022.3 0.0196
+5026.5 0.0123
+5030.7 0.0080
+5034.8 0.0053
+5039.0 0.0035
+5043.2 0.0028
+5047.4 0.0024
+5051.5 0.0024
+5055.7 0.0021
+5059.9 0.0021
+5064.1 0.0020
+5068.3 0.0020
+5072.4 0.0020
+5076.6 0.0019
+5080.8 0.0019
+5085.0 0.0020
+5089.2 0.0019
+5093.3 0.0018
+5097.5 0.0019
__eof__
