This code works and does exactly as I want, but I am wondering if/how it could be done faster/more efficiently? This runs quickly on a demo file, but bogs down when I introduce much larger files (3-100GB).
Premise:
Take a large list of latitudes and longitudes remove the negative values (WGS1984) and put them into a North, South, East, West space. Eliminate decimal places.
Use a shortened version of latitude and longitudes for directory creation.
Code:
import numpy as np
import pandas as pd
import h5py
hdf_file = '/path/to/file/lat_lon.h5'
ndir_root = '/output/path/where/directories/are/created/'
# split the file into two chunks to avoid memory error.
chunks = [0, 50, 100]
# open chunked file
for half in range(len(chunks) - 1):
hf = h5py.File(hdf_file, 'r')
print 'file loaded. reading meta lat/lon.'
meta_latitude = hf['latitude'][chunks[half]:chunks[half + 1]]
meta_longitude = hf['longitude'][chunks[half]:chunks[half + 1]]
meta_df = pd.DataFrame({'longitude': meta_longitude, 'latitude': meta_latitude})
print meta_df
# create an empty list to store coordinates
lat_list = []
lon_list = []
for lat in meta_df['latitude']:
lat_list.append(lat)
for lon in meta_df['longitude']:
lon_list.append(lon)
# take coordinates from list and convert to a numpy array
latitude_list = np.asarray(lat_list, dtype=float)
longitude_list = np.asarray(lon_list, dtype=float)
# convert numpy array to a string
lat_list_str = latitude_list.astype(str)
# make copies to avoid contamination in for loop processing
lat_list_str_copy_1 = lat_list_str
lat_list_str_copy_2 = lat_list_str
# remove negative values, add North, South, East West.
letter = set('-')
for lat_check in lat_list_str_copy_1:
if letter & set(lat_check):
lat_list_str3 = [i.replace('-', 'S') for i in lat_list_str_copy_1]
else:
lat_list_str3 = ['N' + lat_addchar for lat_addchar in lat_list_str_copy_1]
# remove the decimal for directory creation
lat = [i.replace('.', '') for i in lat_list_str3]
lon_list_str = longitude_list.astype(str)
# make copies to avoid contamination in for loop processing
lon_list_str_copy_1 = lon_list_str
lon_list_str_copy_2 = lon_list_str
for lon_check in lon_list_str_copy_1:
if letter & set(lon_check):
lon_list_str3 = [i.replace('-', 'W') for i in lon_list_str_copy_1]
else:
lon_list_str3 = ['E' + lon_addchar for lon_addchar in lon_list_str_copy_1]
lon = [i.replace('.', '') for i in lon_list_str3]
for i in lat:
if len(i) < 4:
i = i + '0'
for i in lon:
if len(i) < 5:
i = '0' + i
meta_df['y'] = lat
meta_df['x'] = lon
meta_df['lon_list_str'] = lon_list_str
meta_df['lat_list_str'] = lat_list_str
# convert to int and take only values to left of decimal as folder name
short_lon_int = [i.split('.')[0] for i in lon_list_str_copy_2]
short_lat_int = [i.split('.')[0] for i in lat_list_str_copy_2]
short_lon = map(int, short_lon_int)
short_lat = map(int, short_lat_int)
for i in short_lat:
if i % 3 > 0:
i -= 1
for i in short_lon:
if i % 3 > 0:
i -= 1
short_lat_str = map(str, short_lat)
short_lon_str = map(str, short_lon)
for lat_check in short_lat_str:
if letter & set(lat_check):
short_lat_int_final = [i.replace('-', 'S') for i in short_lat_str]
else:
short_lat_int_final = ['N' + lat_addchar for lat_addchar in short_lat_str]
for lon_check in short_lon_str:
if letter & set(lon_check):
short_lon_int_final = [i.replace('-', 'W') for i in short_lon_str]
else:
short_lon_int_final = ['E' + lon_addchar for lon_addchar in short_lon_str]
for a, b in zip(short_lon_int_final, short_lat_int_final):
ndir = a + b
print ndir
if not os.path.exists(ndir_root + ndir):
os.mkdir(ndir_root + ndir)
Please let me know if anything needs clarification. This is a smaller chunk out of a larger script so some variables appear undefined.
Edit: Per comment requests a sample file has been linked. link to hdf file with lat/lon data.
meta_df
object, as well as an example input file, so that we can try out your program without errors? \$\endgroup\$