How to fix AIS COG Values

402 Views Asked by At

I am currently working with AIS dataset containing fields MMSI, Timestamp, LAT, LON, SOG, COG, etc. Here, COG is course over ground, and supported COG value ranges from 0 to 360 degrees. But the dataset contains negative values in some rows. I like to know is there any formula or rule to convert it into 0-360. Since I am working with python, if someone knows how to do it in Python would be appreciated but any formula could work. For convenience, I have attached a screenshot of a sample dataset.Sample AIS Dataset

2

There are 2 best solutions below

2
On

To convert an angle to between 0 and 360, find the modulus of dividing by 360:

df["COG_0_to_360"] = df["COG"]%360

examples:

-10%360 == 350
10%360 == 10
0
On

Here is my final code:

def COG_0_To_360(cog):
    cog = np.fmod(cog, 360.0)
    cog = np.where(cog < 0.0, cog + 360.0, cog)
    return np.abs(cog)

df['COG'] = COG_0_To_360(df['COG'])

Example:

values = [-170, -10, -390, -355, 250, -440]
print(COG_0_To_360(values))

[190  350  330  5  250  280]