<commit_before>'''Generate the WEPP prj files based on what our database has for us
'''
import psycopg2
import psycopg2.extras
import os
import datetime
from math import atan2, degrees, pi

PGCONN = psycopg2.connect(database='idep', host='iemdb')
cursor = PGCONN.cursor(cursor_factory=psycopg2.extras.DictCursor)

def compute_aspect(x0, y0, x1, y1):
    """ Compute the aspect angle between two points """
    dx = x1 - x0
    dy = y1 - y0
    rads = atan2(-dy,dx)
    rads %= 2*pi
    return degrees(rads)

def do_flowpath(huc_12, fid):
    """ Process a given flowpathid """
    cursor.execute("""SELECT segid, elevation, length, landuse, surgo, 
    slope, management,
    landuse1 || landuse2 || landuse3 || landuse4 || landuse5 || landuse6 as lstring,
    ST_X(ST_Transform(geom,4326)) as x, 
    ST_Y(ST_Transform(geom,4326)) as y from flowpath_points WHERE flowpath = %s
    ORDER by segid ASC""", (fid,))
    rows = []
    for row in cursor:
        rows.append( row )
        
    res = {}
    res['clifile'] = "/i/cli/%03ix%03i/%07.2fx%07.2f.cli" % (0 - rows[0]['x'],
                                                           rows[0]['y'],
                                                           0 - rows[0]['x'],
                                                           rows[0]['y'])
    res['huc8'] = huc_12[:8]
    res['huc12'] = huc_12
    res['date'] = datetime.datetime.now()
    res['aspect'] = compute_aspect(rows[0]['x'], rows[0]['y'],
                                   rows[-1]['x'], rows[-1]['y'])
    res['prj_fn'] = "/i/prj/%s/%s/%s_%s.prj" % (res['huc8'], huc_12, 
                                                huc_12, fid)
    res['length'] = rows[-1]['length']
    res['slope_points'] = len(rows)

    # Iterate over the rows and figure out the slopes and distance fracs
    slpdata = ""
    prevsoil = None
    lsoilstart = 0
    soils = []
    soillengths = []
    
    for row in rows:
        if prevsoil != row['surgo']:
            if prevsoil is not None:
                soils.append(prevsoil)
                soillengths.append( row['length'] - lsoilstart)
            prevsoil = row['surgo']
            lsoilstart = row['length']
        slpdata += " %.3f,%.5f" % (row['length'] / res['length'],
                                   row['slope'])
    soils.append(prevsoil)
    soillengths.append( res['length'] - lsoilstart)
    res['soilbreaks'] = len(soillengths) -1
    res['soils'] = ""

    res['slpdata'] = slpdata

    mandata = ""
    prevman = None
    lmanstart = 0
    mans = []
    manlengths = []
    
    for row in rows:
        if row['lstring'] is None:
            continue
        if prevman != row['lstring']:
            if prevman is not None:
                mans.append(prevman)
                manlengths.append( row['length'] - lmanstart)
            prevman = row['lstring']
            lmanstart = row['length']

    mans.append(prevman)
    manlengths.append( res['length'] - lmanstart)
    res['manbreaks'] = len(manlengths) -1
    res['managements'] = ""

        
    for d, s in zip(manlengths, mans):
        res['managements'] += """    %s {
        Distance = %.3f
        File = "/i/man/%s.man"
    }\n""" % (s, d, s)

    return res

def write_prj(data):
    """ Create the WEPP prj file """
    dirname = "/i/prj/%s/%s" % (data['huc8'], data['huc12'])
    if not os.path.isdir(dirname):
        os.makedirs(dirname)<commit_msg>Use simplifed WEPP rotation<commit_after>'''Generate the WEPP prj files based on what our database has for us
'''
import psycopg2
import psycopg2.extras
import os
import datetime
from math import atan2, degrees, pi

PGCONN = psycopg2.connect(database='idep', host='iemdb')
cursor = PGCONN.cursor(cursor_factory=psycopg2.extras.DictCursor)

def get_rotation(code, maxmanagement):
    """ Convert complex things into a simple WEPP management for now """
    if code == 'CCCCCC':
        if maxmanagement == 1:
            return 'corn-no till.rot'
        return 'corn-fall mulch till.rot'
    if code.count('C') >= 2 and code.count('B') >= 2:
        if maxmanagement == 1:
            return 'corn,soybean-no till.rot'
        return 'corn,soybean-fall mulch till.rot'
    
    print 'Code: %s was problematic to match a rotation!' % (code,)
    return code

def compute_aspect(x0, y0, x1, y1):
    """ Compute the aspect angle between two points """
    dx = x1 - x0
    dy = y1 - y0
    rads = atan2(-dy,dx)
    rads %= 2*pi
    return degrees(rads)

def do_flowpath(huc_12, fid):
    """ Process a given flowpathid """
    cursor.execute("""SELECT segid, elevation, length, landuse, surgo, 
    slope, management,
    landuse1 || landuse2 || landuse3 || landuse4 || landuse5 || landuse6 as lstring,
    ST_X(ST_Transform(geom,4326)) as x, 
    ST_Y(ST_Transform(geom,4326)) as y from flowpath_points WHERE flowpath = %s
    ORDER by segid ASC""", (fid,))
    rows = []
    maxmanagement = 0
    for row in cursor:
        if row['management'] > maxmanagement:
            maxmanagement = row['management']
        rows.append( row )
        
    res = {}
    res['clifile'] = "/i/cli/%03ix%03i/%07.2fx%07.2f.cli" % (0 - rows[0]['x'],
                                                           rows[0]['y'],
                                                           0 - rows[0]['x'],
                                                           rows[0]['y'])
    res['huc8'] = huc_12[:8]
    res['huc12'] = huc_12
    res['date'] = datetime.datetime.now()
    res['aspect'] = compute_aspect(rows[0]['x'], rows[0]['y'],
                                   rows[-1]['x'], rows[-1]['y'])
    res['prj_fn'] = "/i/prj/%s/%s/%s_%s.prj" % (res['huc8'], huc_12, 
                                                huc_12, fid)
    res['length'] = rows[-1]['length']
    res['slope_points'] = len(rows)

    # Iterate over the rows and figure out the slopes and distance fracs
    slpdata = ""
    prevsoil = None
    lsoilstart = 0
    soils = []
    soillengths = []
    
    for row in rows:
        if prevsoil != row['surgo']:
            if prevsoil is not None:
                soils.append(prevsoil)
                soillengths.append( row['length'] - lsoilstart)
            prevsoil = row['surgo']
            lsoilstart = row['length']
        slpdata += " %.3f,%.5f" % (row['length'] / res['length'],
                                   row['slope'])
    soils.append(prevsoil)
    soillengths.append( res['length'] - lsoilstart)
    res['soilbreaks'] = len(soillengths) -1
    res['soils'] = ""

    res['slpdata'] = slpdata

    prevman = None
    lmanstart = 0
    mans = []
    manlengths = []
    
    for row in rows:
        if row['lstring'] is None:
            continue
        if prevman != row['lstring']:
            if prevman is not None:
                mans.append( get_rotation(prevman, maxmanagement) )
                manlengths.append( row['length'] - lmanstart)
            prevman = row['lstring']
            lmanstart = row['length']

    mans.append( get_rotation(prevman, maxmanagement) )
    manlengths.append( res['length'] - lmanstart)
    res['manbreaks'] = len(manlengths) -1
    res['managements'] = ""

        
    for d, s in zip(manlengths, mans):
        res['managements'] += """    %s {
        Distance = %.3f
        File = "/i/rot/%s"
    }\n""" % (s, d, s)

    return res

def write_prj(data):
    """ Create the WEPP prj file """
    dirname = "/i/prj/%s/%s" % (data['huc8'], data['huc12'])
    if not os.path.isdir(dirname):
        os.makedirs(dirname)
{'fraction_non_alphanumeric': 0.14873501997336885, 'fraction_numerical': 0.020639147802929428, 'mean_word_length': 1.9711234177215189, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4015580', 'n_tokens_mistral': 2553, 'n_tokens_neox': 2463, 'n_words': 584}