예제 #1
0
def date_index(inval, start_date):
    ''' date_index() - Calculate a date inval months from start_date

    Parameters
    ----------
    inval : int
        number of months from start_date
    
    start_date : str
        date in MM/DD/YYYY format

    Returns
    -------
    nothing
    
    '''
    import iwfm as iwfm

    y = iwfm.year(start_date)
    m = iwfm.month(start_date)
    d = iwfm.day(start_date)
    for i in range(1, int(inval)):
        if m == 12:
            y = y + 1
            m = 1
        else:
            m = m + 1
    return iwfm.date2text(d, m, y)
예제 #2
0
def text_date(text):
    ''' text_date() - Convert M/D/YY to MM/DD/YYYY

    Parameters
    ----------
    text : str
        date in some format like M/D/YY

    Returns
    -------
    date string in MM/DD/YYYY format
    '''
    import iwfm as iwfm

    m = iwfm.month(text)
    d = iwfm.day(text)
    y = iwfm.year(text)

    if m < 10:
        mo = '0' + str(m)
    else:
        mo = str(m)
    if d < 10:
        dy = '0' + str(d)
    else:
        dy = str(d)
    return mo + '/' + dy + '/' + str(y)
예제 #3
0
파일: index_date.py 프로젝트: ucdavis/iwfm
def index_date(in_date, start_date='10/01/1984'):
    ''' index_date() - Return the number of days from start_date to in_date

    Parameters
    ----------
    in_date : str
        date after start_date
    
    start_date : str, default='10/01/1984'
        date to count from

    Returns
    -------
    days            (int):   Number of days between two dates
    
    '''
    import iwfm as iwfm

    ys, ms, ds = iwfm.year(start_date), iwfm.month(start_date), iwfm.day(
        start_date)
    yi, mi, di = iwfm.year(in_date), iwfm.month(in_date), iwfm.day(in_date)

    # special case: in_date == start_date
    if ys == yi and ms == mi and ds == di:
        return 0

    mdays = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

    # days from start_date to end of year
    ds = mdays[ms - 1] - ds  # days to end of month
    if int(ys / 4) > 0 and ms > 1:  # leap year
        ds += 1
    for m in range(ms, 12):
        ds += mdays[m]

    # days from beginning of year to in_date
    if mi > 1:
        for m in range(0, mi - 1):  # omits leap years
            di += mdays[m]  # mi=0 points to Jan, etc.

    years = yi - ys
    if yi >= ys:
        years -= 1

    days = ds + di + years * 365 + int(years / 4)
    return days
예제 #4
0
def headall2table(heads_file, output_file, out_date):
    ''' headall2table() - Read IWFM headall.out file and write results
        for one date to a table

    Parameters
    ----------
    heads_file : str
        IWFM headall.out file name
    
    output_file : str
        name of output file
    
    out_date : str
        date to process, mm/dd/yyyy format

    Returns
    -------
    nothing

    '''
    import numpy as np
    import pandas as pd
    import iwfm as iwfm

    out_mon = iwfm.month(out_date)
    out_day = iwfm.day(out_date)
    out_year = iwfm.year(out_date)

    file_lines = open(heads_file).read().splitlines()  # open and read input file

    start = 5  # skip the first 5 lines
    line = start
    nodes = file_lines[line].split()  # read line w/node nos
    nodes.pop(0)  # remove text
    nodes.pop(0)  # remove text

    data = []
    data.append(nodes)

    line += 1
    item = file_lines[line].split()  # read line w/date
    data.append(item)
    end_date = item.pop(0)  # remove the date
    
    while line < len(file_lines) - 1:
        line += 1
        layer = 1
        header = []
        header.append('Node')
        header.append('Layer ' + str(layer))
        while file_lines[line][0].isspace():  # get all the lines for this time step
            item = file_lines[line].split()
            data.append(item)
            layer += 1
            header.append('Layer ' + str(layer))
            line += 1
        # now check the date
        m = iwfm.month(end_date[0:10])
        d = iwfm.day(end_date[0:10])
        y = iwfm.year(end_date[0:10])
        if m == out_mon and d == out_day and y == out_year:
            out_table = np.asarray(data)
            df = pd.DataFrame(np.transpose(out_table))
            df.to_csv(output_file, header=header, index=False)
            return
        else:
            data = []
            data.append(nodes)
            item = file_lines[line].split()  # read line w/date
            data.append(item)
            end_date = item.pop(0)  # remove the date

    return
예제 #5
0
def get_heads_4_date(heads_file, out_date, start=5):
    ''' get_heads_4_date() - Read headall.out file and return heads for a
        specific date 

    Parameters
    ----------
    heads_file : str
        name of IWFM headall.out file
    
    out_date : str
        date in MM/DD/YYYY format
    
    start : int, default=5
        number of header lines to skip

    Returns
    -------
    nothing
    
    '''
    import iwfm as iwfm

    out_mon = iwfm.month(out_date)
    out_day = iwfm.day(out_date)
    out_year = iwfm.year(out_date)

    file_lines = open(
        heads_file).read().splitlines()  # open and read input file

    line_index = start
    nodes = file_lines[line_index].split()  # read line w/node nos
    nodes.pop(0)  # remove text
    nodes.pop(0)  # remove text

    data = []

    line_index += 1
    item = file_lines[line_index].split()  # read line w/date
    data.append(item)
    end_date = item.pop(0)  # remove the date

    while line_index < len(file_lines) - 1:
        line_index += 1
        layer = 1
        header = []
        header.append('Node')
        header.append('Layer ' + str(layer))
        while file_lines[line_index][0].isspace(
        ):  # get all the lines for this time step
            item = file_lines[line_index].split()
            data.append(item)
            layer += 1
            header.append('Layer ' + str(layer))
            line_index += 1
        # now check the date
        m = iwfm.month(end_date[0:10])
        d = iwfm.day(end_date[0:10])
        y = iwfm.year(end_date[0:10])
        if m == out_mon and d == out_day and y == out_year:
            out_table = np.asarray(data)
            return out_table, nodes, header
        else:
            data = []
            data.append(nodes)
            item = file_lines[line_index].split()  # read line w/date
            data.append(item)
            end_date = item.pop(0)  # remove the date

    return