Batch rename MODIS data?












2














I have MOD13Q1 NDVI images. Data are named in this format:



MOD13Q1.A2007049.h26v06.006.2015161224938_MODIS_Grid_16DAY_250m_500m_VI.tif  


(YYYYDDD) Here 2007 is a year and 049 is day in year.



I have to rename it in this format: (YYYY.MM.DD) 2007.02.18



I am renaming it manually for every single file. How to perform a batch process for renaming all the files?










share|improve this question





























    2














    I have MOD13Q1 NDVI images. Data are named in this format:



    MOD13Q1.A2007049.h26v06.006.2015161224938_MODIS_Grid_16DAY_250m_500m_VI.tif  


    (YYYYDDD) Here 2007 is a year and 049 is day in year.



    I have to rename it in this format: (YYYY.MM.DD) 2007.02.18



    I am renaming it manually for every single file. How to perform a batch process for renaming all the files?










    share|improve this question



























      2












      2








      2







      I have MOD13Q1 NDVI images. Data are named in this format:



      MOD13Q1.A2007049.h26v06.006.2015161224938_MODIS_Grid_16DAY_250m_500m_VI.tif  


      (YYYYDDD) Here 2007 is a year and 049 is day in year.



      I have to rename it in this format: (YYYY.MM.DD) 2007.02.18



      I am renaming it manually for every single file. How to perform a batch process for renaming all the files?










      share|improve this question















      I have MOD13Q1 NDVI images. Data are named in this format:



      MOD13Q1.A2007049.h26v06.006.2015161224938_MODIS_Grid_16DAY_250m_500m_VI.tif  


      (YYYYDDD) Here 2007 is a year and 049 is day in year.



      I have to rename it in this format: (YYYY.MM.DD) 2007.02.18



      I am renaming it manually for every single file. How to perform a batch process for renaming all the files?







      r modis batch date string






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 1 at 14:44









      Andre Silva

      7,195113679




      7,195113679










      asked Jan 1 at 10:06









      Tilok ChetriTilok Chetri

      266




      266






















          3 Answers
          3






          active

          oldest

          votes


















          3














          Suppose the following files:



          MOD13Q1.A2007049.h26v06.006.2015161224938_MODIS_Grid_16DAY_250m_500m_VI.tif
          MOD13Q1.A2007051.h26v06.006.2015161224938_MODIS_Grid_16DAY_250m_500m_VI.tif


          and that they are stored in directory C:/modis_files



          Run:



          setwd('C:\modis_files') #path to folder where MODIS files are.

          file.rename(list.files(), paste(as.Date(substr(list.files(),10,16),"%Y%j"),".tif", sep=""))


          It will result in:



          2007-02-18.tif
          2007-02-20.tif




          If you want to keep the entire file name, while only replacing the [year + julian days], then Bharadwaj A K's answer is the one.






          share|improve this answer



















          • 2




            Nice use of the - for the renamed file. Often Windows machines do not handle . characters when used other than the file extension.
            – Aaron
            Jan 1 at 19:10



















          2














          If you were to use R language (open source), this will work.



          setwd("C:/modis") #path of modis files
          li<-as.data.frame(list.files(pattern = ".tif|.TIF"))
          li$nn<-paste0(substr(li[,1],1,9),format(as.Date(substr(li[,1],10,16), "%Y%j"),"%Y.%m.%d"),substr(li[,1],17,75))
          for(i in 1:nrow(li)){
          file.rename(as.character(li[i,1]),li[i,2])
          }





          share|improve this answer





























            2














            For anyone that wants a Python approach, try the following:



            import datetime, os, glob

            inws = '/path/to/tiff/workspace'

            tiffs = glob.glob(os.path.join(inws, "*.tif"))

            for tiff in tiffs:
            julien_date = os.path.basename(tiff)[11:16] # Extract the julien date string
            date = datetime.datetime.strptime(julien_date, '%y%j').date() # Format date
            outname = os.path.join(inws, date.strftime('%y-%m-%d') + ".tif") # Create outfile name and path
            os.rename(tiff, outname) # Rename the files





            share|improve this answer





















              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "79"
              };
              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: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              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%2fgis.stackexchange.com%2fquestions%2f307324%2fbatch-rename-modis-data%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              3














              Suppose the following files:



              MOD13Q1.A2007049.h26v06.006.2015161224938_MODIS_Grid_16DAY_250m_500m_VI.tif
              MOD13Q1.A2007051.h26v06.006.2015161224938_MODIS_Grid_16DAY_250m_500m_VI.tif


              and that they are stored in directory C:/modis_files



              Run:



              setwd('C:\modis_files') #path to folder where MODIS files are.

              file.rename(list.files(), paste(as.Date(substr(list.files(),10,16),"%Y%j"),".tif", sep=""))


              It will result in:



              2007-02-18.tif
              2007-02-20.tif




              If you want to keep the entire file name, while only replacing the [year + julian days], then Bharadwaj A K's answer is the one.






              share|improve this answer



















              • 2




                Nice use of the - for the renamed file. Often Windows machines do not handle . characters when used other than the file extension.
                – Aaron
                Jan 1 at 19:10
















              3














              Suppose the following files:



              MOD13Q1.A2007049.h26v06.006.2015161224938_MODIS_Grid_16DAY_250m_500m_VI.tif
              MOD13Q1.A2007051.h26v06.006.2015161224938_MODIS_Grid_16DAY_250m_500m_VI.tif


              and that they are stored in directory C:/modis_files



              Run:



              setwd('C:\modis_files') #path to folder where MODIS files are.

              file.rename(list.files(), paste(as.Date(substr(list.files(),10,16),"%Y%j"),".tif", sep=""))


              It will result in:



              2007-02-18.tif
              2007-02-20.tif




              If you want to keep the entire file name, while only replacing the [year + julian days], then Bharadwaj A K's answer is the one.






              share|improve this answer



















              • 2




                Nice use of the - for the renamed file. Often Windows machines do not handle . characters when used other than the file extension.
                – Aaron
                Jan 1 at 19:10














              3












              3








              3






              Suppose the following files:



              MOD13Q1.A2007049.h26v06.006.2015161224938_MODIS_Grid_16DAY_250m_500m_VI.tif
              MOD13Q1.A2007051.h26v06.006.2015161224938_MODIS_Grid_16DAY_250m_500m_VI.tif


              and that they are stored in directory C:/modis_files



              Run:



              setwd('C:\modis_files') #path to folder where MODIS files are.

              file.rename(list.files(), paste(as.Date(substr(list.files(),10,16),"%Y%j"),".tif", sep=""))


              It will result in:



              2007-02-18.tif
              2007-02-20.tif




              If you want to keep the entire file name, while only replacing the [year + julian days], then Bharadwaj A K's answer is the one.






              share|improve this answer














              Suppose the following files:



              MOD13Q1.A2007049.h26v06.006.2015161224938_MODIS_Grid_16DAY_250m_500m_VI.tif
              MOD13Q1.A2007051.h26v06.006.2015161224938_MODIS_Grid_16DAY_250m_500m_VI.tif


              and that they are stored in directory C:/modis_files



              Run:



              setwd('C:\modis_files') #path to folder where MODIS files are.

              file.rename(list.files(), paste(as.Date(substr(list.files(),10,16),"%Y%j"),".tif", sep=""))


              It will result in:



              2007-02-18.tif
              2007-02-20.tif




              If you want to keep the entire file name, while only replacing the [year + julian days], then Bharadwaj A K's answer is the one.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jan 1 at 14:41

























              answered Jan 1 at 14:26









              Andre SilvaAndre Silva

              7,195113679




              7,195113679








              • 2




                Nice use of the - for the renamed file. Often Windows machines do not handle . characters when used other than the file extension.
                – Aaron
                Jan 1 at 19:10














              • 2




                Nice use of the - for the renamed file. Often Windows machines do not handle . characters when used other than the file extension.
                – Aaron
                Jan 1 at 19:10








              2




              2




              Nice use of the - for the renamed file. Often Windows machines do not handle . characters when used other than the file extension.
              – Aaron
              Jan 1 at 19:10




              Nice use of the - for the renamed file. Often Windows machines do not handle . characters when used other than the file extension.
              – Aaron
              Jan 1 at 19:10













              2














              If you were to use R language (open source), this will work.



              setwd("C:/modis") #path of modis files
              li<-as.data.frame(list.files(pattern = ".tif|.TIF"))
              li$nn<-paste0(substr(li[,1],1,9),format(as.Date(substr(li[,1],10,16), "%Y%j"),"%Y.%m.%d"),substr(li[,1],17,75))
              for(i in 1:nrow(li)){
              file.rename(as.character(li[i,1]),li[i,2])
              }





              share|improve this answer


























                2














                If you were to use R language (open source), this will work.



                setwd("C:/modis") #path of modis files
                li<-as.data.frame(list.files(pattern = ".tif|.TIF"))
                li$nn<-paste0(substr(li[,1],1,9),format(as.Date(substr(li[,1],10,16), "%Y%j"),"%Y.%m.%d"),substr(li[,1],17,75))
                for(i in 1:nrow(li)){
                file.rename(as.character(li[i,1]),li[i,2])
                }





                share|improve this answer
























                  2












                  2








                  2






                  If you were to use R language (open source), this will work.



                  setwd("C:/modis") #path of modis files
                  li<-as.data.frame(list.files(pattern = ".tif|.TIF"))
                  li$nn<-paste0(substr(li[,1],1,9),format(as.Date(substr(li[,1],10,16), "%Y%j"),"%Y.%m.%d"),substr(li[,1],17,75))
                  for(i in 1:nrow(li)){
                  file.rename(as.character(li[i,1]),li[i,2])
                  }





                  share|improve this answer












                  If you were to use R language (open source), this will work.



                  setwd("C:/modis") #path of modis files
                  li<-as.data.frame(list.files(pattern = ".tif|.TIF"))
                  li$nn<-paste0(substr(li[,1],1,9),format(as.Date(substr(li[,1],10,16), "%Y%j"),"%Y.%m.%d"),substr(li[,1],17,75))
                  for(i in 1:nrow(li)){
                  file.rename(as.character(li[i,1]),li[i,2])
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 1 at 14:03









                  Bharadwaj A KBharadwaj A K

                  453




                  453























                      2














                      For anyone that wants a Python approach, try the following:



                      import datetime, os, glob

                      inws = '/path/to/tiff/workspace'

                      tiffs = glob.glob(os.path.join(inws, "*.tif"))

                      for tiff in tiffs:
                      julien_date = os.path.basename(tiff)[11:16] # Extract the julien date string
                      date = datetime.datetime.strptime(julien_date, '%y%j').date() # Format date
                      outname = os.path.join(inws, date.strftime('%y-%m-%d') + ".tif") # Create outfile name and path
                      os.rename(tiff, outname) # Rename the files





                      share|improve this answer


























                        2














                        For anyone that wants a Python approach, try the following:



                        import datetime, os, glob

                        inws = '/path/to/tiff/workspace'

                        tiffs = glob.glob(os.path.join(inws, "*.tif"))

                        for tiff in tiffs:
                        julien_date = os.path.basename(tiff)[11:16] # Extract the julien date string
                        date = datetime.datetime.strptime(julien_date, '%y%j').date() # Format date
                        outname = os.path.join(inws, date.strftime('%y-%m-%d') + ".tif") # Create outfile name and path
                        os.rename(tiff, outname) # Rename the files





                        share|improve this answer
























                          2












                          2








                          2






                          For anyone that wants a Python approach, try the following:



                          import datetime, os, glob

                          inws = '/path/to/tiff/workspace'

                          tiffs = glob.glob(os.path.join(inws, "*.tif"))

                          for tiff in tiffs:
                          julien_date = os.path.basename(tiff)[11:16] # Extract the julien date string
                          date = datetime.datetime.strptime(julien_date, '%y%j').date() # Format date
                          outname = os.path.join(inws, date.strftime('%y-%m-%d') + ".tif") # Create outfile name and path
                          os.rename(tiff, outname) # Rename the files





                          share|improve this answer












                          For anyone that wants a Python approach, try the following:



                          import datetime, os, glob

                          inws = '/path/to/tiff/workspace'

                          tiffs = glob.glob(os.path.join(inws, "*.tif"))

                          for tiff in tiffs:
                          julien_date = os.path.basename(tiff)[11:16] # Extract the julien date string
                          date = datetime.datetime.strptime(julien_date, '%y%j').date() # Format date
                          outname = os.path.join(inws, date.strftime('%y-%m-%d') + ".tif") # Create outfile name and path
                          os.rename(tiff, outname) # Rename the files






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 2 at 3:08









                          AaronAaron

                          37.6k19107247




                          37.6k19107247






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


                              • 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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2fgis.stackexchange.com%2fquestions%2f307324%2fbatch-rename-modis-data%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

                              Human spaceflight

                              Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

                              File:DeusFollowingSea.jpg