Skip to content

Commit

Permalink
Merge branch 'main' of github.com:ARM-DOE/ACT into precommit_action
Browse files Browse the repository at this point in the history
  • Loading branch information
zssherman committed Feb 13, 2024
2 parents 29646c3 + bbbb767 commit 58f408c
Show file tree
Hide file tree
Showing 87 changed files with 2,285 additions and 1,287 deletions.
13 changes: 4 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ repos:
- id: check-docstring-first
- id: check-json
- id: check-yaml
- id: double-quote-string-fixer
- id: debug-statements
- id: mixed-line-ending

Expand All @@ -24,12 +23,8 @@ repos:
- id: black
- id: black-jupyter

- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: 'v0.0.239'
hooks:
- id: flake8

- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
- id: ruff
args: [ "--fix" ]
4 changes: 2 additions & 2 deletions act/corrections/mpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ def correct_mpl(
x_data = x_data - x_ap

# R-Squared Correction
co_data = co_data * height ** 2
x_data = x_data * height ** 2
co_data = co_data * height**2
x_data = x_data * height**2

# Overlap Correction
for j in range(ds[range_bins_var_name].size):
Expand Down
2 changes: 1 addition & 1 deletion act/discovery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
'cropscape': ['get_crop_type'],
'noaapsl': ['download_noaa_psl_data'],
'neon': ['get_neon_site_products', 'get_neon_product_avail', 'download_neon_data'],
'surfrad': ['download_surfrad_data']
'surfrad': ['download_surfrad_data'],
},
)
172 changes: 130 additions & 42 deletions act/discovery/airnow.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,42 @@ def get_airnow_forecast(token, date, zipcode=None, latlon=None, distance=25):
"""

# default beginning of the query url
query_url = ('https://airnowapi.org/aq/forecast/')
query_url = 'https://airnowapi.org/aq/forecast/'

# checking is either a zipcode or latlon coordinate is defined
# if neither is defined then error is raised
if (zipcode is None) and (latlon is None):
raise NameError("Zipcode or latlon must be defined")

if zipcode:
url = (query_url + ('zipcode/?' + 'format=text/csv' + '&zipCode='
+ str(zipcode) + '&date=' + str(date)
+ '&distance=' + str(distance)
+ '&API_KEY=' + str(token)))
url = query_url + (
'zipcode/?'
+ 'format=text/csv'
+ '&zipCode='
+ str(zipcode)
+ '&date='
+ str(date)
+ '&distance='
+ str(distance)
+ '&API_KEY='
+ str(token)
)

if latlon:
url = (query_url + ('latLong/?' + 'format=text/csv'
+ '&latitude=' + str(latlon[0]) + '&longitude='
+ str(latlon[1]) + '&date=' + str(date)
+ '&distance=' + str(distance)
+ '&API_KEY=' + str(token)))
url = query_url + (
'latLong/?'
+ 'format=text/csv'
+ '&latitude='
+ str(latlon[0])
+ '&longitude='
+ str(latlon[1])
+ '&date='
+ str(date)
+ '&distance='
+ str(distance)
+ '&API_KEY='
+ str(token)
)

df = pd.read_csv(url)

Expand Down Expand Up @@ -103,7 +120,7 @@ def get_airnow_obs(token, date=None, zipcode=None, latlon=None, distance=25):
"""

# default beginning of the query url
query_url = ('https://www.airnowapi.org/aq/observation/')
query_url = 'https://www.airnowapi.org/aq/observation/'

# checking is either a zipcode or latlon coordinate is defined
# if neither is defined then error is raised
Expand All @@ -114,26 +131,67 @@ def get_airnow_obs(token, date=None, zipcode=None, latlon=None, distance=25):
if date is None:
obs_type = 'current'
if zipcode:
url = (query_url + ('zipCode/' + str(obs_type) + '/?' + 'format=text/csv'
+ '&zipCode=' + str(zipcode) + '&distance=' + str(distance)
+ '&API_KEY=' + str(token)))
url = query_url + (
'zipCode/'
+ str(obs_type)
+ '/?'
+ 'format=text/csv'
+ '&zipCode='
+ str(zipcode)
+ '&distance='
+ str(distance)
+ '&API_KEY='
+ str(token)
)
if latlon:
url = (query_url + ('latLong/' + str(obs_type) + '/?' + 'format=text/csv'
+ '&latitude=' + str(latlon[0])
+ '&longitude=' + str(latlon[1]) + '&distance='
+ str(distance) + '&API_KEY=' + str(token)))
url = query_url + (
'latLong/'
+ str(obs_type)
+ '/?'
+ 'format=text/csv'
+ '&latitude='
+ str(latlon[0])
+ '&longitude='
+ str(latlon[1])
+ '&distance='
+ str(distance)
+ '&API_KEY='
+ str(token)
)
else:
obs_type = 'historical'
if zipcode:
url = (query_url + ('zipCode/' + str(obs_type) + '/?' + 'format=text/csv'
+ '&zipCode=' + str(zipcode) + '&date=' + str(date)
+ 'T00-0000&distance=' + str(distance) + '&API_KEY=' + str(token)))
url = query_url + (
'zipCode/'
+ str(obs_type)
+ '/?'
+ 'format=text/csv'
+ '&zipCode='
+ str(zipcode)
+ '&date='
+ str(date)
+ 'T00-0000&distance='
+ str(distance)
+ '&API_KEY='
+ str(token)
)
if latlon:
url = (query_url + ('latLong/' + str(obs_type) + '/?' + 'format=text/csv'
+ '&latitude=' + str(latlon[0])
+ '&longitude=' + str(latlon[1]) + '&date='
+ str(date) + 'T00-0000&distance=' + str(distance)
+ '&API_KEY=' + str(token)))
url = query_url + (
'latLong/'
+ str(obs_type)
+ '/?'
+ 'format=text/csv'
+ '&latitude='
+ str(latlon[0])
+ '&longitude='
+ str(latlon[1])
+ '&date='
+ str(date)
+ 'T00-0000&distance='
+ str(distance)
+ '&API_KEY='
+ str(token)
)

df = pd.read_csv(url)

Expand All @@ -143,8 +201,9 @@ def get_airnow_obs(token, date=None, zipcode=None, latlon=None, distance=25):
return ds


def get_airnow_bounded_obs(token, start_date, end_date, latlon_bnds, parameters='OZONE,PM25', data_type='B',
mon_type=0):
def get_airnow_bounded_obs(
token, start_date, end_date, latlon_bnds, parameters='OZONE,PM25', data_type='B', mon_type=0
):
"""
Get AQI values or data concentrations for a specific date and time range and set of
parameters within a geographic area of intrest
Expand Down Expand Up @@ -184,16 +243,44 @@ def get_airnow_bounded_obs(token, start_date, end_date, latlon_bnds, parameters=
verbose = 1
inc_raw_con = 1

url = ('https://www.airnowapi.org/aq/data/?startDate=' + str(start_date)
+ '&endDate=' + str(end_date) + '&parameters=' + str(parameters)
+ '&BBOX=' + str(latlon_bnds) + '&dataType=' + str(data_type)
+ '&format=text/csv' + '&verbose=' + str(verbose)
+ '&monitorType=' + str(mon_type) + '&includerawconcentrations='
+ str(inc_raw_con) + '&API_KEY=' + str(token))
url = (
'https://www.airnowapi.org/aq/data/?startDate='
+ str(start_date)
+ '&endDate='
+ str(end_date)
+ '&parameters='
+ str(parameters)
+ '&BBOX='
+ str(latlon_bnds)
+ '&dataType='
+ str(data_type)
+ '&format=text/csv'
+ '&verbose='
+ str(verbose)
+ '&monitorType='
+ str(mon_type)
+ '&includerawconcentrations='
+ str(inc_raw_con)
+ '&API_KEY='
+ str(token)
)

# Set Column names
names = ['latitude', 'longitude', 'time', 'parameter', 'concentration', 'unit',
'raw_concentration', 'AQI', 'category', 'site_name', 'site_agency', 'aqs_id', 'full_aqs_id']
names = [
'latitude',
'longitude',
'time',
'parameter',
'concentration',
'unit',
'raw_concentration',
'AQI',
'category',
'site_name',
'site_agency',
'aqs_id',
'full_aqs_id',
]

# Read data into CSV
df = pd.read_csv(url, names=names)
Expand All @@ -211,12 +298,9 @@ def get_airnow_bounded_obs(token, start_date, end_date, latlon_bnds, parameters=
data_vars={
'latitude': (['sites'], latitude),
'longitude': (['sites'], longitude),
'aqs_id': (['sites'], aqs_id)
'aqs_id': (['sites'], aqs_id),
},
coords={
'time': (['time'], times),
'sites': (['sites'], sites)
}
coords={'time': (['time'], times), 'sites': (['sites'], sites)},
)

# Set up emtpy data with nans
Expand All @@ -233,7 +317,11 @@ def get_airnow_bounded_obs(token, start_date, end_date, latlon_bnds, parameters=
data[v, t, s] = list(result[variables[v]])[0]
atts = {'units': ''}
else:
result = df.loc[(df['time'] == times[t]) & (df['site_name'] == sites[s]) & (df['parameter'] == variables[v])]
result = df.loc[
(df['time'] == times[t])
& (df['site_name'] == sites[s])
& (df['parameter'] == variables[v])
]
if len(result['concentration']) > 0:
data[v, t, s] = list(result['concentration'])[0]
atts = {'units': list(result['unit'])[0]}
Expand Down
13 changes: 8 additions & 5 deletions act/discovery/arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
"""

import argparse
import json
import os
import sys
from datetime import timedelta
import requests
import textwrap
import warnings

try:
from urllib.request import urlopen
Expand Down Expand Up @@ -163,7 +160,9 @@ def download_arm_data(username, token, datastream, startdate, enddate, time=None
open_bytes_file.write(data)
file_names.append(output_file)
# Get ARM DOI and print it out
doi = get_arm_doi(datastream, start_datetime.strftime('%Y-%m-%d'), end_datetime.strftime('%Y-%m-%d'))
doi = get_arm_doi(
datastream, start_datetime.strftime('%Y-%m-%d'), end_datetime.strftime('%Y-%m-%d')
)
print('\nIf you use these data to prepare a publication, please cite:\n')
print(textwrap.fill(doi, width=80))
print('')
Expand Down Expand Up @@ -197,7 +196,11 @@ def get_arm_doi(datastream, startdate, enddate):
"""

# Get the DOI information
doi_url = 'https://adc.arm.gov/citationservice/citation/datastream?id=' + datastream + '&citationType=apa'
doi_url = (
'https://adc.arm.gov/citationservice/citation/datastream?id='
+ datastream
+ '&citationType=apa'
)
doi_url += '&startDate=' + startdate
doi_url += '&endDate=' + enddate
try:
Expand Down
3 changes: 1 addition & 2 deletions act/discovery/asos.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

import numpy as np
import pandas as pd
import xarray as xr
from six import StringIO
from io import StringIO

try:
from urllib.request import urlopen
Expand Down
1 change: 0 additions & 1 deletion act/discovery/neon.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"""

import json
import requests
import os
import shutil
Expand Down
27 changes: 18 additions & 9 deletions act/discovery/noaapsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
Function for downloading data from NOAA PSL Profiler Network
"""
import json
from datetime import datetime
import pandas as pd
import numpy as np
import os

try:
Expand All @@ -14,8 +12,9 @@
from urllib import urlopen


def download_noaa_psl_data(site=None, instrument=None, startdate=None, enddate=None,
hour=None, output=None):
def download_noaa_psl_data(
site=None, instrument=None, startdate=None, enddate=None, hour=None, output=None
):
"""
Function to download data from the NOAA PSL Profiler Network Data Library
https://psl.noaa.gov/data/obs/datadisplay/
Expand Down Expand Up @@ -76,9 +75,18 @@ def download_noaa_psl_data(site=None, instrument=None, startdate=None, enddate=N
url = 'https://downloads.psl.noaa.gov/psd2/data/realtime/'

# Set list of strings that all point to the surface meteorology dataset
met_ds = ['Pressure', 'Datalogger', 'Net Radiation', 'Temp/RH',
'Solar Radiation', 'Tipping Bucket', 'TBRG', 'Wind Speed',
'Wind Direction', 'Wind Speed and Direction']
met_ds = [
'Pressure',
'Datalogger',
'Net Radiation',
'Temp/RH',
'Solar Radiation',
'Tipping Bucket',
'TBRG',
'Wind Speed',
'Wind Direction',
'Wind Speed and Direction',
]

# Add to the url depending on which instrument is requested
if 'Parsivel' in instrument:
Expand Down Expand Up @@ -153,8 +161,9 @@ def download_noaa_psl_data(site=None, instrument=None, startdate=None, enddate=N
# Write each file out to a file with same name as online
for f in files:
if hour is not None:
if (str(doy).zfill(3) + str(hour)) not in f and\
(str(doy).zfill(3) + '.' + str(hour)) not in f:
if (str(doy).zfill(3) + str(hour)) not in f and (
str(doy).zfill(3) + '.' + str(hour)
) not in f:
continue
output_file = os.path.join(output_dir, f)
try:
Expand Down
Loading

0 comments on commit 58f408c

Please sign in to comment.