convert-CRISPCAv0-L1B_PCS.sh 2.12 KB
Newer Older
1
2
3
#!/bin/bash -e
BASE="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

4
l1a_nc_copy_vars() {
RKGarcia's avatar
RKGarcia committed
5
  if [[ -d "$CRIS_L1B_HOME" ]]; then
RKGarcia's avatar
RKGarcia committed
6
    "$CRIS_L1B_HOME/lib/ShellB3/bin/python3" -m sips_cris_l0l1a.netcdf "$@"
RKGarcia's avatar
RKGarcia committed
7
  elif [[ -x "$(which nc_copy_vars 2>/dev/null)" ]]; then
8
9
10
11
12
13
14
    nc_copy_vars "$@"
  else
    echo "${Oops:?Unable to find nc_copy_vars utility; is CRIS_L1B_HOME set?}"
  fi
}

py_ncrename() {
RKGarcia's avatar
RKGarcia committed
15
  if [[ -d "$CRIS_L1B_HOME" ]]; then
16
    "$CRIS_L1B_HOME/lib/ShellB3/bin/python3" "${BASE}/nc_rename.py" "$@"
RKGarcia's avatar
RKGarcia committed
17
18
  elif [[ -x "$(which python3 2>/dev/null)" ]]; then
    python3 "${BASE}/nc_rename.py" "$@"
19
20
21
22
23
  else
    echo "${Oops:?Unable to find python3 with netCDF4 library; is CRIS_L1B_HOME set?}"
  fi
}

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
usage() {
  cat <<EOF
Create a L1B_PCS file with PC Scores, given an L1B input and a CRISPCAv0 input.

Usage:
convert-CRISPCAv0-L1B_PCS.sh L1B-file CRISPCAv0-file {optional-output-file}

Example:
/path/to/convert-CRISPCAv0-L1B_PCS.sh \\
  input/SNDR.J1.CRIS.20210411T0448.m06.g049.L1B.std.v03_08.G.210411135350.nc \\
  input/CRISPCAv0.J1.CRIS.20210411T0448.m06.g049.L1B.std.v03_08.G.210411135350.nc \\
  SNDR.J1.CRIS.20210411T0448.m06.g049.L1B_PCS.std.v03_08.G.210411135350.nc
EOF
  exit 1
}

default_output_name() {
  local fn="$(basename "$1")"
  echo "${fn/.L1B./.L1B_PCS.}"
}


FN_L1B="${1}"
FN_PCS="${2}"
FN_L1BPCS="${3:-$(default_output_name "$FN_L1B")}"
49
50
51
FN_GUIDE=${BASE}/anc/cris_l1b_pcs_inherit.nc
FN_TEMPLATE=${BASE}/anc/template.nc

52
53
54
55
56
if [[ ! -f "$FN_PCS" ]]; then
  usage
  exit 1
fi

57
58
59
60
cp "$FN_TEMPLATE" "${FN_L1BPCS}-"

# reformat the pcs file
cp -f "$FN_PCS" "${FN_L1BPCS}-pcs-"
61
62
63
64
65
66
67
68
69
70
71
72
73
py_ncrename \
    v:globalCoef:rad_global_pcs \
    v:localCoef:rad_local_pcs \
    v:U:rad_local_pcs_U \
    v:M:rad_local_pcs_M \
    v:D:rad_local_pcs_D \
    d:nFORs:xtrack \
    d:nFOVs:fov \
    d:nScans:atrack \
    d:gpcs:globalpc \
    d:pcs:localpc \
    d:channels:wnum \
    "${FN_L1BPCS}-pcs-"
74
75

test ! -f "$FN_L1BPCS" || rm -f "$FN_L1BPCS"
76
77
l1a_nc_copy_vars -vv "${FN_L1BPCS}-" / "$FN_L1B" / "$FN_GUIDE"
l1a_nc_copy_vars -vv "${FN_L1BPCS}-" / "${FN_L1BPCS}-pcs-" / "${FN_L1BPCS}-pcs-"
78
79
80
81

rm -f "${FN_L1BPCS}-pcs-"
mv "${FN_L1BPCS}-" "$FN_L1BPCS"
echo "> $FN_L1BPCS"