Skip to content

Instantly share code, notes, and snippets.

@wtbarnes
Created October 1, 2015 17:57
Show Gist options
  • Save wtbarnes/0114a405026edd018320 to your computer and use it in GitHub Desktop.
Save wtbarnes/0114a405026edd018320 to your computer and use it in GitHub Desktop.
#astrophys-abbrev.bib
#Will Barnes
#16 July 2015
#Updated: 1 October 2015
#Abbreviations for common journals used in astronomy and astrophysics according to the journal abbreviations guidelines set out by the AAS.
#All journals and keys obtained from
#--http://james.as.arizona.edu/~psmith/proposals/SOinstructions/node33.html
#--http://doc.adsabs.harvard.edu/abs_doc/aas_macros.html
#Before this bib file can be used, search and replace journal titles with correct keywords by running:
#/path/to/my/python /path/to/my/py/script/replace_journal_with_key.py --abbrevs /path/to/abbrevs/astrophys-abbrev.bib --bib /path/to/my_references.bib
#The python scripts parses this abbreviation file and replaces all journal names in the "--bib" file with the keys listed below. The keys are then mapped to the appropriate abbreviations when TeX is run.
#All entries should follow the format (where "at" and "{}" have here been escaped)
#\"at"string\{<key> = "<abbreviation>"\}#"<journal_name>"
#Extras:
#{apjsupp = "ApJS"}#The Astrophysical Journal, Supplement Series
#{astap = "A\&A"}#Astronomy and Astrophysics
#{apjlett = "ApJ"}#The Astrophysical Journal, Letters
@string{aj = "AJ"}#"The Astronomical Journal"
@string{apj = "ApJ"}#"The Astrophysical Journal"
@string{apjl = "ApJ"}#"The Astrophysical Journal Letters"
@string{apjs = "ApJS"}#"The Astrophysical Journal Supplement Series"
@string{aap = "A\&A"}#"Astronomy and Astrophysics"
@string{aap = "A\&A"}#"Astronomy & Astrophysics"
@string{araa = "ARA\&A"}#"Annual Review of Astronomy and Astrophysics"
@string{mnras = "MNRAS"}#"Monthly Notices of the Royal Astronomical Society"
@string{nat = "Nature"}#"Nature"
@string{sci = "Science"}#"Science"
@string{prl = "Phys. Rev. Lett."}#"Physical Review Letters"
@string{rmp = "Rev. Mod. Phys."}#"Review of Modern Physics"
@string{solphys = "Sol. Phys."}#"Solar Physics"
@string{ssr = "Space Sci. Rev."}#"Space Science Reviews"
#replace_journal_with_key.py
#Will Barnes
#1 October 2015
#Import needed modules
import os
import argparse
import re
#Parse command line arguments
parser = argparse.ArgumentParser(description='Parse abbreviations file and search and replace journal names with appropriate keys in .bib file.')
parser.add_argument("--abbrev",help="Abbreviations file that maps key to abbreviation to journal name.")
parser.add_argument("--bib",help="BibTeX file that contains all references, originally with full journal names.")
args = parser.parse_args()
#Read in file
file = open(args.abbrev,'r')
lines = file.readlines()
file.close()
#Strip comments and newlines
lines_stripped = []
[lines_stripped.append(l[:-1]) for l in lines if l[0] != '#' and l[0] != '\n']
#Compile regex format
template = re.compile(r'^@string\{(?P<key>\w+?) = "(?P<abbrev>(\w+[\\&]*\.*\s*)+)"\}#"(?P<name>.*)"$')
#Create template for sed bash command to search and replace bib file
sed_temp = """sed -i '' 's/["{]%s["}]/%s/g' %s"""
#Iterate over formatted lines
for l in lines_stripped:
try:
kaj_match = template.match(l).groupdict()
except AttributeError:
print("Could not parse %s"%l)
print("Check format in %s"%(args.abbrev))
continue
#Replace journal name with key
os.system(sed_temp%(kaj_match['name'],kaj_match['key'],args.bib))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment