0

I am working with NCDF files and I need to plot certain files only if they contain a certain location. I need some help creating an if statement that operates only if the corresponding elements of two list have a certain value. Basically, I only want to create a plot if the data goes near a certain region.

To make this simple:

For example, given I have two lists containing longitude and latitudes from two different files

file1:

lat1=[40,41,42,43,100]
lon1=[-70,-71,-72,-73,-100]

file2:

lat2=[40,11,12,13]
lon2=[21,22,-71,24]

I want to create an if statement that only operates (say append the string '***' to both lists) provided that at least one element (not necessarily all) from both the latitude list and the corresponding longitude list is within a certain range.In this case, I am looking for a longitude between -74 to -70 and a latitude between 40 to 42.

This means that file1 meets the if statement requirements since it contains the values from the corresponding elements of both lists but file2 does not. While file2 does contain both an element between 40 to 42 for lat2 and an element between -70 to -74 for lon2, they are not corresponding.

result:

file1:

lat1=[40,41,42,43,100,'***']
lon1=[-70,-71,-72,-73,-100,'***']

file2:

lat2=[40,11,12,13]
lon2=[21,22,-71,24]

Here is my incorrect code so far:

if any(40.5<x<42.3 for x in lats) and any(-74<y<-70 for y in lons):
    lats.append('***')
    lons.append('***')

This code does not work as it operates regardless if the values are corresponding, meaning file2 would also work for this statement since it contains values 40 and -71 even though they are not corresponding.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Bob
  • 115
  • 10

1 Answers1

1

You could zip your lists together:

if any(40.5 < x < 42.3 and -74 < y < -70 for x, y in zip(lats, lons)):
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264