#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Sat Feb 13 12:35:18 EST 2021 if __name__ == '__main__': from misc.timer import Timer tt = Timer(f'start {__file__}') import sys, os.path, os, glob, datetime import xarray as xr, numpy as np, pandas as pd #import matplotlib.pyplot as plt #more imports # if __name__ == '__main__': tt.check('end import') # #start from here #if True: def nc2csv(ifile): #ifile = 'chirps.precip.daily.China.subregion.2009-2014.nc' da = xr.open_dataarray(ifile).load() units = da.attrs['units'].replace('/', '_per_').replace(' ', '') ofile = ifile.replace('.nc', f'.{units}.csv') if os.path.exists(ofile): print('[exists]:', ofile) return da.to_pandas().to_csv(ofile) print('[saved]:', ofile) if __name__ == '__main__': #from wyconfig import * #my plot settings #extract all nc files and convert them to csv files if len(sys.argv)>1: ifiles = sys.argv[1:] else: ifiles = glob.glob('*.nc') ifiles.sort() for ifile in ifiles: print('[ifile]:', ifile) nc2csv(ifile) print() """ figname = __file__.replace('.py', f'_{tt.today()}.png') if len(sys.argv) > 1 and sys.argv[1] == 'savefig': plt.savefig(figname) print('[saved]:', figname) """ tt.check(f'**Done**') #plt.show()