Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Subtract images in batch process(leap year and regular year)  (Read 27407 times)

0 Members and 1 Guest are viewing this topic.

_unknown_

    Topic Starter


    Beginner

    • Experience: Beginner
    • OS: Windows 7
    Re: Subtract images in batch process(leap year and regular year)
    « Reply #90 on: January 20, 2015, 12:41:33 AM »
    BINGO!

    Removed the echo and run the batch file. It calculate the first image but the rest of the file was an error. It says:

    C:\path\to\bat\file>test.bat
    C2011060.A1_ABC.ABCD.tif matches to 2011-03-01
    Running calculation
    0...10..Saving output
    .20...30...40...50...60...70...80...90. ..100 - done.
    Press any key to continue . . .

    C:\path\to\bat\file>(
    set "year=C2011061.A1_ABC.ABCD"
     set "daynum=!year:~5,3!"
     set "year=!year:~1,4!"
     call :OrdinalToDate !year! !daynum! yy month dd
     echo C2011061.A1_ABC.ABCD.tif matches to !yy!-!month!-!dd!
     if !Month! == 01 set Month=jan
     if !Month! == 02 set Month=feb
     if !Month! == 03 set Month=mar
     if !Month! == 04 set Month=apr
     if !Month! == 05 set Month=may
     if !Month! == 06 set Month=jun
     if !Month! == 07 set Month=jul
     if !Month! == 08 set Month=aug
     if !Month! == 09 set Month=sep
     if !Month! == 10 set Month=oct
     if !Month! == 11 set Month=nov
     if !Month! == 12 set Month=dec
    gdal_calculate --out=%out_path%\C2011061.A1_ABC.ABCD.tif  --calc="((image1-image2)" --image2=%sample_path%\sample_file_!Month!.tif --image1=%in_path%\C2011061.A1_ABC.ABCD.tif  --extent=INTERSECT
     pause
    )
    Invalid attempt to call batch label outside of batch script.
    C2011061.A1_ABC.ABCD.tif  matches to !yy!-!month!-!dd!

    RuntimeError: `D:\Path\sample_file_!Month!.tif' does not exist in the file system
    ,
    and is not recognised as a supported dataset name.

    Press any key to continue . . .

    C:\path\to\bat\file>(
    set "year=C2011062.A1_ABC.ABCD"
     set "daynum=!year:~5,3!"
     set "year=!year:~1,4!"
     call :OrdinalToDate !year! !daynum! yy month dd
     echo C2011062.A1_ABC.ABCD.tif matches to !yy!-!month!-!dd!
     if !Month! == 01 set Month=jan
     if !Month! == 02 set Month=feb
     if !Month! == 03 set Month=mar
     if !Month! == 04 set Month=apr
     if !Month! == 05 set Month=may
     if !Month! == 06 set Month=jun
     if !Month! == 07 set Month=jul
     if !Month! == 08 set Month=aug
     if !Month! == 09 set Month=sep
     if !Month! == 10 set Month=oct
     if !Month! == 11 set Month=nov
     if !Month! == 12 set Month=dec
    gdal_calculate --out=%out_path%\C2011062.A1_ABC.ABCD.tif  --calc="((image1-image2)" --image2=%sample_path%\sample_file_!Month!.tif --image1=%in_path%\C2011062.A1_ABC.ABCD.tif  --extent=INTERSECT
     pause
    )
    Invalid attempt to call batch label outside of batch script.
    C2011062.A1_ABC.ABCD.tif  matches to !yy!-!month!-!dd!

    RuntimeError: `D:\Path\sample_file_!Month!.tif' does not exist in the file system
    ,
    and is not recognised as a supported dataset name.

    Press any key to continue . . .

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: Subtract images in batch process(leap year and regular year)
    « Reply #91 on: January 20, 2015, 07:14:58 AM »
    Re-post the entire batch file you are using.  I see you made some changes to the code Foxidrive posted.

    _unknown_

      Topic Starter


      Beginner

      • Experience: Beginner
      • OS: Windows 7
      Re: Subtract images in batch process(leap year and regular year)
      « Reply #92 on: January 20, 2015, 06:51:02 PM »
      Re-post the entire batch file you are using.  I see you made some changes to the code Foxidrive posted.

      This is what Foxidrive posted:

      Code: [Select]
      @echo off
      setlocal enabledelayedexpansion

      for /f "delims=" %%a in ('dir /b /on c*.tif ') do (
      set "year=%%~na"
      set "daynum=!year:~5,3!"
      set "year=!year:~1,4!"

      call :OrdinalToDate !year! !daynum! yy month dd
      echo %%a matches to !yy!-!month!-!dd!

      if !Month!==01 set Month=JAN
      if !Month!==02 set Month=FEB
      if !Month!==03 set Month=MAR
      if !Month!==04 set Month=APR
      if !Month!==05 set Month=MAY
      if !Month!==06 set Month=JUN
      if !Month!==07 set Month=JUL
      if !Month!==08 set Month=AUG
      if !Month!==09 set Month=SEP
      if !Month!==10 set Month=OCT
      if !Month!==11 set Month=NOV
      if !Month!==12 set Month=DEC

      echo gdal_calculate --outfile=D:\path\to\file\difference.tif --calc="((image1-image2)/(image1+image2))" --image2=D:\path\to\subtrahend\sample_file_!Month!.tif image1=D:\path\to\minuend\%%a --extent=INTERSECT

      pause
      )
      echo done
      pause
      goto :EOF


      @echo off&setlocal
      call :OrdinalToDate %1 %2 yy mm dd
      echo/%yy%-%mm%-%dd%
      goto :EOF

      :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
      :OrdinalToDate %year% %doy% yy mm dd
      ::
      :: By:   Ritchie Lawrence, 2002-09-29. Version 1.0
      ::
      :: Func: Returns a calendar date from an ISO 8601 Ordinal date.
      ::       For NT4/2K/XP.
      ::
      :: Args: %1 year component to be converted, 4 digits (by val)
      ::       %2 day of year component to be converted, 001 to 366 (by val)
      ::       %3 var to receive year, 4 digits (by ref)
      ::       %4 var to receive month, 2 digits, 01 to 31 (by ref)
      ::       %5 var to receive day of month, 01 to 31 (by ref)
      :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
      setlocal ENABLEEXTENSIONS
      for /f "tokens=1-2" %%a in ('echo/%1 %2') do set /a yy=%%a,o=1%%b%%1000
      set /a z=14-1,z/=12,y=yy+4800-z,m=1+12*z-3,j=153*m+2
      set /a j=j/5+1+y*365+y/4-y/100+y/400-2432046,j+=o-1
      set /a a=j+2432045,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
      set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
      set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
      (if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
      endlocal&set %3=%yy%&set %4=%mm%&set %5=%dd%&goto :EOF
      :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

      This is what I am using:

      Code: [Select]
      @echo off
      setlocal enabledelayedexpansion

      set "in_path=D:\Input"
      set "out_path=D:\Output"
      set "sample_path=D:\Sample"
      set "proc_path=D:\Processed_Files"

      md %out_path%
      md %proc_path%
      cd /d "%in_path%"

      for /f "delims=" %%a in ('dir /b /on ????????*.tif ') do (
      set "year=%%~na"
      set "daynum=!year:~5,3!"
      set "year=!year:~1,4!"

      call :OrdinalToDate !year! !daynum! yy month dd
      echo %%a matches to !yy!-!month!-!dd!

      if !Month!==01 set Month=jan
      if !Month!==02 set Month=feb
      if !Month!==03 set Month=mar
      if !Month!==04 set Month=apr
      if !Month!==05 set Month=may
      if !Month!==06 set Month=jun
      if !Month!==07 set Month=jul
      if !Month!==08 set Month=aug
      if !Month!==09 set Month=sep
      if !Month!==10 set Month=oct
      if !Month!==11 set Month=nov
      if !Month!==12 set Month=dec

      gdal_calculate --out=%out_path%\%%a --calc="((image1-image2))" --image2=%sample_path%\sample_file_!Month!.tif --image1=%in_path%\%%a --extent=INTERSECT

      pause
      )
      echo done
      pause
      goto :EOF

      :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
      :OrdinalToDate %year% %doy% yy mm dd
      ::
      :: By:   Ritchie Lawrence, 2002-09-29. Version 1.0
      ::
      :: Func: Returns a calendar date from an ISO 8601 Ordinal date.
      ::       For NT4/2K/XP.
      ::
      :: Args: %1 year component to be converted, 4 digits (by val)
      ::       %2 day of year component to be converted, 001 to 366 (by val)
      ::       %3 var to receive year, 4 digits (by ref)
      ::       %4 var to receive month, 2 digits, 01 to 31 (by ref)
      ::       %5 var to receive day of month, 01 to 31 (by ref)
      :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
      setlocal ENABLEEXTENSIONS
      for /f "tokens=1-2" %%a in ('echo/%1 %2') do set /a yy=%%a,o=1%%b%%1000
      set /a z=14-1,z/=12,y=yy+4800-z,m=1+12*z-3,j=153*m+2
      set /a j=j/5+1+y*365+y/4-y/100+y/400-2432046,j+=o-1
      set /a a=j+2432045,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
      set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
      set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
      (if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
      endlocal&set %3=%yy%&set %4=%mm%&set %5=%dd%&goto :EOF
      :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: Subtract images in batch process(leap year and regular year)
      « Reply #93 on: January 20, 2015, 08:49:03 PM »
      All the error output you posted above really doesn't make any sense at all.  I tried searching for any person who has ever tried to batch script the GDAL_CALCULATE command and nobody else has done it.  The batch code is as sound as can be.  I see no technical flaws in it. So it has to be something with the python script that is screwing things up.  Since you don't want us trying to test this process for you on our own computers our hands are pretty much tied.

      You might want to just try and have someone write a python program that does the same thing as the batch file above.

      My last and final suggestions is to try and execute the python script using the full path to the python install and the full path to the gdal_calculate.py script.  Make sure you use the .py extension for the script and the .exe extension for the python executable.

      Try each of these one at a time.
      Code: [Select]
      gdal_calculate.py --out=%out_path%\%%a --calc="((image1-image2))" --image2=%sample_path%\sample_file_!Month!.tif --image1=%in_path%\%%a --extent=INTERSECT
      "C:\path to script\gdal_calculate.py" --out=%out_path%\%%a --calc="((image1-image2))" --image2=%sample_path%\sample_file_!Month!.tif --image1=%in_path%\%%a --extent=INTERSECT
      "C:\path to python\python.exe" "C:\path to script\gdal_calculate.py" --out=%out_path%\%%a --calc="((image1-image2))" --image2=%sample_path%\sample_file_!Month!.tif --image1=%in_path%\%%a --extent=INTERSECT
      CALL "C:\path to script\gdal_calculate.py" --out=%out_path%\%%a --calc="((image1-image2))" --image2=%sample_path%\sample_file_!Month!.tif --image1=%in_path%\%%a --extent=INTERSECT
      CALL "C:\path to python\python.exe" "C:\path to script\gdal_calculate.py" --out=%out_path%\%%a --calc="((image1-image2))" --image2=%sample_path%\sample_file_!Month!.tif --image1=%in_path%\%%a --extent=INTERSECT

      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: Subtract images in batch process(leap year and regular year)
      « Reply #94 on: January 20, 2015, 09:19:54 PM »
      Here are my assumptions about the errors.

      This error is coming from the cmd.exe processor. Which makes no sense.  The only real way to replicate this error and the output that came before it would be to PIPE the contents of the batch file to CMD.EXE. How this is happening is beyond me. There is nothing in the batch file that would cause this.
      Invalid attempt to call batch label outside of batch script.
      C2011062.A1_ABC.ABCD.tif  matches to !yy!-!month!-!dd!

      But this error is actually coming from the python interpreter.
      RuntimeError: `D:\Path\sample_file_!Month!.tif' does not exist in the file system
      ,
      and is not recognised as a supported dataset name.

      I could be wrong but seems to make logical sense to me.

      _unknown_

        Topic Starter


        Beginner

        • Experience: Beginner
        • OS: Windows 7
        Re: Subtract images in batch process(leap year and regular year)
        « Reply #95 on: January 26, 2015, 12:39:25 AM »
        Here are my assumptions about the errors.

        This error is coming from the cmd.exe processor. Which makes no sense.  The only real way to replicate this error and the output that came before it would be to PIPE the contents of the batch file to CMD.EXE. How this is happening is beyond me. There is nothing in the batch file that would cause this.
        But this error is actually coming from the python interpreter.
        I could be wrong but seems to make logical sense to me.

        I have tried to modified again the script into this:

        Code: [Select]
        @echo off
        setlocal enabledelayedexpansion

        set "in_path=D:\Input"
        set "out_path=D:\Output"
        set "sample_path=D:\Sample"
        set "proc_path=D:\Processed_Files"

        md %out_path%
        md %proc_path%
        cd /d "%in_path%"

        for /f "delims=" %%a in ('dir /b /on ????????*.tif ') do (
        set "year=%%~na"
        set "daynum=!year:~5,3!"
        set "year=!year:~1,4!"

        gdal_calculate --out=%out_path%\%%a --calc="((image1-image2))" --image2=%sample_path%\sample_file_mar.tif --image1=%in_path%\%%a --extent=INTERSECT

        if !Month!==01 set Month=jan
        if !Month!==02 set Month=feb
        if !Month!==03 set Month=mar
        if !Month!==04 set Month=apr
        if !Month!==05 set Month=may
        if !Month!==06 set Month=jun
        if !Month!==07 set Month=jul
        if !Month!==08 set Month=aug
        if !Month!==09 set Month=sep
        if !Month!==10 set Month=oct
        if !Month!==11 set Month=nov
        if !Month!==12 set Month=dec
        )
        :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        :OrdinalToDate %year% %doy% yy mm dd
        ::
        :: By:   Ritchie Lawrence, 2002-09-29. Version 1.0
        ::
        :: Func: Returns a calendar date from an ISO 8601 Ordinal date.
        ::       For NT4/2K/XP.
        ::
        :: Args: %1 year component to be converted, 4 digits (by val)
        ::       %2 day of year component to be converted, 001 to 366 (by val)
        ::       %3 var to receive year, 4 digits (by ref)
        ::       %4 var to receive month, 2 digits, 01 to 31 (by ref)
        ::       %5 var to receive day of month, 01 to 31 (by ref)
        :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        setlocal ENABLEEXTENSIONS
        for /f "tokens=1-2" %%a in ('echo/%1 %2') do set /a yy=%%a,o=1%%b%%1000
        set /a z=14-1,z/=12,y=yy+4800-z,m=1+12*z-3,j=153*m+2
        set /a j=j/5+1+y*365+y/4-y/100+y/400-2432046,j+=o-1
        set /a a=j+2432045,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
        set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
        set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
        (if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
        endlocal&set %3=%yy%&set %4=%mm%&set %5=%dd%&goto :EOF
        :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

        call :OrdinalToDate !year! !daynum! yy month dd
        echo %%a matches to !yy!-!month!-!dd!

        And specify the sample_path_!Month!.tif into sample_path_mar.tif. The bat file run well without any error like Invalid attempt to call batch label outside of batch script. and RuntimeError: `D:\Path\sample_file_!Month!.tif' does not exist in the file system, and is not recognised as a supported dataset name. What does this mean? Does the problem lies within the !Month!? Though it doesn't based with the label :Ordinaltodate anymore.


        Here is the output:
        Code: [Select]
        C:\path\to\bat\file>sample.bat
        Running calculation
        0...10...20...30...40...50...60...70...80...90Saving output
        ...100 - done.

        C:\path\to\bat\file>(
        set "year=C2011061.A1_ABC.ABCD"
         set "daynum=!year:~5,3!"
         set "year=!year:~1,4!"
         gdal_calculate --calc="((image1-image2))" --outfile=D:\Output\C2011061.A1_ABC.ABCD.tif  --image1=D:\Input\C2011061.A1_ABC.ABCD.tif --image2=D:\sample\mean_chla_mar.tif --extent=INTERSECT
         if !Month! == 01 set Month=jan
         if !Month! == 02 set Month=feb
         if !Month! == 03 set Month=mar
         if !Month! == 04 set Month=apr
         if !Month! == 05 set Month=may
         if !Month! == 06 set Month=jun
         if !Month! == 07 set Month=jul
         if !Month! == 08 set Month=aug
         if !Month! == 09 set Month=sep
         if !Month! == 10 set Month=oct
         if !Month! == 11 set Month=nov
         if !Month! == 12 set Month=dec
        )
        Running calculation
        0...10...20...30...40...50...60...70...80..Saving output
        .90...100 - done.

        C:\path\to\bat\file>(
        set "year=C2011062.A1_ABC.ABCD"
         set "daynum=!year:~5,3!"
         set "year=!year:~1,4!"
         gdal_calculate --calc="((image1-image2))" --outfile=D:\Output\C2011062.A1_ABC.ABCD.tif  --image1=D:\Input\C2011062.A1_ABC.ABCD.tif --image2=D:\sample\mean_chla_mar.tif --extent=INTERSECT
         if !Month! == 01 set Month=jan
         if !Month! == 02 set Month=feb
         if !Month! == 03 set Month=mar
         if !Month! == 04 set Month=apr
         if !Month! == 05 set Month=may
         if !Month! == 06 set Month=jun
         if !Month! == 07 set Month=jul
         if !Month! == 08 set Month=aug
         if !Month! == 09 set Month=sep
         if !Month! == 10 set Month=oct
         if !Month! == 11 set Month=nov
         if !Month! == 12 set Month=dec
        )
        Running calculation
        0...10...20...30...40...50...60...70.Saving output
        ..80...90...100 - done.

        C:\Users\Oogway\Desktop>test.bat
        A subdirectory or file D:\Test\Anomaly already exists.
        A subdirectory or file D:\Test\Processed_Merged already exists.
        Running calculation
        0...10...20...30...40...50...60...70...80...90Saving output
        ...100 - done.

        C:\Users\Oogway\Desktop>(
        set "year=A2014275.L2_LAC.SeAHABS"
         set "daynum=!year:~5,3!"
         set "year=!year:~1,4!"
         gdal_calculate --calc="((ds1.astype(numpy.float32))-(ds2.astype(numpy.float32)))" --outfile=D:
        \Test\Anomaly\A2014275.L2_LAC.SeAHABS.tif --ds1=D:\Test\Merged\A2014275.L2_LAC.SeAHABS.tif --ds
        2=D:\Test\Mean\mean_chla_oct.tif --extent=MAXOF --nodata --overwrite
         if !Month! == 01 set Month=jan
         if !Month! == 02 set Month=feb
         if !Month! == 03 set Month=mar
         if !Month! == 04 set Month=apr
         if !Month! == 05 set Month=may
         if !Month! == 06 set Month=jun
         if !Month! == 07 set Month=jul
         if !Month! == 08 set Month=aug
         if !Month! == 09 set Month=sep
         if !Month! == 10 set Month=oct
         if !Month! == 11 set Month=nov
         if !Month! == 12 set Month=dec
        )
        Running calculation
        0...10...20...30...40...50...60...70...80..Saving output
        .90...100 - done.

        C:\Users\Oogway\Desktop>(
        set "year=A2014276.L2_LAC.SeAHABS"
         set "daynum=!year:~5,3!"
         set "year=!year:~1,4!"
         gdal_calculate --calc="((ds1.astype(numpy.float32))-(ds2.astype(numpy.float32)))" --outfile=D:
        \Test\Anomaly\A2014276.L2_LAC.SeAHABS.tif --ds1=D:\Test\Merged\A2014276.L2_LAC.SeAHABS.tif --ds
        2=D:\Test\Mean\mean_chla_oct.tif --extent=MAXOF --nodata --overwrite
         if !Month! == 01 set Month=jan
         if !Month! == 02 set Month=feb
         if !Month! == 03 set Month=mar
         if !Month! == 04 set Month=apr
         if !Month! == 05 set Month=may
         if !Month! == 06 set Month=jun
         if !Month! == 07 set Month=jul
         if !Month! == 08 set Month=aug
         if !Month! == 09 set Month=sep
         if !Month! == 10 set Month=oct
         if !Month! == 11 set Month=nov
         if !Month! == 12 set Month=dec
        )
        Running calculation
        0...10...20...30...40...50...60...70...80...90Saving output
        ...100 - done.

        Squashman



          Specialist
        • Thanked: 134
        • Experience: Experienced
        • OS: Other
        Re: Subtract images in batch process(leap year and regular year)
        « Reply #96 on: January 26, 2015, 08:08:36 AM »
        I have no idea how you are getting that much Verbose output from the Batch file when ECHO is OFF.
        Your output is also not reflecting what is in the batch file either.  So I have no idea what you are really doing.
        You definitely removed stuff from the batch file that you should not have.

        I am pretty much done helping you because I can't control what you are doing.  I offered to literally do this for you. But you have refused that help.  That is the only way it is going to get done because you obviously do not know what you are doing.
        « Last Edit: January 26, 2015, 08:32:19 AM by Squashman »

        _unknown_

          Topic Starter


          Beginner

          • Experience: Beginner
          • OS: Windows 7
          Re: Subtract images in batch process(leap year and regular year)
          « Reply #97 on: January 27, 2015, 02:33:29 AM »
          Someone suggested to try debugging the bat file using this: Debugging your batch files.

          Tried and this is the result.


          Code: [Select]
          C:\path\to\bat\file>setlocal enabledelayedexpansion

          C:\path\to\bat\file>set "in_path=D:\Input"

          C:\path\to\bat\file>set "out_path=D:\Output"

          C:\path\to\bat\file>set "sample_path=D:\Sample"

          C:\path\to\bat\file>set "proc_path=D:\Processed_Files"

          C:\path\to\bat\file>md D:\Output

          C:\Users\Oogway\Desktop>md D:\Processed_Files

          C:\path\to\bat\file>>cd /d "D:\Output"

          D:\Output>for /F "delims=" %a in ('dir /b /on ????????*.tif') do (
          set "year=%~na" 
           set "daynum=!year:~5,3!" 
           set "year=!year:~1,4!" 
           call :OrdinalToDate !year! !daynum! yy month dd 
           echo %a matches to !yy!-!month!-!dd! 
           if !Month! == 01 set Month=jan 
           if !Month! == 02 set Month=feb 
           if !Month! == 03 set Month=mar 
           if !Month! == 04 set Month=apr 
           if !Month! == 05 set Month=may 
           if !Month! == 06 set Month=jun 
           if !Month! == 07 set Month=jul 
           if !Month! == 08 set Month=aug 
           if !Month! == 09 set Month=sep 
           if !Month! == 10 set Month=oct 
           if !Month! == 11 set Month=nov 
           if !Month! == 12 set Month=dec 
          gdal_calculate --out=%out_path%\%%a --calc="((image1-image2))" --image2=%sample_path%\sample_file_mar.tif --image1=%in_path%\%%a --extent=INTERSECT
          )

          D:\Output>(
          set "year=C2014274.A1_ABC.ABCD" 
           set "daynum=!year:~5,3!" 
           set "year=!year:~1,4!" 
           call :OrdinalToDate !year! !daynum! yy month dd 
           echo C2014274.A1_ABC.ABCD.tif matches to !yy!-!month!-!dd! 
           if !Month! == 01 set Month=jan 
           if !Month! == 02 set Month=feb 
           if !Month! == 03 set Month=mar 
           if !Month! == 04 set Month=apr 
           if !Month! == 05 set Month=may 
           if !Month! == 06 set Month=jun 
           if !Month! == 07 set Month=jul 
           if !Month! == 08 set Month=aug 
           if !Month! == 09 set Month=sep 
           if !Month! == 10 set Month=oct 
           if !Month! == 11 set Month=nov 
           if !Month! == 12 set Month=dec 
           gdal_calculate --out=%out_path%\%%a --calc="((image1-image2))" --image2=%sample_path%\sample_file_mar.tif --image1=%in_path%\%%a --extent=INTERSECT
          )

          D:\Output>setlocal ENABLEEXTENSIONS

          D:\Output>for /F "tokens=1-2" %a in ('echo/2014 274') do set /a yy=%a,o=1%b%1000

          D:\Output>set /a yy=2014,o=1274%1000

          D:\Output>set /a z=14-1,z/=12,y=yy+4800-z,m=1+12*z-3,j=153*m+2

          D:\Output>set /a j=j/5+1+y*365+y/4-y/100+y/400-2432046,j+=o-1

          D:\Output>set /a a=j+2432045,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a

          D:\Output>set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5

          D:\Output>set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10

          D:\Output>(if 10 LSS 10 set mm=010 )  & (if 1 LSS 10 set dd=01 )

          D:\Output>endlocal & set yy=2014  & set month=10  & set dd=01  & goto :EOF
          C2014274.A1_ABC.ABCD.tif matches to 2014-10-01
          Running calculation
          0...10...20...30...40...50...60...70...80...90Saving output
          ...100 - done.

          C:\path\to\bat\file>(
          set "year=C2014275.A1_ABC.ABCD" 
           set "daynum=!year:~5,3!" 
           set "year=!year:~1,4!" 
           call :OrdinalToDate !year! !daynum! yy month dd 
           echo C2014275.A1_ABC.ABCD.tif matches to !yy!-!month!-!dd! 
           if !Month! == 01 set Month=jan 
           if !Month! == 02 set Month=feb 
           if !Month! == 03 set Month=mar 
           if !Month! == 04 set Month=apr 
           if !Month! == 05 set Month=may 
           if !Month! == 06 set Month=jun 
           if !Month! == 07 set Month=jul 
           if !Month! == 08 set Month=aug 
           if !Month! == 09 set Month=sep 
           if !Month! == 10 set Month=oct 
           if !Month! == 11 set Month=nov 
           if !Month! == 12 set Month=dec 
          gdal_calculate --out=%out_path%\%%a --calc="((image1-image2))" --image2=%sample_path%\sample_file_mar.tif --image1=%in_path%\%%a --extent=INTERSECT
          )
          [b]The system cannot find the batch label specified - OrdinalToDate
          C2014275.A1_ABC.ABCD.tif matches to --[/b]
          RuntimeError: `D:\Sample\.tif' does not exist in the file system,
          and is not recognised as a supported dataset name.

          C:\path\to\bat\file>(
          set "year=C2014276.A1_ABC.ABCD" 
           set "daynum=!year:~5,3!" 
           set "year=!year:~1,4!" 
           call :OrdinalToDate !year! !daynum! yy month dd 
           echo C2014276.A1_ABC.ABCD.tif matches to !yy!-!month!-!dd! 
           if !Month! == 01 set Month=jan 
           if !Month! == 02 set Month=feb 
           if !Month! == 03 set Month=mar 
           if !Month! == 04 set Month=apr 
           if !Month! == 05 set Month=may 
           if !Month! == 06 set Month=jun 
           if !Month! == 07 set Month=jul 
           if !Month! == 08 set Month=aug 
           if !Month! == 09 set Month=sep 
           if !Month! == 10 set Month=oct 
           if !Month! == 11 set Month=nov 
           if !Month! == 12 set Month=dec 
          gdal_calculate --out=%out_path%\%%a --calc="((image1-image2))" --image2=%sample_path%\sample_file_mar.tif --image1=%in_path%\%%a --extent=INTERSECT
          )
          [b]Invalid attempt to call batch label outside of batch script.
          A2014276.L2_LAC.SeAHABS.tif matches to !yy!-!month!-!dd![/b]
          RuntimeError: `D:\Sample\!Month!.tif' does not exist in the file system,
          and is not recognised as a supported dataset name.

          _unknown_

            Topic Starter


            Beginner

            • Experience: Beginner
            • OS: Windows 7
            Re: Subtract images in batch process(leap year and regular year)
            « Reply #98 on: February 05, 2015, 11:40:58 PM »
            This is untested - it displays the command to the screen only.

            It is right then you can thank Squashman for supplying the meat of the batch file on the 3rd post in this thread.

            Code: [Select]
            @echo off
            setlocal enabledelayedexpansion

            for /f "delims=" %%a in ('dir /b /on c*.tif ') do (
            set "year=%%~na"
            set "daynum=!year:~5,3!"
            set "year=!year:~1,4!"

            call :OrdinalToDate !year! !daynum! yy month dd
            echo %%a matches to !yy!-!month!-!dd!

            if !Month!==01 set Month=JAN
            if !Month!==02 set Month=FEB
            if !Month!==03 set Month=MAR
            if !Month!==04 set Month=APR
            if !Month!==05 set Month=MAY
            if !Month!==06 set Month=JUN
            if !Month!==07 set Month=JUL
            if !Month!==08 set Month=AUG
            if !Month!==09 set Month=SEP
            if !Month!==10 set Month=OCT
            if !Month!==11 set Month=NOV
            if !Month!==12 set Month=DEC

            echo gdal_calculate --outfile=D:\path\to\file\difference.tif --calc="((image1-image2)/(image1+image2))" --image2=D:\path\to\subtrahend\sample_file_!Month!.tif image1=D:\path\to\minuend\%%a --extent=INTERSECT

            pause
            )
            echo done
            pause
            goto :EOF


            @echo off&setlocal
            call :OrdinalToDate %1 %2 yy mm dd
            echo/%yy%-%mm%-%dd%
            goto :EOF

            :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
            :OrdinalToDate %year% %doy% yy mm dd
            ::
            :: By:   Ritchie Lawrence, 2002-09-29. Version 1.0
            ::
            :: Func: Returns a calendar date from an ISO 8601 Ordinal date.
            ::       For NT4/2K/XP.
            ::
            :: Args: %1 year component to be converted, 4 digits (by val)
            ::       %2 day of year component to be converted, 001 to 366 (by val)
            ::       %3 var to receive year, 4 digits (by ref)
            ::       %4 var to receive month, 2 digits, 01 to 31 (by ref)
            ::       %5 var to receive day of month, 01 to 31 (by ref)
            :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
            setlocal ENABLEEXTENSIONS
            for /f "tokens=1-2" %%a in ('echo/%1 %2') do set /a yy=%%a,o=1%%b%%1000
            set /a z=14-1,z/=12,y=yy+4800-z,m=1+12*z-3,j=153*m+2
            set /a j=j/5+1+y*365+y/4-y/100+y/400-2432046,j+=o-1
            set /a a=j+2432045,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
            set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
            set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
            (if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
            endlocal&set %3=%yy%&set %4=%mm%&set %5=%dd%&goto :EOF
            :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

            Hi foxidrive, if it's okay, can you explain to me the piece/s of code for every line starting from the
            Code: [Select]
            for /f "delims=" %%a in ('dir /b /on c*.tif ') do ( up to the last piece of code just for me to understand everything most importantly those numbers,  "year=%%~na", "daynum=!year:~5,3!", and "year=!year:~1,4!". Thank you!