#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Tue May 16 10:52:37 EDT 2023 if __name__ == '__main__': import sys,os from misc.timer import Timer tt = Timer(f'[{os.getcwd()}] start ' + ' '.join(sys.argv)) import sys, os.path, os, glob, datetime import xarray as xr, numpy as np, pandas as pd, matplotlib.pyplot as plt #more imports wython = '/tigress/wenchang/wython' if wython not in sys.path: sys.path.append(wython); print('added to python path:', wython) #from misc import get_kws_from_argv # if __name__ == '__main__': tt.check('end import') # #start from here csvfile = 'african_cities_latlon.csv' ncfile = csvfile.replace('.csv', '.nc') if os.path.exists(ncfile) and 'o' not in sys.argv: print('[exists]:', ncfile) sys.exit() df = pd.read_csv(csvfile, encoding='unicode_escape') #default encoding failed #create dataarray of lon/lat using location ({City}_{Country}) as coords locations = [f'{r[1].City.strip()} ({r[1].Country.strip()})' for r in df.iterrows()] lat = xr.DataArray(df.Latitude, dims='location', coords=[locations,]) lon = xr.DataArray(df.Longitude, dims='location', coords=[locations,]) lon = lon.where(lon>0, other=lon+360) #lon range from -180/180 to 0/360 #convert to dataset and save it ds = xr.Dataset(dict(lat=lat, lon=lon)) ds.to_netcdf(ncfile) print('[saved]', ncfile) if __name__ == '__main__': #from wyconfig import * #my plot settings #savefig if 'savefig' in sys.argv or 's' in sys.argv: figname = __file__.replace('.py', f'.png') if 'overwritefig' in sys.argv or 'o' in sys.argv: wysavefig(figname, overwritefig=True) else: wysavefig(figname) tt.check(f'**Done**') print() if 'notshowfig' in sys.argv or 'n' in sys.argv: pass else: if 'plt' in globals(): plt.show()