<commit_before>import os
import os.path
import string
from datetime import date
import itertools
import operator

import utils

# These determine default isochrone time maximums and increments
DEF_ISO_MAX=40
DEF_ISO_INC=10

CORRECT_NODATA_VALUE_BYTE=128

def make_average_raster(loc_name, timestr, nearby_mins, num_each_side,
        rel_dir=None, suffix=None):
    mins_diffs = utils.get_nearby_min_diffs(nearby_mins,
                    num_each_side)
    today_str = date.today().isoformat()
    date_time_str_set = utils.get_date_time_string_set(today_str, timestr,
                mins_diffs)
    fnames = utils.get_raster_filenames(loc_name, date_time_str_set, rel_dir,
        suffix)
        
    # This VRT step is necessary since :- for this kind of 'raster algebra',
    # the problem is that the original type is Byte, and it won't hold a value
    # of over 128 properly. So we temporarily transform to Float32 using the
    # "VRT" feature of GDAL before doing the calculation. See:
    # http://gis.stackexchange.com/questions/33152/why-do-i-get-different-results-with-gdal-calc-within-the-osgeo4w-shell-and-qgis
    vrtnames = []
    for fname in fnames:
        vrtname = os.path.splitext(fname)[0]+".vrt"
        vrtnames.append(vrtname)
    
    for fname in fnames:
        editcmd = "gdal_edit.py -a_nodata %d %s" \
            % (CORRECT_NODATA_VALUE_BYTE, fname)
        print "Running %s:" % editcmd
        os.system(editcmd)
    for ii in range(len(fnames)):
        transcmd = "gdal_translate -ot Float32 -of VRT %s %s" \
            % (fnames[ii], vrtnames[ii])
        print "Running %s:" % transcmd
        os.system(transcmd)

    # Now do the big average
    fname_main = utils.rasterName(loc_name, timestr, rel_dir, suffix)
    avg_fname = os.path.splitext(fname_main)[0] + "-avg%d.tiff" % len(fnames)

    caps = string.ascii_uppercase[:len(fnames)]
    vrts_str = " ".join(["-%s %s" % (a, b) for a, b in zip(caps, vrtnames)])
    calc_str = "("+"+".join(caps)+")"+"/"+str(len(vrtnames))
    calccmd = 'gdal_calc.py %s --outfile=%s --calc="%s" --type=Byte --overwrite' \
        % (vrts_str, avg_fname, calc_str)
    print "Running %s:" % calccmd    
    os.system(calccmd)
    return

def make_contours_isobands(loc_name, timestr, nearby_mins, num_each_side,
        rel_dir=None, suffix=None, iso_max=DEF_ISO_MAX, iso_inc=DEF_ISO_INC):
    iso_timeset = range(0, iso_max+1, iso_inc)[1:]
    numavg = 1 + 2*num_each_side
    fname_base = utils.rasterName(loc_name, timestr, rel_dir, suffix)
    avg_fname = os.path.splitext(fname_base)[0] + "-avg%d.tiff" % numavg
    ctr_fname = os.path.splitext(fname_base)[0] + "-avg%d-isos.shp" % numavg
    if os.path.exists(ctr_fname):<commit_msg>Forced saving correct NoData value when creating avg rasters.<commit_after>import os
import os.path
import string
from datetime import date
import itertools
import operator

import utils

# These determine default isochrone time maximums and increments
DEF_ISO_MAX=40
DEF_ISO_INC=10

CORRECT_NODATA_VALUE_BYTE=128

def make_average_raster(loc_name, timestr, nearby_mins, num_each_side,
        rel_dir=None, suffix=None):
    mins_diffs = utils.get_nearby_min_diffs(nearby_mins,
                    num_each_side)
    today_str = date.today().isoformat()
    date_time_str_set = utils.get_date_time_string_set(today_str, timestr,
                mins_diffs)
    fnames = utils.get_raster_filenames(loc_name, date_time_str_set, rel_dir,
        suffix)
        
    # This VRT step is necessary since :- for this kind of 'raster algebra',
    # the problem is that the original type is Byte, and it won't hold a value
    # of over 128 properly. So we temporarily transform to Float32 using the
    # "VRT" feature of GDAL before doing the calculation. See:
    # http://gis.stackexchange.com/questions/33152/why-do-i-get-different-results-with-gdal-calc-within-the-osgeo4w-shell-and-qgis
    vrtnames = []
    for fname in fnames:
        vrtname = os.path.splitext(fname)[0]+".vrt"
        vrtnames.append(vrtname)
    
    for fname in fnames:
        editcmd = "gdal_edit.py -a_nodata %d %s" \
            % (CORRECT_NODATA_VALUE_BYTE, fname)
        print "Running %s:" % editcmd
        os.system(editcmd)
    for ii in range(len(fnames)):
        transcmd = "gdal_translate -ot Float32 -of VRT %s %s" \
            % (fnames[ii], vrtnames[ii])
        print "Running %s:" % transcmd
        os.system(transcmd)

    # Now do the big average
    fname_main = utils.rasterName(loc_name, timestr, rel_dir, suffix)
    avg_fname = os.path.splitext(fname_main)[0] + "-avg%d.tiff" % len(fnames)

    caps = string.ascii_uppercase[:len(fnames)]
    vrts_str = " ".join(["-%s %s" % (a, b) for a, b in zip(caps, vrtnames)])
    calc_str = "("+"+".join(caps)+")"+"/"+str(len(vrtnames))
    calccmd = 'gdal_calc.py %s --outfile=%s --NoDataValue=%d --calc="%s" --type=Byte --overwrite' \
        % (vrts_str, avg_fname, CORRECT_NODATA_VALUE_BYTE, calc_str)
    print "Running %s:" % calccmd    
    os.system(calccmd)
    return

def make_contours_isobands(loc_name, timestr, nearby_mins, num_each_side,
        rel_dir=None, suffix=None, iso_max=DEF_ISO_MAX, iso_inc=DEF_ISO_INC):
    iso_timeset = range(0, iso_max+1, iso_inc)[1:]
    numavg = 1 + 2*num_each_side
    fname_base = utils.rasterName(loc_name, timestr, rel_dir, suffix)
    avg_fname = os.path.splitext(fname_base)[0] + "-avg%d.tiff" % numavg
    ctr_fname = os.path.splitext(fname_base)[0] + "-avg%d-isos.shp" % numavg
    if os.path.exists(ctr_fname):
{'fraction_non_alphanumeric': 0.11926273942898447, 'fraction_numerical': 0.010480664980122878, 'mean_word_length': 3.32421875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3491846', 'n_tokens_mistral': 2133, 'n_tokens_neox': 2075, 'n_words': 441}