I have a standalone script using data opened from a Geopackage with Fiona. When I run this portion of my script:

import os 
import fiona 
import numpy as np

datapath= '/media/sf_SharesShapefiles/'

layer_keys =fiona.listlayers(os.path.join(datapath, "test.gpkg"))

for l in layer_keys:

    layer1 = fiona.open(os.path.join(datapath, "test.gpkg"),layer=l)

seasons = ['ann','djf','mam','jja','son'] RCPS = ['rcp45', 'rcp85'] vari =  ['Precipitations_totales_CRU_3_21TS_CMIP5_']

nColors = 10 # number of categories Eq_break_flag = False # equal breaks vs quantiles NoData =-9999 # nodata value if required

break_vals_Seas ={} for v in vari:
    break_vals_Seas[v] = {}
    for s in seasons: # loop season\
        for r in RCPS:  # loop rcps

            layer1 = fiona.open('/media/sf_SharesShapefiles/test.gpkg', 'r', layer = 'Precipitations_totales_CRU_3_21TS_CMIP5_' + r)
            #column_names = [row[:] for row in cols]
            dataorig = {}

        for i in range(0, len(layer1)):

            rec = layer1.next()
            test = rec['properties']

            if i == 0:
                for k in test.keys():
                    dataorig[k] = np.asarray(test[k])
                for k in test.keys():
                    dataorig[k] = np.append(dataorig[k], test[k])

        # print(data1.keys())
        column_names = list(dataorig.keys())

        indices = [i for i, s in enumerate(column_names) if 'CRU' in s]
        #df = (dataframe[:, indices[0]:3])
        column_names = column_names[indices[0]:]

        for c in column_names:
            if 'Shape' and 'Length' in c:

        fieldNamesSubset = [ss for ss in column_names if s in ss]
        for ff in range(0,len(fieldNamesSubset)): # Concatenate data from field subset (each season) into a single numpy array

            data1 = dataorig[fieldNamesSubset[ff]]  #arcpy.da.FeatureClassToNumPyArray(l,fieldNamesSubset[ff]) # read field into numpy array

            if ff == 0 and r in RCPS[0]:
                data_all1 = data1
                data_all1 = np.vstack((data_all1,data1))

    if Eq_break_flag:
        interv1 = (np.max(data_all1[~np.isnan(data_all1)]) - np.min(data_all1[~np.isnan(data_all1)])) / nColors
        break_vals = []
        for i in range(1, nColors):
            break_vals.append(break_vals[i - 1] + interv1)

        break_vals.append(np.max(data_all1[~np.isnan(data_all1)]) + .001)

        interv = 100 / nColors
        np.arange(0.0, 1.0, 0.1)
        break_vals = np.percentile(data_all1[~np.isnan(data_all1)], np.arange(0, 100 + 1, interv))

        break_vals_Seas[v][s] = break_vals

I get this error message:

break_vals = np.percentile(data_all1[~np.isnan(data_all1)], np.arange(0, 100 + 1, interv))

NameError: name 'data_all1' is not defined

I think this might be a scope error, however I am not sure how and where to define data_all1

  • Can you provide a valid piece of python code? Otherwise it is difficult to say what might go wrong. – joris Jun 14 '18 at 16:54
  • @joris I updated my entry! – sla514 Jun 14 '18 at 16:57
  • data_all1 might not be defined in the case that fieldNamesSubset is an empty list (as then the for loop's body will not be executed once) – joris Jun 14 '18 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.