aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron LI <aaronly.me@outlook.com>2015-11-09 11:32:20 +0800
committerAaron LI <aaronly.me@outlook.com>2015-11-09 11:32:20 +0800
commitd37336c948e7c47aff743a47378a4183ee06b3d6 (patch)
treee312c9095d15ae51c95ce6e9953cb23f79308265
parent1b00b0326064cded4a2b7b06573cc3a44d297dd5 (diff)
downloadchandra-acis-analysis-d37336c948e7c47aff743a47378a4183ee06b3d6.tar.bz2
chandra_json2csv_v3.py: Rewrite; Automatically get keys; Use python3
-rwxr-xr-xscripts/chandra_json2csv_v3.py285
1 files changed, 36 insertions, 249 deletions
diff --git a/scripts/chandra_json2csv_v3.py b/scripts/chandra_json2csv_v3.py
index cf69cf9..e7d67c9 100755
--- a/scripts/chandra_json2csv_v3.py
+++ b/scripts/chandra_json2csv_v3.py
@@ -1,25 +1,37 @@
-#!/usr/bin/python
-# avoid using `/usr/bin/env python'
+#!/usr/bin/env python3
#
-# convert JSON to CSV format
-# Ref: http://stackoverflow.com/questions/1871524/convert-from-json-to-csv-using-python
-## Ref: http://json.parser.online.fr/
+# Convert JSON to CSV format
#
-# LIweitiaNux <liweitianux@gmail.com>
-# August 31, 2012
+# References:
+# [1] https://docs.python.org/3/library/json.html
+# [2] https://docs.python.org/3/library/csv.html
+# [3] http://stackoverflow.com/questions/1871524/convert-from-json-to-csv-using-python
+# [4] http://json.parser.online.fr/
+#
+#
+# Weitian LI <liweitianux@gmail.com>
+# Created: 2012-08-31
#
# ChangeLogs:
-# v3.1, 2013/05/18, LIweitiaNux
-# add key `Feature', corresponding to `collectdata_v3.1'
-# v3.2, 2013/05/29, LIweitiaNux
-# add key `XCNTRD_RA, XCNTRD_DEC'
-# v3.3, 2013/10/14, LIweitiaNux
-# add key `Unified Name'
+# v3.5, 2015-11-09
+# * Get column keys from the first input json block
+# * Use python3
+# v3.4, 2015-11-09
+# * Add keys 'XPEAK_RA', 'XPEAK_DEC', and 'XPEAK_XCNTRD_dist (pix)'
+# * Add 'colkeys' to record CSV column keys
+# * Update file header description
+# v3.3, 2013-10-14
+# * add key `Unified Name'
+# v3.2, 2013-05-29
+# * add key `XCNTRD_RA, XCNTRD_DEC'
+# v3.1, 2013-05-18
+# * add key `Feature', corresponding to `collectdata_v3.1'
#
import sys
import csv
import json
+from collections import OrderedDict
argc = len(sys.argv)
if (argc != 3):
@@ -28,243 +40,18 @@ if (argc != 3):
sys.exit(255)
infile = open(sys.argv[1], 'r')
-data = json.load(infile)
+data = json.load(infile, object_pairs_hook=OrderedDict)
infile.close()
-outfile = csv.writer(open(sys.argv[2], "wb+"))
-
-# write header {{{
-outfile.writerow([
- "Obs. ID",
- "Source Name",
- "Unified Name",
- "Obs. Date",
- "Detector",
- "Exposure (ks)",
- "Clean Exposure (ks)",
- "R. A.",
- "Dec.",
- "XCNTRD_RA",
- "XCNTRD_DEC",
- "nH (10^22 cm^-2)",
- "redshift",
- "E(z)",
- "T_ref (keV)",
- "Z_ref (solar)",
- "Rmax_SBP (pixel)",
- "Rmax_Tpro (pixel)",
- "Rmax_SBP (kpc)",
- "Rmax_Tpro (kpc)",
- "NFW_Rmin (kpc)",
- "Model_SBP",
- "n01",
- "beta1",
- "rc1",
- "rc1_kpc",
- "n02",
- "beta2",
- "rc2",
- "rc2_kpc",
- "bkg",
- "R200 (kpc)",
- "R200_err_lower (1sigma)",
- "R200_err_upper (1sigma)",
- "M200 (M_sun)",
- "M200_err_lower (1sigma)",
- "M200_err_upper (1sigma)",
- "L200 (erg/s)",
- "L200_err (1sigma)",
- "M_gas200 (M_sun)",
- "M_gas200_err_lower (1sigma)",
- "M_gas200_err_upper (1sigma)",
- "F_gas200",
- "F_gas200_err_lower (1sigma)",
- "F_gas200_err_upper (1sigma)",
- "R500 (kpc)",
- "R500_err_lower (1sigma)",
- "R500_err_upper (1sigma)",
- "M500 (M_sun)",
- "M500_err_lower (1sigma)",
- "M500_err_upper (1sigma)",
- "L500 (erg/s)",
- "L500_err (1sigma)",
- "M_gas500 (M_sun)",
- "M_gas500_err_lower (1sigma)",
- "M_gas500_err_upper (1sigma)",
- "F_gas500",
- "F_gas500_err_lower (1sigma)",
- "F_gas500_err_upper (1sigma)",
- "R1500",
- "R1500_err_lower",
- "R1500_err_upper",
- "M1500",
- "M1500_err_lower",
- "M1500_err_upper",
- "L1500",
- "L1500_err",
- "M_gas1500",
- "M_gas1500_err_lower",
- "M_gas1500_err_upper",
- "F_gas1500",
- "F_gas1500_err_lower",
- "F_gas1500_err_upper",
- "R2500",
- "R2500_err_lower",
- "R2500_err_upper",
- "M2500",
- "M2500_err_lower",
- "M2500_err_upper",
- "L2500",
- "L2500_err",
- "M_gas2500",
- "M_gas2500_err_lower",
- "M_gas2500_err_upper",
- "F_gas2500",
- "F_gas2500_err_lower",
- "F_gas2500_err_upper",
- "T(0.1-0.5 R500)",
- "T_err(0.1-0.5 R500)",
- "T_err_l(0.1-0.5 R500)",
- "T_err_u(0.1-0.5 R500)",
- "Z(0.1-0.5 R500)",
- "Z_err(0.1-0.5 R500)",
- "Z_err_l(0.1-0.5 R500)",
- "Z_err_u(0.1-0.5 R500)",
- "T(0.2-0.5 R500)",
- "T_err(0.2-0.5 R500)",
- "T_err_l(0.2-0.5 R500)",
- "T_err_u(0.2-0.5 R500)",
- "Z(0.2-0.5 R500)",
- "Z_err(0.2-0.5 R500)",
- "Z_err_l(0.2-0.5 R500)",
- "Z_err_u(0.2-0.5 R500)",
- "F_gas(R2500-R500)",
- "F_gas_err_l(R2500-R500)",
- "F_gas_err_u(R2500-R500)",
- "R_cool (kpc)",
- "Cooling_time (Gyr)",
- "Cool_core",
- "Feature",
- "NOTE"
-])
-## header }}}
-
-# write data {{{
-for item in data:
- outfile.writerow([
- item["Obs. ID"],
- item["Source Name"],
- item["Unified Name"],
- item["Obs. Date"],
- item["Detector"],
- item["Exposure (ks)"],
- item["Clean Exposure (ks)"],
- item["R. A."],
- item["Dec."],
- item["XCNTRD_RA"],
- item["XCNTRD_DEC"],
- item["nH (10^22 cm^-2)"],
- item["redshift"],
- item["E(z)"],
- item["T_ref (keV)"],
- item["Z_ref (solar)"],
- item["Rmax_SBP (pixel)"],
- item["Rmax_Tpro (pixel)"],
- item["Rmax_SBP (kpc)"],
- item["Rmax_Tpro (kpc)"],
- item["NFW_Rmin (kpc)"],
- item["Model_SBP"],
- item["n01"],
- item["beta1"],
- item["rc1"],
- item["rc1_kpc"],
- item["n02"],
- item["beta2"],
- item["rc2"],
- item["rc2_kpc"],
- item["bkg"],
- item["R200 (kpc)"],
- item["R200_err_lower (1sigma)"],
- item["R200_err_upper (1sigma)"],
- item["M200 (M_sun)"],
- item["M200_err_lower (1sigma)"],
- item["M200_err_upper (1sigma)"],
- item["L200 (erg/s)"],
- item["L200_err (1sigma)"],
- item["M_gas200 (M_sun)"],
- item["M_gas200_err_lower (1sigma)"],
- item["M_gas200_err_upper (1sigma)"],
- item["F_gas200"],
- item["F_gas200_err_lower (1sigma)"],
- item["F_gas200_err_upper (1sigma)"],
- item["R500 (kpc)"],
- item["R500_err_lower (1sigma)"],
- item["R500_err_upper (1sigma)"],
- item["M500 (M_sun)"],
- item["M500_err_lower (1sigma)"],
- item["M500_err_upper (1sigma)"],
- item["L500 (erg/s)"],
- item["L500_err (1sigma)"],
- item["M_gas500 (M_sun)"],
- item["M_gas500_err_lower (1sigma)"],
- item["M_gas500_err_upper (1sigma)"],
- item["F_gas500"],
- item["F_gas500_err_lower (1sigma)"],
- item["F_gas500_err_upper (1sigma)"],
- item["R1500"],
- item["R1500_err_lower"],
- item["R1500_err_upper"],
- item["M1500"],
- item["M1500_err_lower"],
- item["M1500_err_upper"],
- item["L1500"],
- item["L1500_err"],
- item["M_gas1500"],
- item["M_gas1500_err_lower"],
- item["M_gas1500_err_upper"],
- item["F_gas1500"],
- item["F_gas1500_err_lower"],
- item["F_gas1500_err_upper"],
- item["R2500"],
- item["R2500_err_lower"],
- item["R2500_err_upper"],
- item["M2500"],
- item["M2500_err_lower"],
- item["M2500_err_upper"],
- item["L2500"],
- item["L2500_err"],
- item["M_gas2500"],
- item["M_gas2500_err_lower"],
- item["M_gas2500_err_upper"],
- item["F_gas2500"],
- item["F_gas2500_err_lower"],
- item["F_gas2500_err_upper"],
- item["T(0.1-0.5 R500)"],
- item["T_err(0.1-0.5 R500)"],
- item["T_err_l(0.1-0.5 R500)"],
- item["T_err_u(0.1-0.5 R500)"],
- item["Z(0.1-0.5 R500)"],
- item["Z_err(0.1-0.5 R500)"],
- item["Z_err_l(0.1-0.5 R500)"],
- item["Z_err_u(0.1-0.5 R500)"],
- item["T(0.2-0.5 R500)"],
- item["T_err(0.2-0.5 R500)"],
- item["T_err_l(0.2-0.5 R500)"],
- item["T_err_u(0.2-0.5 R500)"],
- item["Z(0.2-0.5 R500)"],
- item["Z_err(0.2-0.5 R500)"],
- item["Z_err_l(0.2-0.5 R500)"],
- item["Z_err_u(0.2-0.5 R500)"],
- item["F_gas(R2500-R500)"],
- item["F_gas_err_l(R2500-R500)"],
- item["F_gas_err_u(R2500-R500)"],
- item["R_cool (kpc)"],
- item["Cooling_time (Gyr)"],
- item["Cool_core"],
- item["Feature"],
- item["NOTE"]
- ])
-## write data }}}
+# column keys
+colkeys = list(data[0].keys())
-## EOF
+with open(sys.argv[2], 'w') as csvfile:
+ outfile_writer = csv.writer(csvfile)
+ # CSV header row
+ outfile_writer.writerow(colkeys)
+ # CSV data rows
+ for row in data:
+ outfile_writer.writerow([ row.get(key) for key in colkeys ])
+# vim: set ts=4 sw=4 tw=0 fenc=utf-8 ft=python: #