[Thread Prev][Thread Next][Index]

Re: [ferret_users] Multiplying by days in month



Hi,
Thank you for the update.  Using @ASN to put the data onto the grid of your variable is fine - so long as there aren't time gaps in the data set.

Thinking about it still more, here's a simpler idea.  You could use the pseudo-variable TBOX to define a variable which is the length of each month, and converting it to days.
yes? let tlen = tbox[gt=ba_rate]

! Convert to days. A general script should verify that the
! time axis starts in January and represents full months.


yes? let factr = `tbox[L=1]/31.0` 

yes? let month_length = tlen* factr
I haven't tested this, but it should work fine. 

-Ansley

On 10/22/2014 6:51 PM, Sam Rabin wrote:
Awesome, thanks! This gets me most of the way there, but I noticed a discrepancy. It looks like the result of what you gave me isn’t exactly the same as if I multiplied each January by 31, each February by 28.25, etc. — except, for some reason, for every July and December, which work perfectly.

I made a few variations:

  • BA_v1: ba_rate_month as calculated via your procedure
  • BA_v2: Used four years’ worth of days in month so that leap year Februaries have 29 days, others with 28. (To generalize, I can get fancy with IF clauses and the MOD function — Y is a leap year if MOD(Y,4) EQ 0.)
  • BA_v3: Instead of [gt=ba_rate] on the last line, use [gt=ba_rate@asn].

As you can see in the results printed below my signature, BA_v3 works correctly.

Thanks for your help!

Sam



yes? list/prec=7 ba_rate[i=@sum,j=@sum,l=1:48], BA_v1[i=@sum,j=@sum,l=1:48], BA_v2[i=@sum,j=@sum,l=1:48], BA_v3[i=@sum,j=@sum,l=1:48]
             DATA SET: ./land_month_meanFxAvgProper_1_1501-1723_BArate.nc
             TIME: 01-JAN-1501 00:00 to 01-JAN-1505 00:00 JULIAN
             LONGITUDE: 0E to 0E(360) (XY summed)
             LATITUDE: 90S to 90N (XY summed)
 Column  1: BA_RATE is Burned area (km2/day)
 Column  2: BA_V1 is BA_RATE * DAYS_IN_MONTHS[GT=BA_RATE]
 Column  3: BA_V2 is BA_RATE * DAYS_IN_MONTHS2[GT=BA_RATE]
 Column  4: BA_V3 is BA_RATE * DAYS_IN_MONTHS2[GT=BA_RATE@ASN]
                      BA_RATE    BA_V1     BA_V2     BA_V3
16-JAN-1501 12 /  1:     80.44     2488.     2486.     2494.
15-FEB-1501 00 /  2:   2338.79    66202.    65719.    65486.
16-MAR-1501 12 /  3:   9201.38   285098.   284947.   285243.
16-APR-1501 00 /  4:  13499.57   405199.   405420.   404987.
16-MAY-1501 12 /  5:  14870.32   460746.   460503.   460980.
16-JUN-1501 00 /  6:  16933.16   508261.   508538.   507995.
16-JUL-1501 12 /  7:  21494.46   666328.   666328.   666328.
16-AUG-1501 12 /  8:  27994.54   867391.   866932.   867831.
16-SEP-1501 00 /  9:  33873.29  1016731.  1017286.  1016199.
16-OCT-1501 12 / 10:  25794.03   799209.   798787.   799615.
16-NOV-1501 00 / 11:  21719.95   651940.   652296.   651599.
16-DEC-1501 12 / 12:  20904.34   648035.   648035.   648035.
16-JAN-1502 12 / 13:  22102.71   684201.   682983.   685184.
15-FEB-1502 00 / 14:  22847.21   646185.   641997.   639722.
16-MAR-1502 12 / 15:  22879.40   709089.   708527.   709261.
16-APR-1502 00 / 16:  22050.57   661683.   662225.   661517.
16-MAY-1502 12 / 17:  21413.66   663662.   663136.   663823.
16-JUN-1502 00 / 18:  18749.40   562623.   563084.   562482.
16-JUL-1502 12 / 19:  23282.84   721768.   721768.   721768.
16-AUG-1502 12 / 20:  31219.10   967558.   966790.   967792.
16-SEP-1502 00 / 21:  32838.06   985389.   986196.   985142.
16-OCT-1502 12 / 22:  29327.18   908922.   908201.   909143.
16-NOV-1502 00 / 23:  18861.25   565979.   566443.   565837.
16-DEC-1502 12 / 24:  24675.93   764954.   764954.   764954.
16-JAN-1503 12 / 25:  24545.29   760382.   759302.   760904.
15-FEB-1503 00 / 26:  23400.15   661281.   679352.   655204.
16-MAR-1503 12 / 27:  24048.15   745493.   745493.   745493.
16-APR-1503 00 / 28:  25808.78   774263.   774263.   774263.
16-MAY-1503 12 / 29:  19201.62   595250.   595250.   595250.
16-JUN-1503 00 / 30:  17855.02   535651.   535651.   535651.
16-JUL-1503 12 / 31:  16090.21   498797.   498797.   498797.
16-AUG-1503 12 / 32:  21976.96   681286.   681286.   681286.
16-SEP-1503 00 / 33:  27334.76   820043.   820043.   820043.
16-OCT-1503 12 / 34:  28825.21   893581.   893581.   893581.
16-NOV-1503 00 / 35:  30561.32   916840.   916840.   916840.
16-DEC-1503 12 / 36:  28564.04   885485.   885485.   885485.
16-JAN-1504 12 / 37:  25712.30   797081.   797081.   797081.
15-FEB-1504 12 / 38:  25385.38   717972.   712028.   736176.
16-MAR-1504 12 / 39:  24186.73   749210.   749012.   749789.
16-APR-1504 00 / 40:  22441.96   673796.   673980.   673259.
16-MAY-1504 12 / 41:  19744.92   611620.   611458.   612092.
16-JUN-1504 00 / 42:  16672.43   500572.   500708.   500173.
16-JUL-1504 12 / 43:  15625.86   484402.   484402.   484402.
16-AUG-1504 12 / 44:  20372.97   631075.   630908.   631562.
16-SEP-1504 00 / 45:  26970.94   809773.   809994.   809128.
16-OCT-1504 12 / 46:  28341.75   877916.   877684.   878594.
16-NOV-1504 00 / 47:  27011.61   810994.   811216.   810348.
16-DEC-1504 12 / 48:  26251.07   813783.   813783.   813783.
yes? list BA_v1[i=@sum,j=@sum,l=1:48]/ba_rate[i=@sum,j=@sum,l=1:48], BA_v2[i=@sum,j=@sum,l=1:48]/ba_rate[i=@sum,j=@sum,l=1:48], BA_v3[i=@sum,j=@sum,l=1:48]/ba_rate[i=@sum,j=@sum,l=1:48]
             DATA SET: ./land_month_meanFxAvgProper_1_1501-1723_BArate.nc
             TIME: 01-JAN-1501 00:00 to 01-JAN-1505 00:00 JULIAN
             LONGITUDE: 0E to 0E(360)
             LATITUDE: 90S to 90N
 Column  1: EX#1 is BA_V1[I=@SUM,J=@SUM,L=1:48]/BA_RATE[I=@SUM,J=@SUM,L=1:48]
 Column  2: EX#2 is BA_V2[I=@SUM,J=@SUM,L=1:48]/BA_RATE[I=@SUM,J=@SUM,L=1:48]
 Column  3: EX#3 is BA_V3[I=@SUM,J=@SUM,L=1:48]/BA_RATE[I=@SUM,J=@SUM,L=1:48]
                      EX#1   EX#2   EX#3
16-JAN-1501 12 /  1:  30.93  30.90  31.00
15-FEB-1501 00 /  2:  28.31  28.10  28.00
16-MAR-1501 12 /  3:  30.98  30.97  31.00
16-APR-1501 00 /  4:  30.02  30.03  30.00
16-MAY-1501 12 /  5:  30.98  30.97  31.00
16-JUN-1501 00 /  6:  30.02  30.03  30.00
16-JUL-1501 12 /  7:  31.00  31.00  31.00
16-AUG-1501 12 /  8:  30.98  30.97  31.00
16-SEP-1501 00 /  9:  30.02  30.03  30.00
16-OCT-1501 12 / 10:  30.98  30.97  31.00
16-NOV-1501 00 / 11:  30.02  30.03  30.00
16-DEC-1501 12 / 12:  31.00  31.00  31.00
16-JAN-1502 12 / 13:  30.96  30.90  31.00
15-FEB-1502 00 / 14:  28.28  28.10  28.00
16-MAR-1502 12 / 15:  30.99  30.97  31.00
16-APR-1502 00 / 16:  30.01  30.03  30.00
16-MAY-1502 12 / 17:  30.99  30.97  31.00
16-JUN-1502 00 / 18:  30.01  30.03  30.00
16-JUL-1502 12 / 19:  31.00  31.00  31.00
16-AUG-1502 12 / 20:  30.99  30.97  31.00
16-SEP-1502 00 / 21:  30.01  30.03  30.00
16-OCT-1502 12 / 22:  30.99  30.97  31.00
16-NOV-1502 00 / 23:  30.01  30.03  30.00
16-DEC-1502 12 / 24:  31.00  31.00  31.00
16-JAN-1503 12 / 25:  30.98  30.93  31.00
15-FEB-1503 00 / 26:  28.26  29.03  28.00
16-MAR-1503 12 / 27:  31.00  31.00  31.00
16-APR-1503 00 / 28:  30.00  30.00  30.00
16-MAY-1503 12 / 29:  31.00  31.00  31.00
16-JUN-1503 00 / 30:  30.00  30.00  30.00
16-JUL-1503 12 / 31:  31.00  31.00  31.00
16-AUG-1503 12 / 32:  31.00  31.00  31.00
16-SEP-1503 00 / 33:  30.00  30.00  30.00
16-OCT-1503 12 / 34:  31.00  31.00  31.00
16-NOV-1503 00 / 35:  30.00  30.00  30.00
16-DEC-1503 12 / 36:  31.00  31.00  31.00
16-JAN-1504 12 / 37:  31.00  31.00  31.00
15-FEB-1504 12 / 38:  28.28  28.05  29.00
16-MAR-1504 12 / 39:  30.98  30.97  31.00
16-APR-1504 00 / 40:  30.02  30.03  30.00
16-MAY-1504 12 / 41:  30.98  30.97  31.00
16-JUN-1504 00 / 42:  30.02  30.03  30.00
16-JUL-1504 12 / 43:  31.00  31.00  31.00
16-AUG-1504 12 / 44:  30.98  30.97  31.00
16-SEP-1504 00 / 45:  30.02  30.03  30.00
16-OCT-1504 12 / 46:  30.98  30.97  31.00
16-NOV-1504 00 / 47:  30.02  30.03  30.00
16-DEC-1504 12 / 48:  31.00  31.00  31.00



On Oct 22, 2014, at 6:11 PM, Ansley Manke <ansley.b.manke@xxxxxxxx> wrote:

Hi Sam,
The Julian axis makes it a bit tricky.  If you don't mind treating all the February's as having length 28.25, the average length in a Julian calendar, then here's a method:

! Define a variables with the number of days per month
yes? let days = tsequence({31,28.25,31,30,31,30,31,31,30,31,30,31})
! Define a monthly modulo axis, using the /EDGES qualifier, and the running sum of days per
! month, starting with zero at the beginning of January.  I've referenced this to year
! 0000, but you could have the time origin in January of any year.
yes? let edges_days = tsequence({0,31,28.25,31,30,31,30,31,31,30,31,30,31})
yes? define axis/modulo/EDGES/calendar=julian/t0="1-jan-0000:00:00"/units=days \
 monthly_julian = edges_days[l=@iin]


yes? list/prec=6 t[gt=monthly_julian]
             VARIABLE : T
                        axis MONTHLY_JULIAN
             SUBSET   : 12 points (TIME)
             CALENDAR : JULIAN
 16-JAN-1501 12 /  1:   15.500
 15-FEB-1501 03 /  2:   45.125
 16-MAR-1501 18 /  3:   74.750
 16-APR-1501 06 /  4:  105.250
 16-MAY-1501 18 /  5:  135.750
 16-JUN-1501 06 /  6:  166.250
 16-JUL-1501 18 /  7:  196.750
 16-AUG-1501 18 /  8:  227.750
 16-SEP-1501 06 /  9:  258.250
 16-OCT-1501 18 / 10:  288.750
 16-NOV-1501 06 / 11:  319.250
 16-DEC-1501 18 / 12:  349.750


yes? let days_in_months = 
days[gt=monthly_julian@asn]

yes? ! try this to check the time axis:
yes? !
list/t=1-jan-1501:31-dec-1624 days_in_months
! Now this variable is on a monthly Julian axis that should be compatible with your time axis.
yes? let ba_rate_month = ba_rate* days_in_months[gt=ba_rate]



On 10/22/2014 6:56 AM, Sam Rabin wrote:
Ferret gurus —

I have a NetCDF dataset that shows, for each month, the mean daily burned area in each grid cell. I want to convert these daily rates to monthly values, which requires multiplying them by the number of days in each month. As a kludge, I’ve just been multiplying the entire dataset by 365.25/12, but I’d like to do it properly. That is, I want to multiply the values for each January by 31, each February by 28 (29 in leap years!), etc.

I will need to do this regularly in the future, so any advice pertinent to making an external function and/or .jnl script would be much appreciated. Please note that the length of the time axis can differ wildly between instances, so I’d need something general. (Although if it helps, the time axis will almost always be a multiple of 12 months starting in January.)

If it helps, info on the variable (BA_RATE) can be found after my signature.

Thanks in advance!

Sam





yes? show grid BA_RATE
    GRID GKY2
 name       axis              # pts   start                end
 LON       LONGITUDE          144mr   1.25E                1.25W
 LAT       LATITUDE            90 i   89.494S              89.494N
 normal    Z
 TIME      TIME              1488 i   16-JAN-1501 12:00    16-DEC-1624 12:00
yes? list BA_RATE[i=@sum,j=@sum,l=1:5]
             VARIABLE : Burned area (km2/day)
             DATA SET : SSR_force30_luN_esm2mb-FIREunpacked_meanFx_avgProper
             FILENAME : land_month_meanFxAvgProper_1_1501-1624.nc
             SUBSET   : 5 points (TIME)
             CALENDAR : JULIAN
             LONGITUDE: 0E to 0E(360) (XY summed)
             LATITUDE : 90S to 90N (XY summed)
 16-JAN-1501 12 / 1:     80.
 15-FEB-1501 00 / 2:   2339.
 16-MAR-1501 12 / 3:   9201.
 16-APR-1501 00 / 4:  13500.
 16-MAY-1501 12 / 5:  14870.





[Thread Prev][Thread Next][Index]
Contact Us
Dept of Commerce / NOAA / OAR / PMEL / Ferret

Privacy Policy | Disclaimer | Accessibility Statement