- Timestamp:
- Apr 6, 2016, 4:34:35 PM (9 years ago)
- Location:
- trunk/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var.py
r693 r706 10 10 ## e.g. # nc_var.py -o filter_2dim -S '80,y,x,lon,lat' -f 'tahiti_5m_ll.grd' -v 'z' 11 11 ## e.g. # nc_var.py -o selvar -f /home/lluis/PY/met_em.d01.1979-01-01_00:00:00.nc -S 'west_east@XLONG_M,south_north@XLAT_M,num_metgrid_levels@int,Time@Times' -v TT,UU,VV,SKINTEMP 12 ## e.g. # nc_var.py -o 'Partialmap_Entiremap' -f carteveg5km.nc -S 'longitude,latitude,std,5000.,Goode,Goode_5km.nc' -v vegetation_map 12 13 13 14 from optparse import OptionParser -
trunk/tools/nc_var_tools.py
r703 r706 3 3 import os 4 4 import re 5 import numpy.ma as ma 5 6 6 7 main = 'nc_var_tools.py' … … 242 243 243 244 return newstring 245 246 def period_information(idate, edate, totunits): 247 """ Function to provide the information of a given period idate, edate (dates in [YYYY][MM][DD][HH][MI][SS] format) 248 [idate]= initial date of the period (in [YYYY][MM][DD][HH][MI][SS] format) 249 [edate]= end date of the period (in [YYYY][MM][DD][HH][MI][SS] format) 250 [totunits]= latest number of entire temporal units to compute: 'year', 'month', 'day', 'hour', 'minute', 'second' 251 252 resultant information given as ',' list of: 253 [dT]: sign of the temporal increment ('pos', 'neg') 254 [idate]: initial date as [YYYY]:[MM]:[DD]:[HH]:[MI]:[SS] 255 [edate]: end date as [YYYY]:[MM]:[DD]:[HH]:[MI]:[SS] 256 [Nsecs]: total seconds between dates 257 [Nyears]: Number of years taken as entire units ([iYYYY]0101000000 to [eYYYY+1]0101000000) 258 [ExactYears]: Exact year-dates of the period as a '@' separated list of [YYYY]0101000000 259 [Nmonths]: Number of months taken as entire units ([iYYYY][iMM]01000000 to [eYYYY][eMM+1]01000000) 260 [ExactMonths]: Exact mon-dates of the period as a '@' separated list of [YYYY][MM]01000000 261 [Ndays]: Number of days taken as entire units ([iYYYY][iMM][iDD]000000 to [eYYYY][eMM][eDD+1]000000) 262 [ExactDays]: Exact day-dates of the period as a '@' separated list of [YYYY][MM][DD]000000 263 [Nhours]: Number of hours taken as entire units ([iYYYY][iMM][iDD][iHH]0000 to [eYYYY][eMM][eDD][eHH+1]0000) 264 [ExactHours]: Exact hour-dates of the period as a '@' separated list of [YYYY][MM][DD][HH]0000 265 [Nminutes]: Number of minutes taken as entire units ([iYYYY][iMM][iDD][iHH][iMI]00 to [eYYYY][eMM][eDD][eHH][eMI+1]00) 266 [ExactMinutes]: Exact minutes-dates of the period as a '@' separated list of [YYYY][MM][DD][HH][MI]00 267 [Nsecs]: Number of minutes taken as entire units ([iYYYY][iMM][iDD][iHH][iMI][iS] to [eYYYY][eMM][eDD][eHH][eMI][eSE+1]) 268 [ExactSeconds]: Exact seconds-dates of the period as a '@' separated list of [YYYY][MM][DD][HH][MI][SS] 269 270 >>> period_information( '19760217083110', '19770828000000', 'month') 271 pos,1976:02:17:08:31:10,1977:08:28:00:00:00,48180530.0,19,19760201000000@19760301000000@19760401000000@ 272 19760501000000@19760601000000@19760701000000@19760801000000@19760901000000@19761001000000@19761101000000@ 273 19761201000000@19770101000000@19770201000000@19770301000000@19770401000000@19770501000000@19770601000000@ 274 19770701000000@19770801000000@19770901000000 275 """ 276 import datetime as dt 277 278 fname = 'period_information' 279 280 if idate == 'h': 281 print fname + '_____________________________________________________________' 282 print variables_values.__doc__ 283 quit() 284 285 expectargs = '[idate],[edate],[totunits]' 286 287 check_arguments(fname,str(idate)+','+str(edate)+','+str(totunits),expectargs,',') 288 289 # Checking length 290 Lidate = len(idate) 291 Ledate = len(edate) 292 293 if Lidate != Ledate: 294 print errormsg 295 print ' ' + fname + ': string dates of the period have different length !!' 296 print " idate: '" + idate + "' (L=", Lidate, ") edate: '" + edate + \ 297 "' (L=", Ledate, ')' 298 quit(-1) 299 else: 300 if Lidate != 14: 301 print errormsg 302 print ' ' + fname + ": wrong initial date: '" + idate + "' must " + \ 303 'have 14 characters!!' 304 print ' and it has:', Lidate 305 quit(-1) 306 if Ledate != 14: 307 print errormsg 308 print ' ' + fname + ": wrong end date: '" + edate + "' must " + \ 309 'have 14 characters!!' 310 print ' and it has:', Ledate 311 quit(-1) 312 313 # Checking for right order of dates 314 if int(idate) > int(edate): 315 print warnmsg 316 print ' ' + fname + ": initial date '" + idate + "' after end date '" + \ 317 edate + "' !!" 318 print " re-sorting them!" 319 i1 = edate 320 e1 = idate 321 idate = i1 322 edate = e1 323 oinf = 'neg' 324 else: 325 oinf = 'pos' 326 327 # Year, month, day, hour, minute, second initial date 328 iyrS = idate[0:4] 329 imoS = idate[4:6] 330 idaS = idate[6:8] 331 ihoS = idate[8:10] 332 imiS = idate[10:12] 333 iseS = idate[12:14] 334 335 oinf = oinf + ',' + iyrS+':'+imoS+':'+idaS+':'+ihoS+':'+imiS+':'+iseS 336 337 # Year, month, day, hour, minute, second end date 338 eyrS = edate[0:4] 339 emoS = edate[4:6] 340 edaS = edate[6:8] 341 ehoS = edate[8:10] 342 emiS = edate[10:12] 343 eseS = edate[12:14] 344 345 oinf = oinf + ',' + eyrS+':'+emoS+':'+edaS+':'+ehoS+':'+emiS+':'+eseS 346 347 idateT = dt.datetime(int(iyrS),int(imoS),int(idaS),int(ihoS),int(imiS),int(iseS)) 348 edateT = dt.datetime(int(eyrS),int(emoS),int(edaS),int(ehoS),int(emiS),int(eseS)) 349 350 # Seconds between dates 351 DT = edateT - idateT 352 Nsecs = DT.total_seconds() 353 354 oinf = oinf + ',' + str(Nsecs) 355 356 # Looking for number of years tacking exact beginning of the units [iYYYY]0101000000, [eYYYY+1]0101000000 357 ## 358 if totunits == 'year': 359 itime = int(iyrS) 360 if edate[4:15] != '0101000000': 361 etime = int(eyrS)+1 362 else: 363 etime = int(eyrS) 364 365 itimeT = dt.datetime(itime,1,1,0,0,0) 366 367 Nyears = 1 368 ExactYears = itimeT.strftime("%Y%m%d%H%M%S") 369 it = int(iyrS) 370 while it + 1 <= etime: 371 it = it + 1 372 Nyears = Nyears + 1 373 itT = dt.datetime(it,1,1,0,0,0) 374 ExactYears = ExactYears + '@' + itT.strftime("%Y%m%d%H%M%S") 375 376 oinf = oinf + ',' + str(Nyears) + ',' + ExactYears 377 378 # Looking for number of months tacking exact beginning of the units [iYYYY][iMM]01000000, [eYYYY][eMM+1]01000000 379 ## 380 if totunits == 'month': 381 itime = int(iyrS)*100 + int(imoS) 382 if edate[6:15] != '01000000': 383 emo1 = int(emoS)+1 384 if emo1 > 13: 385 eyr1 = str(int(eyrS) + 1) 386 emo1 = 01 387 else: 388 eyr1 = eyrS 389 390 else: 391 eyr1 = eyrS 392 emo1 = emoS 393 394 etime = int(eyr1)*100 + int(emo1) 395 itimeT = dt.datetime(int(iyrS),int(imoS),1,0,0,0) 396 397 Nmonths = 1 398 ExactMonths = itimeT.strftime("%Y%m%d%H%M%S") 399 it = itime 400 while it + 1 <= etime: 401 it = it + 1 402 Nmonths = Nmonths + 1 403 ityr = it/100 404 itmo = it - ityr*100 405 if itmo > 12: 406 ityr = ityr + 1 407 itmo = 1 408 it = ityr * 100 + itmo 409 410 itT = dt.datetime(ityr,itmo,1,0,0,0) 411 ExactMonths = ExactMonths + '@' + itT.strftime("%Y%m%d%H%M%S") 412 413 oinf = oinf + ',' + str(Nmonths) + ',' + ExactMonths 414 415 # Looking for number of days tacking exact beginning of the units [iYYYY][iMM][iDD]000000, [eYYYY][eMM][eDD+1]000000 416 ## 417 if totunits == 'day': 418 itime = dt.datetime(int(iyrS),int(imoS),int(idaS),0,0,0) 419 if edate[8:15] != '000000': 420 etime = dt.datetime(int(eyrS), int(emoS), int(edaS)+1,0,0,0) 421 else: 422 etime = edateT 423 424 Ndays = 1 425 ExactDays = itime.strftime("%Y%m%d%H%M%S") 426 it = itime 427 while it + dt.timedelta(days=1) <= etime: 428 it = it + dt.timedelta(days=1) 429 Ndays = Ndays + 1 430 ExactDays = ExactDays + '@' + it.strftime("%Y%m%d%H%M%S") 431 432 oinf = oinf + ',' + str(Ndays) + ',' + ExactDays 433 434 # Looking for number of hours tacking exact beginning of the units [iYYYY][iMM][iDD][iHH]0000, [eYYYY][eMM][eDD][iHH+1]0000 435 ## 436 if totunits == 'hour': 437 itime = dt.datetime(int(iyrS),int(imoS),int(idaS),int(ihoS),0,0) 438 if edate[10:15] != '0000': 439 etime = dt.datetime(int(eyrS), int(emoS), int(edaS), int(ehoS)+1,0,0) 440 else: 441 etime = edateT 442 443 Nhours = 1 444 ExactHours = itime.strftime("%Y%m%d%H%M%S") 445 it = itime 446 while it + dt.timedelta(hours=1) <= etime: 447 it = it + dt.timedelta(hours=1) 448 Nhours = Nhours + 1 449 ExactHours = ExactHours + '@' + it.strftime("%Y%m%d%H%M%S") 450 451 oinf = oinf + ',' + str(Nhours) + ',' + ExactHours 452 453 # Looking for number of minutes tacking exact beginning of the units [iYYYY][iMM][iDD][iHH][iMI]00, [eYYYY][eMM][eDD][iHH][iMI+1]00 454 ## 455 if totunits == 'minute': 456 itime = dt.datetime(int(iyrS),int(imoS),int(idaS),int(ihoS),int(imiS),0) 457 if edate[12:15] != '00': 458 etime = dt.datetime(int(eyrS), int(emoS), int(edaS), int(ehoS), int(emiS)+1,0) 459 else: 460 etime = edateT 461 462 Nminutes = 1 463 ExactMinutes = itime.strftime("%Y%m%d%H%M%S") 464 it = itime 465 while it + dt.timedelta(minutes=1) <= etime: 466 it = it + dt.timedelta(minutes=1) 467 Nminutes = Nminutes + 1 468 ExactMinutes = ExactMinutes + '@' + it.strftime("%Y%m%d%H%M%S") 469 470 oinf = oinf + ',' + str(Nminutes) + ',' + ExactMinutes 471 472 # Looking for number of seconds tacking exact beginning of the units [iYYYY][iMM][iDD][iHH][iMI][iSE], 473 # [eYYYY][eMM][eDD][iHH][iMI][iSE+1] 474 ## 475 if totunits == 'second': 476 itime = dt.datetime(int(iyrS),int(imoS),int(idaS),int(ihoS),int(imiS),int(iseS)) 477 print 'Lluis ', edate[12:15], '00' 478 if edate[12:15] != '00': 479 etime = dt.datetime(int(eyrS), int(emoS), int(edaS), int(ehoS), int(emiS), int(eseS)+1) 480 else: 481 etime = edateT 482 483 Nseconds = 1 484 ExactSeconds = itime.strftime("%Y%m%d%H%M%S") 485 it = itime 486 while it + dt.timedelta(seconds=1) < etime: 487 it = it + dt.timedelta(seconds=1) 488 Nseconds = Nseconds + 1 489 ExactSeconds = ExactSeconds + '@' + it.strftime("%Y%m%d%H%M%S") 490 491 oinf = oinf + ',' + str(Nseconds) + ',' + ExactSeconds 492 493 return oinf 494 495 # LluisWORKING 244 496 245 497 ####### ###### ##### #### ### ## # … … 6018 6270 quit(-1) 6019 6271 6020 6021 6272 class cls_seasonal_means(object): 6022 6273 """ Class to compute the seasonal mean of a time series of values … … 12727 12978 12728 12979 return origvar, dimsnewvar 12980 12981 def var_3desc(ovar): 12982 """ Function to provide std_name, long_name and units from an object variable 12983 ovar= object variable 12984 """ 12985 fname = 'var_desc' 12986 varattrs = ovar.ncattrs() 12987 12988 if searchInlist(varattrs,'std_name'): 12989 stdn = ovar.getncattr('std_name') 12990 else: 12991 vvalues = variables_values(ovar._name) 12992 stdn = vvalues[1] 12993 12994 if searchInlist(varattrs,'long_name'): 12995 lonn = ovar.getncattr('long_name') 12996 else: 12997 lonn = vvalues[4].replace('|',' ') 12998 12999 if searchInlist(varattrs,'units'): 13000 un = ovar.getncattr('units') 13001 else: 13002 un = vvalues[5] 13003 13004 return stdn, lonn, un 12729 13005 12730 13006 def file_oper_alongdims(values, ncfile, varn): … … 17773 18049 #SpatialWeightedMean('reglonlat,XLONG,XLAT,west_east,south_north','wrfout_d01_2001-11-11_00:00:00','HGT') 17774 18050 17775 def fill _value(vartype, fillval0):18051 def fillvalue_kind(vartype, fillval0): 17776 18052 """ Function to provide the fiven fill_Value for a kind of variable 17777 18053 [vartype]= type of the variable … … 17784 18060 Integer32 = -99999 17785 18061 """ 17786 fname = 'fill _value'17787 17788 if vartype == '|S':18062 fname = 'fillvalue_kind' 18063 18064 if vartype == type('c'): 17789 18065 if fillval0 == 'std': 17790 18066 fillval = fillvalueC … … 17798 18074 elif vartype == type(np.int16(1)): 17799 18075 if fillval0 == 'std': 17800 fillval = np.int 16(fillValueI)18076 fillval = np.int(9999999) 17801 18077 else: 17802 18078 fillval = np.int16(fillval0) … … 17828 18104 return fillval 17829 18105 18106 def nctype(vartype): 18107 """ Function to provide the string for the variable creation in a netCDF file 18108 [vartype]= type of the variable 18109 """ 18110 fname = 'nctype' 18111 18112 if vartype == type('c'): 18113 ncs = 'c' 18114 elif vartype == type(int(1)): 18115 ncs = 'i' 18116 elif vartype == type(np.int16(1)): 18117 ncs = 'i' 18118 elif vartype == type(np.int32(1)): 18119 ncs = 'i8' 18120 elif vartype == type(np.float(1.)): 18121 ncs = 'f' 18122 elif vartype == type(np.float32(1.)): 18123 ncs = 'f4' 18124 elif vartype == type(np.float64(1.)): 18125 ncs = 'f8' 18126 else: 18127 print errormsg 18128 print ' ' + fname + ': variable type', vartype, 'not ready !!' 18129 quit(-1) 18130 18131 return ncs 18132 17830 18133 def VarVal_FillValue(values, filen, varn): 17831 18134 """ Function to transform a given value from a given variable to _FillValue in a netCDF file … … 18070 18373 #quit() 18071 18374 18375 def CoarselonlatFind(ilon, ilat, lonv, latv, per): 18376 """ Function to search a given value from a coarser version of the data 18377 ilon, ilat= original 2D matrices with the longitudes and the latitudes 18378 lonv, latv= longitude and latitude to find 18379 per= period (as fraction over 1) of the fractions of the original grid to use to explore 18380 >>> npt=100 18381 >>> lonval0 = np.arange(0.,npt*1.,1.) 18382 >>> latval0 = np.arange(0.,npt*1.,1.) 18383 >>> lonval = np.zeros((npt,npt), dtype=np.float) 18384 >>> latval = np.zeros((npt,npt), dtype=np.float) 18385 18386 >>> for i in range(npt): 18387 >>> lonval[:,i] = lonval0[i] 18388 >>> latval[i,:] = latval0[i] 18389 >>> CoarselonlatFind(lonval, latval, 5.25, 5.25, .1) 18390 (array([5, 5]), 0.35355339059327379) 18391 """ 18392 fname = 'CoarselonlatFind' 18393 18394 dx = ilon.shape[1] 18395 dy = ilon.shape[0] 18396 18397 nlon = np.min(ilon) 18398 xlon = np.max(ilon) 18399 nlat = np.min(ilat) 18400 xlat = np.max(ilat) 18401 18402 if lonv < nlon or lonv > xlon: 18403 print errormsg 18404 print ' ' + fname + ': longitude outside data range!!' 18405 print ' given value:', lonv,'outside [',nlon,',',xlon,']' 18406 quit(-1) 18407 if latv < nlat or latv > xlat: 18408 print errormsg 18409 print ' ' + fname + ': latitude outside data range!!' 18410 print ' given value:', latv,'outside [',nlat,',',xlat,']' 18411 quit(-1) 18412 18413 fracx = int(dx*per) 18414 fracy = int(dy*per) 18415 18416 fraclon = ilon[::fracy,::fracx] 18417 fraclat = ilat[::fracy,::fracx] 18418 fracdx = fraclon.shape[1] 18419 fracdy = fraclon.shape[0] 18420 18421 # print 'fraclon _______' 18422 # print fraclon 18423 18424 # print 'fraclat _______' 18425 # print fraclat 18426 18427 # Fraction point 18428 difffraclonlat = np.sqrt((fraclon-lonv)**2. + (fraclat-latv)**2.) 18429 mindifffracLl = np.min(difffraclonlat) 18430 ilatlonfrac = index_mat(difffraclonlat, mindifffracLl) 18431 18432 # print 'mindifffracLl:', mindifffracLl, 'ilatlonfrac:', ilatlonfrac 18433 # print 'frac lon, lat:', fraclon[ilatlonfrac[0],ilatlonfrac[1]], ',', \ 18434 # fraclat[ilatlonfrac[0],ilatlonfrac[1]] 18435 # ilatlon = [ilatlonfrac[0]*fracy, ilatlonfrac[1]*fracx] 18436 # print 'input index:',ilatlon, 'lon lat:', ilon[ilatlon[0],ilatlon[1]], ',', \ 18437 # ilat[ilatlon[0],ilatlon[1]] 18438 18439 # Providing fraction range 18440 if fraclon[ilatlonfrac[0],ilatlonfrac[1]] >= lonv and \ 18441 fraclat[ilatlonfrac[0],ilatlonfrac[1]] >= latv: 18442 # ifracbeg = [ilatlonfrac[0]-1, ilatlonfrac[1]-1] 18443 # ifracend = [ilatlonfrac[0], ilatlonfrac[1]] 18444 if ilatlonfrac[0] > 0: 18445 iybeg = (ilatlonfrac[0]-1)*fracy 18446 iyend = ilatlonfrac[0]*fracy+1 18447 else: 18448 iybeg = 0 18449 iyend = fracy+1 18450 if ilatlonfrac[1] > 0: 18451 ixbeg = (ilatlonfrac[1]-1)*fracx 18452 ixend = ilatlonfrac[1]*fracx+1 18453 else: 18454 ixbeg = 0 18455 ixend = fracx+1 18456 elif fraclon[ilatlonfrac[0],ilatlonfrac[1]] < lonv and \ 18457 fraclat[ilatlonfrac[0],ilatlonfrac[1]] >= latv: 18458 # ifracbeg = [ilatlonfrac[0]-1, ilatlonfrac[1]] 18459 # ifracend = [ilatlonfrac[0], ilatlonfrac[1]+1] 18460 if ilatlonfrac[0] > 0: 18461 iybeg = (ilatlonfrac[0]-1)*fracy 18462 iyend = ilatlonfrac[0]*fracy+1 18463 else: 18464 iybeg = 0 18465 iyend = fracy+1 18466 if ilatlonfrac[1] < fracdx: 18467 ixbeg = (ilatlonfrac[1]-1)*fracx 18468 ixend = ilatlonfrac[1]*fracx+1 18469 else: 18470 ixbeg = fracdx*fracx 18471 ixend = dx+1 18472 elif fraclon[ilatlonfrac[0],ilatlonfrac[1]] < lonv and \ 18473 fraclat[ilatlonfrac[0],ilatlonfrac[1]] < latv: 18474 # ifracbeg = [ilatlonfrac[0], ilatlonfrac[1]] 18475 # ifracend = [ilatlonfrac[0]+1, ilatlonfrac[1]+1] 18476 if ilatlonfrac[0] < fracdy: 18477 iybeg = (ilatlonfrac[0]-1)*fracy 18478 iyend = ilatlonfrac[0]*fracy+1 18479 else: 18480 iybeg = fracdy*fracy 18481 iyend = dy+1 18482 if ilatlonfrac[1] < fracdx: 18483 ixbeg = (ilatlonfrac[1]-1)*fracx 18484 ixend = ilatlonfrac[1]*fracx+1 18485 else: 18486 ixbeg = fracdx*fracx 18487 ixend = dx+1 18488 elif fraclon[ilatlonfrac[0],ilatlonfrac[1]] >= lonv and \ 18489 fraclat[ilatlonfrac[0],ilatlonfrac[1]] < latv: 18490 # ifracbeg = [ilatlonfrac[0], ilatlonfrac[1]-1] 18491 # ifracend = [ilatlonfrac[0]+1, ilatlonfrac[1]] 18492 if ilatlonfrac[0] < fracdy: 18493 iybeg = (ilatlonfrac[0]-1)*fracy 18494 iyend = ilatlonfrac[0]*fracy+1 18495 else: 18496 iybeg = fracdy*fracy 18497 iyend = dy+1 18498 if ilatlonfrac[1] > 0: 18499 ixbeg = (ilatlonfrac[1]-1)*fracx 18500 ixend = ilatlonfrac[1]*fracx+1 18501 else: 18502 ixbeg = 0 18503 ixend = fracx+1 18504 18505 # ibeg = [iybeg, ixbeg] 18506 # iend = [iyend, ixend] 18507 # print 'find window; beginning', ibeg, 'end:', iend 18508 lon = ilon[iybeg:iyend,ixbeg:ixend] 18509 lat = ilat[iybeg:iyend,ixbeg:ixend] 18510 18511 # print 'lon _______' 18512 # print lon 18513 # print 'lat _______' 18514 # print lat 18515 18516 # Find point 18517 difflonlat = np.sqrt((lon-lonv)**2. + (lat-latv)**2.) 18518 mindiffLl = np.min(difflonlat) 18519 ilatlon = index_mat(difflonlat, mindiffLl) 18520 18521 ilatlon[0] = ilatlon[0] + iybeg 18522 ilatlon[1] = ilatlon[1] + ixbeg 18523 18524 # print 'mindiffLl:', mindiffLl, 'ilatlon:', ilatlon 18525 # print 'lon, lat:', lon[ilatlon[0],ilatlon[1]], ',', lat[ilatlon[0],ilatlon[1]] 18526 # quit() 18527 18528 return ilatlon, mindiffLl 18529 18530 def lonlatFind(ilon, ilat, lonv, latv): 18531 """ Function to search a given value from a lon, lat 2D data 18532 ilon, ilat= original 2D matrices with the longitudes and the latitudes 18533 lonv, latv= longitude and latitude to find 18534 >>> npt=100 18535 >>> lonval0 = np.arange(0.,npt*1.,1.) 18536 >>> latval0 = np.arange(0.,npt*1.,1.) 18537 >>> lonval = np.zeros((npt,npt), dtype=np.float) 18538 >>> latval = np.zeros((npt,npt), dtype=np.float) 18539 18540 >>> for i in range(npt): 18541 >>> lonval[:,i] = lonval0[i] 18542 >>> latval[i,:] = latval0[i] 18543 >>> lonlatFind(lonval, latval, 5.25, 5.25) 18544 (array([5, 5]), 0.35355339059327379) 18545 """ 18546 fname = 'lonlatFind' 18547 18548 dx = ilon.shape[1] 18549 dy = ilon.shape[0] 18550 18551 nlon = np.min(ilon) 18552 xlon = np.max(ilon) 18553 nlat = np.min(ilat) 18554 xlat = np.max(ilat) 18555 18556 if lonv < nlon or lonv > xlon: 18557 print errormsg 18558 print ' ' + fname + ': longitude outside data range!!' 18559 print ' given value:', lonv,'outside [',nlon,',',xlon,']' 18560 quit(-1) 18561 if latv < nlat or latv > xlat: 18562 print errormsg 18563 print ' ' + fname + ': latitude outside data range!!' 18564 print ' given value:', latv,'outside [',nlat,',',xlat,']' 18565 quit(-1) 18566 18567 # Find point 18568 difflonlat = np.sqrt((ilon-lonv)**2. + (ilat-latv)**2.) 18569 mindiffLl = np.min(difflonlat) 18570 ilatlon = index_mat(difflonlat, mindiffLl) 18571 18572 # print 'mindiffLl:', mindiffLl, 'ilatlon:', ilatlon 18573 # print 'lon, lat:', lon[ilatlon[0],ilatlon[1]], ',', lat[ilatlon[0],ilatlon[1]] 18574 18575 return ilatlon, mindiffLl 18576 18072 18577 def Partialmap_Entiremap(values, filen, varn): 18073 18578 """ Function to transform from a partial global map (e.g.: only land points) to an entire one 18074 values= [lonmame],[latname],[fillVal],[resolution],[kind] 18579 values= [lonmame],[latname],[fillVal],[resolution],[kind],[lonlatProjfile] 18075 18580 [lonname]: name of the longitude variable 18076 18581 [latname]: name of the latitude variable … … 18085 18590 'lonlat': standard lon/lat projection tacking number of points from [dx/dy]res at the Equator 18086 18591 'lonlat_dxdyFix': projection with a fixed grid distance 18592 'Goode': Goode projection 18593 [lonlatProjfile]: file with the lon,lat of the desired projection. 'None' to be computed and written on fly 18087 18594 filen= name of the netCDF file 18088 18595 varn= name of the variable … … 18093 18600 fname = 'Partialmap_Entiremap' 18094 18601 18095 arguments = '[lonmame],[latname],[fillVal],[resolution],[kind]'18096 check_arguments(fname, values, arguments, ',')18097 18098 18602 if values == 'h': 18099 18603 print fname + '_____________________________________________________________' 18100 18604 print Partialmap_Entiremap.__doc__ 18101 18605 quit() 18606 18607 arguments = '[lonmame],[latname],[fillVal],[resolution],[kind],[lonlatProjfile]' 18608 check_arguments(fname, values, arguments, ',') 18102 18609 18103 18610 ofile = 'EntireGlobalMap.nc' … … 18115 18622 resolution = np.float(values.split(',')[3]) 18116 18623 kind = values.split(',')[4] 18117 18118 # Input reference lonlat map 18119 lonlatProjfile = filen.split('.')[0] + '_' + kind + '.nc' 18624 Projfile = values.split(',')[5] 18625 if Projfile == 'None': 18626 lonlatProjfile = None 18627 else: 18628 lonlatProjfile = Projfile 18120 18629 18121 18630 if not onc.variables.has_key(lonname): … … 18136 18645 latvs = olat[:] 18137 18646 18647 Ninpts = len(lonvs) 18648 18138 18649 nlat = np.min(latvs) 18139 18650 nlon = np.min(lonvs) … … 18188 18699 quit(-1) 18189 18700 18190 if not os.path.isfile(lonlatProjfile): 18701 if lonlatProjfile is None: 18702 lonlatProjfile = kind + '.nc' 18191 18703 print warnmsg 18192 print ' ' + fname + ": no reference map '" + lonlatProjfile + "' found !!" 18193 print ' creating it via: lonlatProj(', resolution, ',', resolution, \ 18704 print ' ' + fname + ": no reference map !!" 18705 print " creation of '" + lonlatProjfile + "'" 18706 print ' creating it via: lonlatProj(', resolution, ',', resolution, \ 18194 18707 ', ' + kind + ', True)' 18195 18708 lonmap, latmap = lonlatProj(resolution, resolution, kind, True) … … 18197 18710 18198 18711 oproj = NetCDFFile(lonlatProjfile, 'r') 18199 if kind == 'lonlat': 18712 if kind == 'Goode': 18713 olon = oproj.variables['lon'] 18714 olat = oproj.variables['lat'] 18715 lonmap = olon[:] 18716 latmap = olat[:] 18717 projlon = olon[:] 18718 projlat = olat[:] 18719 omapvals = oproj.variables['ptid'] 18720 Ntotvals = omapvals.shape[0] * omapvals.shape[1] 18721 Nmaplon = olon.shape[1] 18722 Nmaplat = olat.shape[0] 18723 18724 elif kind == 'lonlat': 18200 18725 olon = oproj.variables['lon'] 18201 18726 olat = oproj.variables['lat'] … … 18233 18758 ## 18234 18759 ovar = onc.variables[varn] 18235 vartype = ovar.dtype18760 vartype = type(ovar[0]) 18236 18761 varlongname = ovar.long_name 18237 18762 varunits = ovar.units 18238 18763 18239 fval = fill _value(vartype, fval0)18764 fval = fillvalue_kind(type(ovar[0]), fval0) 18240 18765 18241 18766 # Final map creation 18242 18767 ## 18243 newnc = NetCDFFile(ofile, 'w') 18244 18768 if not os.path.isfile(ofile): 18769 print ' ' + fname + "File '" + ofile + "' does not exist !!" 18770 print ' cration of output file' 18771 newnc = NetCDFFile(ofile, 'w') 18245 18772 # dimensions 18246 newdim = newnc.createDimension('lon',Nmaplon) 18247 newdim = newnc.createDimension('lat',Nmaplat) 18248 newdim = newnc.createDimension('Npts', Ntotvals) 18773 newdim = newnc.createDimension('lon',Nmaplon) 18774 newdim = newnc.createDimension('lat',Nmaplat) 18775 newdim = newnc.createDimension('inpts', Ninpts) 18776 newdim = newnc.createDimension('pts', Ntotvals) 18249 18777 18250 18778 # Variables 18251 newvar = newnc.createVariable('lon','f8',('lon')) 18252 basicvardef(newvar, 'lon', 'Longitudes','degrees East') 18253 newvar[:] = lonmap 18254 newvar.setncattr('axis', 'X') 18255 newvar.setncattr('_CoordinateAxisType', 'Lon') 18256 18257 newvar = newnc.createVariable('lat','f8',('lat')) 18258 basicvardef(newvar, 'lat', 'Latitudes','degrees North') 18259 newvar[:] = latmap 18260 newvar.setncattr('axis', 'Y') 18261 newvar.setncattr('_CoordinateAxisType', 'Lat') 18779 if kind == 'Goode': 18780 newvar = newnc.createVariable('lon','f8',('lat', 'lon')) 18781 else: 18782 newvar = newnc.createVariable('lon','f8',('lon')) 18783 basicvardef(newvar, 'lon', 'Longitudes','degrees East') 18784 newvar[:] = lonmap 18785 newvar.setncattr('axis', 'X') 18786 newvar.setncattr('_CoordinateAxisType', 'Lon') 18787 18788 if kind == 'Goode': 18789 newvar = newnc.createVariable('lat','f8',('lat','lon')) 18790 else: 18791 newvar = newnc.createVariable('lat','f8',('lat')) 18792 basicvardef(newvar, 'lat', 'Latitudes','degrees North') 18793 newvar[:] = latmap 18794 newvar.setncattr('axis', 'Y') 18795 newvar.setncattr('_CoordinateAxisType', 'Lat') 18796 18797 newvarinpt = newnc.createVariable('locinpt','i',('inpts')) 18798 basicvardef(newvarinpt, 'locinpt', 'input point located: 0: no, 1: yes','-') 18799 18800 newvarindiff = newnc.createVariable('locindiff','f4',('inpts')) 18801 basicvardef(newvarindiff, 'locindiff', 'distance between input point and its final location','degree') 18262 18802 18263 18803 # map variable 18264 Lmapvalsshape = len(omapvals.shape) 18265 if Lmapvalsshape == 1: 18266 newvar = newnc.createVariable(varn,vartype,('Npts'), fill_value=fval) 18804 Lmapvalsshape = len(omapvals.shape) 18805 if Lmapvalsshape == 1: 18806 newvar = newnc.createVariable(varn, nctype(vartype), ('pts'), fill_value=fval) 18807 else: 18808 newvar = newnc.createVariable(varn, nctype(vartype), ('lat','lon'), fill_value=fval) 18809 18810 basicvardef(newvar, varn, varlongname, varunits) 18811 newvar.setncattr('coordinates', 'lon lat') 18812 18813 if Lmapvalsshape == 1: 18814 newvarin = newnc.createVariable('inpts', 'i4', ('pts')) 18815 else: 18816 newvarin = newnc.createVariable('inpts', 'i4', ('lat','lon')) 18817 18818 basicvardef(newvarin, 'inpts', 'Equivalent point from the input source', '-') 18819 newvar.setncattr('coordinates', 'lon lat') 18820 18267 18821 else: 18268 newvar = newnc.createVariable(varn,vartype,('lat','lon'), fill_value=fval) 18269 18270 basicvardef(newvar, varn, varlongname, varunits) 18271 newvar.setncattr('coordinates', 'lon lat') 18272 18273 print ' ' + fname + ': re-locating:',Ntotvals,'points...' 18274 if kind == 'lonlat': 18822 print ' ' + fname + "File '" + ofile + "' exists !!" 18823 print ' reading values from file' 18824 newnc = NetCDFFile(ofile, 'a') 18825 newvar = newnc.variables[varn] 18826 newvarin = newnc.variables['inpts'] 18827 newvarinpt = newnc.variables['locinpt'] 18828 newvarindiff = newnc.variables['locindiff'] 18829 18830 amsk = np.arange(3) 18831 amsk = ma.masked_equal(amsk, 0) 18832 18833 # fraclon = projlon[::Nmaplon*0.1] 18834 # fraclat = projlat[::Nmaplat*0.1] 18835 18836 # print 'fraclon________________', fraclon.shape 18837 # print fraclon 18838 18839 # print 'fraclat________________', fraclat.shape 18840 # print fraclat 18841 18842 print ' ' + fname + ': re-locating:',Ninpts,'points...' 18843 if kind == 'Goode': 18844 for iv in range(Ninpts): 18845 if newvarinpt[iv] == 0: 18846 18847 difflonlat = np.sqrt((projlon-lonvs[iv])**2. + (projlat-latvs[iv])**2.) 18848 mindiffLl = np.min(difflonlat) 18849 ilatlon = index_mat(difflonlat, mindiffLl) 18850 # ilatlon2, mindiffLl2 = CoarselonlatFind(projlon, projlat, lonvs[iv], latvs[iv], .1) 18851 18852 # if ilatlon[0] != ilatlon2[0] or ilatlon[1] != ilatlon2[1]: 18853 # print ilatlon, '<', ilatlon2 18854 # print 'diff:', mindiffLl, '<', mindiffLl2 18855 # quit(-1) 18856 18857 if type(newvar[ilatlon[0],ilatlon[1]]) != type(amsk): 18858 print errormsg 18859 print ' ' + main + ': point', projlon[ilatlon[0],ilatlon[1]], \ 18860 ',', projlat[ilatlon[0],ilatlon[1]], 'already filled!!' 18861 print ' value:', newvar[ilatlon[0],ilatlon[1]], 'distance:', \ 18862 newvarindiff[newvarin[ilatlon[0],ilatlon[1]]] 18863 18864 print ' iv:', iv, 'lon:', lonvs[iv],'lat:',latvs[iv] 18865 print ' mindiffLl:',mindiffLl,'ilatlon:',ilatlon 18866 quit(-1) 18867 # print ' Lluis; ' + fname + ' iv:', iv, 'lon:', lonvs[iv],'lat:',latvs[iv] 18868 # print ' Lluis; ' + fname + ' mindiffLl:',mindiffLl,'ilatlon:',ilatlon 18869 # print ' Lluis; ' + fname + ' A found lon:',projlon[ilatlon[0], ilatlon[1]], 'lat:', projlat[ilatlon[0], ilatlon[1]] 18870 # quit() 18871 18872 percendone(iv,Ninpts,0.5,'done:') 18873 if mindiffLl > mindiff: 18874 print errormsg 18875 print ' ' + fname + ': for point #', iv,'lon,lat in ' + \ 18876 'incomplet map:', lonvs[iv], ',', latvs[iv], 'there is not ' + \ 18877 'a set of lon,lat in the completed map closer than: ', mindiff,\ 18878 '!!' 18879 print ' minimum difference:', mindiffLl 18880 quit() 18881 18882 if ilatlon[0] >= 0 and ilatlon[1] >= 0: 18883 newvar[ilatlon[0],ilatlon[1]] = ovar[iv] 18884 newvarin[ilatlon[0],ilatlon[1]] = iv 18885 newvarinpt[iv] = 1 18886 newvarindiff[iv] = mindiffLl 18887 # print 'Lluis iv:', newvarin[ilatlon[0],ilatlon[1]], \ 18888 # 'localized:', newvarinpt[iv], 'values:', \ 18889 # newvar[ilatlon[0],ilatlon[1]], 'invalues:', ovar[iv], \ 18890 # 'mindist:', newvarindiff[iv], 'point:',ilatlon 18891 else: 18892 print errormsg 18893 print ' ' + fname + ': point iv:', iv, 'at', lonvs[iv], ',' , \ 18894 latvs[iv],' not relocated !!' 18895 print ' mindiffl:', mindiffLl, 'ilon:', ilatlon[1], \ 18896 'ilatlon:', ilatlon[1] 18897 quit(-1) 18898 18899 if np.mod(iv,100) == 0: 18900 newnc.sync() 18901 # print ' ' + fname + 'values localized:', newvar[:].compressed() 18902 # if iv > 10: 18903 # newnc.sync() 18904 # newnc.close() 18905 # quit(-1) 18906 18907 elif kind == 'lonlat': 18275 18908 for iv in range(Ntotvals): 18276 18909 difflat = np.abs(projlat - latvs[iv]) … … 18299 18932 18300 18933 newnc.sync() 18301 18302 18934 18303 18935 elif kind == 'lonlat_dxdyFix': … … 18358 18990 return 18359 18991 18992 #Partialmap_Entiremap('longitude,latitude,std,5000.,Goode,Goode_5km.nc', '/home/lluis/etudes/DYNAMICO/ORCHIDEE/interpolation/data/#carteveg5km.nc', 'vegetation_map') 18360 18993 #Partialmap_Entiremap('longitude,latitude,std,5000.,lonlat_dxdyFix', '/home/lluis/etudes/DYNAMICO/ORCHIDEE/interpolation/data/carteveg5km.nc', 'vegetation_map') 18361 18994 #Partialmap_Entiremap('longitude,latitude,std,5000.,lonlat', '/home/lluis/etudes/DYNAMICO/ORCHIDEE/interpolation/data/carteveg5km.nc', 'vegetation_map')
Note: See TracChangeset
for help on using the changeset viewer.