csv script

How do I...?
Post Reply
User avatar
jconrado
Posts: 10
Joined: Thu Jan 28, 2016 5:16 pm

csv script

Post by jconrado »

Bob Carp,

You send me a script to write a csv file. I've always used IDL to read and plot my data and now I' starting to use Pyhton. In this code:

# Define the data. In this Field Selector
# window, choose your data and domain. This is
# dependent on the data already being loaded in.
data = selectData()

# JPython Methods to extract info from data
domain = getDomainSet(data[0])
lat, lon = getLatLons(domain)
val = getValues(data[0])

# At this point, we are working with arrays,
# verify this by running:
print 'lat: %s' %type(lat)
print 'lon: %s' %type(lon)
print 'val: %s' %type(val)

# Data currently in arrays, change to lists
# I believe vals is a multidimensional array
# which is why tolist() must be run twice.
lats = lat.tolist()
lons = lon.tolist()
vals = val.tolist()[0]
vals2 = vals.tolist()

# Now, lats, lons, and vals2 are all lists.
# You can verify this by running:
print 'lats: %s' %type(lats)
print 'lons: %s' %type(lons)
print 'vals2: %s' %type(vals2)

# Output to an already existing CSV file. Change
# csv_out to point at your file
import csv
csv_out = open('C:/Users/rcarp/test.csv', 'wb')
mywriter = csv.writer(csv_out)
rows = zip(lats, lons, vals2)
mywriter.writerows(rows)
csv_out.close()
print 'File written'


Where can I write a line (header) with the dimensions of my data. The size of data is 2852 columns by 1550 rows

Thanks,

Conrado
User avatar
bobc
Posts: 990
Joined: Mon Nov 15, 2010 5:57 pm

Re: csv script

Post by bobc »

Hi Conrado -

Here's a slightly modified version of the script I gave you earlier that includes a header that labels each of the three columns of data (Latitude, Longitude, Temperature):

Code: Select all

# Define the data. In this Field Selector
# window, choose your data and domain. This is
# dependent on the data already being loaded in.
data = selectData()

# JPython Methods to extract info from data
domain = getDomainSet(data[0])
lat, lon = getLatLons(domain)
val = getValues(data[0])

# Data currently in arrays, change to lists
# I believe vals is a multidimensional array
# which is why tolist() must be run twice.
lats = lat.tolist()
lons = lon.tolist()
vals = val.tolist()[0]
vals2 = vals.tolist()

# Output to an already existing CSV file. Change
# csv_out to point at your file
header = ['Latitude', 'Longitude', 'Temperature']
import csv
with open('C:/Users/rcarp/test2.csv', 'wb') as csv_out:
    mywriter = csv.writer(csv_out)
    mywriter.writerow(header)
    rows = zip(lats, lons, vals2)
    mywriter.writerows(rows)
print 'File written'

The changes from the previous script are:
  • I removed unnecessary print lines that indicate what the data types are along the way
  • I moved the section of writing the CSV file into a with block, meaning the CSV file closes as soon as it is written
  • I added steps to include a header. First, I defined 'header' to be a list of the column names. I then had to add the 'mywriter.writerow(header)' line to add the header to the file

It sounds like your case may be a bit more complex than mine with so many columns, but perhaps you can put all of your column names into a list (similar to what I did with my 'header' variable) and use the above example as a template.

Thanks -
Bob Carp
McIDAS Help Desk
User avatar
jconrado
Posts: 10
Joined: Thu Jan 28, 2016 5:16 pm

csv script

Post by jconrado »

Bob Carp,

Thanks for your help.

Gonrado
Post Reply