Error message while reading Sentinel2 data


#1

I want to read a Sentinel 2 image and subset it. I ran the code below in Spyder and I got this error message for the last line: AttributeError: ‘NoneType’ object has no attribute ‘getRasterWidth’ . So it seems that the subset was not conducted successfully.

import sys
import numpy as np
import snappy
from snappy import ProductIO

p2 = ProductIO.readProduct("D:\Sentinel2\S2A_MSIL1C_20181024T161331_N0206_R140_T16QEJ_20181024T195951\S2A_MSIL1C_20181024T161331_N0206_R140_T16QEJ_20181024T195951.SAFE\MTD_MSIL1C.xml")

Read B2

b2=p2.getBand("B2")
width_b2=b2.getRasterWidth()
height_b2=b2.getRasterWidth()
b2_data=np.zeros(width_b2*height_b2,dtype=np.float32)
b2.readPixels(0,0,width_b2,height_b2,b2_data)

HashMap=jpy.get_type("java.util.HashMap")
parameters=HashMap()
parameters.put("targetResolution",20)
result=GPF.createProduct("Resample",parameters,p2)

SubsetOp=jpy.get_type("org.esa.snap.core.gpf.common.SubsetOp")
WKTReader=jpy.get_type("com.vividsolutions.jts.io.WKTReader")
wkt="POLYGON((20.880019 -86.870835, 20.877639 -86.851230, 20.839102 -86.867525, 20.846000 -86.883562, 20.880019 -86.870835))"
geometry=WKTReader().read(wkt)

op=SubsetOp()
op.setSourceProduct(result)
op.setGeoRegion(geometry)
sub_product=op.getTargetProduct()
sub_b1=sub_product.getBand(“B1”)
width=sub_b1.getRasterWidth() #AttributeError: ‘NoneType’ object has no attribute ‘getRasterWidth’

Double checking the code, I ran the first part of the code in the Anaconda prompt

import sys
import numpy as np
import snappy
from snappy import ProductIO

p2 = ProductIO.readProduct("D:\Sentinel2\S2A_MSIL1C_20181024T161331_N0206_R140_T16QEJ_20181024T195951\S2A_MSIL1C_20181024T161331_N0206_R140_T16QEJ_20181024T195951.SAFE\MTD_MSIL1C.xml")

… and I got the following error message:

INFO: org.esa.s2tbx.dataio.s2.ortho.S2OrthoProductReaderPlugIn: Building product reader - EPSG:32616
WARNING: org.esa.s2tbx.dataio.metadata.GenericXmlMetadata: Metadata: the path to element [metadata_level] does not exist
WARNING: org.esa.s2tbx.dataio.metadata.GenericXmlMetadata: Metadata: the path to element [granuleidentifier] does not exist
WARNING: org.esa.s2tbx.dataio.metadata.GenericXmlMetadata: Metadata: the path to element [bandid] does not exist
INFO: org.hsqldb.persist.Logger: dataFileCache open start

What this error means and does this has to do with the error message I got in Spyder? Thank you for your help!