def get_depth_mask(plon,plat,**kwargs): ncfil = 'ETOPO/ETOPO_Arctic_10arcmin.nc' # ~20km # ncfil = 'ETOPO/ETOPO_Arctic_5arcmin.nc' # ~10km # ncfil = 'ETOPO/ETOPO_Arctic_2arcmin.nc' # ~4km # ncfil = 'ETOPO/ETOPO_Arctic_1arcmin.nc' # ~2km #interp depth to plon,plat (centres of wim grid cells) nci = mr.nc_getinfo(ncfil) depth = nci.interp2points('z', (plon,plat),**kwargs) # masked array landmask = np.ones(depth.shape) good = np.logical_not(depth.mask) data = depth.data[good] lmgood = landmask[good] lmgood[data<0.] = 0. # z<0 is water landmask[good] = lmgood return depth,landmask
def __init__(self,datestr=None,fctype='ice_only'): fmt = '%Y%m%d' if datestr is not None: self.startdate = dtm.strptime(datestr,fmt) else: self.startdate = dtm.today() datestr = self.startdate.strftime(fmt) SR = os.getenv('SWARP_ROUTINES') tfil = SR+'/forecast_scripts/' if fctype=='ice_only': nc0 = 'SWARPiceonly_forecast' elif fctype=='wavesice': nc0 = 'SWARPwavesice_forecast' elif fctype=='wavesice_ww3arctic': nc0 = 'SWARPwavesiceWW3_forecast' else: raise ValueError('Invalid value of fctype: '+fctype) ncbase = nc0+'_start'+datestr+'T000000Z' fcdir = '/work/timill/RealTime_Models/results/TP4a0.12/'+fctype+'/'+datestr+'/final_output' ncfil = fcdir+'/'+ncbase+'.nc' print('\nOpening '+ncfil+' ...\n') nci = Mr.nc_getinfo(ncfil) # self.netcdf_info = nci self.basename = ncbase self.datetimes = [] for tval in nci.timevalues: dtmo = nci.timeval_to_datetime(tval) self.datetimes.append(dtmo) return
FCdir = os.getenv('TP4_REALTIME_RES') + '/ice_only/' + yday if 1: # binary file jday = int(dtm.strftime('%j')) - 1 # dts = '%4.4i_%3.3i' % (dtm.year, jday) ddir = FCdir + '/bin/' afil = ddir + '/TP4archv.' + dts + '_120000Z.a' print(afil) fobj = MR.HYCOM_binary_info(afil) elif 0: # single-record netcdf ddir = FCdir + '/netcdf/' dts = yday + '_%2.2i' % (dtm.hour) ncfil = ddir + '/TP4archv_' + dts + '.nc' print(ncfil) fobj = MR.nc_getinfo(ncfil) vobj = 'fice' elif 1: # multi-record netcdf ddir = FCdir + '/final_output/' dts = yday + 'T000000Z' ncfil = ddir + '/SWARPiceonly_forecast_start' + dts + '.nc' print(ncfil) fobj = MR.nc_getinfo(ncfil) vobj = 'fice' idx = 2 else: # file_object_list pattern = 'archv' if 1: # list of binary files
print "fdir: ", fdir if not os.path.exists(fdir): os.makedirs(fdir) vo = mr.make_plot_options('hice', ice_mask=True) vo2 = mr.make_plot_options('fice', ice_mask=True) # fice vs OSI-SAF print "afil: ", afil hi = mr.HYCOM_binary_info(afil) dtm = hi.datetime cdate = dtm.strftime('%Y%m%d') figname = 'comp_OSISAF_' + hycomreg + '_' + cdate + '.png' hi.compare_ice_edge_obs(figname=figname, show='False', HYCOMreg=hycomreg) # only OSI-SAF fice osfil = '' + osdir + 'ice_conc_nh_polstere-100_multi_' + cdate + '1200.nc' print "osfil: ", osfil nci = mr.nc_getinfo(osfil) figname = 'OSISAF_fice_' + hycomreg + '_' + cdate + '.png' #nci.plot_var(vo,show=showfigs,figname=figname) nci.make_png(vo2, show='False', figdir=fdir, HYCOMreg=hycomreg) # plot hice #nc=mr.nc_getinfo(ncall) figname = 'TOPAZ_hice_' + hycomreg + '_' + cdate + '.png' hi.make_png(vo, clim=[0, 4], show='False', figdir=fdir, HYCOMreg=hycomreg) # plot SMOS ice thickness smfil = '' + smdir + 'SMOS_Icethickness_north_' + cdate + '.nc' print "smfil: ", smfil nci = mr.nc_getinfo(smfil) figname = 'SMOS_hice_' + hycomreg + '_' + cdate + '.png' vo = mr.make_plot_options('sea_ice_thickness', ice_mask=True)
# define netcdf file ##################################################################################### # cday = '20150707' # cyear = '2015' # pday = '20150706' # pday2 = '20150705' # tday = '2015-07-07' # yday2 = '2015-07-05' wmsc = '/work/shared/nersc/msc/WAMNSEA/' + cyear + '/forecasts/' ncfil = wmsc + 'wam_nsea.fc.' + cday + '.nc' # should be determined from today's date use "fc" if print_info: print('WAMNSEA file = ' + ncfil + '\n') # get info about nc file nci = Mrdg.nc_getinfo(ncfil) times = nci.timevalues # hours from 1st time in file Ntimes = len(times) if print_info: print('Time values (h):') print(times) print(' ') # get lon/lat and restrict to relevant area sswh = 'significant_wave_height' lon, lat = nci.get_lonlat() X, Y = bm(lon[:, :], lat[:, :], inverse=False) in_area = np.logical_and(X > bm.xmin, X < bm.xmax) in_area = np.logical_and(in_area, Y < bm.ymax) in_area = np.logical_and(in_area, Y > bm.ymin)
os.mkdir(odir) # ===================================================================================== TSfil = outdir + '/test_area.txt' w = open(TSfil, 'w') w.write('date\tmodel_area\tchart_area\t max_swh\n') w.close() # ======================================================================================================== if 1: # do computations vertices = [(-50, 50), (20, 50), (20, 84), (-50, 84)] # region of analysis (gre) dates = [] flist = os.listdir(chartdir) nci = mr.nc_getinfo(wavefile) wlon, wlat = nci.get_lonlat() #lon,lat of wave grid mask = GP.maskgrid_outside_polygon( wlon, wlat, vertices) # if True, these are wave points inside vertices for tf in flist: # print tf # if (tf != '201505101200_Greenland_WA_MIZpolys.txt'): # continue cdate = tf[0:8] t = datetime.datetime(int(cdate[0:4]), int(cdate[4:6]), int(cdate[6:8])) dates += [t] dates.sort() for day in dates:
aid.close() bid = open(outdir+'/'+gridname+'.b','w') write_bfile(bid,fields,nx,ny) bid.close() print("\n grid-files "+outdir+'/'+gridname+'.[a,b] saved\n') # =============================================================== # =============================================================== if TEST_PLOT>0: # plot grid outline on top of data from netcdf file import mod_reading as mr import fns_plotting as Fplt bmap = Fplt.start_HYCOM_map(HYCOMreg) print('Using '+ncfil) nci = mr.nc_getinfo(ncfil,lonlat_file=lonlat_file) print(' - contains:') print(nci.variables) po,bmap = nci.plot_var(vbl,bmap=bmap,show=False,time_index=time_index,date_label=dlabel,clabel=vbl) # plot new selection xyVerts = [] xyVerts.append((xmin,ymin)) xyVerts.append((xmin,ymax)) xyVerts.append((xmax,ymax)) xyVerts.append((xmax,ymin)) Verts = [] for x,y in xyVerts: lon,lat = mapx(x,y,inverse=True) Verts.append((lon,lat))
def hyc2proj_to_grib2(ncfil, grb2fil, KEEP_MASK=1): ncgv = m_rdg.nc_get_var ############################################################################################################# # read hyc2proj netcdf: ncinfo = m_rdg.nc_getinfo(ncfil) time_indices = range(ncinfo.number_of_time_records) vbl_list = ncinfo.variable_list # Open outfile before starting to encode messages f_out = open(grb2fil, 'wb') ################################################################################################################ for time_index in time_indices: for vbl_name in vbl_list: # encode all variables and all times as one message data = ncgv(ncfil, vbl_name, time_index) ncinfo.datatime = ncinfo.timevalues[time_index] ########################################################################################################## # INITIALISATION # INPUT 1) discipline code (pygrib/ncepgrib2.py, grib2message) discipline_code = 10 #Oceanographic Products > http://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_table0-0.shtml # INPUT 2) identification section: pygrib/g2clib.c ("unpack1" method) ident_sect = def_ident_sect(ncinfo.reftime, ncinfo.reftime_sig) # CALL Grib2Encode: grb_out = g2e(discipline_code, ident_sect) ########################################################################################################## ########################################################################################################## # DEFINE GRID: # INPUT 2) # grid definition template: # > http://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_temp3-20.shtml gdtnum, gdt = def_grid_template(ncinfo) # print('grid definition template: '+str(gdt)) # INPUT 1) # grid definition section: gdsinfo = def_gdsinfo(ncinfo, gdtnum) # CALL addgrid grb_out.addgrid(gdsinfo, gdt) ########################################################################################################## ########################################################################################################## # add product definition template, data representation template # and data (including bitmap which is read from data mask). # INPUT 1): pdtnum = 0 # product_definition_template_number > http://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_table4-0.shtml # INPUT 2): pdtmpl = def_prod_template(ncinfo, vbl_name) # INPUT 3): # data representation template number > http://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_table5-0.shtml if 1: drtnum = 0 # grid point data, simple packing else: drtnum = 40 # grid point data, jpeg 2000 compression # INPUT 4): # data representation template > http://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_table5-0.shtml drtmpl = def_datarep_template(drtnum) # INPUT 5) if KEEP_MASK == 1: data_arr = data[:, :] # masked array else: data_arr = data[:, :].data # array # CALL addfield: grb_out.addfield(pdtnum, pdtmpl, drtnum, drtmpl, data_arr) # finalize the grib message. grb_out.end() print('Writing grib message to ' + grb2fil + '\n') f_out.write(grb_out.msg) ########################################################################################################## f_out.close() return ncinfo
width = 1000.e3 # width of domain in m height = 1000.e3 # height of domain in m #) basemap corresponding to the projection bmap = Basemap(projection='stere',ellps=ellps,\ lon_0=lonc,lat_0=latc,lat_ts=lat_ts,\ width=width,height=height) ################################################################ # TODO Do we need this ? ################################################################ # wave data from ERA_Interim eraidir='/work/shared/nersc/ERA-I_0.125deg/' eraifile='SWH_'+str(year)+'.nc' ncfil=eraidir+eraifile nci=mr.nc_getinfo(ncfil) ################################################################ nci_nearestdate = nearestDate(nci.datetimes,pdate) rec=nci.datetimes.index(nci_nearestdate) print 'Nearest Date Time: ',nci.datetimes[rec].__str__(),' record: ',rec # TODO FIND CLOSEST TIME AND GET FILE ? # TODO LOOP OVER SOME PERIOD AROUND EVENT # GET TOPAZ DATA adir = '/home/nersc/timill/test_lucia/' afil = 'TP4archv_wav.2015_351_120000.a' hi = mr.HYCOM_binary_info(adir+afil) hidt = hi.datetime
if opt == '--FCtype': FCtype = arg if ncfile is None: raise ValueError('no netcdf file specified (use --ncfile=)') if FCtype is None: raise ValueError('forecast type not specified (use --FCtype=)') # ========================================================================== print(' ') print('Using netcdf file: ' + ncfile) print('Saving figures to: ' + outdir) print(' ') nci = Mr.nc_getinfo(ncfile) print('Start of forecast:') print(nci.datetimes[0]) print('\nEnd of forecast:') print(nci.datetimes[-1]) print('\n') dct = vars(nci.ncattrs) if 'area' in dct: HYCOMreg = dct['area'][:3] elif 'area_name' in dct: HYCOMreg = dct['area_name'][:3] else: HYCOMreg = 'TP4' if FCtype != "ice_only":
else: mdir = '/mnt/sda1/work/Model-outputs/thickness_comp_ifremer/TP4/2015_060' odir = '/mnt/sda1/work/Model-outputs/thickness_comp_ifremer/cersat' flist = mr.file_list(mdir, 'DAILY', '.a', gridpath=mdir + '/../topo') if 1: olist = ['cs2_smos_ice_thickness_20150302_20150308.nc'] else: olist = os.listdir(odir) # mlon,mlat = flist.get_corners() Mgrid = flist.create_ESMF_grid() Mfld = create_field(Mgrid, 'hice') ofil = olist[-1] nci = mr.nc_getinfo(odir + '/' + ofil) lon, lat = nci.get_lonlat() vhobs = 'analysis_thickness' hobs = nci.get_var(vhobs) centres, corners, areas = get_ESMF_inputs(lon, lat, ODLmap) # print(centres[0].shape) # print(corners[0].shape) # print(areas.shape) # print(mask.shape) Ogrid = EU.create_ESMF_grid(centres, corners, AREA=areas, MASK=hobs.values.mask[1:-1, 1:-1]) Ofld = create_field(Ogrid, 'hice')
from netCDF4 import Dataset as ncopen import numpy as np from datetime import datetime import pyproj from skimage import measure as msr TEST_PLOT = 1 STEP = 4 # don't take all points (eg STEP=4 gives every 4th) # stereographic projection map = pyproj.Proj(proj='stere', lon_0=-45, lat_0=90, lat_ts=70, ellps='WGS84') # file info fcdir = '/work/users/timill/RealTime_Models/results/TP4a0.12/ice_only' ncfil = fcdir + '/20170126/final_output/SWARPiceonly_forecast_start20170126T000000Z.nc' nci = MR.nc_getinfo(ncfil) print('\nReading\n' + ncfil) # global attributes glob_atts = nci.get_global_atts() del (glob_atts['history']) #print(glob_atts) #print('\n') # lon-lat LON, LAT = nci.get_lonlat() X, Y = map(LON, LAT) # time info time_index = 0 tval = nci.get_time()[time_index]
dres = '/work/users/timill/RealTime_Models/results_hindcasts/TP4a0.12/wavesice/analysis/binaries_all/DAILY/' # resdir='/work/timill/RealTime_Models/results_hindcasts/TP4a0.12/ice_only/'+wy+'_GOOD/'+wy+'_'+yd+'/binaries/' outdir = '/work/timill/RealTime_Models/results_hindcasts/Figures/' osdir = '/work/shared/nersc/msc/OSI-SAF/' + wy + '_nh_polstere/' afiles = ['' + dres + 'TP4DAILY_2015_347_2015_351.a'] #,\ # 'TP4DAILY_2016_010_2016_011.a',\ # 'TP4DAILY_2016_010_2016_011.a',\ # 'TP4DAILY_2016_010_2016_011.a' ] for afil in afiles: vo = mr.make_plot_options('hice', ice_mask=True) vo2 = mr.make_plot_options('fice', ice_mask=True) # fice vs OSI-SAF print "afil: ", afil hi = mr.HYCOM_binary_info(afil) dtm = hi.datetime cdate = dtm.strftime('%Y%m%d') figname = 'comp_OSISAF_' + hycomreg + '_' + cdate + '.png' hi.compare_ice_edge_obs(figname=figname, show='False', HYCOMreg=hycomreg) # only OSI-SAF fice osfil = '' + osdir + 'ice_conc_nh_polstere-100_multi_' + cdate + '1200.nc' print "osfil: ", osfil nci = mr.nc_getinfo(osfil) figname = 'OSISAF_' + hycomreg + '_' + cdate + '.png' #nci.plot_var(vo,show=showfigs,figname=figname) nci.make_png(vo2, clim=[0, 1], show='False', HYCOMreg=hycomreg) # hi.plot_var(vo,clim=[0,4])
from matplotlib import pyplot as plt from matplotlib import gridspec as GrdSpc import mod_reading as mr import numpy as np nci = mr.nc_getinfo('SWARPwavesice_WW3_forecast_start20151216T000000Z.nc') fig = plt.figure(figsize=(20, 20)) gs = GrdSpc.GridSpec(1, 2) gs.update(left=0.075, right=0.9, wspace=0.25) Hs_max = 6 dH = 1 Dmax_max = 250 dD = 50 reg = 'FR1' # 'gre' dtl = 2 for idx in range(nci.number_of_time_records): dto = nci.datetimes[idx] cdt1 = dto.strftime('%Y%m%dT%H%M%SZ') cdt2 = dto.strftime('%d %b %Y %H:%M') if 0: mon = dto.strftime('%b') cdt2 = cdt2.replace(mon, mon.upper()) if dtl == 0: fig.text(.5,.77,cdt2,\ horizontalalignment='center',\ fontsize=18,\ color='k')
os.mkdir(outdir) hmax = 4 po = FP.plot_object() shorts = ['BackThick' ,'CS2' ,'SMOS' ,'AnThick' ,'AnThickErr'] clabs = ['Background Thickness, m','Cryosat-2 thickness, m','SMOS thickness, m','Analysis Thickness, m','Analysis Thickness Error, m'] vlist = ['background_thickness' ,'cs2_thickness' ,'smos_thickness' ,'analysis_thickness' ,'analysis_thickness_err'] ts_data = {'RMSE_both_ice':[],'Bias_both_ice':[],\ 'RMSE_either_ice':[],'Bias_either_ice':[]} for i,ofil in enumerate(olist): # ======================================================================= # get arrays for later print(mdir+'/'+Mdir[i]) flist = mr.file_list(mdir+'/'+Mdir[i],'DAILY','.a',gridpath=gridpath) nci = mr.nc_getinfo(odir+'/'+ofil,lonlat_file=odir+'/'+lonlat_file) # nci.plot_var('analysis_thickness',clim=[0,hmax]) DOCB = False if i==0: olon,olat = nci.get_lonlat() oX,oY = ODLmap(olon,olat) mlon,mlat = flist.get_lonlat() mX,mY = ODLmap(mlon,mlat) DOCB = True hobs = nci.get_var('analysis_thickness') # ======================================================================= # ======================================================================= if PLOT_EG: # plot obs