1

I have the following code:

for filename in os.listdir('/home/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/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'.

frichter
  • 61
  • 6
  • have you tried using string formmating? – Ade_1 May 25 '21 at 19:15
  • No, I am a beginner and do not know many techniques. – frichter May 25 '21 at 19:21
  • okay, file = filename.split('.')[0], will give the the name without the ('.tif') then new_filename= f'{file}.npz' – Ade_1 May 25 '21 at 19:25
  • I suggest you use [`os.path,splitext()`](https://docs.python.org/3/library/os.path.html#os.path.splitext) to get the name without an extension (as well as the extension itself). – martineau May 25 '21 at 19:54
  • Unfortunately not. Perhaps I have expressed myself incorrectly.The file read out at the beginning should be converted and at the end with the same name except for the ending, which is given by np.save to be saved in a new folder. – frichter May 25 '21 at 20:10

2 Answers2

0

Try this. Also, try passing in variables rather than hardcoding especially paths

DIR = '/home/Downloads/nightlight_geotiffs'
files = [x for x in os.listdir(DIR) if x.endswith('.tif')] # gives you all the list of ('.tif files')
NUMPY_DIR = '/home/Downloads/nightlight_z-array/'

for file in files:
    path = os.path.join(DIR, file)
    with open(path,'r') as f:            
        img = f.read()                    
        matrix = img[0]                    
        z_norm = stats.zscore(matrix)           
        file= file.split('.')[0]
        new_file= os.path.join(NUMPY_DIR,f'{file}')
        
       # save to npy file
        np.save(new_file, z_norm)
Ade_1
  • 1,480
  • 1
  • 6
  • 17
  • oh, missed the np.save args. Please try again – Ade_1 May 25 '21 at 20:20
  • the code creates a file named 'nightlight_z-array.npy'in the folder Downloads – frichter May 25 '21 at 20:20
  • np,.save(filename, data)... whereas, we just passed in the folder name instead. Please try again. Also ensure that z_norm has actual data in it. – Ade_1 May 25 '21 at 20:22
  • Traceback (most recent call last): File "/home/ripperflo/PycharmProjects/pythonProject/z-norm_geotiff2.py", line 22, in new_file = os.path(NUMPY_DIR, f'{file}.npy') TypeError: 'module' object is not callable – frichter May 25 '21 at 20:23
  • sorry sleepy eyes. it should be os.path.join() – Ade_1 May 25 '21 at 20:25
  • FileNotFoundError: [Errno 2] File or directory not found: '/home/Downloads/nightlight_z-array/BJ2012_1.npz.npy'. – frichter May 25 '21 at 20:34
  • i don't know if we are getting at the same thing. the code works so far. i just can't get the file to be saved as an npy file at the end to have the same name as the original file that is read in at the beginning. – frichter May 25 '21 at 20:38
  • '/home/Downloads/nightlight_z-array/BJ2012_1.npz.npy'. <---there is the error i have updated the code, apparently we dont need to add the file extension – Ade_1 May 25 '21 at 20:52
0

How about extracting the filename string without the last 4 characters?

np.save('/home/Downloads/nightlight_z-array/' + str(filename)[0:-4], z_norm)
MiH
  • 354
  • 4
  • 11
  • generates error:FileNotFoundError: [Errno 2] File or directory not found: '/home/Downloads/nightlight_z-array/.npy'. – frichter May 25 '21 at 20:26
  • sorry I've updated my code... by the way, does the path `/home/Downloads/nightlight_z-array/` exist? – MiH May 25 '21 at 20:54