Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read swath in loop #2703

Closed
kameshvinjamuri opened this issue Dec 21, 2023 · 8 comments
Closed

read swath in loop #2703

kameshvinjamuri opened this issue Dec 21, 2023 · 8 comments

Comments

@kameshvinjamuri
Copy link

kameshvinjamuri commented Dec 21, 2023

hi I am reading slstr l1b product, using satpy by the following

slstr_sawth = 'directory of all folders containing swath folders'
if __name__ == '__main__':
    files = find_files_and_readers(base_dir=slstr_swath, reader='slstr_l1b', start_time=datetime.datetime(2017, 6, 2), end_time=datetime.datetime(2017, 6, 3))
    scn = Scene(filenames=files, reader='slstr_l1b')

when I do the above, I am reading all swath folders on the dates, but I want to iterate through the loop through each folder and extract outpout, and save. is there any easy way to find the swath folders one by one and save the output?

@djhoese
Copy link
Member

djhoese commented Dec 21, 2023

Could you describe your directory structure more? It sounds like all you want is:

from glob import glob

for swath_dir in glob("dir_of_all_swaths/*"):
    files = find_files_and_readers(base_dir=swath_dir, reader="slstr_l1b")
    scn = Scene(filenames=files, reader="slstr_l1b")

Note I have no experience with this reader and its files so let me know if I'm completely wrong. Most people familiar with this reader might have started their holidays already though so we might have to wait a while for a better answer.

@kameshvinjamuri
Copy link
Author

hi, the code you mention is also effective for one swath. Maybe I should explain the structure bit better...

the swath files are as follows ...

/home/usr/slstr/s3a_20170602_unique_id/files.nc
/home/usr/slstr/s3a_20170602_unique_id/files.nc
/home/usr/slstr/s3a_20170602_unique_id/files.nc
/home/usr/slstr/s3a_20170602_unique_id/files.nc

the codes above take base_dir which is till /home/usr/slstr/ but unfortunately not /home/usr/slstr/s3a_20170602_unique_id/

hope this is not reader specific

@djhoese
Copy link
Member

djhoese commented Dec 21, 2023

If the glob string in my code is /home/usr/slstr/* does that not work? Wouldn't that make each iteration operate on one swath directory at a time?

@kameshvinjamuri
Copy link
Author

If the glob string in my code is /home/usr/slstr/* does that not work? Wouldn't that make each iteration operate on one swath directory at a time?

not exactly! the base_dir=swath_dir working with /home/usr/slstr/ but not /home/usr/slstr/s3a_20170602_unique_id/

@simonrp84
Copy link
Member

I'm not sure I understand the question fully, but would something like this be what you need?

slstr_swath = 'directory of all folders containing swath folders'
if __name__ == '__main__':
    dir_list = glob(f'{slstr_swath}/*')
    for cur_dir in dir_list:
       if not os.path.isdir(cur_dir):
           continue
       files = find_files_and_readers(base_dir=cur_dir, reader='slstr_l1b', start_time=datetime.datetime(2017, 6, 2), end_time=datetime.datetime(2017, 6, 3))
       scn = Scene(filenames=files, reader='slstr_l1b')

@kameshvinjamuri
Copy link
Author

I'm not sure I understand the question fully, but would something like this be what you need?

slstr_swath = 'directory of all folders containing swath folders'
if __name__ == '__main__':
    dir_list = glob(f'{slstr_swath}/*')
    for cur_dir in dir_list:
       if not os.path.isdir(cur_dir):
           continue
       files = find_files_and_readers(base_dir=cur_dir, reader='slstr_l1b', start_time=datetime.datetime(2017, 6, 2), end_time=datetime.datetime(2017, 6, 3))
       scn = Scene(filenames=files, reader='slstr_l1b')

hi, not exactly!!
the base_dir in find_files_and_readers is only happy with one folder outside the swath directories like ~/slstr_swath/ but not happy with ~/slstr_swath/uniq_swath_id

@djhoese
Copy link
Member

djhoese commented Dec 21, 2023

Are your "unique_id" directories the directories ending in ".SEN3"? If so, then I think you can avoid using find_files_and_readers and instead do:

from glob import glob

for swath_dir in glob("dir_of_all_swaths/*.SEN3"):
    scn = Scene(filenames=glob(os.path.join(swath_dir, "*.nc")), reader="slstr_l1b")

That is, I expect swath_dir to be the path that ends in ".SEN3" which should mean the result of the filenames= glob be all of the .nc files in that swath directory.

@kameshvinjamuri
Copy link
Author

kameshvinjamuri commented Dec 21, 2023

hi, @djhoese that does work, thanks and cheers both @simonrp84 @djhoese

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants