Issue with Polymer (Sentinel-2 and Sentinel-3)

Post Reply
nicola.ghirardi
Posts: 2
Joined: Tue Apr 23, 2024 7:34 am
company / institution: CNR - IREA
Location: Milan

Issue with Polymer (Sentinel-2 and Sentinel-3)

Post by nicola.ghirardi »

Hello everyone, I have encountered a problem with Polymer.
One of my colleagues passed me two scripts to process Sentinel-2 and Sentinel-3 images, however on my computer they don't seem to work. Below is a description of the error (I report the example for S3 images).

After installing Polymer (the following steps from the "readme" file on Github: 1.1, 1.2 and 1.3) I ran the following commands:

(base) nicola@polymer:~$ cd /home/nicola/Scaricati/polymer-master
(base) nicola@polymer:~/Scaricati/polymer-master$ conda activate polymer
(polymer) nicola@polymer:~/Scaricati/polymer-master$ python3 olci_multi.py
Traceback (most recent call last):
File "/home/nicola/Scaricati/polymer-master/olci_multi.py", line 7, in <module>
from polymer.main import run_atm_corr, Level1, Level2
ImportError: cannot import name 'Level1' from 'polymer.main' (/home/nicola/Scaricati/polymer-master/polymer/main.py)

Following is the script for S3 images:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import glob
import datetime

from polymer.main import run_atm_corr, Level1, Level2
from polymer.level2_hdf import Level2_HDF
from polymer.level2_nc import Level2_NETCDF
from polymer.level1_ascii import Level1_ASCII
from polymer.level1_nasa import Level1_NASA
from polymer.level1_msi import Level1_MSI
from polymer.level1_olci import Level1_OLCI
from polymer.gsw import GSW
from polymer.ancillary import Ancillary_NASA
from polymer.ancillary_era5 import Ancillary_ERA5

source_folder = "/home/nicola/Scaricati/Input_S3"
output_folder = "/home/nicola/Scaricati/Output"

class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'

def process():
product_paths = glob.glob(source_folder + "*.SEN3")

index = 0



for product_path in product_paths:
process_product(product_path)
print(bcolors.OKGREEN + '{} {}/{} done'.format(datetime.datetime.utcnow().isoformat(), index + 1, len(product_paths)) + bcolors.ENDC)
index += 1
#if index >= 3:
#break

# polymer(Level1_OLCI('/mnt/d/polymer/S3A_OL_1_EFR____20160426T093603_20160426T093803_20161217T004355_0119_003_250______MR1_R_NT_002.SEN3'),
# Level2_NETCDF(filename='output_olci.nc'),
# #Level2(filename='test.hdf'),
# multiprocessing=0, # activate multiprocessing
# )

# NOTES:
# * netcdf4 output can be selected with
# Level2(filename='output.nc', fmt='netcdf4')
# * instead of the generic Level1 and Level2 you can use directly
# the appropriate Level1 and Level2 classes (see other examples)


def process_product(product_path):
print(bcolors.OKBLUE + '{} Processing {}'.format(datetime.datetime.utcnow().isoformat(), product_path) + bcolors.ENDC)

basename = os.path.basename(product_path)

if basename.endswith('.SEN3'):
basename = basename[:-5]

output_name = output_folder + basename + '.nc'

print(output_name)


try:
run_atm_corr(
Level1_OLCI(product_path, ancillary=Ancillary_ERA5(),landmask=GSW(agg=8)),
Level2_NETCDF(filename=output_name, tmpdir='/tmp/'),
multiprocessing=0
)
except Exception as e:
print(bcolors.FAIL, end='')
print(e)
print(bcolors.ENDC, end='')

if __name__ == "__main__":
if not os.path.exists(output_folder):
os.makedirs(output_folder)

process()

process_product("/home/nicola/Scaricati/Input_S3/S3A_OL_1_EFR____20240412T094702_20240412T095002_20240413T100751_0179_111_136_2160_PS1_O_NT_003.SEN3")
#process_product("/home/nicola/Scaricati/Input/S3A_OL_1_EFR____20240412T094702_20240412T095002_20240413T100751_0179_111_136_2160_PS1_O_NT_003.SEN3")
#process_product("/home/nicola/Scaricati/Input/S3A_OL_1_EFR____20240412T094702_20240412T095002_20240413T100751_0179_111_136_2160_PS1_O_NT_003.SEN3")
wxfei19whu
Posts: 1
Joined: Tue Apr 02, 2024 11:30 am
company / institution: wuhan university
Location: wuhan

Re: Issue with Polymer (Sentinel-2 and Sentinel-3)

Post by wxfei19whu »

ImportError: cannot import name 'Level1' from 'polymer.main'

you should change the
from polymer.main import run_atm_corr, Level1, Level2
to
from polymer.main import run_atm_corr
from polymer.level1 import Level1
from polymer.level2 import Level2

I am a newbee with polymer, and I also puzzled why the code in example.py exist error.
maybe the code in write in old version.
nicola.ghirardi
Posts: 2
Joined: Tue Apr 23, 2024 7:34 am
company / institution: CNR - IREA
Location: Milan

Re: Issue with Polymer (Sentinel-2 and Sentinel-3)

Post by nicola.ghirardi »

Hi @wxfei19whu,
Unfortunately, it didn't work (same error)
Do you have any other ideas?
User avatar
fsteinmetz
Site Admin
Posts: 314
Joined: Fri Sep 07, 2018 1:34 pm
company / institution: Hygeos
Location: Lille, France
Contact:

Re: Issue with Polymer (Sentinel-2 and Sentinel-3)

Post by fsteinmetz »

Hello,
Apologies for the late answer, I have made some changes in example.py which was indeed based on an older version.
The fix indicated by wxfei19whu should indeed fix the problem, at least you shouldn't receive an error such as :
ImportError: cannot import name 'Level1' from 'polymer.main'
These changes are also included on the latest github version.
If this error is still present, could you post the error message ?
Cheers, François
Post Reply