1

I have twenty .tif files which contain data from a globally mapped model. I am importing the files to python so I can rotate them, and make the .tif files Atlantic centered rather than Pacific centered. I can rotate individual files using the code below, however, I want to import all 20 files, rotate them all, and save them all in a new location using a loop rather than repeating the below lines of code for every single file.

TLDR: I need advice on how to loop the code below so can rotate 20 .tif files which are all in the same folder.

import rioxarray
rds_2000 = rioxarray.open_rasterio("path_to_tif")
rds_2000 = rds_2000.assign_coords(x=(((rds_2000.x + 180) % 360) - 180)).sortby('x')
rds_2000.rio.to_raster("path_to_newly_saved_tif)

enter image description here

1 Answer 1

2

Maybe something like this?

Assuming you write a function that captures the code you attached and returns the processed file

def process_function(filepath):
   do_something
   return processed_file

Then you can either created a dict or a list, if you want to access the files by filename or some other method dict is probably better.

files=os.listdir(files) #os some other method may be list comprehension
# alternative
# files=[file for file in os.listdir() if file.endswith(".tiff")
processed={}
for file in files:
   processed[file]=process_function(file)

now you can access the data by going processed[<filename>].

Hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.