Package: cdo-api-py
[![PyPI version](https://badge.fury.io/py/cdo-api-py.svg)](https://badge.fury.io/py/cdo-api-py)# cdo-api-pyPython interface to cdo api, which is described in full detail [here](https://www.ncdc.noaa.gov/cdo-web/webservices/v2#gettingStarted)Built to allow quick and easy query for weather data to pandas dataframe objects.## Installation```pip install cdo-api-py```or for python3```pip3 install cdo-api-py```## Example UseTo start, you'll need a token, which you can request [here](https://www.ncdc.noaa.gov/cdo-web/token).Import a few libraries and instantiate a client. default_units and default_limit are optional keyword arguments.```pythonfrom cdo_api_py import Clientimport pandas as pdfrom datetime import datetimefrom pprint import pprinttoken = "my_token_here"     # be sure not to share your token publiclymy_client = Client(token, default_units='metric', default_limit=1000)```Once a client has been initialized, we can define a few variables to outline what we really want.Since this repo is just a python client to interface with the CDO api, the user has the optionto use keyword arguments that are passed directly to the API and aren't detailed here, so youmay need to browse the options available for the dataset of choice.The example we will use is the very common GHCN-Daily (ghcnd) weather set. We have foundthe north, south, east, and west lat/lon coordinates that describe the bounding box of thegeneral Washington DC area. Next we define the dates we're interested in (optional) andthe dataset id. As an added step, we really want specific values from the dataset so letssave those in a list as well as datatypeid (optional).```pythonextent = {"north": 39.14,"south": 38.68,"east": -76.65,"west": -77.35,}startdate = datetime(2016, 12, 1)enddate = datetime(2016, 12, 31)datasetid='GHCND'datatypeid=['TMIN', 'TMAX', 'PRCP']```Now we pass all these into a single function call to our client `my_client` to find stations of interest.We can use `return_dataframe=True` to automatically assemble the information into a dataframe.```pythonstations = my_client.find_stations(datasetid=datasetid,extent=extent,startdate=startdate,enddate=enddate,datatypeid=datatypeid,return_dataframe=True)pprint(stations)```Now that we have a list of stations that have data useful to us, we can iterate throughthe list of stations and pass the stationid argument to a `get_data_by_station` method.```pythonfor rowid, station in stations.iterrows():  # remember this is a pandas dataframe!station_data = my_client.get_data_by_station(datasetid=datasetid,stationid=station['id'],startdate=startdate,enddate=enddate,return_dataframe=True,include_station_meta=True   # flatten station metadata with ghcnd readings)pprint(station_data)```We can modify this slightly to concatenate all the small dataframes into one big dataframeand save it as a CSV.```pythonbig_df = pd.DataFrame()for rowid, station in stations.iterrows():  # remember this is a pandas dataframe!station_data = my_client.get_data_by_station(datasetid=datasetid,stationid=station['id'],startdate=startdate,enddate=enddate,return_dataframe=True,include_station_meta=True   # flatten station metadata with ghcnd readings)pprint(station_data)big_df = pd.concat([big_df, station_data])print(big_df)big_df = big_df.sort_values(by='date').reset_index()big_df.to_csv('dc_ghcnd_example_output.csv')```see all the example code here: [DC weather data example](docs/example/dc_weather_data.py)It may take a bit of manual searching to familiarize yourself with the NOAA CDO offerings, butonce you figure out the arguments you'd like to use, this client should make it quite easyto automate weather data retrievals. There are many requirements and limits as to the nature ofrequests that the server will allow, and this client will automatically determine if a requestmust be split up into multiple smaller pieces and create them, send them, and piece theresults back together into a single coherent response without any additional effort.***You can explore the endpoints available, either at the CDO documentation site or quickly with```pythonpprint(my_client.list_endpoints())# returned at time of writing{'data': 'A datum is an observed value along with any ancillary attributes at ''a specific place and time.','datacategories': 'A data category is a general type of data used to group ''similar data types.','datasets': 'A dataset is the primary grouping for data at NCDC','datatypes': 'A data type is a specific type of data that is often unique to ''a dataset.','locationcategories': 'A location category is a grouping of similar ''locations.','locations': 'A location is a geopolitical entity.','stations': 'A station is a any weather observing platform where data is ''recorded.'}```At the time of writing, there are about 11 available datasets, they are `['GHCND', 'GSOM', 'GSOY', 'NEXRAD2', 'NEXRAD3', 'NORMAL_ANN', 'NORMAL_DLY', 'NORMAL_HLY', 'NORMAL_MLY', 'PRECIP_15', 'PRECIP_HLY']`. View the full details with:```pythonpprint(my_client.list_datasets())```There are more than 1000 datatypes, but you can see them all with```pythonpprint(my_client.list_datatypes())```## TODO:* Another example or two for non GHCND* Build a gh-pages branch with sphinx