Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Save 2 variables from an array to .csv

I have 2 coordinate variables lon and lat

(array([-50.940605, -37.424145, -41.501717, -37.98916 , -60.632664,
       -38.158283, -58.0372  , -39.06596 , -60.815792, -49.303684,
       -38.46074 , -50.979015, -38.479317, -58.656593, -38.40213 ,
       -38.007423, -43.785126, -57.75844 , -42.781937, -74.90217 ,
       -34.498913, -49.227158, -96.485504, -72.128716, -39.414085],
      dtype=float32), array([-15.90009   , -15.913551  , -15.7658    , -16.47591   ,
        -4.1940403 , -14.563205  ,  -6.032389  , -14.852597  ,
        -4.24735   ,  15.848547  , -14.71412   , -17.052591  ,
       -14.079368  ,  -5.9365387 , -14.50551   , -16.48979   ,
       -16.616753  ,  -0.23096395, -15.882113  ,  -5.0795455],
      dtype=float32))

I want to short them and save as a .csv file like this:

-50.940605,-15.90009
-37.424145,-15.913551 
 -41.501717-15.7658 
 -37.98916,-16.47591
 -60.632664-4.1940403 
 -38.158283-14.563205
.... and so on

I’m using the commands

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

coord_file = (flag_lon.data[0:100], flag_lat.data[0:100]) # they are variables extracted from a NetCDF fila

np.savetxt('coord_file.csv', coord_file, delimiter=",", fmt="%s")
 
print(open("coord_file.csv").read()) 

But it returns:

-50.940605,-37.424145,-41.501717,-37.98916,-60.632664,-38.158283,-58.0372,-39.06596,-60.815792,-49.303684,-38.46074,-50.979015,-38.479317,-58.656593,-38.40213,-38.007423,-43.785126
-15.90009,-15.913551,-15.7658,-16.47591,-4.1940403,-14.563205,-6.032389,-14.852597,-4.24735,15.848547,-14.71412,-17.052591,-14.079368,-5.9365387,-14.50551,-16.48979,-16.616753,-0.23096395,-15.882113,-5.0795455,-20.313402,15.771035,-7.3693175,-7.8538485,-14.27069,-16.976015,

How can I arrange them so that they are as pair lon,lat?

>Solution :

You can try like this

import numpy as np

lon = np.array([-50.940605, -37.424145, -41.501717, -37.98916, -60.632664])
lat = np.array([-15.90009, -15.913551, -15.7658, -16.47591, -4.1940403])
coords = np.column_stack((lon, lat))
np.savetxt('coordinates.csv', coords, delimiter=',', fmt='%.6f')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading