In [1]:
# Import Libraries
#import gdal
import os
import json
In [2]:
import gdal
In [3]:
# Import Folders
data_folder = os.path.abspath("data")
output_folder = os.path.abspath("output")
#check if outfolder exists if not create it
if not os.path.exists(output_folder):
    os.makedirs(output_folder)
vendor_assets_folder = os.path.abspath("vendor-assets")
gdal_bin = os.path.join(vendor_assets_folder, 'GDAL')
In [4]:
# Get All scenes
files = os.listdir(data_folder)
scenes = [x for x in files if x.split('.')[-1] == 'zip']
print("Found", len(scenes), "Scenes")
scenes
Found 1 Scenes
Out[4]:
['S2A_MSIL2A_20190104T051211_N0211_R019_T43PGS_20190104T094623.zip']
In [9]:
gdal.Info(infilepath, format='SENTINEL2')
In [5]:
#LEVEL 2

# Process scenes
print("\nProcessing Total", len(scenes), "scenes")
for i, a in enumerate(scenes):
    infilepath = os.path.join(data_folder, a)
    print(infilepath)
    fileinfo = gdal.Info(infilepath, format='json')
    print(infilepath)
    l1c_name = fileinfo['metadata']['SUBDATASETS']['SUBDATASET_1_NAME']
    #print('l1c_name \n\n', l1c_name)
    outfile = ".".join(a.split('.')[:-1]) + "_10m_b3_b4_b8.img"
    outfilepath = os.path.join(output_folder, outfile)
    gdal_translate = os.path.join(gdal_bin, 'gdal_translate.exe')
    gdal_translate = gdal_translate.replace(" ", "\ ")
    command = '' + gdal_translate + '  -f "HFA" "' + l1c_name + '" -b 2 -b 1 -b 4 "' + outfilepath + '" ' + '  -stats -a_nodata 0'
    print(command)
    print("\n", a, " >> ", outfile)  
    #os.system(command)  
    print("Progress: ", i+1, "/", len(scenes), "Scenes")

#Done
print("\nProcessed Total", len(scenes), "scenes")
    
    
    
    
Processing Total 1 scenes
D:\codebase\copernicus-scripts\data\S2A_MSIL2A_20190104T051211_N0211_R019_T43PGS_20190104T094623.zip
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-1653df193781> in <module>
      6     infilepath = os.path.join(data_folder, a)
      7     print(infilepath)
----> 8     fileinfo = gdal.Info(infilepath, format='json')
      9     print(infilepath)
     10     l1c_name = fileinfo['metadata']['SUBDATASETS']['SUBDATASET_1_NAME']

~\AppData\Local\conda\conda\envs\geo\lib\site-packages\osgeo\gdal.py in Info(ds, **kwargs)
    246     if format == 'json' and deserialize:
    247         import json
--> 248         ret = json.loads(ret)
    249     return ret
    250 

~\AppData\Local\conda\conda\envs\geo\lib\json\__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    339     else:
    340         if not isinstance(s, (bytes, bytearray)):
--> 341             raise TypeError(f'the JSON object must be str, bytes or bytearray, '
    342                             f'not {s.__class__.__name__}')
    343         s = s.decode(detect_encoding(s), 'surrogatepass')

TypeError: the JSON object must be str, bytes or bytearray, not NoneType
In [4]:
#LEVEL 1

# Process scenes 
print("\nProcessing Total", len(scenes), "scenes")
for i, a in enumerate(scenes):
    infilepath = os.path.join(data_folder, a)
    fileinfo = gdal.Info(infilepath, format='json')
    l1c_name = fileinfo['metadata']['SUBDATASETS']['SUBDATASET_1_NAME']
    #print('l1c_name \n\n', l1c_name)
    outfile = ".".join(a.split('.')[:-1]) + "_10m_b3_b4_b8.img"
    outfilepath = os.path.join(output_folder, outfile)
    gdal_translate = os.path.join(gdal_bin, 'gdal_translate.exe')
    gdal_translate = gdal_translate.replace(" ", "\ ")
    command = '' + gdal_translate + '  -f "HFA" "' + l1c_name + '" -b 2 -b 1 -b 4 "' + outfilepath + '" ' + '  -stats -a_nodata 0'
    #print(command)
    print("\n", a, " >> ", outfile)  
    os.system(command)  
    print("Progress: ", i+1, "/", len(scenes), "Scenes")

#Done
print("\nProcessed Total", len(scenes), "scenes")
    
    
    
    
Processing Total 5 scenes

 S2A_MSIL1C_20190101T050211_N0207_R119_T44PLB_20190101T075353.zip  >>  S2A_MSIL1C_20190101T050211_N0207_R119_T44PLB_20190101T075353_10m_b3_b4_b8.img
Progress:  1 / 5 Scenes

 S2A_MSIL1C_20190101T050211_N0207_R119_T44QMD_20190101T075353.zip  >>  S2A_MSIL1C_20190101T050211_N0207_R119_T44QMD_20190101T075353_10m_b3_b4_b8.img
Progress:  2 / 5 Scenes

 S2A_MSIL1C_20190101T050211_N0207_R119_T44QME_20190101T075353.zip  >>  S2A_MSIL1C_20190101T050211_N0207_R119_T44QME_20190101T075353_10m_b3_b4_b8.img
Progress:  3 / 5 Scenes

 S2A_MSIL1C_20190101T050211_N0207_R119_T44QND_20190101T075353.zip  >>  S2A_MSIL1C_20190101T050211_N0207_R119_T44QND_20190101T075353_10m_b3_b4_b8.img
Progress:  4 / 5 Scenes

 S2A_MSIL1C_20190104T051211_N0207_R019_T43PGP_20190104T085820.zip  >>  S2A_MSIL1C_20190104T051211_N0207_R019_T43PGP_20190104T085820_10m_b3_b4_b8.img
Progress:  5 / 5 Scenes

Processed Total 5 scenes
In [26]:
cmd = 'gdalinfo  -json ' + r'"D:\codebase\copernicus-scripts\data\S2A_MSIL2A_20190104T051211_N0211_R019_T43PGS_20190104T094623.zip"'
os.system(cmd)
Out[26]:
1
In [27]:
import subprocess
user = subprocess.check_output(cmd, shell=True,stderr=subprocess.STDOUT)
---------------------------------------------------------------------------
CalledProcessError                        Traceback (most recent call last)
<ipython-input-27-0f05fd0fb4c4> in <module>
      1 import subprocess
----> 2 user = subprocess.check_output(cmd, shell=True,stderr=subprocess.STDOUT)

c:\users\apsac243\appdata\local\programs\python\python37\Lib\subprocess.py in check_output(timeout, *popenargs, **kwargs)
    393 
    394     return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
--> 395                **kwargs).stdout
    396 
    397 

c:\users\apsac243\appdata\local\programs\python\python37\Lib\subprocess.py in run(input, capture_output, timeout, check, *popenargs, **kwargs)
    485         if check and retcode:
    486             raise CalledProcessError(retcode, process.args,
--> 487                                      output=stdout, stderr=stderr)
    488     return CompletedProcess(process.args, retcode, stdout, stderr)
    489 

CalledProcessError: Command 'gdalinfo  -json "D:\codebase\copernicus-scripts\data\S2A_MSIL2A_20190104T051211_N0211_R019_T43PGS_20190104T094623.zip"' returned non-zero exit status 1.
In [23]:
import subprocess
subprocess.Popen('gdalinfo -json "D:\codebase\copernicus-scripts\data\S2A_MSIL2A_20190104T051211_N0211_R019_T43PGS_20190104T094623.zip"', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
Out[23]:
(b'',
 b"ERROR 4: `D:\\codebase\\copernicus-scripts\\data\\S2A_MSIL2A_20190104T051211_N0211_R019_T43PGS_20190104T094623.zip' not recognized as a supported file format.\r\ngdalinfo failed - unable to open 'D:\\codebase\\copernicus-scripts\\data\\S2A_MSIL2A_20190104T051211_N0211_R019_T43PGS_20190104T094623.zip'.\r\n")
In [22]:
'gdalinfo -json "D:\codebase\copernicus-scripts\data\S2A_MSIL2A_20190104T051211_N0211_R019_T43PGS_20190104T094623.zip"'.replace('\\\\', '\\')
Out[22]:
'gdalinfo -json "D:\\codebase\\copernicus-scripts\\data\\S2A_MSIL2A_20190104T051211_N0211_R019_T43PGS_20190104T094623.zip"'
In [ ]:
'''D:\codebase\copernicus-scripts\vendor-assets\GDAL\gdal_translate.exe -f "HFA" "SENTINEL2_L1C:/vsizip/D:\codebase\copernicus-scripts\data\S2A_MSIL1C_20190101T050211_N0207_R119_T44PLB_20190101T075353.zip/S2A_MSIL1C_20190101T050211_N0207_R119_T44PLB_20190101T075353.SAFE/MTD_MSIL1C.xml:10m:EPSG_32644" -b 2 -b 3 -b 4 "D:\codebase\copernicus-scripts\output\S2A_MSIL1C_20190101T050211_N0207_R119_T44PLB_20190101T075353_10m_b3_b4_b8.img" -stats -a_nodata 0'''
In [35]:
import gdal
In [39]:
infilepath
Out[39]:
'D:\\codebase\\copernicus-scripts\\data\\S2A_MSIL2A_20190104T051211_N0211_R019_T43PGS_20190104T094623.zip'
In [43]:
gdal.Info(r'D:\codebase\copernicus-scripts\data\S2A_MSIL2A_20190104T051211_N0211_R019_T43PGS_20190104T094623.zip', format='json')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-43-247f0471e8f2> in <module>
----> 1 gdal.Info(r'D:\codebase\copernicus-scripts\data\S2A_MSIL2A_20190104T051211_N0211_R019_T43PGS_20190104T094623.zip', format='json')

c:\users\apsac243\envs\geo\lib\site-packages\osgeo\gdal.py in Info(ds, **kwargs)
    252     if format == 'json' and deserialize:
    253         import json
--> 254         ret = json.loads(ret)
    255     return ret
    256 

c:\users\apsac243\appdata\local\programs\python\python37\Lib\json\__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    339     else:
    340         if not isinstance(s, (bytes, bytearray)):
--> 341             raise TypeError(f'the JSON object must be str, bytes or bytearray, '
    342                             f'not {s.__class__.__name__}')
    343         s = s.decode(detect_encoding(s), 'surrogatepass')

TypeError: the JSON object must be str, bytes or bytearray, not NoneType
In [44]:
dir(gdal)
Out[44]:
['AllRegister',
 'ApplyGeoTransform',
 'ApplyVerticalShiftGrid',
 'AsyncReader',
 'AsyncReader_swigregister',
 'AutoCreateWarpedVRT',
 'Band',
 'Band_swigregister',
 'BuildVRT',
 'BuildVRTInternalNames',
 'BuildVRTInternalObjects',
 'BuildVRTOptions',
 'CE_Debug',
 'CE_Failure',
 'CE_Fatal',
 'CE_None',
 'CE_Warning',
 'CPLBinaryToHex',
 'CPLES_BackslashQuotable',
 'CPLES_CSV',
 'CPLES_SQL',
 'CPLES_URL',
 'CPLES_XML',
 'CPLE_AppDefined',
 'CPLE_AssertionFailed',
 'CPLE_FileIO',
 'CPLE_IllegalArg',
 'CPLE_NoWriteAccess',
 'CPLE_None',
 'CPLE_NotSupported',
 'CPLE_OpenFailed',
 'CPLE_OutOfMemory',
 'CPLE_UserInterrupt',
 'CPLHexToBinary',
 'CXT_Attribute',
 'CXT_Comment',
 'CXT_Element',
 'CXT_Literal',
 'CXT_Text',
 'ColorEntry',
 'ColorEntry_swigregister',
 'ColorTable',
 'ColorTable_swigregister',
 'ComputeMedianCutPCT',
 'ComputeProximity',
 'ContourGenerate',
 'CreatePansharpenedVRT',
 'DCAP_CREATE',
 'DCAP_CREATECOPY',
 'DCAP_DEFAULT_FIELDS',
 'DCAP_NOTNULL_FIELDS',
 'DCAP_NOTNULL_GEOMFIELDS',
 'DCAP_OPEN',
 'DCAP_RASTER',
 'DCAP_VECTOR',
 'DCAP_VIRTUALIO',
 'DEMProcessing',
 'DEMProcessingInternal',
 'DEMProcessingOptions',
 'DMD_CONNECTION_PREFIX',
 'DMD_CREATIONDATATYPES',
 'DMD_CREATIONFIELDDATATYPES',
 'DMD_CREATIONOPTIONLIST',
 'DMD_EXTENSION',
 'DMD_EXTENSIONS',
 'DMD_HELPTOPIC',
 'DMD_LONGNAME',
 'DMD_MIMETYPE',
 'DMD_SUBDATASETS',
 'DataTypeIsComplex',
 'Dataset',
 'Dataset_swigregister',
 'Debug',
 'DecToDMS',
 'DecToPackedDMS',
 'DitherRGB2PCT',
 'DontUseExceptions',
 'Driver',
 'Driver_swigregister',
 'Error',
 'ErrorReset',
 'EscapeString',
 'FPolygonize',
 'FileFromMemBuffer',
 'FillNodata',
 'FindFile',
 'FinderClean',
 'GARIO_COMPLETE',
 'GARIO_ERROR',
 'GARIO_PENDING',
 'GARIO_UPDATE',
 'GA_ReadOnly',
 'GA_Update',
 'GCI_AlphaBand',
 'GCI_BlackBand',
 'GCI_BlueBand',
 'GCI_CyanBand',
 'GCI_GrayIndex',
 'GCI_GreenBand',
 'GCI_HueBand',
 'GCI_LightnessBand',
 'GCI_MagentaBand',
 'GCI_PaletteIndex',
 'GCI_RedBand',
 'GCI_SaturationBand',
 'GCI_Undefined',
 'GCI_YCbCr_CbBand',
 'GCI_YCbCr_CrBand',
 'GCI_YCbCr_YBand',
 'GCI_YellowBand',
 'GCP',
 'GCP_swigregister',
 'GCPsToGeoTransform',
 'GDALBuildVRTOptions',
 'GDALBuildVRTOptions_swigregister',
 'GDALDEMProcessingOptions',
 'GDALDEMProcessingOptions_swigregister',
 'GDALDestroyDriverManager',
 'GDALGridOptions',
 'GDALGridOptions_swigregister',
 'GDALInfoOptions',
 'GDALInfoOptions_swigregister',
 'GDALNearblackOptions',
 'GDALNearblackOptions_swigregister',
 'GDALRasterizeOptions',
 'GDALRasterizeOptions_swigregister',
 'GDALTransformerInfoShadow',
 'GDALTransformerInfoShadow_swigregister',
 'GDALTranslateOptions',
 'GDALTranslateOptions_swigregister',
 'GDALVectorTranslateOptions',
 'GDALVectorTranslateOptions_swigregister',
 'GDALWarpAppOptions',
 'GDALWarpAppOptions_swigregister',
 'GDAL_DATA_COVERAGE_STATUS_DATA',
 'GDAL_DATA_COVERAGE_STATUS_EMPTY',
 'GDAL_DATA_COVERAGE_STATUS_UNIMPLEMENTED',
 'GDAL_GCP_GCPLine_get',
 'GDAL_GCP_GCPLine_set',
 'GDAL_GCP_GCPPixel_get',
 'GDAL_GCP_GCPPixel_set',
 'GDAL_GCP_GCPX_get',
 'GDAL_GCP_GCPX_set',
 'GDAL_GCP_GCPY_get',
 'GDAL_GCP_GCPY_set',
 'GDAL_GCP_GCPZ_get',
 'GDAL_GCP_GCPZ_set',
 'GDAL_GCP_Id_get',
 'GDAL_GCP_Id_set',
 'GDAL_GCP_Info_get',
 'GDAL_GCP_Info_set',
 'GDT_Byte',
 'GDT_CFloat32',
 'GDT_CFloat64',
 'GDT_CInt16',
 'GDT_CInt32',
 'GDT_Float32',
 'GDT_Float64',
 'GDT_Int16',
 'GDT_Int32',
 'GDT_TypeCount',
 'GDT_UInt16',
 'GDT_UInt32',
 'GDT_Unknown',
 'GFT_Integer',
 'GFT_Real',
 'GFT_String',
 'GFU_Alpha',
 'GFU_AlphaMax',
 'GFU_AlphaMin',
 'GFU_Blue',
 'GFU_BlueMax',
 'GFU_BlueMin',
 'GFU_Generic',
 'GFU_Green',
 'GFU_GreenMax',
 'GFU_GreenMin',
 'GFU_Max',
 'GFU_MaxCount',
 'GFU_Min',
 'GFU_MinMax',
 'GFU_Name',
 'GFU_PixelCount',
 'GFU_Red',
 'GFU_RedMax',
 'GFU_RedMin',
 'GF_Read',
 'GF_Write',
 'GMF_ALL_VALID',
 'GMF_ALPHA',
 'GMF_NODATA',
 'GMF_PER_DATASET',
 'GOA2GetAccessToken',
 'GOA2GetAuthorizationURL',
 'GOA2GetRefreshToken',
 'GPI_CMYK',
 'GPI_Gray',
 'GPI_HLS',
 'GPI_RGB',
 'GRA_Average',
 'GRA_Bilinear',
 'GRA_Cubic',
 'GRA_CubicSpline',
 'GRA_Lanczos',
 'GRA_Max',
 'GRA_Med',
 'GRA_Min',
 'GRA_Mode',
 'GRA_NearestNeighbour',
 'GRA_Q1',
 'GRA_Q3',
 'GRIORA_Average',
 'GRIORA_Bilinear',
 'GRIORA_Cubic',
 'GRIORA_CubicSpline',
 'GRIORA_Gauss',
 'GRIORA_Lanczos',
 'GRIORA_Mode',
 'GRIORA_NearestNeighbour',
 'GTO_BIT',
 'GTO_BSQ',
 'GTO_TIP',
 'GeneralCmdLineProcessor',
 'GetActualURL',
 'GetCacheMax',
 'GetCacheUsed',
 'GetColorInterpretationName',
 'GetConfigOption',
 'GetDataTypeByName',
 'GetDataTypeName',
 'GetDataTypeSize',
 'GetDriver',
 'GetDriverByName',
 'GetDriverCount',
 'GetErrorCounter',
 'GetFileSystemOptions',
 'GetFileSystemsPrefixes',
 'GetJPEG2000Structure',
 'GetJPEG2000StructureAsString',
 'GetLastErrorMsg',
 'GetLastErrorNo',
 'GetLastErrorType',
 'GetPaletteInterpretationName',
 'GetSignedURL',
 'GetUseExceptions',
 'Grid',
 'GridInternal',
 'GridOptions',
 'HasThreadSupport',
 'IdentifyDriver',
 'IdentifyDriverEx',
 'Info',
 'InfoInternal',
 'InfoOptions',
 'InvGeoTransform',
 'MajorObject',
 'MajorObject_swigregister',
 'Mkdir',
 'MkdirRecursive',
 'Nearblack',
 'NearblackOptions',
 'OF_ALL',
 'OF_GNM',
 'OF_RASTER',
 'OF_READONLY',
 'OF_SHARED',
 'OF_UPDATE',
 'OF_VECTOR',
 'OF_VERBOSE_ERROR',
 'Open',
 'OpenEx',
 'OpenShared',
 'PackedDMSToDec',
 'ParseCommandLine',
 'ParseXMLString',
 'Polygonize',
 'PopErrorHandler',
 'PopFinderLocation',
 'PushErrorHandler',
 'PushFinderLocation',
 'RGBFile2PCTFile',
 'RasterAttributeTable',
 'RasterAttributeTable_swigregister',
 'Rasterize',
 'RasterizeLayer',
 'RasterizeOptions',
 'ReadDir',
 'ReadDirRecursive',
 'RegenerateOverview',
 'RegenerateOverviews',
 'Rename',
 'ReprojectImage',
 'Rmdir',
 'RmdirRecursive',
 'SerializeXMLTree',
 'SetCacheMax',
 'SetConfigOption',
 'SetErrorHandler',
 'SieveFilter',
 'StatBuf',
 'StatBuf_swigregister',
 'TermProgress',
 'TermProgress_nocb',
 'Transformer',
 'Translate',
 'TranslateInternal',
 'TranslateOptions',
 'Unlink',
 'UseExceptions',
 'VSICurlClearCache',
 'VSIFCloseL',
 'VSIFEofL',
 'VSIFGetRangeStatusL',
 'VSIFOpenExL',
 'VSIFOpenL',
 'VSIFReadL',
 'VSIFSeekL',
 'VSIFTellL',
 'VSIFTruncateL',
 'VSIFWriteL',
 'VSIGetLastErrorMsg',
 'VSIGetLastErrorNo',
 'VSIStatL',
 'VSISupportsSparseFiles',
 'VSI_RANGE_STATUS_DATA',
 'VSI_RANGE_STATUS_HOLE',
 'VSI_RANGE_STATUS_UNKNOWN',
 'VSI_STAT_EXISTS_FLAG',
 'VSI_STAT_NATURE_FLAG',
 'VSI_STAT_SIZE_FLAG',
 'VectorTranslate',
 'VectorTranslateOptions',
 'VersionInfo',
 'VirtualMem',
 'VirtualMem_swigregister',
 'Warp',
 'WarpOptions',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 'array_modes',
 'byteorders',
 'deprecation_warn',
 'gdalconst',
 'have_warned',
 'ogr',
 'osr',
 'sys',
 'wrapper_GDALNearblackDestDS',
 'wrapper_GDALNearblackDestName',
 'wrapper_GDALRasterizeDestDS',
 'wrapper_GDALRasterizeDestName',
 'wrapper_GDALVectorTranslateDestDS',
 'wrapper_GDALVectorTranslateDestName',
 'wrapper_GDALWarpDestDS',
 'wrapper_GDALWarpDestName']
In [ ]: