0

I have a program where I read in a path to a file and save it as a variable for opening and accessing the file later on. The filename can vary very slightly, in that it will be either S2A_TOA_rad_10m_atm.bsq or S2B_TOA_rad_10m_atm.bsq.

At the moment, I've set up a try-except statement which will catch for an attribute error as shown below:

try:
    dataset_10m = str(dataset)+'S2A_TOA_rad_10m_atm.bsq'
    dataset_20m = str(dataset)+'S2A_TOA_rad_20m_atm.bsq'
    dataset_mask = str(dataset)+'S2A_TOA_rad_10m_out_hcw.bsq'
    driver,metadata,EPSG_coords,wkt_start,wkt_end = get_metadata(dataset_10m)
except AttributeError:
    dataset_10m = str(dataset)+'S2B_TOA_rad_10m_atm.bsq'
    dataset_20m = str(dataset)+'S2B_TOA_rad_20m_atm.bsq'
    dataset_mask = str(dataset)+'S2B_TOA_rad_10m_out_hcw.bsq'
    driver,metadata,EPSG_coords,wkt_start,wkt_end = get_metadata(dataset_10m)

where the variable dataset is the path to the folder containing the .bsq file. This code works as I expect it to, but there is a different error message which prints when a file starting with S2B is found (even though the code itself implements properly), which I can't find a way to get rid of:

ERROR 4: /*path to folder*/S2A_TOA_rad_10m_atm.bsq: No such file or directory

This doesn't appear to be a Python error necessarily but I haven't been able to find any specifics on what this error message is or how to get rid of it. Is this the best approach to handling minor name differences in a file?

Could it be that this error is occurring later on in the try-except statement when I attempt to open the file (in the get_metadata function) which then leads to the exception clause?

EDIT

def get_metadata(filename):
    '''
    Runs gdalinfo on the input file and gathers the required metadata for creating the .yaml file
    '''

    metadata = gdal.Info(filename)
    metadata = metadata.split('\n')

    for line in range(len(metadata)):
        if metadata[line][0:6] == 'Driver':
            driver = metadata[line][8:12]

        if metadata[line][0:10] == 'Upper Left':
            splitline = metadata[line].split(' ')
            ul_lon_EPSG = int(splitline[5][:-5])
            ul_lat_EPSG = int(splitline[6][:-5])
        if metadata[line][0:10] == 'Lower Left':
            splitline = metadata[line].split(' ')
            ll_lon_EPSG = int(splitline[5][:-5])
            ll_lat_EPSG = int(splitline[6][:-5])
        if metadata[line][0:11] == 'Upper Right':
            splitline = metadata[line].split(' ')
            ur_lon_EPSG = int(splitline[4][:-5])
            ur_lat_EPSG = int(splitline[5][:-5])
        if metadata[line][0:11] == 'Lower Right':
            splitline = metadata[line].split(' ')
            lr_lon_EPSG = int(splitline[4][:-5])
            lr_lat_EPSG = int(splitline[5][:-5])

        if metadata[line][0:21] == 'Coordinate System is:':
            wkt_start = line+1
        if metadata[line][0:6] == 'Origin':
            wkt_end = line

    EPSG_coords = [[ll_lon_EPSG,ll_lat_EPSG],[lr_lon_EPSG,lr_lat_EPSG],[ul_lon_EPSG,ul_lat_EPSG],[ur_lon_EPSG,ur_lat_EPSG]]
    return driver,metadata,EPSG_coords,wkt_start,wkt_end
JackLidge
  • 391
  • 4
  • 16
  • 1
    It seems like even though you have set the path to be starting with `'S2B'` that something is still expecting an `'S2A'` path? I would guess it's somewhere in your `get_metadata()` function? Also, I would consider replacing your string concatenation of paths with calls to [`os.path.join()`](https://stackoverflow.com/questions/13944387/why-use-os-path-join-over-string-concatenation). You could also use `os.path.exists` to check if the file exists, or if only one of the files will ever exist, you could use some of the functionality of the `glob` module – PyPingu Jun 04 '19 at 09:53
  • I will have a look at the glob module. Yes, the get_metadata() function actually attempts to read the file (using gdal.Info function) so perhaps that is where the error message is arising from as that section is still inside the try-except clause. I think the reason I originally put it inside the clause was to ensure that an error would be incurred if it was an 'S2B' file. But there would be an easier method (probably using os.path.exists which I use elsewhere in this function) might achieve the same result. – JackLidge Jun 04 '19 at 10:08
  • If you could post the `get_metadata()` function code it would be easier to determine where your issue is. – PyPingu Jun 04 '19 at 10:11
  • function posted – JackLidge Jun 04 '19 at 10:14
  • Ok, so I don't think that function is to blame. But I have realised I don't know what exactly you are expecting to return an `AttributeError`? If I understand the question properly I think what is happening is you are never hitting the except block. The `get_metadata` call in the `try` section is being reached, called, but returning that error instead of the expected `AttributeError` you are catching and so your script just errors out. – PyPingu Jun 04 '19 at 10:19
  • I commented out the try/except statement to see what caused the Attribute Error and it is brought up by the `metadata.split('\n')` line as it is trying to do a split on a None type object. This is after the ERROR 4 has been printed so I'm guessing that must occur in the `gdal.Info(filename)` line. – JackLidge Jun 04 '19 at 10:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194427/discussion-between-pypingu-and-jacklidge). – PyPingu Jun 04 '19 at 11:16

1 Answers1

0

In the end, I replaced the try/except statement with the following four lines:

dataset_10m = os.path.join(dataset,glob.glob(dataset+'S2?_TOA_rad_10m_atm.bsq')[0])
dataset_20m = os.path.join(dataset,glob.glob(dataset+'S2?_TOA_rad_20m_atm.bsq')[0])
dataset_mask = os.path.join(dataset,glob.glob(dataset+'S2?_TOA_rad_10m_out_hcw.bsq')[0])
driver,metadata,EPSG_coords,wkt_start,wkt_end = get_metadata(dataset_10m)

As there is only ever one file in a directory, starting with either "S2A" or "S2B", the glob.glob function with the wildcard ascertains which one is present and then os.path.join safely joins the directory and file strings together.

JackLidge
  • 391
  • 4
  • 16