Running metadata script from one file type to another using glob












0















I am new to python and am currently stuck on how to run my metadata script on 2 different file types within a directory. I have calculated statistics for a set of GeoTIFFs and need to take those band statistics and paste them into the metadata for another set of files.
Basically, I have .tif files with the calculated statistics and I need to paste those statistics into the original source file headers.



I can do this for each individual file using GDAL with the script below, but I'm at a standstill on how to make this work for multiple files and have the data match correctly. I used a for loop bash script but I don't think it matches the correct statistics to each source file header.



Do I need to restructure my script to use for/if statements?



My working script for individual files works with this:



import sys
from osgeo import gdal, gdalconst

calculated_files = gdal.Open(*.tif, gdalconst.GA_ReadOnly)
source_files = gdal.Open(*.anotherfiletype, gdalconst.GA_Update)

stats = calculated_files.GetMetadata()
band = calculated_files.GetRasterBand(1)

bandStats = band.GetMetadata()

print('bandStats is set to')
print(bandStats)

source_files.SetMetadata(bandStats, )

del(calculated_files)
del(source_files)


I've been messing with this addition using glob and a for loop:



import glob
import os

types = ('*.tif', '*.anotherfiletype')
all_files =
for files in types:
all_files.extend(glob.glob(files))









share|improve this question

























  • It's a little unclear what you need. Are you saying that the first script works fine and is able to (for instance) put the metadata from file1.tiff onto file.anotherfiletype? And you want to do it for all file1, file2, file3, etc? Do the TIFFs and other files have the same name (just different extension)?

    – HFBrowning
    Nov 21 '18 at 20:47













  • Yes, the first script works fine if I use individual file names, sorry if my question is unclear. I'm having trouble figuring out how to make this work for an entire directory of File1 to File2 metadata migration. The file types do not currently have the same naming convention before the extension because they're from 2 different sources. I'm guessing this may be an issue.

    – muskrat
    Nov 21 '18 at 20:57
















0















I am new to python and am currently stuck on how to run my metadata script on 2 different file types within a directory. I have calculated statistics for a set of GeoTIFFs and need to take those band statistics and paste them into the metadata for another set of files.
Basically, I have .tif files with the calculated statistics and I need to paste those statistics into the original source file headers.



I can do this for each individual file using GDAL with the script below, but I'm at a standstill on how to make this work for multiple files and have the data match correctly. I used a for loop bash script but I don't think it matches the correct statistics to each source file header.



Do I need to restructure my script to use for/if statements?



My working script for individual files works with this:



import sys
from osgeo import gdal, gdalconst

calculated_files = gdal.Open(*.tif, gdalconst.GA_ReadOnly)
source_files = gdal.Open(*.anotherfiletype, gdalconst.GA_Update)

stats = calculated_files.GetMetadata()
band = calculated_files.GetRasterBand(1)

bandStats = band.GetMetadata()

print('bandStats is set to')
print(bandStats)

source_files.SetMetadata(bandStats, )

del(calculated_files)
del(source_files)


I've been messing with this addition using glob and a for loop:



import glob
import os

types = ('*.tif', '*.anotherfiletype')
all_files =
for files in types:
all_files.extend(glob.glob(files))









share|improve this question

























  • It's a little unclear what you need. Are you saying that the first script works fine and is able to (for instance) put the metadata from file1.tiff onto file.anotherfiletype? And you want to do it for all file1, file2, file3, etc? Do the TIFFs and other files have the same name (just different extension)?

    – HFBrowning
    Nov 21 '18 at 20:47













  • Yes, the first script works fine if I use individual file names, sorry if my question is unclear. I'm having trouble figuring out how to make this work for an entire directory of File1 to File2 metadata migration. The file types do not currently have the same naming convention before the extension because they're from 2 different sources. I'm guessing this may be an issue.

    – muskrat
    Nov 21 '18 at 20:57














0












0








0








I am new to python and am currently stuck on how to run my metadata script on 2 different file types within a directory. I have calculated statistics for a set of GeoTIFFs and need to take those band statistics and paste them into the metadata for another set of files.
Basically, I have .tif files with the calculated statistics and I need to paste those statistics into the original source file headers.



I can do this for each individual file using GDAL with the script below, but I'm at a standstill on how to make this work for multiple files and have the data match correctly. I used a for loop bash script but I don't think it matches the correct statistics to each source file header.



Do I need to restructure my script to use for/if statements?



My working script for individual files works with this:



import sys
from osgeo import gdal, gdalconst

calculated_files = gdal.Open(*.tif, gdalconst.GA_ReadOnly)
source_files = gdal.Open(*.anotherfiletype, gdalconst.GA_Update)

stats = calculated_files.GetMetadata()
band = calculated_files.GetRasterBand(1)

bandStats = band.GetMetadata()

print('bandStats is set to')
print(bandStats)

source_files.SetMetadata(bandStats, )

del(calculated_files)
del(source_files)


I've been messing with this addition using glob and a for loop:



import glob
import os

types = ('*.tif', '*.anotherfiletype')
all_files =
for files in types:
all_files.extend(glob.glob(files))









share|improve this question
















I am new to python and am currently stuck on how to run my metadata script on 2 different file types within a directory. I have calculated statistics for a set of GeoTIFFs and need to take those band statistics and paste them into the metadata for another set of files.
Basically, I have .tif files with the calculated statistics and I need to paste those statistics into the original source file headers.



I can do this for each individual file using GDAL with the script below, but I'm at a standstill on how to make this work for multiple files and have the data match correctly. I used a for loop bash script but I don't think it matches the correct statistics to each source file header.



Do I need to restructure my script to use for/if statements?



My working script for individual files works with this:



import sys
from osgeo import gdal, gdalconst

calculated_files = gdal.Open(*.tif, gdalconst.GA_ReadOnly)
source_files = gdal.Open(*.anotherfiletype, gdalconst.GA_Update)

stats = calculated_files.GetMetadata()
band = calculated_files.GetRasterBand(1)

bandStats = band.GetMetadata()

print('bandStats is set to')
print(bandStats)

source_files.SetMetadata(bandStats, )

del(calculated_files)
del(source_files)


I've been messing with this addition using glob and a for loop:



import glob
import os

types = ('*.tif', '*.anotherfiletype')
all_files =
for files in types:
all_files.extend(glob.glob(files))






python metadata glob gdal






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 21:00







muskrat

















asked Nov 21 '18 at 20:19









muskratmuskrat

13




13













  • It's a little unclear what you need. Are you saying that the first script works fine and is able to (for instance) put the metadata from file1.tiff onto file.anotherfiletype? And you want to do it for all file1, file2, file3, etc? Do the TIFFs and other files have the same name (just different extension)?

    – HFBrowning
    Nov 21 '18 at 20:47













  • Yes, the first script works fine if I use individual file names, sorry if my question is unclear. I'm having trouble figuring out how to make this work for an entire directory of File1 to File2 metadata migration. The file types do not currently have the same naming convention before the extension because they're from 2 different sources. I'm guessing this may be an issue.

    – muskrat
    Nov 21 '18 at 20:57



















  • It's a little unclear what you need. Are you saying that the first script works fine and is able to (for instance) put the metadata from file1.tiff onto file.anotherfiletype? And you want to do it for all file1, file2, file3, etc? Do the TIFFs and other files have the same name (just different extension)?

    – HFBrowning
    Nov 21 '18 at 20:47













  • Yes, the first script works fine if I use individual file names, sorry if my question is unclear. I'm having trouble figuring out how to make this work for an entire directory of File1 to File2 metadata migration. The file types do not currently have the same naming convention before the extension because they're from 2 different sources. I'm guessing this may be an issue.

    – muskrat
    Nov 21 '18 at 20:57

















It's a little unclear what you need. Are you saying that the first script works fine and is able to (for instance) put the metadata from file1.tiff onto file.anotherfiletype? And you want to do it for all file1, file2, file3, etc? Do the TIFFs and other files have the same name (just different extension)?

– HFBrowning
Nov 21 '18 at 20:47







It's a little unclear what you need. Are you saying that the first script works fine and is able to (for instance) put the metadata from file1.tiff onto file.anotherfiletype? And you want to do it for all file1, file2, file3, etc? Do the TIFFs and other files have the same name (just different extension)?

– HFBrowning
Nov 21 '18 at 20:47















Yes, the first script works fine if I use individual file names, sorry if my question is unclear. I'm having trouble figuring out how to make this work for an entire directory of File1 to File2 metadata migration. The file types do not currently have the same naming convention before the extension because they're from 2 different sources. I'm guessing this may be an issue.

– muskrat
Nov 21 '18 at 20:57





Yes, the first script works fine if I use individual file names, sorry if my question is unclear. I'm having trouble figuring out how to make this work for an entire directory of File1 to File2 metadata migration. The file types do not currently have the same naming convention before the extension because they're from 2 different sources. I'm guessing this may be an issue.

– muskrat
Nov 21 '18 at 20:57












1 Answer
1






active

oldest

votes


















0














It looks like you need to turn your first script into a function, and then pass in a dictionary that maps what two files go together:



def transfer_metadata(tif_file, other_file):
calculated_files = gdal.Open(tif_file, gdalconst.GA_ReadOnly)
source_files = gdal.Open(other_file, gdalconst.GA_Update)

stats = calculated_files.GetMetadata()
band = calculated_files.GetRasterBand(1)
bandStats = band.GetMetadata()
print('bandStats is set to {}'.format(bandStats))

source_files.SetMetadata(bandStats, )

# optional because I don't see how you're using stats otherwise?
return stats

data_dict = {'file1.tif': 'monkey.xml',
'file2.tif': 'giraffe.txt',
'file40.tif': 'kitten.html'
}
for k, v in data_dict.items():
# The print will print stats since you're returning them
# If you don't need stats for anything, delete the return and
# this print call
print(transfer_metadata(tif_file=k, other_file=v))





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419899%2frunning-metadata-script-from-one-file-type-to-another-using-glob%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    It looks like you need to turn your first script into a function, and then pass in a dictionary that maps what two files go together:



    def transfer_metadata(tif_file, other_file):
    calculated_files = gdal.Open(tif_file, gdalconst.GA_ReadOnly)
    source_files = gdal.Open(other_file, gdalconst.GA_Update)

    stats = calculated_files.GetMetadata()
    band = calculated_files.GetRasterBand(1)
    bandStats = band.GetMetadata()
    print('bandStats is set to {}'.format(bandStats))

    source_files.SetMetadata(bandStats, )

    # optional because I don't see how you're using stats otherwise?
    return stats

    data_dict = {'file1.tif': 'monkey.xml',
    'file2.tif': 'giraffe.txt',
    'file40.tif': 'kitten.html'
    }
    for k, v in data_dict.items():
    # The print will print stats since you're returning them
    # If you don't need stats for anything, delete the return and
    # this print call
    print(transfer_metadata(tif_file=k, other_file=v))





    share|improve this answer




























      0














      It looks like you need to turn your first script into a function, and then pass in a dictionary that maps what two files go together:



      def transfer_metadata(tif_file, other_file):
      calculated_files = gdal.Open(tif_file, gdalconst.GA_ReadOnly)
      source_files = gdal.Open(other_file, gdalconst.GA_Update)

      stats = calculated_files.GetMetadata()
      band = calculated_files.GetRasterBand(1)
      bandStats = band.GetMetadata()
      print('bandStats is set to {}'.format(bandStats))

      source_files.SetMetadata(bandStats, )

      # optional because I don't see how you're using stats otherwise?
      return stats

      data_dict = {'file1.tif': 'monkey.xml',
      'file2.tif': 'giraffe.txt',
      'file40.tif': 'kitten.html'
      }
      for k, v in data_dict.items():
      # The print will print stats since you're returning them
      # If you don't need stats for anything, delete the return and
      # this print call
      print(transfer_metadata(tif_file=k, other_file=v))





      share|improve this answer


























        0












        0








        0







        It looks like you need to turn your first script into a function, and then pass in a dictionary that maps what two files go together:



        def transfer_metadata(tif_file, other_file):
        calculated_files = gdal.Open(tif_file, gdalconst.GA_ReadOnly)
        source_files = gdal.Open(other_file, gdalconst.GA_Update)

        stats = calculated_files.GetMetadata()
        band = calculated_files.GetRasterBand(1)
        bandStats = band.GetMetadata()
        print('bandStats is set to {}'.format(bandStats))

        source_files.SetMetadata(bandStats, )

        # optional because I don't see how you're using stats otherwise?
        return stats

        data_dict = {'file1.tif': 'monkey.xml',
        'file2.tif': 'giraffe.txt',
        'file40.tif': 'kitten.html'
        }
        for k, v in data_dict.items():
        # The print will print stats since you're returning them
        # If you don't need stats for anything, delete the return and
        # this print call
        print(transfer_metadata(tif_file=k, other_file=v))





        share|improve this answer













        It looks like you need to turn your first script into a function, and then pass in a dictionary that maps what two files go together:



        def transfer_metadata(tif_file, other_file):
        calculated_files = gdal.Open(tif_file, gdalconst.GA_ReadOnly)
        source_files = gdal.Open(other_file, gdalconst.GA_Update)

        stats = calculated_files.GetMetadata()
        band = calculated_files.GetRasterBand(1)
        bandStats = band.GetMetadata()
        print('bandStats is set to {}'.format(bandStats))

        source_files.SetMetadata(bandStats, )

        # optional because I don't see how you're using stats otherwise?
        return stats

        data_dict = {'file1.tif': 'monkey.xml',
        'file2.tif': 'giraffe.txt',
        'file40.tif': 'kitten.html'
        }
        for k, v in data_dict.items():
        # The print will print stats since you're returning them
        # If you don't need stats for anything, delete the return and
        # this print call
        print(transfer_metadata(tif_file=k, other_file=v))






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 21:48









        HFBrowningHFBrowning

        1,27311428




        1,27311428
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419899%2frunning-metadata-script-from-one-file-type-to-another-using-glob%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            Npm cannot find a required file even through it is in the searched directory