-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed data_cruncher to save year
- Loading branch information
Showing
3 changed files
with
131 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,169 @@ | ||
import os, json | ||
import json | ||
import os | ||
|
||
import pandas as pd | ||
from django.conf import settings | ||
from django.core.management.base import BaseCommand | ||
from utils.epa.sdw_data_cruncher import ( SDW_Data_Cruncher ) | ||
from django.utils import timezone | ||
from django_pandas.io import read_frame | ||
|
||
from app import models as app_models | ||
from rawdata import models as raw_models | ||
from utils.epa.sdw_data_cruncher import SDW_Data_Cruncher | ||
from utils.log import log | ||
from django_pandas.io import read_frame | ||
import pandas as pd | ||
from django.utils import timezone | ||
|
||
|
||
class Command(BaseCommand): | ||
def handle(self, *args, **options): | ||
|
||
now = timezone.now() | ||
year = now.year | ||
if now.month >= 11: | ||
quarter = 'q4' | ||
quarter = "q4" | ||
elif now.month >= 8: | ||
quarter = 'q3' | ||
quarter = "q3" | ||
elif now.month >= 5: | ||
quarter = 'q2' | ||
quarter = "q2" | ||
else: | ||
quarter = 'q1' | ||
|
||
quarter = "q1" | ||
|
||
cruncher = SDW_Data_Cruncher() | ||
|
||
states = [ | ||
"AL", "AK", "AS", "AZ", "AR", "CA", "CO", "CT", | ||
"DE", "DC", "FM", "FL", "GA", "GU", "HI", "ID", | ||
"IL", "IN", "IA", "KS", "KY", "LA", "ME", "MH", | ||
"MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", | ||
"NV", "NH", "NJ", "NM", "NY", "NC", "ND", "MP", | ||
"OH", "OK", "OR", "PW", "PA", "PR", "RI", "SC", | ||
"SD", "TN", "TX", "UT", "VT", "VI", "VA", "WA", | ||
"WV", "WI", "WY" | ||
"AL", | ||
"AK", | ||
"AS", | ||
"AZ", | ||
"AR", | ||
"CA", | ||
"CO", | ||
"CT", | ||
"DE", | ||
"DC", | ||
"FM", | ||
"FL", | ||
"GA", | ||
"GU", | ||
"HI", | ||
"ID", | ||
"IL", | ||
"IN", | ||
"IA", | ||
"KS", | ||
"KY", | ||
"LA", | ||
"ME", | ||
"MH", | ||
"MD", | ||
"MA", | ||
"MI", | ||
"MN", | ||
"MS", | ||
"MO", | ||
"MT", | ||
"NE", | ||
"NV", | ||
"NH", | ||
"NJ", | ||
"NM", | ||
"NY", | ||
"NC", | ||
"ND", | ||
"MP", | ||
"OH", | ||
"OK", | ||
"OR", | ||
"PW", | ||
"PA", | ||
"PR", | ||
"RI", | ||
"SC", | ||
"SD", | ||
"TN", | ||
"TX", | ||
"UT", | ||
"VT", | ||
"VI", | ||
"VA", | ||
"WA", | ||
"WV", | ||
"WI", | ||
"WY", | ||
] | ||
|
||
for state in states: | ||
|
||
areas = cruncher.calc_state_scores(state, print_test = True) | ||
areas = cruncher.calc_state_scores(state, print_test=True) | ||
for __, area in areas.iterrows(): | ||
# systems = raw_models.EpaFacilitySystem.objects.filter(FacFIPSCode = area['fips_county']).values() | ||
# systems_df = read_frame(systems) | ||
# if systems_df.shape[0] != 0: | ||
# systems_df['in_violation'] = systems_df.apply(lambda x: x['CurrVioFlag']==1, axis=1) | ||
# systems_df = systems_df[['PWSId', 'FacName', 'FacLong', 'FacLat', 'in_violation']] | ||
if area['score'] == 0: | ||
if not app_models.data.objects.filter(location = location, score = area['score'], quarter = quarter, year = year).exists(): | ||
if area["score"] == 0: | ||
if not app_models.data.objects.filter( | ||
location=location, | ||
score=area["score"], | ||
quarter=quarter, | ||
year=year, | ||
).exists(): | ||
data = app_models.data() | ||
data.year = year | ||
data.quarter = quarter | ||
data.location = location | ||
data.score = area['score'] | ||
data.score = area["score"] | ||
try: | ||
data.save() | ||
log('%s, %s [%s]' % (location.major_city, location.state, area['score']), 'success') | ||
log( | ||
"%s, %s [%s]" | ||
% (location.major_city, location.state, area["score"]), | ||
"success", | ||
) | ||
except: | ||
print(location) | ||
else: | ||
log('%s, %s [%s]' % (location.major_city, location.state, area['score']), 'info') | ||
log( | ||
"%s, %s [%s]" | ||
% (location.major_city, location.state, area["score"]), | ||
"info", | ||
) | ||
else: | ||
location = app_models.location.objects.filter(fips_county = area['fips_county']).first() | ||
location = app_models.location.objects.filter( | ||
fips_county=area["fips_county"] | ||
).first() | ||
print(area.systems.head()) | ||
if area['score'] - 0.05 < 0: | ||
if area["score"] - 0.05 < 0: | ||
min_score = 0 | ||
else: | ||
min_score = area['score'] - 0.05 | ||
max_score = area['score'] + 0.05 | ||
min_score = area["score"] - 0.05 | ||
max_score = area["score"] + 0.05 | ||
|
||
if not app_models.data.objects.filter(location = location, score__gte = min_score, score__lte = max_score, quarter = quarter, year = year).exists(): | ||
if not app_models.data.objects.filter( | ||
location=location, | ||
score__gte=min_score, | ||
score__lte=max_score, | ||
quarter=quarter, | ||
year=year, | ||
).exists(): | ||
data = app_models.data() | ||
# todo: need to insert facilities here | ||
data.location = location | ||
data.quarter = quarter | ||
data.year = year | ||
data.location = location | ||
data.score = area['score'] | ||
data.score = area["score"] | ||
try: | ||
data.save() | ||
log('%s, %s [%s]' % (location.major_city, location.state, area['score']), 'success') | ||
log( | ||
"%s, %s [%s]" | ||
% (location.major_city, location.state, area["score"]), | ||
"success", | ||
) | ||
except: | ||
print(location) | ||
else: | ||
log('%s, %s [%s]' % (location.major_city, location.state, area['score']), 'info') | ||
log( | ||
"%s, %s [%s]" | ||
% (location.major_city, location.state, area["score"]), | ||
"info", | ||
) |