jpy-consortium / jpy

Apache License 2.0
68 stars 16 forks source link

installing today #124

Open mczakk opened 6 months ago

mczakk commented 6 months ago

HI, I am trying to install this so that i can read beam/dimap files via python. your documentation says: With Visual Studio 14 and higher it is much easier::

SET VS100COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\ SET JDK_HOME= pip install setuptools wheel python setup.py build maven bdist_wheel

I am running Microsoft Visual Studio 2022, the folder 'common7\tools' does not exist. please could you advise how to install on an up to date windows machine?

devinrsmith commented 6 months ago

@mczakk, you might be able to take advantage of the binary wheels that we publish. With our 0.14.0 release, we have Windows (amd64) support for python 3.6 - 3.10, in which case you can simply install the wheelvia pip install jpy. Let me know if this works!

mczakk commented 6 months ago

@devinrsmith I tried this, but the snappy-conf failed as it couldn't find the jpy wheel. Ill upload the snappy.utils. Log tomorrow when I'm back at my machine. BTW I'm using python 3.11 and everything is up to date.

devinrsmith commented 6 months ago

I'm not familiar w/ snappy-conf, but it looks like you are referring to https://senbox.atlassian.net/wiki/spaces/SNAP/pages/50855941/Configure+Python+to+use+the+SNAP-Python+snappy+interface+SNAP+versions+9 ?

mczakk commented 6 months ago

I was using this onehttps://senbox.atlassian.net/wiki/spaces/SNAP/pages/2499051521/Configure+Python+to+use+the+new+SNAP-Python+esa+snappy+interface+SNAP+version+10 as i'd only installed SNAP onto my windows machine today to do this (I ysually run SNAP processing on a Linux machine) and assumed that it would be the latest version. I'm actually running SNAO 9.0.0 on thw windows machine, but the guides have the dame instruction, and when i run it the snappyutil.log file reads like this: INFO: Installing from Java module 'C:\Users\mtc20tfq\AppData\Roaming\SNAP\modules\org-esa-snap-snap-python.jar' INFO: Installing jpy... ERROR: The module 'jpy' is required to run snappy, but no binary 'jpy' wheel matching the pattern 'jpy-{version}-cp311-{abi_tag}-win_amd64.whl' could be found. You can try to build a 'jpy' wheel yourself, then copy it into "C:\Users\mtc20tfq.snap\snap-python\snappy", and then run the configuration again. Unzip the jpy sources in C:\Users\mtc20tfq.snap\snap-python\snappy/jpy-.zip, then $ cd jpy- $ python setup.py bdist_wheel $ cp dist/*.whl "C:\Users\mtc20tfq.snap\snap-python\snappy" Or get the source code from https://github.com/bcdev/jpy and follow the build instructions: $ git clone https://github.com/bcdev/jpy.git $ cd jpy ERROR: Configuration failed with exit code 10 Am i going to have to roll back my python install?

devinrsmith commented 6 months ago

I'm not sure - I've not run SNAP before. You may want to open an issue with them. We do publish Windows wheels that may work https://pypi.org/project/jpy/#files. https://senbox.atlassian.net/wiki/spaces/SNAP/pages/2499051521/Configure+Python+to+use+the+new+SNAP-Python+esa+snappy+interface+SNAP+version+10 says only python versions 2.7, 3.3 to 3.10 are supported, so maybe that's the issue if you are trying to use Python 3.11? For jpy, we publish wheels for 3.6-3.11 (3.12 to come shortly).

mczakk commented 6 months ago

I had this code working yesterday:

import pandas as pd
import re
import numpy as np
import time
import os
from datetime import datetime

from snappy import ProductIO
from snappy import GeoPos
from snappy import  PixelPos

# Specify the directory path
directory_path = r"E:\PhD\gis_stuff\Lai_Processed"

# Get a list of all files in the directory
file_list = os.listdir(directory_path)

# Filter files with the .dim extension
dim_files = [file for file in file_list if file.endswith(".dim")]

# Iterate through each .dim file
for dim_file in dim_files:
    # Construct the full path to the file
    product_path = os.path.join(directory_path, dim_file)
    # Measure execution time amd print start time
    start_time = time.time()
    # Get the current time
    current_time = datetime.now()

    # Format and print the current time
    formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
    print("File start time:", formatted_time)
    #product_path = r"E:\PhD\gis_stuff\Lai_Processed\Subset_S2A_MSIL2A_20180420T112121_N0207_R037_T30UVD_20180420T132427_resampled_BandMath.dim"
    product = ProductIO.readProduct(product_path)

    # Define the list of bands to retrieve
    bands_to_retrieve = ['lai', 'ndvi', 'fapar', 'fcover', 'gndvi', 'gci', 'EVI', 'EVI2', 'lai2']

    # Create an empty DataFrame to store the data
    data = {'Date': [], 'X': [], 'Y': [], 'Latitude': [], 'Longitude': []}
    for band in bands_to_retrieve:
        data[band] = []

    df = pd.DataFrame(data)

    # Extract date from the filename
    match = re.search(r'L2A_(.*?)T', product_path)
    if match:
        date = match.group(1)
    else:
        date = None

    # Get the width and height of the product
    width =10# product.getSceneRasterWidth()
    height =10# product.getSceneRasterHeight()

    # Get geocoding
    gc = product.getSceneGeoCoding()

    # Initialize arrays to store band values
    band_arrays = {band_name: np.zeros((height, width), dtype=np.float32) for band_name in bands_to_retrieve}

    for band_name in bands_to_retrieve:
        band = product.getBand(band_name)
        band.readPixels(0, 0, width, height, band_arrays[band_name])

    # Iterate over each pixel
    for y in range(height):
        for x in range(width):
            #print(x,y)
            # Get geo-coordinates for each pixel
            geoPos = gc.getGeoPos(PixelPos(x, y), None)
            lat = geoPos.getLat()
            lon = geoPos.getLon()

             # Retrieve values for each band
            band_values = {band_name: band_arrays[band_name][y, x] for band_name in bands_to_retrieve}

            # Append data to the DataFrame
            df = df.append({'Date': date, 'X': x, 'Y': y, 'Latitude': lat, 'Longitude': lon, **band_values}, ignore_index=True)

    # Save the DataFrame to a CSV file
    dpath=r"E:\PhD\gis_stuff\Lai_Processed\Processed_csv_files"
    csv_filename = os.path.join(dpath, f"{date}.csv")

    df.to_csv(csv_filename, index=False)  
    # Calculate and print the execution time
    end_time = time.time()
    execution_time = end_time - start_time
    print(f"Execution time: {execution_time} seconds")   

but now when i try and run it it returns the error:

RuntimeError: jpy: internal error: static method not found: unwrapProxy(Ljava/lang/Object;)Lorg/jpy/PyObject;

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\mtc20tfq\OneDrive - Bangor University\uni phd\gis stuff\Thonny_scripts\get_lai.py", line 15, in <module>
    from snappy import ProductIO
  File "c:\users\mtc20tfq\appdata\local\programs\python\python36\lib\site-packages\snappy\__init__.py", line 236, in <module>
    jpy.create_jvm(options=_get_snap_jvm_options())
SystemError: <built-in function create_jvm> returned a result with an error set

I have uninstalled and re-installed java, python, jpy, and esa snap countless times, but it still will not run, i have followed the instructions in here, as i am using snap 9.0 :https://senbox.atlassian.net/wiki/spaces/SNAP/pages/50855941/Configure+Python+to+use+the+SNAP-Python+snappy+interface+SNAP+versions+9 but i constantly get the same error. iam running java version: java 21.0.1 2023-10-17 LTS Java(TM) SE Runtime Environment (build 21.0.1+12-LTS-29) Java HotSpot(TM) 64-Bit Server VM (build 21.0.1+12-LTS-29, mixed mode, sharing)

It would appear that the error is somewhere within jpy, but I am at a loss toi understand where

devinrsmith commented 6 months ago

This error appears be caused by a mismatch between your python jpy version and your java jar jpy version. Ideally, the two versions should be in-sync.

You can also turn on more jpy debugging before you run your script:

import jpy
jpy.diag.flags = jpy.diag.F_ALL

and that may give some more details.

https://repo1.maven.org/maven2/org/jpyconsortium/jpy/ https://pypi.org/project/jpy/#history

mczakk commented 6 months ago

i added that to my code, and ran in spyder, the only thing i got was a File "C:\Users\mtc20tfq\AppData\Local\Programs\Python\Python36\lib\site-packages\snappy__init__.py", line 236, in jpy.create_jvm(options=_get_snap_jvm_options())

RuntimeError: jpy: failed to create Java VM i then tried the same code in thonny, and got

%Run get_lai.py Traceback (most recent call last): File "C:\Users\mtc20tfq.snap\snap-python\snappy\get_lai.py", line 7, in import jpy ImportError: DLL load failed: The specified module could not be found.

but the really bizarre thing is that i moved the 'get_lai.py' file into this folder on my computer: 'C:\Users\mtc20tfq.snap\snap-python\snappy' and ran it from the command line using python, and it appears to be running successfully (admittedl;y wont know that for sure ofr 60 hours) I am completely confused!!

mczakk commented 6 months ago

ok, i removed the import jpy.... lines from the code in the snappy folder and tried to run it using Thonny, and it worked, but still doesnt in spyder??

devinrsmith commented 6 months ago

Can you print out the contents of _get_snap_jvm_options()?

mczakk commented 6 months ago

Will do, but probly after Xmas, as the script is running now and will be for the next few days I think. How do I get it? Print(_get_snap_jvm_options())?