Julian date with python 3

to convert julian date to standard date and vice versa

Please NOTE

Regarding some comments in this post, and to avoid any misunderstood, please note that Julian Date is regarding Ordinal Day Calendar :

Leap years: (2000, 2004, 2008, 2012, 2016, 2020…)

Month Day Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1 1 32 61 92 122 153 183 214 245 275 306 336
2 2 33 62 93 123 154 184 215 246 276 307 337
3 3 34 63 94 124 155 185 216 247 277 308 338
4 4 35 64 95 125 156 186 217 248 278 309 339
5 5 36 65 96 126 157 187 218 249 279 310 340
6 6 37 66 97 127 158 188 219 250 280 311 341
7 7 38 67 98 128 159 189 220 251 281 312 342
8 8 39 68 99 129 160 190 221 252 282 313 343
9 9 40 69 100 130 161 191 222 253 283 314 344
10 10 41 70 101 131 162 192 223 254 284 315 345
11 11 42 71 102 132 163 193 224 255 285 316 346
12 12 43 72 103 133 164 194 225 256 286 317 347
13 13 44 73 104 134 165 195 226 257 287 318 348
14 14 45 74 105 135 166 196 227 258 288 319 349
15 15 46 75 106 136 167 197 228 259 289 320 350
16 16 47 76 107 137 168 198 229 260 290 321 351
17 17 48 77 108 138 169 199 230 261 291 322 352
18 18 49 78 109 139 170 200 231 262 292 323 353
19 19 50 79 110 140 171 201 232 263 293 324 354
20 20 51 80 111 141 172 202 233 264 294 325 355
21 21 52 81 112 142 173 203 234 265 295 326 356
22 22 53 82 113 143 174 204 235 266 296 327 357
23 23 54 83 114 144 175 205 236 267 297 328 358
24 24 55 84 115 145 176 206 237 268 298 329 359
25 25 56 85 116 146 177 207 238 269 299 330 360
26 26 57 86 117 147 178 208 239 270 300 331 361
27 27 58 87 118 148 179 209 240 271 301 332 362
28 28 59 88 119 149 180 210 241 272 302 333 363
29 29 60 89 120 150 181 211 242 273 303 334 364
30 30 90 121 151 182 212 243 274 304 335 365
31 31 91 152 213 244 305 366

Regular years: (2001-2003, 2005-2007, 2009-2011, 2013-2015…)

Month Day Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1 1 32 60 91 121 152 182 213 244 274 305 335
2 2 33 61 92 122 153 183 214 245 275 306 336
3 3 34 62 93 123 154 184 215 246 276 307 337
4 4 35 63 94 124 155 185 216 247 277 308 338
5 5 36 64 95 125 156 186 217 248 278 309 339
6 6 37 65 96 126 157 187 218 249 279 310 340
7 7 38 66 97 127 158 188 219 250 280 311 341
8 8 39 67 98 128 159 189 220 251 281 312 342
9 9 40 68 99 129 160 190 221 252 282 313 343
10 10 41 69 100 130 161 191 222 253 283 314 344
11 11 42 70 101 131 162 192 223 254 284 315 345
12 12 43 71 102 132 163 193 224 255 285 316 346
13 13 44 72 103 133 164 194 225 256 286 317 347
14 14 45 73 104 134 165 195 226 257 287 318 348
15 15 46 74 105 135 166 196 227 258 288 319 349
16 16 47 75 106 136 167 197 228 259 289 320 350
17 17 48 76 107 137 168 198 229 260 290 321 351
18 18 49 77 108 138 169 199 230 261 291 322 352
19 19 50 78 109 139 170 200 231 262 292 323 353
20 20 51 79 110 140 171 201 232 263 293 324 354
21 21 52 80 111 141 172 202 233 264 294 325 355
22 22 53 81 112 142 173 203 234 265 295 326 356
23 23 54 82 113 143 174 204 235 266 296 327 357
24 24 55 83 114 144 175 205 236 267 297 328 358
25 25 56 84 115 145 176 206 237 268 298 329 359
26 26 57 85 116 146 177 207 238 269 299 330 360
27 27 58 86 117 147 178 208 239 270 300 331 361
28 28 59 87 118 148 179 209 240 271 301 332 362
29 29 88 119 149 180 210 241 272 302 333 363
30 30 89 120 150 181 211 242 273 303 334 364
31 31 90 151 212 243 304 365

It possible to find an application about it on https://aces.nmsu.edu/pubs/_b/B130/welcome.html.

Introduction

The work in data science must be tidy. A simple file name can help you to save a lot of time. For instance, if you have a series of satellite images from LANDSAT 8, the files are ordered by Julian date:

rt@rt-d5567:~/Documents/$ find -name '*305*.TIF'
./LO82280702018305CUB00_B3.TIF
./LO82280702018305CUB00_B9.TIF
./LO82280702018305CUB00_B5.TIF

If you did not realize, note that after 2018 there are three numbers 305. The number 305 means the 305th day of the year. If we join 2018 and 305, we get 2018305 (305th day of 2018 year). It is the same of 2018-11-01 (standard date: year, month, day).

Another user of Julian date is in meteorology. Usually the data are organized by Julian date:

Table 1: Climate date table

year jday temperature ur
2017 55 23 70
2017 56 22 74
2018 55 26 68
2018 56 25 71

The idea in this post is to provide a simple code to extract day of year and Julian day from a string date in python.

From standard Date to Julian date

Once your date is in datetime format, just convert to timetuple, as follows:

import datetime

def datestdtojd (stddate):
    fmt='%Y-%m-%d'
    sdtdate = datetime.datetime.strptime(stddate, fmt)
    sdtdate = sdtdate.timetuple()
    jdate = sdtdate.tm_yday
    return(jdate)

Let’s suppose that you would like to know the Julian date for 2018-11-01. Running the code:

>>> datestdtojd('2018-11-01')
305

It means 305th day of 2018.

From Julian date to standard Date

def jdtodatestd (jdate):
    fmt = '%Y%j'
    datestd = datetime.datetime.strptime(jdate, fmt).date()
    return(datestd)

If you need to know month and day for the 305th of 2018.

>>> jdtodatestd('2018305')
datetime.date(2018, 11, 1)

Conclusion

Julian date is pretty handy to tidy files and data bank. Thus some functions are important to manage transformation from one to another format.

Let me know you have some question.

References

 Share!

 
comments powered by Disqus