'Python - Making For loops to 1x1 degree grids Only for seas

I would like to do some meteorological calculation in the sea area. What I'm trying to do is making calculation for every 1x1 degree grid cells. like,

Go to the grid (to start somewhere with coordinates)

Make Calculations.

Go to another grid

Make calculations..

so on...

I made it with square but it doesn't help me to skip land areas.

I would like to make only for seas. when the coordinate falls on the land areas it has to skip (I don't know how). and move on to the next grid.

I can appreciate any tips from you guys.

an example

edit : Is there a any possibility to determining all the coordinates and make with them a database or some kind of lists. and when the calculation begins the code runs for loop only in the list? honestly I don't know do it as well.

Sorry for mentioning the data. I've downloaded ECMWF's meteorological data model called ERA5 and the data looks like this.

data

The code below is my first trial actually its working for a square

latt = [48,49,50,51] lonn = [38,39,40,41]

for the information this list in this code represents a 4x4 matrix the numbers inside doesn't mean anything actually.

import math
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from windrose import WindroseAxes
import matplotlib.cm as cm
import timeit
from matplotlib.gridspec import GridSpec
from matplotlib.ticker import PercentFormatter
import seaborn as sns
import matplotlib as mpl

data = pd.read_csv("/opt/local/var/macports/software/A.xlsx", index_col=0, parse_dates=True, header=None)
df = data.reset_index()
df.rename(columns={0:"Date", 1:"Lon", 2:"Lat", 3:"Value"}, inplace=True)
df["Value"] = (df["Value"]-273.14)

#Coordinate starting points.
n = 48
y = 49
z = 38
t = 39

#aylar
aylar = ["OCAK","ŞUBAT","MART","NİSAN","MAYIS","HAZİRAN","TEMMUZ","AĞUSTOS","EYLÜL","EKİM","KASIM","ARALIK"]
kaylar = ["oca","sub","mar","nis","may","haz","tem","agu","eyl","eki","kas","ara"]
ay = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
loop = ["a","b","c","d","e","f","g","h","i","j","k","l"]

mpl.rcParams['agg.path.chunksize'] = 10000

latt = [48,49,50,51]
lonn = [38,39,40,41]
j=ay[0]

for h in range(len(latt)):
    for g in range(len(lonn)):
        #This is where i filter my first grid (A1)
        s = df[(df['Lat'] >= n) & (df['Lat'] <= y)]
        s = s[(s['Lon'] >= z) & (s['Lon'] <= t)]
        
        m = 0
        x = 1
        r = kaylar[m]
        p = aylar[m]
        
        for i in range(len(loop)):
            
            #plots#
            
            plt.savefig("/Users/muratoksuz/Desktop/URUN/KAR/"+str(r)+"/K{j}/sic/K{j}_sicaklik.jpeg".format(j=j),dpi=500, bbox_inches="tight")
            m += 1
            x += 1
            
        j += 1
        
        n += 1
        y += 1
        
    z += 1
    t += 1
    n = 48
    y = 49

That code starts from given coordinates and make a calculation for whole months and saves figures to different folders in my pc. and goes to the another grid to do same calculation for that grid. K or A is just a name for grids.

This is the folder structure.

And this is the wished result.

Here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source