1

Code for a single raster file:

import geopandas as gpd 
#import os 
import rasterio
import scipy.sparse as sparse
import pandas as pd
import numpy as np


# Create an empty pandas dataframe called 'table'
table = pd.DataFrame(index = np.arange(0,1))

# Read the points shapefile using GeoPandas 
stations = gpd.read_file(r'E:/anakonda/Shape files/AAQ_st1/AAQ_ST1.shp')
stations['lon'] = stations['geometry'].x
stations['lat'] = stations['geometry'].y

Matrix = pd.DataFrame()

# Iterate through the rasters and save the data as individual arrays to a Matrix 
dataset = rasterio.open(r'E:/anakonda/LST_day/MOD11A1.006_LST_Day_1km_doy2019082_aid0001.tif')
data_array = dataset.read(1)
data_array_sparse = sparse.coo_matrix(data_array, shape = (351, 545))
   

for records_date in Matrix.columns.tolist():
        a = Matrix
        LST_day_value = a.loc[int(row)][int(col)]
        table[records_date] = LST_day_value
        transpose_mat = table.T
        transpose_mat.rename(columns = {0: 'LST_Day(Kel)'}, inplace = True)
     
transpose_mat.to_csv(r'E:/anakonda/LST_day'+'\\'+station_name+'.csv') 

Error code lines: LST_day_value = a.loc[int(row)][int(col)]

transpose_mat.to_csv(r'E:/anakonda/LST_day'+'\'+station_name+'.csv')

Errors Shown: Undefined Name 'row' (pyflakes E) Undefined Name 'col' (pyflakes E) NameError: name 'transpose_mat' is not defined

I'm using the above code for creating a Raster Time-series for Modis LST data. the code ran well till 'transposing the matrix'. the error shown is mentioned below the code. Im new to python, so kindly help me with this issue.

import os 
import rasterio
import scipy.sparse as sparse 
import pandas as pd
import numpy as np

# Create an empty pandas dataframe called 'table'
table = pd.DataFrame(index = np.arange(0,1))

# Read the points shapefile using GeoPandas 
stations = gpd.read_file(r'E:/anakonda/Shape files/AAQ_st1/AAQ_ST1.shp')
stations['lon'] = stations['geometry'].x
stations['lat'] = stations['geometry'].y

Matrix = pd.DataFrame()

# Iterate through the rasters and save the data as individual arrays to a Matrix 
for files in os.listdir(r'E:/anakonda/LST_Night'):
    if files[-4: ] == '.tif':
        dataset = rasterio.open(r'E:/anakonda/LST_Night'+'\\'+files)
        data_array = dataset.read(1)
        data_array_sparse = sparse.coo_matrix(data_array, shape = (351,545))
        data = files[ :-20]
        Matrix[data] = data_array_sparse.toarray().tolist()
        print('Processing is done for the raster: '+ files[:-20])

# Iterate through the stations and get the corresponding row and column for the related x, y coordinates    
for index, row in stations.iterrows():
    station_name = str(row['Station'])
    lon = float(row['lon'])
    lat = float(row['lat'])
    x,y = (lon, lat)
    row, col = dataset.index(x, y)
    print('Processing: '+ station_name)
    
    # Pick the LST value from each stored raster array and record it into the previously created 'table'
    for records_date in Matrix.columns.tolist():
        a = Matrix[records_date]
        LST_Night_value = a.loc[int(row)][int(col)]
        table[records_date] = LST_Night_value
        transpose_mat = table.T
        transpose_mat.rename(columns = {0: 'LstNight(Kel)'}, inplace = True)
        
    transpose_mat.to_csv(r'E:/anakonda/LST_Night'+'\\'+station_name+'.csv')```





This is the error shown:

```File "C:\Anaconda\envs\timeseries\lib\site-packages\pandas\core\indexes\range.py", line 357, in get_loc
    raise KeyError(key) from err

KeyError: 2278```
      
Bhargavi
  • 31
  • 4
  • What makes you so sure it is the transposition that fails? – Serge de Gosson de Varennes Dec 10 '20 at 12:56
  • Hii @Serge de Gosson de Varennes, I have Ran the code before giving the transpose command it ran well without any errors. and I have checked each step while writing the code by running it simultaneously. – Bhargavi Dec 11 '20 at 06:57
  • OK. I imagine your matrix might be too large to share it, but do you have a low dimensional example that doesn't work that you can share? It would help to see more that just code. – Serge de Gosson de Varennes Dec 11 '20 at 07:12
  • Hii @Serge de Gosson de Varennes, I greatly appreciate your help, sorry for the delay. I tried the same code for a single file as you said, but got an error at the same steps as the previous code. I'm sharing the code above, kindly go through it. – Bhargavi Dec 13 '20 at 17:34

0 Answers0