# Verifying formula that will convert longitude 0-360 to -180 to 180?

I have a netCDF file that contains climate data in a Gaussian grid. The longitudes are given from 0 to 360. I need to convert this to -180 to 180.

Is this the correct formula -

If longitude is greater than 180 subtract 360

I am looking to do this in Fortran.

## 2 Answers

Here is an example for fortran:

Variables in explanation:

``````long1 is the longitude varying from -180 to 180 or 180W-180E
long3 is the longitude variable from 0 to 360 (all positive)
``````

To convert longitude from (0-360) to (-180 to 180)

`````` Matlab and fortran long1=mod((long3+180),360)-180

Matlab long1=rem((long3+180),360)-180

Fortran long1=modulo((long3+180),360)-180
``````

To convert longitude from (-180 to 180) to (0-360)

`````` Matlab lon3=mod(lon1,360)

Fortran lon3=modulo(lon1,360)
``````

The “mod” function is Fortran is equivalent to “rem” function in Matlab.

The “modulo” function in Fortran is equivalent to “mod” function in Matlab.

http://vikas-ke-funde.blogspot.com/2010/06/convert-longitude-0-360-to-180-to-180.html

If you know for certain that your input longitudes range between `0` and `360`, then yes: just subtract `360` from values that are greater than (or equal to) `180`.

If there is any possibility of "crazy" values (less than `-180` or greater than `540`) you might consider using the `IEEE_REM` function:

``````fixed_longitude = IEEE_REM(longitude, 360.0)
``````

This will gracefully handle any longitude value with no other logic required.