zkwurst / GSoC2017-GRASS-GIS

GNU General Public License v2.0
2 stars 1 forks source link

USGS_product_dict: Indexing and formatting #2

Closed zkwurst closed 7 years ago

zkwurst commented 7 years ago

This is the dictionary I made for NED:

# Data dictionary for NED parameters
USGS_product_dict = [{"product": 
        {"NED": 
            {"title": "National Elevation Dataset (NED)", 
             "format": "IMG", 
             # Need to work on dynamic 'file_string' formatting
             # Currently hardcoded for NED format around line 237
             "file_string": "'img{0}_13.img'",
             "resolution": {"1 arc-second": "1 x 1 degree", 
                            "1/3 arc-second": "1 x 1 degree", 
                            "1/9 arc-second": "15 x 15 minute"
                            },
             "srs": "nad83",
             "srs_proj4": "+proj=longlat +ellps=GRS80 +datum=NAD83 +nodefs"
             }}}]

In this entry: "file_string": "'img{0}_13.img'",

I want to insert the zip archive name using .format later on in the script. THe zip archive name is coming from the zip download URL.

for url in dwnld_URL:
      dwnldREQ = requests.get(url, timeout=12, stream=True)
      zipName = url.split('/')[-1]
      local_temp = work_DIR + '/' + zipName
      zipSplit = zipName.split('.')[0]

zipSplit is the string I want to insert into "'img{0}_13.img'" which i've assigned to product_format_string

But this gets caught in one of the timeout errors. I don't remember which. imgName = product_format_string.format(zipSplit)

I have it hardcoded like this and it works, but only for NED IMG files from 2013. I don't know if other datasets use a similar naming convention.

imgName = "img" + zipSplit + "_13.img"
LT_base = imgName.split('.')[0]

I didn't want to waste much time on it, but I feel like I'm completely missing something obvious and more pythonic.

A future move to vsizip/vsicurl would solve this anyway.

petrasovaa commented 7 years ago

I am not quite sure what is the problem. I don't see a need to use nested quotes in "'img{0}_13.img'", otherwise you would have to show me the error.

Regarding the hardcoded name, can't you loop through the zip archive and find the file with img extension and use that name directly?

zkwurst commented 7 years ago

The nested quotes were my attempt to get .format to work with the string literal when I concatenated them later.

product_format_string = "'img{0}_13.img'"
imgName = product_format_string.format(zipSplit)

So that it would work like this: imgName = 'img{0}_13.img'.format(zipSplit) But it didn't work. So I figured I'd have to to .format the .format, and that's when I realized I was probably doing something that didn't need doing.

If you happen to know what I was doing wrong, I'd be curious, but it's definitely not important.

Looping through the zip will work if there's just one .img file in the zip. I was thinking there were two because of the thumbnail, but that's .jpg. I'll check the zip and implement that today.

zkwurst commented 7 years ago

imgName = product_format_string + ".format({0})".format(zipSplit)

is what I meant to type.

zkwurst commented 7 years ago

imgName = product_format_string.format(zipSplit)

That's obviously not how any of this works.

petrasovaa commented 7 years ago

Is this what you needed?

In [1]: a = "Hello {0}!"

In [2]: a
Out[2]: 'Hello {0}!'

In [3]: a.format('Zeke')
Out[3]: 'Hello Zeke!'