0

When I tried to run a batch file using gdal_calculate command. The following error occurs:

  • batfile is not recognized as an internal or external command, operable program or batch file.
  • RuntimeError: D:\Sample\sample_file_.tif' does not exist in the file system, and is not recognised as a supported dataset name.
  • RuntimeError: D:\Sample\sample_file_!Month!.tif' does not exist in the file system, and is not recognised as a supported dataset name.

Is gdal_calculate not allowed to be use in batch process? Any idea?

Here is the script I am using:

@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 ordinal.bat !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

)
echo done
goto :EOF

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:ordinal %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
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
2
  • Debugging your batch files
    – DavidPostill
    Jan 27, 2015 at 6:51
  • See edit. How come that The system cannot find the batch label specified - OrdinalToDate error is showing when it doesn't appear on the first calculation? And as well as the Invalid attempt to call batch label outside of batch script.?
    – lovelyvm
    Jan 28, 2015 at 3:08

1 Answer 1

1

Reference Call

CALL a subroutine (:label)

The CALL command will pass control to the statement after the label specified along with any specified parameters. To exit the subroutine specify GOTO:eof this will transfer control to the end of the current subroutine.

A label is defined by a single colon followed by a name. This is the basis of a batch file function.

CALL :sub_display 123
CALL :sub_display 456
ECHO All Done
GOTO :eof

:sub_display
ECHO The result is %1
GOTO :eof

At the end of the subroutine, GOTO :eof will return to the position where you used CALL.

Command blocks do not work with the call command. Redirection with & | <> also does not work as expected.

Reference Using parenthesis/brackets to group expressions:

Parenthesis can be used to split commands across multiple lines. This can make code more readable. Variables will be evaluated for the code block just as if the command was a single line.

  (command)

  (
    command
    command )

Example

IF EXIST C:\pagefile.sys ( ECHO pagefile found on C: drive)

If the command will fit on a single line, then the parenthesis can be omitted

You have the following command block inside your call block:

(if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)

...

A GOTO command inside a bracketed code block will break the parenthesis context and cause errors. A GOTO will also break a For-Do Loop.

I believe call works like goto. So ( ... call... ... ) could be your problem.

Why don't you make OrdinalToDate a seperate batch file?

10
  • Then you have a different problem. Please try to create a *small reproducible test script. Edit you question to include the new information.
    – DavidPostill
    Jan 28, 2015 at 9:05
  • I wasn't able to cd to the input path that is why it resulted to file not found. Sorry about that. Now when I added the input_path the error was this 'OrdinalToDate.bat' is not recognized as an internal or external command, operable program or batch file.
    – lovelyvm
    Jan 29, 2015 at 2:28
  • So I made the OrdinalToDate a separate batch file. I just add a @echo off and saved it as OrdinalToDate.bat. Then from the call batch file, I just change it to call OrdinalToDate.bat. And inserted endlocal after the gdal_calculate command and the result was 'OrdinalToDate.bat' is not recognized as an internal or external command, operable program or batch file.
    – lovelyvm
    Jan 29, 2015 at 2:29
  • If you have a cd first you need to make sure OrdinalToDate.bat is on your path or use a fully quealified name when you call it ...
    – DavidPostill
    Jan 29, 2015 at 6:27
  • I already made sure that the batch file is on the right path, and changed the batch name into ordinal.bat. When I run the batch file, only the first file from the input path was processed the rest was failed. It says 'ordinal.bat' is not recognized as an internal or external command, operable program or batch file. And RuntimeError: D:\Sample\sample_file_.tif' does not exist in the file system, and is not recognised as a supported dataset name. And RuntimeError: D:\Sample\sample_file_!Month!.tif' does not exist in the file system, and is not recognised as a supported dataset name.
    – lovelyvm
    Jan 29, 2015 at 7:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .