3
import glob
import numpy as np
import pandas
import os
import rasterio
import geopandas as gpd
import matplotlib.pyplot as plt

def get_image_stats_only(img_array):


    raw_stats_dictionary = {'mean': np.mean(img_array), 
                            'min': np.min(img_array),
                            'max': np.max(img_array)}

    raw_stats_df = pandas.DataFrame(data = raw_stats_dictionary, index=[0])

    return(raw_stats_df)


def get_image_variable(file_single):


    img_variable = search_criteria.split("/")[0]
    return(img_variable)



def get_image_time(file_single):
    img_time_raw = os.path.splitext(file_single)[0].split('_')[-2:] #['2020', '061']
    img_time = pandas.to_datetime(img_time_raw[0], format='%Y') + 
               pandas.DateOffset(days=int(img_time_raw[1])-1)
    return img_time.strftime('%Y-%m-%d')      
 

def get_image_product(file_single):


    img_product = os.path.basename(file_single).split("_")[0]

    return(img_product)
 

def get_image_meta_only(file_single):
#  returns a pandas data frame with TIME, band, variable, etc. the "META" data

    img_meta_dictionary = {'TIME': get_image_time(file_single),
                           'product': get_image_product(file_single),
                           'variable_name': get_image_variable(file_single),
                           'file_name': file_single}

    raw_meta_dictionary = {'temporary_value': "a_temporary_metadata_value", 
                           'temporary_value1': "a_temporary_metadata_value1"}

    raw_meta_df = pandas.DataFrame(data = raw_meta_dictionary, index=[0])
    raw_meta_dic = pandas.DataFrame(data = img_meta_dictionary, index=[0])


    return(raw_meta_df, raw_meta_dic)


def get_image_stats(file_single, shp_filename = None):

    img_single_opened = rasterio.open(file_single)

    if shp_filename is None: 
       img_single_read = img_single_opened.read(masked = True)
    else:
       img_single_read = get_mask_raster_array(img_single_opened, shp_filename)

    img_stats_raw_df = get_image_stats_only(img_single_read)
    img_meta_df = get_image_meta_only(file_single)

    #bind the rows of img_stats_df and img_meta_df
    img_stats_df = pandas.DataFrame.join(img_stats_raw_df, img_meta_df)
    return(img_stats_df)


def get_mask_raster_array(img_single_opened, shp_filename):

    crop_extent = gpd.read_file(shp_filename)
    crop_extent["common_attribute"] = 0
    crop_extent_reproj = crop_extent.to_crs(img_single_opened.crs).dissolve(by = "common_attribute")
    shape = crop_extent_reproj["geometry"]
  
    img_single_masked, img_single_masked_transform = rasterio.mask.mask(img_single_opened, shape, 
                                                             crop = True, filled = False)

    return(img_single_masked)

# Make a search criteria to select the DEM files
search_criteria = "Alb_WSA_Vis/*.tif"

dirpath = "F:/Albedo_Daily/Albedo_Daily_500m_v6/"
q = os.path.join(dirpath, search_criteria)

print(q)

shpfile = "C:/Users/admin/London_extent/London_Borough_Excluding_MHW.shp"
shpfile = None
file_list = glob.glob(q)

file_list

img_stats_df = pandas.DataFrame()

for file_single in file_list:
    print(file_single)
    image_stats = get_image_stats(file_single, shp_filename = shpfile)
    img_stats_df = img_stats_df.append(image_stats, ignore_index = True)

#plot all
img_stats_df.plot(x = 'TIME')
#plot only mean
img_stats_df.plot.scatter(x = 'TIME', y = 'mean')

Above is my code.I want to draw the picture of white-sky albedo for shortwave band from 2018_03_01 to 2020_03_01.But it stopped running at 2019_107.tif, I do not know why.

and here is the error.

File "<ipython-input-9-4e151feb33db>", line 1, in <module>
  runfile('C:/Users/admin/temp_WM_c.py', wdir='C:/Users/admin')

File "C:\Users\admin\Anaconda3\lib\site- 
 packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
 execfile(filename, namespace)

File "C:\Users\admin\Anaconda3\lib\site- 
 packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
 exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/admin/temp_WM_c.py", line 130, in <module>
image_stats = get_image_stats(file_single, shp_filename = shpfile)

File "C:/Users/admin/temp_WM_c.py", line 91, in get_image_stats
img_stats_raw_df = get_image_stats_only(img_single_read)

File "C:/Users/admin/temp_WM_c.py", line 23, in get_image_stats_only
raw_stats_df = pandas.DataFrame(data = raw_stats_dictionary, index=[0])

File "C:\Users\admin\Anaconda3\lib\site-packages\pandas\core\frame.py", line 
411, in __init__
mgr = init_dict(data, index, columns, dtype=dtype)

File "C:\Users\admin\Anaconda3\lib\site- 
packages\pandas\core\internals\construction.py", line 257, in init_dict
  return arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)

File "C:\Users\admin\Anaconda3\lib\site- 
packages\pandas\core\internals\construction.py", line 82, in arrays_to_mgr
arrays = _homogenize(arrays, index, dtype)

File "C:\Users\admin\Anaconda3\lib\site- 
packages\pandas\core\internals\construction.py", line 323, in _homogenize
val, index, dtype=dtype, copy=False, raise_cast_failure=False

File "C:\Users\admin\Anaconda3\lib\site- 
packages\pandas\core\internals\construction.py", line 642, in sanitize_array
data.soften_mask()  # set hardmask False if it was True

File "C:\Users\admin\Anaconda3\lib\site-packages\numpy\ma\core.py", line 
3508, in soften_mask
self._hardmask = False

File "C:\Users\admin\Anaconda3\lib\site-packages\numpy\ma\core.py", line 
6353, in __setattr__
 "attributes of {!r} are not writeable".format(self))

AttributeError: attributes of masked are not writeable
  • 2
    Please Edit your Question to contain the error message as text (which makes it legible on any device and searchable) – Vince Dec 7 '20 at 12:33

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.