I have the following code:
for filename in os.listdir('/home/ripperflo/Downloads/nightlight_geotiffs'):
if filename.endswith('.tif'): # take TIFF-files only
with rasterio.open(os.path.join('/home/ripperflo/Downloads/nightlight_geotiffs', filename)) as f: # open GeoTiff and store in f
img = f.read() # open GeoTiff as 3D numpy array
matrix = img[0] # 3D array to 2D array because nighlight images has only one band
z_norm = stats.zscore(matrix) # normalize 2D array
# save to npy file
np.save('/home/ripperflo/Downloads/nightlight_z-array/', filename, z_norm)
The Code is running so far. The only thing I need to know is: how can I save the numpy array as .npy file with the same name as the origin input file?
So the input file is called 'BJ2012_2.tif'
and the output file should be called 'BJ2012_2.npy'
. The process will later run in a loop. So each file from the folder will be normalized and saved with the same name but in a different file format in a different folder.