How to get citations ? (Advanced version)¶
Here, we show how you can get citations for CMIP data with a fine-level of user control. For the basic intro, please see How to get citations ? (Basic version).
Imports¶
from functools import partial
import httpx
from cmipcite.citations import (
AuthorListStyle,
DOIGranularity,
get_bibtex_citation,
get_citations,
get_text_citation,
)
The get_citations function allows for more control over how citations are retrieved.
It supports dependency injection which allows you to pass function that defined the
behavior. A few such functions are provided out-of-the-box.
Bibtex¶
This support is built-in.
Simply pass get_bibtex_citation to get_citation.
bibtex_citations = get_citations(
["hdl:21.14100/90f93a05-357c-4ea2-b61f-bf2418700791"],
doi_granularity=DOIGranularity.EXPERIMENT,
get_citation=get_bibtex_citation,
)
print(f"{len(bibtex_citations)=}")
print(bibtex_citations[0])
len(bibtex_citations)=1
@misc{https://doi.org/10.22033/esgf/cmip6.3610,
doi = {10.22033/ESGF/CMIP6.3610},
url = {http://cera-www.dkrz.de/WDCC/meta/CMIP6/CMIP6.CMIP.CCCma.CanESM5.historical},
author = {Swart, Neil Cameron and Cole, Jason N.S. and Kharin, Viatcheslav V. and Lazare, Mike and Scinocca, John F. and Gillett, Nathan P. and Anstey, James and Arora, Vivek and Christian, James R. and Jiao, Yanjun and Lee, Warren G. and Majaess, Fouad and Saenko, Oleg A. and Seiler, Christian and Seinen, Clint and Shao, Andrew and Solheim, Larry and von Salzen, Knut and Yang, Duo and Winter, Barbara and Sigmond, Michael},
keywords = {CMIP6, climate, CMIP6.CMIP.CCCma.CanESM5.historical},
language = {en},
title = {CCCma CanESM5 model output prepared for CMIP6 CMIP historical. Version 20190429.},
publisher = {Earth System Grid Federation},
year = {2019},
copyright = {Creative Commons Attribution 4.0 International}
}
Plain text¶
A specific implementation of this is built-in.
You can pass in get_text_citation
with a 'pre-loaded' value for author_list_style
using functools.partial.
plaintex_citations = get_citations(
["hdl:21.14100/90f93a05-357c-4ea2-b61f-bf2418700791"],
doi_granularity=DOIGranularity.EXPERIMENT,
get_citation=partial(get_text_citation, author_list_style=AuthorListStyle.LONG),
)
print(plaintex_citations[0])
Swart, Neil Cameron; Cole, Jason N.S.; Kharin, Viatcheslav V.; Lazare, Mike; Scinocca, John F.; Gillett, Nathan P.; Anstey, James; Arora, Vivek; Christian, James R.; Jiao, Yanjun; Lee, Warren G.; Majaess, Fouad; Saenko, Oleg A.; Seiler, Christian; Seinen, Clint; Shao, Andrew; Solheim, Larry; von Salzen, Knut; Yang, Duo; Winter, Barbara; Sigmond, Michael (2019): CCCma CanESM5 model output prepared for CMIP6 CMIP historical. Version 20190429. Earth System Grid Federation. https://doi.org/10.22033/ESGF/CMIP6.3610.
Plain text with a short author list
If you need a different value for author_list_style,
just change it in the call to partial.
plaintex_citations = get_citations(
["hdl:21.14100/f2f502c9-9626-31c6-b016-3f7c0534803b"],
doi_granularity=DOIGranularity.MODEL,
get_citation=partial(get_text_citation, author_list_style=AuthorListStyle.SHORT),
)
print(plaintex_citations[0])
Wieners et al. (2019): MPI-M MPIESM1.2-LR model output prepared for CMIP6 CMIP. Version 20211412. Earth System Grid Federation. https://doi.org/10.22033/ESGF/CMIP6.742.
Inject your own function¶
You can also inject your own, custom function.
def get_my_citation(doi: str, version: str) -> str:
"""
Get custom citation
"""
r = httpx.get(f"https://api.datacite.org/dois/{doi}", follow_redirects=True)
data = r.raise_for_status().json()["data"]["attributes"]
creators = ", ".join(
[
f"{c['name'].split(',')[1].strip()} {c['name'].split(',')[0]}"
for c in data["creators"]
]
)
citation = f"{version} {data['titles'][0]['title']} by {creators}. DOI: https://doi.org/{doi}"
return citation
custom_citations = get_citations(
["hdl:21.14100/f2f502c9-9626-31c6-b016-3f7c0534803b"],
doi_granularity=DOIGranularity.MODEL,
get_citation=get_my_citation,
)
print(custom_citations[0])
20211412 MPI-M MPIESM1.2-LR model output prepared for CMIP6 CMIP by Karl-Hermann Wieners, Marco Giorgetta, Johann Jungclaus, Christian Reick, Monika Esch, Matthias Bittner, Stephanie Legutke, Martin Schupfner, Fabian Wachsmann, Veronika Gayler, Helmuth Haak, Philipp de Vrese, Thomas Raddatz, Thorsten Mauritsen, Jin-Song von Storch, Jörg Behrens, Victor Brovkin, Martin Claussen, Traute Crueger, Irina Fast, Stephanie Fiedler, Stefan Hagemann, Cathy Hohenegger, Thomas Jahns, Silvia Kloster, Stefan Kinne, Gitta Lasslop, Luis Kornblueh, Jochem Marotzke, Daniela Matei, Katharina Meraner, Uwe Mikolajewicz, Kameswarrao Modali, Wolfgang Müller, Julia Nabel, Dirk Notz, Karsten Peters-von Gehlen, Robert Pincus, Holger Pohlmann, Julia Pongratz, Sebastian Rast, Hauke Schmidt, Reiner Schnur, Uwe Schulzweida, Katharina Six, Bjorn Stevens, Aiko Voigt, Erich Roeckner. DOI: https://doi.org/10.22033/ESGF/CMIP6.742