Exemple #1
0
def initialize_initial_conditions_dict(pairs=None):
    """

    :return:
    """
    # read in initial soil moisture conditions from spin up, put in dict

    if pairs is None:
        print 'Using default initial inputs path: {}'.format(paths.initial_inputs)
        pairs = make_pairs(paths.initial_inputs, INITIAL_KEYS)

    d = {}

    print '-------------------------------------------------------'
    print '        Key                  Name'
    for k, p in pairs:
        print 'initial {:<20s} {}'.format(k, p)
        raster = Raster(p, root=paths.initial_inputs)
        data = raster.masked()
        d[k] = data

        nans = count_nonzero(isnan(data))
        if nans:
            print '{} has {} nan values'.format(k, nans)
        nons = count_nonzero(data < 0.0)
        if nons:
            print '{} has {} negative values'.format(k, nons)

    print '-------------------------------------------------------\n'

    return d
def get_penman(date_object, variable='etrs'):
    """
    Find PENMAN image.

    :param variable: type of PENMAN variable sought
    :type variable: str
    :param date_object: Datetime object of date.
    :return: numpy array object
    """
    names = ('etrs', 'rlin', 'rg')
    if variable not in names:
        raise NotImplementedError('Invalid PENMAN variable name {}. must be in {}'.format(variable, names))

    is_walnut_gulch = False
    # Changed for Walnut gulch NDVI naming convention

    year = date_object.year
    tail = '{}_{:03n}.tif'.format(year, date_object.timetuple().tm_yday)

    if variable == 'etrs':
        name = os.path.join('PM{}'.format(year), 'PM_NM_{}'.format(tail)) # default is this one

    elif variable == 'rlin':
        name = os.path.join('PM{}'.format(year), 'RLIN_NM_{}'.format(tail)) # Makes no sense. Not in directory.

    elif variable == 'rg':
        if is_walnut_gulch:
            name = os.path.join('rad{}'.format(year), 'RTOT_NM_{}'.format(tail))
        else:
            name = os.path.join('rad{}'.format(year), 'RTOT_{}'.format(tail))


    raster = Raster(name, root=paths.penman)
    return raster.masked()
Exemple #3
0
def extract_mask(out, geo, bounds):
    raster = Raster(paths.mask)
    p = make_reduced_path(out, 'Mask', 'mask')
    arr = raster.masked()
    slice_and_save(p, arr, geo, *bounds)

    print 'mask reduced'
Exemple #4
0
def initialize_initial_conditions_dict(pairs=None):
    """

    :return:
    """
    # read in initial soil moisture conditions from spin up, put in dict

    if pairs is None:
        print 'Using default initial inputs path: {}'.format(
            paths.initial_inputs)
        pairs = make_pairs(paths.initial_inputs, INITIAL_KEYS)

    d = {}

    print '-------------------------------------------------------'
    print '        Key                  Name'
    for k, p in pairs:
        print 'initial {:<20s} {}'.format(k, p)
        raster = Raster(p, root=paths.initial_inputs)
        data = raster.masked()
        d[k] = data

        nans = count_nonzero(isnan(data))
        if nans:
            print '{} has {} nan values'.format(k, nans)
        nons = count_nonzero(data < 0.0)
        if nons:
            print '{} has {} negative values'.format(k, nons)

    print '-------------------------------------------------------\n'

    return d
Exemple #5
0
def extract_mask(out, geo, bounds):
    raster = Raster(paths.mask)
    p = make_reduced_path(out, 'Mask', 'mask')
    arr = raster.masked()
    slice_and_save(p, arr, geo, *bounds)

    print 'mask reduced'
Exemple #6
0
def _extract(tag, pairs, root, out, geo, bounds):
    for k, pair in pairs:
        raster = Raster(pair, root=root)
        p = make_reduced_path(out, tag, k)
        arr = raster.masked()
        slice_and_save(p, arr, geo, *bounds)

        print '{} {} reduced'.format(tag, k)
Exemple #7
0
def _extract(tag, pairs, root, out, geo, bounds):
    for k, pair in pairs:
        raster = Raster(pair, root=root)
        p = make_reduced_path(out, tag, k)
        arr = raster.masked()
        slice_and_save(p, arr, geo, *bounds)

        print '{} {} reduced'.format(tag, k)
Exemple #8
0
def get_individ_ndvi(date_object):
    year = str(date_object.year)
    tail = 'NDVI{}_{:02n}_{:02n}.tif'.format(year,
                                             date_object.timetuple().tm_mon,
                                             date_object.timetuple().tm_mday)

    name = os.path.join(year, tail)
    raster = Raster(name, root=paths.ndvi_individ)
    return raster.masked()
def get_individ_ndvi(date_object):
    year = str(date_object.year)
    tail = 'NDVI{}_{:02n}_{:02n}.tif'.format(year,
                                             date_object.timetuple().tm_mon,
                                             date_object.timetuple().tm_mday)

    name = os.path.join(year, tail)
    raster = Raster(name, root=paths.ndvi_individ)
    return raster.masked()
Exemple #10
0
def generate_rew_tiff(sand_tif, clay_tif, output):
    sand = Raster(sand_tif)
    clay = Raster(clay_tif)

    # From ASCE pg 195, equations from Ritchie et al., 1989
    rew = 8 + 0.08 * clay
    rew = where(sand > 80.0, 20.0 - 0.15 * sand, rew)
    rew = where(clay > 50, 11 - 0.06 * clay, rew)

    out = Raster.fromarray(rew, sand.geo)
    out.save(output)
Exemple #11
0
    def update_raster_obj(self, master, date_object):
        """
        Checks if the date is specified for writing output and calls the write and tabulation methods.

        :param master: master dict object from etrm.Processes
        :param date_object: datetime date object
        :return: None
        """
        mo_date = monthrange(date_object.year, date_object.month)

        # save daily data (this will take a long time)
        # don't use 'tot_parameter' or you will sum totals
        # just use the normal daily fluxes from master, aka _daily_outputs

        if self._write_freq == 'daily':

            # dailys = [(element, Raster.fromarray(master[element] - master['p{}'.format(element)]).unmasked()) for element in self._cfg.daily_outputs]

            print "self tiff ", self._cfg.tiff_shape
            tiff_shp = self._cfg.tiff_shape
            dailys = [(element, Raster.fromarray(
                master[element]).unmasked(tiff_shape=tiff_shp))
                      for element in self._cfg.daily_outputs]

            # print 'new dailys -> {}'.format(dailys)

            for element, arr in dailys:
                self._sum_raster_by_shape(element, date_object, arr)

            print "Heres the save dates", self._save_dates
            if self._save_dates:
                print 'Date object {}. {}'.format(
                    date_object, date_object in self._save_dates)
                if date_object in self._save_dates:
                    self._set_outputs(dailys, date_object, 'daily')

        outputs = [(element, Raster.fromarray(
            master[element]).unmasked(tiff_shape=self._cfg.tiff_shape))
                   for element in self._cfg.daily_outputs]
        # print 'outputs rm {}'.format(outputs)

        # save monthly data
        # etrm_processes.run._save_tabulated_results_to_csv will re-sample to annual
        if date_object.day == mo_date[1]:
            print 'saving monthly data for {}'.format(date_object)
            self._set_outputs(outputs, date_object, 'monthly')

        # save annual data
        if date_object.month == 12 and date_object.day == 31:
            self._set_outputs(outputs, date_object, 'annual')
def get_spline_kcb(date_object, previous_kcb=None):
    """
    Find NDVI image and convert to Kcb.

    :param date_object: Datetime object of date.
    :param previous_kcb: Previous day's kcb value.
    :return: numpy array object
    """
    year = str(date_object.year)

    tail = 'ndvi{}_{:03n}.tif'.format(year, date_object.timetuple().tm_yday)
    path = os.path.join(year, tail)

    raster = Raster(path, root=paths.ndvi_spline)
    ndvi = raster.masked()
    return post_process_ndvi(ndvi, previous_kcb=previous_kcb)
Exemple #13
0
def get_spline_kcb(date_object, previous_kcb=None):
    """
    Find NDVI image and convert to Kcb.

    :param date_object: Datetime object of date.
    :param previous_kcb: Previous day's kcb value.
    :return: numpy array object
    """
    year = str(date_object.year)

    tail = 'ndvi{}_{:03n}.tif'.format(year, date_object.timetuple().tm_yday)
    path = os.path.join(year, tail)

    raster = Raster(path, root=paths.ndvi_spline)
    ndvi = raster.masked()
    return post_process_ndvi(ndvi, previous_kcb=previous_kcb)
Exemple #14
0
def slice_and_save(p, arr, geo, startc, endc, startr, endr):
    if not os.path.isdir(os.path.dirname(p)):
        os.makedirs(os.path.dirname(p))
    raster = Raster.fromarray(arr)
    marr = raster.unmasked()
    marr = marr[slice(startr, endr), slice(startc, endc)]
    # print 'saving {}'.format(p)
    raster.save(p, marr, geo)
Exemple #15
0
def slice_and_save(p, arr, geo, startc, endc, startr, endr):
    if not os.path.isdir(os.path.dirname(p)):
        os.makedirs(os.path.dirname(p))
    raster = Raster.fromarray(arr)
    marr = raster.unmasked()
    marr = marr[slice(startr, endr), slice(startc, endc)]
    # print 'saving {}'.format(p)
    raster.save(p, marr, geo)
Exemple #16
0
    def update_raster_obj(self, master, date_object):
        """
        Checks if the date is specified for writing output and calls the write and tabulation methods.

        :param master: master dict object from etrm.Processes
        :param date_object: datetime date object
        :return: None
        """
        mo_date = monthrange(date_object.year, date_object.month)

        # save daily data (this will take a long time)
        # don't use 'tot_parameter' or you will sum totals
        # just use the normal daily fluxes from master, aka _daily_outputs

        if self._write_freq == 'daily':

            # dailys = [(element, Raster.fromarray(master[element] - master['p{}'.format(element)]).unmasked()) for element in self._cfg.daily_outputs]

            print "self tiff ", self._cfg.tiff_shape
            tiff_shp = self._cfg.tiff_shape
            dailys = [(element, Raster.fromarray(master[element]).unmasked(tiff_shape=tiff_shp)) for element in
                      self._cfg.daily_outputs]

            # print 'new dailys -> {}'.format(dailys)

            for element, arr in dailys:
                self._sum_raster_by_shape(element, date_object, arr)

            print "Heres the save dates", self._save_dates
            if self._save_dates:
                print 'Date object {}. {}'.format(date_object, date_object in self._save_dates)
                if date_object in self._save_dates:
                    self._set_outputs(dailys, date_object, 'daily')

        outputs = [(element, Raster.fromarray(master[element]).unmasked(tiff_shape=self._cfg.tiff_shape)) for element in self._cfg.daily_outputs]
        # print 'outputs rm {}'.format(outputs)

        # save monthly data
        # etrm_processes.run._save_tabulated_results_to_csv will re-sample to annual
        if date_object.day == mo_date[1]:
            print 'saving monthly data for {}'.format(date_object)
            self._set_outputs(outputs, date_object, 'monthly')

        # save annual data
        if date_object.month == 12 and date_object.day == 31:
            self._set_outputs(outputs, date_object, 'annual')
Exemple #17
0
def post_process_ndvi(name, in_path, previous_kcb, band=1, scalar=1.25):
    """
    convert to ndvi to Kcb.

    :param date_object: Datetime object of date.
    :param previous_kcb: Previous day's kcb value.
    :return: numpy array object
    """
    raster = Raster(name, root=in_path, band=band)
    ndvi = raster.masked()
    # ndvi *= 0.0001
    kcb = ndvi * scalar

    if previous_kcb is not None:
        kcb = np.where(np.isnan(kcb) is True, previous_kcb, kcb)
        kcb = np.where(abs(kcb) > 100.0, previous_kcb, kcb)

    return kcb
Exemple #18
0
def setup_geo():
    raster = Raster(paths.mask)
    mask_arr = raster.as_bool_array

    # get raster to provide geo data (need one that is not "Byte")
    root = paths.initial_inputs
    name = tiff_list(root)[0]
    raster = Raster(name, root=root)
    geo = raster.geo

    startc, endc, startr, endr = bounding_box(mask_arr)
    geo['rows'] = endr - startr
    geo['cols'] = endc - startc
    transform = get_tiff_transform(paths.mask)
    transform *= Affine.translation(startc, startr)
    geo['geotransform'] = transform.to_gdal()

    return geo, (startc, endc, startr, endr)
def post_process_ndvi(name, in_path, previous_kcb, band=1, scalar=1.25):
    """
    convert to ndvi to Kcb.

    :param date_object: Datetime object of date.
    :param previous_kcb: Previous day's kcb value.
    :return: numpy array object
    """
    raster = Raster(name, root=in_path, band=band)
    ndvi = raster.masked()
    # ndvi *= 0.0001
    kcb = ndvi * scalar

    if previous_kcb is not None:
        kcb = np.where(np.isnan(kcb) is True, previous_kcb, kcb)
        kcb = np.where(abs(kcb) > 100.0, previous_kcb, kcb)

    return kcb
Exemple #20
0
def get_geo(date_object):
    # TODO: tiff vs. tif extension on file names breaks mac or PC
    tail = '{}{:02n}{:02n}.tif'.format(date_object.year, date_object.month,
                                       date_object.day)

    root = os.path.join('precip', '800m_std_all')  # this will need to be fixed
    name = 'PRISMD2_NMHW2mi_{}'.format(tail)
    raster = Raster(name, root=os.path.join(paths.prism, root))

    return raster.geo
Exemple #21
0
def generate_rew_tiff(sand_tif, clay_tif, output):
    sand = Raster(sand_tif)
    clay = Raster(clay_tif)

    # From ASCE pg 195, equations from Ritchie et al., 1989
    rew = 8 + 0.08 * clay
    rew = where(sand > 80.0, 20.0 - 0.15 * sand, rew)
    rew = where(clay > 50, 11 - 0.06 * clay, rew)

    out = Raster.fromarray(rew, sand.geo)
    out.save(output)
Exemple #22
0
def get_penman(date_object, variable='etrs'):
    """
    Find PENMAN image.

    :param variable: type of PENMAN variable sought
    :type variable: str
    :param date_object: Datetime object of date.
    :return: numpy array object
    """
    names = ('etrs', 'rlin', 'rg')
    if variable not in names:
        raise NotImplementedError(
            'Invalid PENMAN variable name {}. must be in {}'.format(
                variable, names))

    is_walnut_gulch = False
    # Changed for Walnut gulch NDVI naming convention

    year = date_object.year
    tail = '{}_{:03n}.tif'.format(year, date_object.timetuple().tm_yday)

    if variable == 'etrs':
        name = os.path.join('PM{}'.format(year),
                            'PM_NM_{}'.format(tail))  # default is this one

    elif variable == 'rlin':
        name = os.path.join(
            'PM{}'.format(year),
            'RLIN_NM_{}'.format(tail))  # Makes no sense. Not in directory.

    elif variable == 'rg':
        if is_walnut_gulch:
            name = os.path.join('rad{}'.format(year),
                                'RTOT_NM_{}'.format(tail))
        else:
            name = os.path.join('rad{}'.format(year), 'RTOT_{}'.format(tail))

    raster = Raster(name, root=paths.penman)
    return raster.masked()
Exemple #23
0
def initialize_static_dict(pairs=None):
    """

    :return:
    """
    def initial_plant_height(r):
        # I think plant height is recorded in ft, when it should be m. Not sure if *= works on rasters.
        return r * 0.3048

    def initial_root_z(r):
        return minimum(r, 100)

    def initial_soil_ksat(r):
        return maximum(
            r, 0.01
        ) * 86.4 / 10  # KSat raster is in um/s; convert using 1 um/s = 86.4 mm/day
        # This / 10 is temporary, to reduce k-sat until better estimate can be obtained

    def initial_tew(r):
        return maximum(minimum(r, 10), 0.001)

    def initial_rew(r):
        return maximum(r, 0.001)

    d = {}
    if pairs is None:
        print 'Using default static inputs path: {}'.format(
            paths.static_inputs)
        pairs = make_pairs(paths.static_inputs, STATIC_KEYS)

    print '-------------------------------------------------------'
    print '       Key                  Name'
    for k, p in pairs:
        print 'static {:<20s} {}'.format(k, p)
        raster = Raster(p, root=paths.static_inputs)
        # print 'raster jjj', raster
        arr = raster.masked()

        if k == 'plant_height':
            arr = initial_plant_height(arr)
            # print arr
        elif k == 'rew':
            arr = initial_rew(arr)
        elif k == 'root_z':
            arr = initial_root_z(arr)
        elif k == 'soil_ksat':
            arr = initial_soil_ksat(arr)
            # print arr
        elif k == 'tew':
            arr = initial_tew(arr)

        d[k] = arr
    print '-------------------------------------------------------'

    q = d['quat_deposits']
    taw = d['taw']
    tew = d['tew']
    land_cover = d['land_cover']
    print 'original Ksat = {}, REW = {}, TAW = {}, land cover = {}'.format(
        d['soil_ksat'], d['rew'], d['taw'], d['land_cover'])

    # apply high TAW to unconsolidated Quaternary deposits
    min_val = 250
    taw = where(q > 0.0, maximum(min_val, taw), taw)

    # apply bounds to TAW
    # min_val = 50.0
    # max_val = 320.0
    #data = d['taw']
    #taw = where(data < min_val, min_val, taw)
    #taw = where(data > max_val, max_val, taw)

    # v = tew + d['rew']
    taw = where(taw < tew, tew, taw)

    # non_zero = count_nonzero(data < min_val)
    # print 'taw has {} cells below the minimum'.format(non_zero, min_val)
    print 'taw median: {}, mean {}, max {}, min {}\n'.format(
        median(taw), taw.mean(), taw.max(), taw.min())
    d['taw'] = taw

    # apply tew adjustment
    # tew = where(land_cover == 41, tew * 0.25, tew)
    # tew = where(land_cover == 42, tew * 0.25, tew)
    # tew = where(land_cover == 43, tew * 0.25, tew)
    # d['tew'] = where(land_cover == 52, tew * 0.75, tew)

    return d
Exemple #24
0
def initialize_static_dict(pairs=None):
    """

    :return:
    """

    def initial_plant_height(r):
        # I think plant height is recorded in ft, when it should be m. Not sure if *= works on rasters.
        return r * 0.3048

    def initial_root_z(r):
        return minimum(r, 100)

    def initial_soil_ksat(r):
        return maximum(r, 0.01) * 86.4 / 10  # KSat raster is in um/s; convert using 1 um/s = 86.4 mm/day
                                     # This / 10 is temporary, to reduce k-sat until better estimate can be obtained

    def initial_tew(r):
        return maximum(minimum(r, 10), 0.001)

    def initial_rew(r):
        return maximum(r, 0.001)

    d = {}
    if pairs is None:
        print 'Using default static inputs path: {}'.format(paths.static_inputs)
        pairs = make_pairs(paths.static_inputs, STATIC_KEYS)

    print '-------------------------------------------------------'
    print '       Key                  Name'
    for k, p in pairs:
        print 'static {:<20s} {}'.format(k, p)
        raster = Raster(p, root=paths.static_inputs)
        # print 'raster jjj', raster
        arr = raster.masked()

        if k == 'plant_height':
            arr = initial_plant_height(arr)
            # print arr
        elif k == 'rew':
            arr = initial_rew(arr)
        elif k == 'root_z':
            arr = initial_root_z(arr)
        elif k == 'soil_ksat':
            arr = initial_soil_ksat(arr)
            # print arr
        elif k == 'tew':
            arr = initial_tew(arr)

        d[k] = arr
    print '-------------------------------------------------------'

    q = d['quat_deposits']
    taw = d['taw']
    tew = d['tew']
    land_cover = d['land_cover']
    print 'original Ksat = {}, REW = {}, TAW = {}, land cover = {}'.format(d['soil_ksat'],d['rew'],d['taw'],d['land_cover'])

    # apply high TAW to unconsolidated Quaternary deposits
    min_val = 250
    taw = where(q > 0.0, maximum(min_val, taw), taw)

    # apply bounds to TAW
    # min_val = 50.0
    # max_val = 320.0
    #data = d['taw']
    #taw = where(data < min_val, min_val, taw)
    #taw = where(data > max_val, max_val, taw)

    # v = tew + d['rew']
    taw = where(taw < tew, tew, taw)

    # non_zero = count_nonzero(data < min_val)
    # print 'taw has {} cells below the minimum'.format(non_zero, min_val)
    print 'taw median: {}, mean {}, max {}, min {}\n'.format(median(taw), taw.mean(), taw.max(), taw.min())
    d['taw'] = taw

    # apply tew adjustment
    # tew = where(land_cover == 41, tew * 0.25, tew)
    # tew = where(land_cover == 42, tew * 0.25, tew)
    # tew = where(land_cover == 43, tew * 0.25, tew)
    # d['tew'] = where(land_cover == 52, tew * 0.75, tew)

    return d
def get_prism(date_object, variable='precip', is_reduced = False):
    """
    Find PRISM image.

    :param variable: type of PRISM variable sought
    :type variable: str
    :param date_object: Datetime object of date.
    :return: numpy array object
    """

    is_walnut_gulch = False
    # Changed for Walnut gulch NDVI naming convention

    names = ('precip', 'min_temp', 'max_temp')
    if variable not in names:
        raise NotImplementedError('Invalid PRISM variable name {}. must be in {}'.format(variable, names))

    year = date_object.year
    year_str = str(year)

    # TODO: tiff vs. tif extension on file names breaks mac or PC
    if variable == 'precip':
        if is_reduced:
            root = os.path.join('precip', '800m_std_all')
            tail = '{}{:02n}{:02n}.tif'.format(year, date_object.month, date_object.day)
            name = 'precip_{}'.format(tail)
        else:
            if is_walnut_gulch:
                root = os.path.join('precip', '800m_std_all', year_str)
                tail = '{}{:02n}{:02n}.tiff'.format(year, date_object.month, date_object.day)
                name = 'Walnut_precip_{}'.format(tail)
            else:
                root = os.path.join('precip', '800m_std_all')
                tail = '{}{:02n}{:02n}.tif'.format(year, date_object.month, date_object.day)
                name = 'PRISMD2_NMHW2mi_{}'.format(tail)

    elif variable == 'min_temp':
        if is_reduced:
            root = os.path.join('Temp', 'Minimum_standard')
            tail = '{}{:02n}{:02n}.tif'.format(year, date_object.month, date_object.day)
            name = 'min_temp_{}'.format(tail)
        else:
            if is_walnut_gulch:
                root = os.path.join('Temp', 'Minimum_standard', year_str)
                tail = '{}{:02n}{:02n}.tiff'.format(year, date_object.month, date_object.day)
                name = 'Walnut_MinTemp_{}'.format(tail)
            else:
                root = os.path.join('Temp', 'Minimum_standard')
                tail = '{}{:02n}{:02n}.tif'.format(year, date_object.month, date_object.day)
                if year in PRISM_YEARS:
                    name = 'cai_tmin_us_us_30s_{}'.format(tail)
                else:
                    name = 'TempMin_NMHW2Buff_{}'.format(tail)

    elif variable == 'max_temp':
        if is_reduced:
            root = os.path.join('Temp', 'Maximum_standard')
            tail = '{}{:02n}' \
                   '  {:02n}.tif'.format(year, date_object.month, date_object.day)
            name = 'max_temp_{}'.format(tail)
        else:
            if is_walnut_gulch:
                root = os.path.join('Temp', 'Maximum_standard', year_str)
                tail = '{}{:02n}{:02n}.tiff'.format(year, date_object.month, date_object.day)
                name = 'Walnut_MaxTemp_{}'.format(tail)
            else:
                root = os.path.join('Temp', 'Maximum_standard')
                tail = '{}{:02n}{:02n}.tif'.format(year, date_object.month, date_object.day)
                name = 'TempMax_NMHW2Buff_{}'.format(tail)

    raster = Raster(name, root=os.path.join(paths.prism, root))
    return raster.masked()
Exemple #26
0
def run():
    cfg = Config()

    outdir = os.path.dirname(cfg.output_path)
    if not os.path.isdir(outdir):
        print('output directory does not exist. making it now: {}'.format(
            outdir))
        os.makedirs(outdir)

    # startday = datetime(2000, 1, 1)
    # endday = datetime(2013, 12, 31)

    startday, endday = cfg.date_range

    # mask_path = os.path.join(root, 'Mask')
    # statics_to_save = os.path.join(root, 'NDVI_statics')
    # ndvi = os.path.join(root, 'NDVI_spline',)
    # prism = os.path.join(root, 'PRISM')
    # penman = os.path.join(root, 'PM_RAD')
    # nlcd_name = 'nlcd_nm_utm13.tif'
    # dem_name = 'NMbuffer_DEM_UTM13_250m.tif'
    # aspect_name = 'NMbuffer_DEMAspect_UTM13_250m.tif'
    # slope_name = 'NMbuffer_DEMSlope_UTM13_250m.tif'
    # x_cord_name = 'NDVI_1300ptsX.tif'
    # y_cord_name = 'NDVI_1300ptsY.tif'

    # Gets the arrays using function convert_raster_to_array() in raster_tools.py
    # apply_mask is done on the same array.
    # nlcd = apply_mask(mask_path, convert_raster_to_array(statics_to_save, nlcd_name, 1))
    # dem = apply_mask(mask_path, convert_raster_to_array(statics_to_save, dem_name, 1))
    # slope = apply_mask(mask_path, convert_raster_to_array(statics_to_save, slope_name, 1))
    # aspect = apply_mask(mask_path, convert_raster_to_array(statics_to_save, aspect_name, 1))
    # x_cord = apply_mask(mask_path, convert_raster_to_array(statics_to_save, x_name, 1))
    # y_cord = apply_mask(mask_path, convert_raster_to_array(statics_to_save, y_name, 1))

    root = paths.ndvi_statics

    nlcd = Raster(cfg.nlcd_name, root).masked()
    dem = Raster(cfg.dem_name, root).masked()
    x_cord = Raster(cfg.x_cord_name, root).masked()
    y_cord = Raster(cfg.y_cord_name, root).masked()

    rslope = Raster(cfg.slope_name, root)
    raspect = Raster(cfg.aspect_name, root)

    rslope.apply_mask()
    rslope.filter_less(0, 0)
    slope = rslope.array

    raspect.apply_mask()
    raspect.filter_less(0, 0)
    raspect.filter_greater(360, 0)
    aspect = raspect.array

    keys = ('Year', 'Month', 'Day', 'X', 'Y', 'NDVI', 'Tavg', 'Precip', 'ETr',
            'PminusEtr', 'NLCD_class', 'Elev', 'Slope', 'Aspect')
    st_begin = time.time()

    # do you really want to be appending to the output file every time you run this script?
    # is so then 'a' is appropriate otherwise 'w' is the correct choice.
    # notice also in my write the file is only opened *once* as apposed to every iteration.
    with open(cfg.output_path, 'w') as wfile:
        wfile.write('{}\n'.format(','.join(keys)))

        for day in rrule.rrule(rrule.DAILY, dtstart=startday, until=endday):
            st = time.time()

            ndvi_data = get_kcb(paths.ndvi_spline, day)
            precip_data = get_prism(paths.prism, day, variable="precip")
            tmin_data = get_prism(paths.prism, day, variable="min_temp")
            tmax_data = get_prism(paths.prism, day, variable="max_temp")
            tavg = tmin_data + tmax_data / 2
            # Finds the penman image.
            etrs_data = get_penman(paths.penman, day, variable="etrs")

            p_minus_etr = precip_data - etrs_data

            data = array([
                x_cord, y_cord, ndvi_data, tavg, precip_data, etrs_data,
                p_minus_etr, nlcd, dem, slope, aspect
            ]).T

            save_daily_pts(wfile, day, data)

            runtime = time.time() - st

            print(('Time for day = {}'.format(runtime)))

    runtime_full = time.time() - st_begin

    print(('Time to save entire period = {}'.format(runtime_full)))
Exemple #27
0
def get_prism(date_object, variable='precip', is_reduced=False):
    """
    Find PRISM image.

    :param variable: type of PRISM variable sought
    :type variable: str
    :param date_object: Datetime object of date.
    :return: numpy array object
    """

    is_walnut_gulch = False
    # Changed for Walnut gulch NDVI naming convention

    names = ('precip', 'min_temp', 'max_temp')
    if variable not in names:
        raise NotImplementedError(
            'Invalid PRISM variable name {}. must be in {}'.format(
                variable, names))

    year = date_object.year
    year_str = str(year)

    # TODO: tiff vs. tif extension on file names breaks mac or PC
    if variable == 'precip':
        if is_reduced:
            root = os.path.join('precip', '800m_std_all')
            tail = '{}{:02n}{:02n}.tif'.format(year, date_object.month,
                                               date_object.day)
            name = 'precip_{}'.format(tail)
        else:
            if is_walnut_gulch:
                root = os.path.join('precip', '800m_std_all', year_str)
                tail = '{}{:02n}{:02n}.tiff'.format(year, date_object.month,
                                                    date_object.day)
                name = 'Walnut_precip_{}'.format(tail)
            else:
                root = os.path.join('precip', '800m_std_all')
                tail = '{}{:02n}{:02n}.tif'.format(year, date_object.month,
                                                   date_object.day)
                name = 'PRISMD2_NMHW2mi_{}'.format(tail)

    elif variable == 'min_temp':
        if is_reduced:
            root = os.path.join('Temp', 'Minimum_standard')
            tail = '{}{:02n}{:02n}.tif'.format(year, date_object.month,
                                               date_object.day)
            name = 'min_temp_{}'.format(tail)
        else:
            if is_walnut_gulch:
                root = os.path.join('Temp', 'Minimum_standard', year_str)
                tail = '{}{:02n}{:02n}.tiff'.format(year, date_object.month,
                                                    date_object.day)
                name = 'Walnut_MinTemp_{}'.format(tail)
            else:
                root = os.path.join('Temp', 'Minimum_standard')
                tail = '{}{:02n}{:02n}.tif'.format(year, date_object.month,
                                                   date_object.day)
                if year in PRISM_YEARS:
                    name = 'cai_tmin_us_us_30s_{}'.format(tail)
                else:
                    name = 'TempMin_NMHW2Buff_{}'.format(tail)

    elif variable == 'max_temp':
        if is_reduced:
            root = os.path.join('Temp', 'Maximum_standard')
            tail = '{}{:02n}' \
                   '  {:02n}.tif'.format(year, date_object.month, date_object.day)
            name = 'max_temp_{}'.format(tail)
        else:
            if is_walnut_gulch:
                root = os.path.join('Temp', 'Maximum_standard', year_str)
                tail = '{}{:02n}{:02n}.tiff'.format(year, date_object.month,
                                                    date_object.day)
                name = 'Walnut_MaxTemp_{}'.format(tail)
            else:
                root = os.path.join('Temp', 'Maximum_standard')
                tail = '{}{:02n}{:02n}.tif'.format(year, date_object.month,
                                                   date_object.day)
                name = 'TempMax_NMHW2Buff_{}'.format(tail)

    raster = Raster(name, root=os.path.join(paths.prism, root))
    return raster.masked()
def run():
    cfg = Config()

    outdir = os.path.dirname(cfg.output_path)
    if not os.path.isdir(outdir):
        print 'output directory does not exist. making it now: {}'.format(outdir)
        os.makedirs(outdir)

    # startday = datetime(2000, 1, 1)
    # endday = datetime(2013, 12, 31)

    startday, endday = cfg.date_range

    # mask_path = os.path.join(root, 'Mask')
    # statics_to_save = os.path.join(root, 'NDVI_statics')
    # ndvi = os.path.join(root, 'NDVI_spline',)
    # prism = os.path.join(root, 'PRISM')
    # penman = os.path.join(root, 'PM_RAD')
    # nlcd_name = 'nlcd_nm_utm13.tif'
    # dem_name = 'NMbuffer_DEM_UTM13_250m.tif'
    # aspect_name = 'NMbuffer_DEMAspect_UTM13_250m.tif'
    # slope_name = 'NMbuffer_DEMSlope_UTM13_250m.tif'
    # x_cord_name = 'NDVI_1300ptsX.tif'
    # y_cord_name = 'NDVI_1300ptsY.tif'

    # Gets the arrays using function convert_raster_to_array() in raster_tools.py
    # apply_mask is done on the same array.
    # nlcd = apply_mask(mask_path, convert_raster_to_array(statics_to_save, nlcd_name, 1))
    # dem = apply_mask(mask_path, convert_raster_to_array(statics_to_save, dem_name, 1))
    # slope = apply_mask(mask_path, convert_raster_to_array(statics_to_save, slope_name, 1))
    # aspect = apply_mask(mask_path, convert_raster_to_array(statics_to_save, aspect_name, 1))
    # x_cord = apply_mask(mask_path, convert_raster_to_array(statics_to_save, x_name, 1))
    # y_cord = apply_mask(mask_path, convert_raster_to_array(statics_to_save, y_name, 1))

    root = paths.ndvi_statics

    nlcd = Raster(cfg.nlcd_name, root).masked()
    dem = Raster(cfg.dem_name, root).masked()
    x_cord = Raster(cfg.x_cord_name, root).masked()
    y_cord = Raster(cfg.y_cord_name, root).masked()

    rslope = Raster(cfg.slope_name, root)
    raspect = Raster(cfg.aspect_name, root)

    rslope.apply_mask()
    rslope.filter_less(0,0)
    slope = rslope.array

    raspect.apply_mask()
    raspect.filter_less(0, 0)
    raspect.filter_greater(360, 0)
    aspect = raspect.array

    keys = ('Year', 'Month', 'Day', 'X', 'Y', 'NDVI', 'Tavg', 'Precip', 'ETr',
            'PminusEtr', 'NLCD_class', 'Elev', 'Slope', 'Aspect')
    st_begin = time.time()

    # do you really want to be appending to the output file every time you run this script?
    # is so then 'a' is appropriate otherwise 'w' is the correct choice.
    # notice also in my write the file is only opened *once* as apposed to every iteration.
    with open(cfg.output_path, 'w') as wfile:
        wfile.write('{}\n'.format(','.join(keys)))

        for day in rrule.rrule(rrule.DAILY, dtstart=startday, until=endday):
            st = time.time()

            ndvi_data = get_kcb(paths.ndvi_spline, day)
            precip_data = get_prism(paths.prism, day, variable="precip")
            tmin_data = get_prism(paths.prism, day, variable="min_temp")
            tmax_data = get_prism(paths.prism, day, variable="max_temp")
            tavg = tmin_data + tmax_data / 2
            # Finds the penman image.
            etrs_data = get_penman(paths.penman, day, variable="etrs")

            p_minus_etr = precip_data - etrs_data

            data = array([x_cord, y_cord, ndvi_data, tavg, precip_data,
                          etrs_data, p_minus_etr, nlcd, dem, slope, aspect]).T

            save_daily_pts(wfile, day, data)

            runtime = time.time() - st

            print('Time for day = {}'.format(runtime))

    runtime_full = time.time() - st_begin

    print('Time to save entire period = {}'.format(runtime_full))