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
|
#!/usr/bin/mawk -We
# *********************************************************************
# Written by and copyright Carlo Strozzi <carlos@linux.it>.
#
# gregorian: translate selected date columns from Julian to calendar
# format.
# Copyright (C) 2001 Carlo Strozzi <carlos@linux.it>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# 2001-03-27 Alpha code.
# 2001-04-17 Added inline help.
# 2001-08-17 Added stdio portability
#
# $Id$
# *********************************************************************
BEGIN {
NULL = ""; FS = OFS = "\t"; fmt = "c"
y_start = 1; y_len = 4
m_start = 5
d_start = 7
while (ARGV[++i] != NULL) {
if (ARGV[i] == "-l" || ARGV[i] == "--last") pick_last = 1
else if (ARGV[i] == "-N" || ARGV[i] == "--no-header") no_hdr = 1
else if (ARGV[i] == "-c" || ARGV[i] == "--computer") fmt = "c"
else if (ARGV[i] == "-I" || ARGV[i] == "--iso") fmt = "i"
else if (ARGV[i] == "-e" || ARGV[i] == "--europe") fmt = "e"
else if (ARGV[i] == "-u" || ARGV[i] == "--us") fmt = "u"
else if (ARGV[i] == "-i" || ARGV[i] == "--input") i_file = ARGV[++i]
else if (ARGV[i] == "-o" || ARGV[i] == "--output") o_file = ARGV[++i]
else if (ARGV[i] == "-h" || ARGV[i] == "--help") {
system("grep -v '^#' @DOCPATH@/gregorian.txt")
rc = 1
exit(rc)
}
else if (ARGV[i] == "-S" || ARGV[i] == "--short") {
y_start = 3; y_len = 2
m_start = 3
d_start = 5
}
else target_cols[ARGV[i]] = ARGV[i]
}
ARGC = 1 # Fix argv[]
if (o_file == NULL) o_file = "@STDOUT@"
if (i_file != NULL) { ARGV[1] = i_file; ARGC = 2 }
}
#
# Main loop
#
NR == 1 {
# Load the column position array.
while (++p <= NF) {
# Unless '-l' was specified, make sure we pick the first occurrence
# of duplicated column names (it may happen after a join).
if (P[$p] == NULL) auto_col = auto_col " " $p
if (pick_last) { P[$p] = p; N[p] = $p }
else {
if (P[$p] == NULL) { P[$p] = p; N[p] = $p }
}
}
if (!no_hdr) print > o_file
next
}
# Dashline
NR == 2 { if (!no_hdr) print > o_file; next }
# Table body.
{
for (i=1; i<=NF; i++) {
if (i > 1) printf(OFS) > o_file
if (target_cols[N[i]] == NULL) {
printf("%s", $i) > o_file
continue
}
$i += 0 # Cast to number.
if (!$i) { # Zero, or not a number.
printf("%s", $i) > o_file
continue # Not a julian date.
}
out = jd_to_cal($i)
if (fmt == "c") printf("%s", out) > o_file
else if (fmt == "i") {
printf("%s-%s-%s",substr(out,y_start,y_len), \
substr(out,m_start,2),substr(out,d_start,2)) > o_file
} else if (fmt == "e") {
printf("%s.%s.%s",substr(out,d_start,2), \
substr(out,m_start,2),substr(out,y_start,y_len)) > o_file
} else
printf("%s/%s/%s",substr(out,m_start,2), \
substr(out,d_start,2),substr(out,y_start,y_len)) > o_file
}
printf("\n") > o_file
}
# ---------------------------------------------------------------------
# Convert Julian date to calendar date
# (algorithm adopted from Press et al.)
# Note: although this function can handle hh:mm:ss as well, I'm
# currently only interested in yyyymmdd, so I do not return the formers.
# ---------------------------------------------------------------------
function jd_to_cal(jd, j1,j2,j3,j4,j5,intgr,gregjd,tmp, \
dayfrac,frac,hr,mn,sc,f,y,m,d) {
# Make sure args cast to numbers, or rounding errors may occur.
jd += 0
# get the date from the Julian day number
intgr = int(jd)
frac = jd - intgr
gregjd = 2299161
if (intgr >= gregjd) { # Gregorian calendar correction
tmp = int(((intgr - 1867216) - 0.25) / 36524.25)
j1 = intgr + 1 + tmp - int(0.25*tmp)
} else j1 = intgr
# correction for half day offset
dayfrac = frac + 0.5
if (dayfrac >= 1.0) {
dayfrac -= 1.0
++j1
}
j2 = j1 + 1524
j3 = int(6680.0 + ((j2 - 2439870) - 122.1)/365.25)
j4 = int(j3*365.25)
j5 = int((j2 - j4)/30.6001)
d = int(j2 - j4 - int(j5*30.6001))
m = int(j5 - 1)
if (m > 12) m -= 12
y = int(j3 - 4715)
if (m > 2) --y
if (y <= 0) --y
# get time of day from day fraction
hr = int(dayfrac * 24.0)
mn = int((dayfrac*24.0 - hr)*60.0)
f = ((dayfrac*24.0 - hr)*60.0 - mn)*60.0
sc = int(f)
f -= sc
if (f > 0.5) ++sc
if (y < 0) y = -y
return sprintf("%04d%02d%02d", y, m, d)
}
#
# End of program.
#
|